/*
CLAS JavaScript Functions
Last updated: 8.1.09
Richard Porter
notes: - functions not completely abstracted (yet)
	  - if you stumble upon this and see inefficiencies, please let me know -> richard-b-porter@uiowa.edu
*/

/*
Image Replacement for headings
Image file names must be hyphen delimited (-) and ignore special symbols
*/ 
function jqir() {
    $('.img').each(function() {
      string = $(this).text();
      filename = string.toLowerCase().replace(/ /g, '-').replace(/([^0-9a-z-])/g,'');
	  
	  if (location.href == "http://www.clas.uiowa.edu/" || location.href == "http://www.clas.uiowa.edu/index.shtml")
	  {
		  $(this).html('<img src="/_includes/images/home/' + filename + '.png" alt="' + string + '" />');
	  }
      else
	  {
		  $(this).html('<img src="/_includes/images/_global/' + filename + '.png" alt="' + string + '" />');
	  }
    });
}

/*
Drop down functionality for local navigation
*/
function dropDown() {
  $('#local_navigation ul').hide();
  
  $('#local_navigation li:has(li)').addClass('has_children');
   
  $('#local_navigation li a').click(
    function(event) {
	if ( $(this).parent().hasClass('has_children') )
	{
	  event.preventDefault();
	  $(this).next().slideToggle('normal');
	} 
	  $.cookie('show_last_selection', null);
	  $.cookie('show_last_selection', $(this).text());
  });
  
  var last = $.cookie('show_last_selection');
  
  $("#local_navigation li a[text='" +last+ "']").parents().show();
}

/*
Auto-selecting navigation
*/
function autoSelect() {  
  var currentLocation = location.pathname.substring(0);
  var p = currentLocation.slice(0, currentLocation.lastIndexOf('/') + 1);
  
  if (currentLocation.match('index.shtml')) {
	  $("#local_navigation a[href='" +p+ "']").addClass('selected');
	  $(".selected").parents().show();
  } else {
	  $("#local_navigation a[href='" +currentLocation+ "']").addClass('selected');
	  $(".selected").parents().show();
  }
}

/*
Swap the default value of the search bar on focus/blur
*/
function searchSwap() {
	var search_field = $("#search_field");
	var default_value = "Search...";
	
	search_field.focus(function() {
		if($(this).attr("value") == default_value) $(this).attr("value", "");
	});
	
	search_field.blur(function() {
		if($(this).attr("value") == "") $(this).attr("value", default_value);
	});
}

/*
Add an icon to all external links and files
*/
function addIcons() {
  $('a').filter(function() {
         return this.hostname && this.hostname !== location.hostname;
        }).addClass('external');
  $('#ui_wordmark').removeClass('external');
 
  $('table a:has(img)').removeClass('external');
  $('.events a').removeClass('external');
  
  $('a[href$=".pdf"]').addClass('pdf');
  
  //Remove icons
  if (location.hostname != "www.clas.uiowa.edu/")
  {
	  $('.events a').removeClass('pdf');
  }
}

/*
Hover effect for the global navigation
*/
function navHover() {  
  $('#global_navigation li a').hover(
    function() {
		$(this).stop().animate({ color: '#FFFFFF'}, 200);
		return false;
	}, 
	function() {
		$(this).stop().animate({ color: '#000000'}, 400);
		return false;
	}
  );
}

/* go */
$(document).ready(function() {
  jqir();
  dropDown();
  autoSelect();
  searchSwap();
  addIcons();
  navHover();
  if ( $('.forms').length > 0 )
  {
	  $('.forms textarea').css('resize', 'both');
  }
});