$(document).ready(function() {

		// Preload all rollovers
		$("#nav img").each(function() {
			// Set the original src
			rollsrc = $(this).attr("src");
			rollON = rollsrc.replace(/.gif$/ig,"_over.gif");
			$("<img>").attr("src", rollON);
		});
		
		// Navigation rollovers
		$("#nav a").mouseover(function(){
			imgsrc = $(this).children("img").attr("src");
			matches = imgsrc.match(/_over/);
			
			// don't do the rollover if state is already ON
			if (!matches) {
			imgsrcON = imgsrc.replace(/.gif$/ig,"_over.gif"); // strip off extension
			$(this).children("img").attr("src", imgsrcON);
			}
			
		});
		$("#nav a").mouseout(function(){
			$(this).children("img").attr("src", imgsrc);
		});
		
		// Rollovers for 'Help' images on homepage
		$("a#helpleft").mouseover(function() {
			$("#helpleft img").attr("src", "/lib/images/hp_help_left_over.jpg");
			$("#helpmid img").attr("src", "/lib/images/hp_help_mid_over.jpg");
		});
		
		$("a#helpright").mouseover(function() {
			$("#helpright img").attr("src", "/lib/images/hp_help_right_over.jpg");
			$("#helpmid img").attr("src", "/lib/images/hp_help_mid_over.jpg");
		});
		
		$("a#helpmid").mouseover(function() {
			$("#helpmid img").attr("src", "/lib/images/hp_help_mid_over.jpg");
			$("#helpleft img").attr("src", "/lib/images/hp_help_left_over.jpg");
			$("#helpright img").attr("src", "/lib/images/hp_help_right_over.jpg");
		});
		
		$("a#helpmid, a#helpleft, a#helpright").mouseout(function() {
			$("#helpmid img").attr("src", "/lib/images/hp_help_mid.jpg");
			$("#helpleft img").attr("src", "/lib/images/hp_help_left.jpg");
			$("#helpright img").attr("src", "/lib/images/hp_help_right.jpg");
		});
		
		
		// Determine the active section button
		if (location.pathname.substring(1)) {
			// get the path
			thePath = location.pathname.substring(1);
			thePath = thePath.match(/^\/?([a-z0-9_\.-]+)/); // only get the first part of the path

			thePath = '/' + thePath[0];
			
			// edge cases for the hotels and poi paths
			if (thePath == '/hotels' || thePath == '/poi') {
				thePath = '/facilities';
			}
			
			navLink = $('#nav a[@href$="' + thePath + '"]');
			imgsrc = $(navLink).children("img").attr("src");
			if (imgsrc) {
			imgsrcON = imgsrc.replace(/.gif$/ig,"_over.gif"); // strip off .gif
			$(navLink).children("img").attr("src", imgsrcON);
			}
		}
		
	// Every 7th Tier 3 result, clear for a new line
	//$('div.tier3').filter(':nth-child(7n)').css("clear","left");
	
	//$('div.tier2').filter(':nth-child(3n)').css("clear","left");


	// AJAX Functions for Facility search
	
	$('#search_country').change( function() {
			getDrops('country', this.value, '');
		});


		// Triggers when an ajax request starts
		 $("#loading img").ajaxStart(function(){
	   		$(this).show();
	 		});
	 	$("#loading img").ajaxSuccess(function(){
	   		$(this).hide();
	 		});

	



	// Swap out dropdowns, loads ajax_getdrops.php
	function getDrops(type,value,other) {
	
		$.ajax({
		    type: "GET",
		   	url: "/lib/modules/ajax_getdrops.php",
		   	data: "type="+type+"&value="+value+"&other="+other,
		   	dataType: "json",
		   	success: function(json){
		   	
		   		dropCountry = json.country;
		   		dropState = json.state;
		   		dropCity = json.city;
		   		
		   		if (type == 'country') {
				$("select#search_state").html(dropState);
				
				//$("select#search_city").html(dropCity);
				$("#search_state option[@value='All']").attr("selected", "selected");
				}
   				} // end success
		 }) // end $.ajax
		 
	} // end getDrops

	$("#quicksearch select").change(function() {
		$("#quicksearch").trigger('submit'); 	
		});
	$("#quicksubmit").click(function() {
		$("#quicksearch").trigger('submit'); 
	});
	

    /* function adjustFooter() { 
     	// Get the window height
     	divheight = $(window).height();
     	if (divheight > 720) {
     	newheight = divheight - 371;
     	$("div#hpwrap").css("height", newheight);
     	}
     } 
     $(window).bind('resize', adjustFooter).trigger('resize'); 
	*/


	// Rotate customer logos on homepage
	var logos = new Array('att','covad','ibm','nokia','redhat','salesforce','sap');
	
	 	// how many logos are available?
		totalNum = logos.length; 
		// pick a random number
		rndNum = Math.ceil(Math.random() * totalNum);
		rndNum = rndNum - 1; //allow for the array index
		$("#customers").html('<img src="/lib/images/customer_' + logos[rndNum] + '.gif" alt="" width="185" height="55" />').fadeIn();
	
		setInterval(quote_rotate,5000); //time in milliseconds
	
		function quote_rotate() {
			// fade the image out, then swap the img src and fade it back in
			$("#customers").fadeOut(function() {
					$(this).hide();
					rndNum = Math.ceil(Math.random() * totalNum);
					rndNum = rndNum - 1; //allow for the array index
					$("#customers").html('<img src="/lib/images/customer_' + logos[rndNum] + '.gif" alt="" width="185" height="55" />');
					$("#customers").fadeIn();
			});
		}

	


});
