// Close menu images:
function mlImgClose() {
 // Traverse each image looking for an image to close:
 $("#header_navigation > li > img").each(function() {

  var imgSrc = this.src.slice(this.src.search('images'));   
																																																			
  if(imgSrc == 'images/navigation/header/arrow_open.gif') {
   this.src = "uploads/v2/images/navigation/header/arrow_closed.gif"; } }); }

// Toggle content in main tabs:
function mlcToggle(targetDiv) {
 $(".mlc_div").hide();
 $("#"+targetDiv.toLowerCase().replace(/-/g, "_")).show(); }


// Custom actions when items are closed:
function customClose() {
 // Handle opened arrows if needed:
 mlImgClose();
	
	// Set the search query to a lower layer:
	$("#header_search").css("z-index", "1");

 //Remove close tabs:
 $("#close_mltab").remove();
	
	// Handle tabs:
	$(".open-item").empty(); 
	$(".open-item").hide(); 
	$(".open-item").removeClass("open-item"); }



// Toggle search floaty:
function showSearch() {
	
	closeOpenedItems();
	
	makeFloaty('lightbox', 'fullscreen', '', 1100, 1);
	maximizeFloaty('#lightbox');
	showFloaty('#lightbox');
	
	makeFloaty('header_search_results', 'pop-in', '<h1 class="header_narrow"></h1>', 1200, 1);
	
	var target = $("#header_search");
 var offset = target.offset();
	
	$('#header_search_results').css("top", (offset.top + 35));
	$('#header_search_results').css("left", offset.left);
	
	$("#header_search").css("z-index", "1150");
	
	performSearch(); }
	
// Search for contents in header query:
function performSearch() {

 if($("#header_query").val() == '') {
  $("#header_search_results").empty();
  $("#header_search_results").append('<h1 class="header_narrow">type to search</h1>'); }
		
	else {
		
  if(timeout) clearTimeout(timeout);

		timeout = setTimeout(function() {

   $("#header_search_results").empty();
			$("#header_search_results").append('<h1 class="header_narrow">searching...</h1>');

   $("#header_search_results").append('<div class="container_narrow"><div class="content_padding"></div></div>');
	
				function showSearchLoader() {
	
					$("#header_search_results > h1").html('searching <img id="header_search_loader" src="uploads/v2/images/header/search/ajax_loader.gif">');
					$("#header_search_results > .container_narrow").show();
					$("#header_search_loader").fadeIn("fast", loadContent()); }
					
				function loadContent () {
					
					$("#header_search_results > .container_narrow > .content_padding").empty();
					$("#header_search_results > .container_narrow > .content_padding").load('http://www.wku.edu/gfcb/index.php?mact=Search%2Ccntnt01%2Cdosearch%2C0&cntnt01returnid=296&limit=5&cntnt01searchinput=' + $("#header_query").val().replace(/ /g, "+") + ' #search_results', function() {
										
						//if($("#header_search_results > .container_narrow > .content_padding").html() == '') {
						//	$("#header_search_results > .container_narrow > .content_padding").html("<em>No results found.</em>"); }
							
						var searchCount = $("ul#search_results > li").length;
						 
						if(searchCount == 0) {
							$("#header_search_results > .container_narrow > .content_padding").html("<em>No results found.</em>"); }
						else {
							if(searchCount > 10) {
								$("ul#search_results > li").each(function (i) {

         var thisIndex = $("ul#search_results > li").index(this);
									
         if (thisIndex > 10) {
          $(this).remove(); } });
								
								$("#header_search_results > .container_narrow > .content_padding").append('<a class="read_more" href="http://www.wku.edu/gfcb/index.php?mact=Search%2Ccntnt01%2Cdosearch%2C0&cntnt01returnid=296&limit=5&cntnt01searchinput=' + $("#header_query").val().replace(/ /g, "+") +'">More results</a>');
							}
						}
	
						$("#header_search_results > h1").html("search results");
						$("#header_search_loader").hide(); } ); }
						
			showSearchLoader(); }, 600); } }




