// ***************************************************************************
// ***                Core Javascript - AWM Public Website                 ***
// ***************************************************************************
// ***                                                                     ***
// *** Provides core javascript functionality for the AWM Public Website   ***
// ***                                                                     ***
// ***************************************************************************


// ***************************************************************************
// *** Google Analytics File Links                                         ***
// ***************************************************************************
//
// Version: 2.0
// Last Updated: 10/05/2011
//
// Function: Creates Google Analytics tracking onClick events for each non
//   HTML page within the AWM Public Website (excludes CAS)
//
// Inputs:
//   none
//
// Outputs:
//   none

jQuery(document)
  .ready(function(){
    jQuery('a')
      .filter(function() {
        return this.href.match(/^(?:(?:https?:\/\/(?!cas\.)(?:[^\/]+?\.)*?awm\.gov\.au\/)|\/|\.)[^\.]+?\.(?!aspx|asp|html|htm|php|nsf|\d{3})/i);
      })
      .click(function(){
        pageTracker._trackPageview(this.href.replace(/^https?:\/\/(?:[^\/]+?\.)*?awm\.gov\.au\//i, "/"));
      })
  });


// ***************************************************************************
// *** Setup Toggles                                                       ***
// ***************************************************************************
//
// Version: 2.0
// Last Updated: 10/05/2011
//
// Function: Creates a dynamic toggle from each element with an id starting
//   with "toggle_" that shows or hides the corresponding element on click.
//
// Inputs:
//   none
//
// Outputs:
//   none

jQuery(document)
  .ready(function(){
    jQuery('[id^="toggle_"]')
	  .each(function () {
        var el = jQuery(this);
        if (!el.data('toggle_target')) el.data('toggle_target', jQuery(el.attr('id').replace(/^toggle_/i, '#')));
        var tt = el.data('toggle_target');

        if (typeof tt == 'object') {
          if (!tt.data('original_display')) tt.data('original_display', tt.css('display'));
          if (tt.css('display') == 'none' || el.hasClass('toggle_off')) {
            tt.css('display', 'none');
            el.addClass('toggle_off');
            el.removeClass('toggle_on');
          } else {
            tt.css('display', tt.data('original_display'));
            el.addClass('toggle_on');
            el.removeClass('toggle_off');
          }

          el
		    .click(function () {
              var el = jQuery(this);
              var tt = el.data('toggle_target');

              if (el.hasClass('toggle_off')) {
                tt.css('display', tt.data('original_display'));
                el.addClass('toggle_on');
                el.removeClass('toggle_off');
              } else {
                tt.css('display', 'none');
                el.addClass('toggle_off');
                el.removeClass('toggle_on');
              }
            });
        } else {
          el.removeData('toggle_target');
        }
      });
  });


// ***************************************************************************
// *** Setup Search Field Example                                          ***
// ***************************************************************************
//
// Version: 2.0
// Last Updated: 10/05/2011
//
// Function: Creates the sample text of "Search" within the website search
//   field.
//
// Inputs:
//   none
//
// Outputs:
//   none

jQuery(document)
  .ready(function(){
    jQuery('#site_search_widget_q')
      .each(function () {
        var el = jQuery(this);
        el.attr('placeholder', 'Search');

        if (el.val() == '') {
          el.val(el.attr('placeholder'));
          el.addClass('placeholder');
		}

        el.focus(function () {
          if (el.val().toLowerCase() == el.attr('placeholder').toLowerCase()) el.val('');
          el.removeClass('placeholder');
		});

        el.blur(function () {
          if (el.val() == '' || el.val().toLowerCase() == el.attr('placeholder').toLowerCase()) {
            el.val(el.attr('placeholder'));
            el.addClass('placeholder');
		  }
		});
	  });
  });;

