jQuery(document).ready(function() {
	
	/**
	 * Custom array reverse function
	 */
	//jQuery.fn.reverse = [].reverse;
	
	/**
	 * Main navigation
	 */
	jQuery("ul#nav li").each( function() {
	    var link = jQuery(this).find('a');
		var item = jQuery(this);
		
		// If this is the home page
		if (location.pathname == '/' && link.attr('href') == '/') {
			jQuery("ul#nav li").removeClass("nav_on");
			item.addClass("nav_on");
			
		// Check to see if this is a top level nav item
	    } else if ((location.pathname.indexOf(link.attr('href')) > -1) && link.attr('href') != '/') {
			jQuery("ul#nav li").removeClass("nav_on");
			item.addClass("nav_on");
			
		// Is this a sub page (We're using the Breadcrumbs to work this out - is there a better way?)
		} else {
			jQuery("div#breadcrumbs_rail a").each( function() {							
				if ((link.attr('href').indexOf(jQuery(this).attr('href')) > -1) && jQuery(this).attr('href') != '/') {
					jQuery("ul#nav li").removeClass("nav_on");
					item.addClass("nav_on");
				}
			});    
		}
	});
	
	// Used for sub nav on's
	var show_on_state = true;
	
	/**
	 * Sub Navigation
	 */
 	jQuery("ul#sub_nav li").each( function() {
	    var link = jQuery(this).find('a');
		var item = jQuery(this);
		

		if ((location.pathname.indexOf(link.attr('href')) > -1) && link.attr('href') != '/') {
			jQuery("ul#sub_nav li").removeClass("sub_nav_on");
			item.addClass("sub_nav_on");
		
		// Is this a sub page (We're using the Breadcrumbs to work this out - is there a better way?)
		} else {
			//jQuery("div#breadcrumbs_rail a").reverse().each( function() { // Throws an error in Content Editor
			jQuery("div#breadcrumbs_rail a").each( function() {						
				if ((link.attr('href').indexOf(jQuery(this).attr('href')) > -1) && jQuery(this).attr('href') != '/') {
					jQuery("ul#sub_nav li").removeClass("nav_on");
					if (show_on_state) {
						item.addClass("sub_nav_on");
						show_on_state = false; // We only want to show 1 item as "on"
					}
				}
			});    
		}
	});

  /**
   * site search text
   */
  var search_text = 'Enter keywords to search site';
  jQuery('input#search_input').val(search_text);
  jQuery('input#search_input').blur(function() {
    if(jQuery(this).val() == '')
      jQuery(this).val(search_text);
  }).focus(function() {
    if(jQuery(this).val() == search_text)
      jQuery(this).val('');
  });
});