// Listener functions:
$(document).ready(function() {

 // Correctly size the main column:
 timeout = setTimeout(function(){

  var leftHeight = $("div#content_left").height();
 	var rightHeight = $("div#content_right").height();
		var tallestHeight = 0;
		
		if(leftHeight > rightHeight) {
			tallestHeight = leftHeight; }
		else {
			tallestHeight = rightHeight; }
  
  $("div#content_main").css("min-height", (tallestHeight -68)); }, 250);
/*
 // 
 $('#header_search').submit(function() {
  performSearch();
		return false; });  

 // Show search results onFocus():
 $("#header_query").focus(function () {
  showSearch(); });

 // Show search results on keyup():
	
 $("#header_query").keyup(function() {
  performSearch(); }); */

 // Toggle menu:'
	
	/* Awaiting content... 
 $('#header_navigation li a').click(function() {

  // Caputre the URL of the link:
  var URL = $(this).attr('href');
		
  // Determine the div to get content from:
  //var targetDiv = '#ml_'+ URL.slice(URL.search('page')+5);
		var targetDiv = 'ml_'+ URL.slice(URL.search('page')+5);
  var targetDiv = targetDiv.replace(/-/g, "_");
	
  // Determine the img to toggle:
  var targetImg = $(this).siblings("img");
		
  if($("#" + targetDiv).css("display") == "block") {
 																																																																				
   closeOpenedItems(); }
		
  else {
			
   closeOpenedItems();
			
			createLoader(200);
			
			makeFloaty('shadowbox', 'fullscreen', '', 100, 1);
 	 maximizeFloaty('#shadowbox');
  	showFloaty('#shadowbox');
			
  	makeFloaty(targetDiv, 'ml_content_wrapper', '<div class="ml_content"></div>', 300, 1);
			
  	var target = $("#tab_target");
   var offset = target.offset();
			
			$('#' + targetDiv).css("top", (offset.top));
			
   $('#' + targetDiv + '> .ml_content').load(URL + ' #content_summary > *', function() {
					
				showFloaty(targetDiv);
    $(targetImg).attr("src", "uploads/v2/images/navigation/header/arrow_open.gif"); });
					
   $('#' + targetDiv).ajaxComplete(function() {
 
    $('#' + targetDiv + '> .ml_content').prepend('<div id="close_mltab" class="close-button" onclick="closeOpenedItems()">close</div>');
    stopLoader();
				
    // Determine the div to toggle:
    var toggleDiv = '#mlc_'+ URL.slice(URL.search('page')+5);
    var toggleDiv = toggleDiv.replace(/-/g, "_");
					
    $(toggleDiv).show(); }); }

 return false; });
  ------------------- */


 // Toggle breadcrumbs:
 $("#breadcrumbs > ul > li > a").hover(function(e) {
  var targetLink = this;
  var targetImg = $(targetLink).children("img");
		
  if(timeout) clearTimeout(timeout);

  timeout = setTimeout(function() {
																																
   // Generate content and show breadcrumb tool tip:
   var ttContent = $(targetImg).attr("alt") + '<br /><a href="' + $(targetLink).attr("href") + '">' + $(targetLink).attr("href") + '</a>';

   // Create TT:
			makeFloaty('breadcrumbs_tip', 'tool-tip', ttContent, 100, 1);
   $("#breadcrumbs_tip").addClass("container_narrow");
			
   // Animate thumbnail:
   $("#breadcrumbs").animate({height: 90}, "fast");
   $(targetImg).animate({width: 75}, "fast");
			
   showFloaty("#breadcrumbs_tip");
			
   // Position tip:
   trackMouse(e, "#breadcrumbs_tip", 20, 0); }, 600);
		
  }, function() {

   // Empty/Hide breadcrumb:
   closeOpenedItems();	

   var targetLink = this;
   var targetImg = $(targetLink).children("img");
		
   if(timeout) clearTimeout(timeout);
		
   // Animate thumbnail:
   $(targetImg).animate({width: 40}, "fast");
   $("#breadcrumbs").animate({height: 60}, "fast"); });

 // Track breadcrumbs with URL:
 $("#breadcrumbs > ul > li > a > img").mousemove(function(e) {
	 trackMouse(e, "#breadcrumbs_tip", 20, 0);
	});


 // Toggle calendar event pop-in:
	$(".event_count > a").click(function(e) {
  // Supress click:
		e.preventDefault();
		
		// Close any open items:
		closeOpenedItems();

  // Caputre the URL of the link:
		var URL = $(this).attr('href');
		
  // Start fullscreen / AJAX loader:
  makeFloaty('shadowbox', 'fullscreen', '', 1100, 1);
	 maximizeFloaty('#shadowbox');
 	showFloaty('#shadowbox');
			
 	createLoader(1200);
		
		// Create TT:
		makeFloaty('calendar_pop', 'pop-in', '', 1300, 1);
		
  // Load content:
		$('#calendar_pop').load(URL + ' div#calendar_events_list > *', function() {

   // Position floaty:
 		centerFloaty('#calendar_pop');
 
   // Show floaty:
 		showFloaty('#calendar_pop');
			
   stopLoader();	}); }); 
});