/* Author:
Brandon McQueen
*/
// jQuery on load
$(function(){
	
// Add zoom and target to .pdf links
	$("a[href*='.pdf']").each(function(){
		var href = $(this).attr("href");
		$(this).attr("href", href+"#zoom=90").attr("target", "_blank");								  
	});
		
// Header menu	
	$('.parent').mouseenter(function(){
		$(this).children('a:first').addClass('bg');
		$(this).children('ul').slideDown(150);
	}).mouseleave(function(){
		$(this).children('a:first').removeClass('bg');
		$(this).children('ul').hide();
	});
	
//  Homepage Fader
	if ( $('#main').hasClass('homepage') ){		
		// Set some stuff up
		var slideshow = $('#slideshow');
		
		// Get the number of items. This is going to be used a lot.
		var num = how_many();
		
		// Set the z-index for the <li> stack
		slideshow.find('li').each(function(){
			$(this).css('z-index', num - $(this).index());						 
		});
		
		// Set the first items to .active
		slideshow.find('ul li:first-child').addClass('active');
		
		// Create bullets
		createBullets(slideshow);		
		
		// Run the fader
		start_fade(num);
	}	
	
// Show / hide stuff
	$(".toggle-me").hide();
	$(".show-trigger").show();
	$(".toggle-trigger").click(function(){
		$(this)
			.toggleClass("show-trigger")
			.prev(".toggle-me").toggle("fast");
		return false;
	});
	
// Sales representative
	$("#sales_region").change(function(){
		$("#country").slideUp();
		$("#zip").slideUp();
		$("#company").slideUp();
		var selected_region = $(this).val();
		
		if(selected_region != "USA")
		{
			$.getJSON("/ajax.php", {region: selected_region, action: "get_country_or_zip"},
					function(returnVal){
					  var i = 0;
					  var tmp = '';
					  //create select for locations
					  if(selected_region == "Canada")
					  {
						tmp = "Select Region: <select id=\"sales_location\">";
						tmp += "<option value=\"\">Select Location</option>";
					  }
						
					else
					{
						tmp = "Select Country: <select id=\"sales_location\">";
						tmp += "<option value=\"\">Select Country</option>";
					}
						
					  for(i = 0; i < returnVal.length; i++)
					  {
						  tmp += "<option value=\""+returnVal[i]+"\">"+returnVal[i]+"</option>";
					  }
					  tmp += "</select>";
					  
					  $("#country").html(tmp);
					  $("#country").slideDown();
					  
		  });
		}
		else
		{
			$("#zip").slideDown();
		}
	});
	
	$("#sales_location").live("change",function(){
		$.getJSON("/ajax.php", {loc: $(this).val(), action: "get_company"},
				  function(returnVal){
					var i = 0;
					var tmp = '';
					//create select for locations
					tmp = "<p>";
					
					for(i = 0; i < returnVal.length; i++)
					{
						if(returnVal[i]["type"])
							tmp += "<h3>"+returnVal[i]["type"]+"</h3><br/>";
						tmp += "Company Name: "+returnVal[i]["name"];
						tmp += "<br />";
						tmp += "Address: "+returnVal[i]["address"];
						if(returnVal[i]["city"])
							tmp += ", "+returnVal[i]["city"];
						if(returnVal[i]["state"])
							tmp += ", "+returnVal[i]["state"];
						if(returnVal[i]["zip"])
							tmp += ", "+returnVal[i]["zip"];
						if(returnVal[i]["country"])
							tmp += ", "+returnVal[i]["country"];	
						
						tmp += "<br />";
						tmp += "Phone Number: "+returnVal[i]["phone"];
						tmp += "<br />";
						tmp += "Fax: "+returnVal[i]["fax"];
						tmp += "<br />";
						tmp += "Email Address: <a href=\"mailto:"+returnVal[i]["email"]+"\">"+returnVal[i]["email"]+"</a>";
						tmp += "<br />";
						if(returnVal[i]["website"])
							tmp += "Website: <a href=\"http://"+returnVal[i]["website"]+"\" target=\"_blank\">"+returnVal[i]["website"]+"</a><br />";
						
					}
					
					$("#company").html(tmp);
					$("#company").slideDown();
					
		});
	});
	
	$("#zip_button").live("click",function(){
		if($("#zip_code").val() == "")
		{
			$("#zip_code").css("background-color", "red");
			return false;
		}
		
		$.getJSON("/ajax.php", {zip: $("#zip_code").val(), action: "get_us_company"},
				  function(returnVal){
					var i = 0;
					var tmp = '';
					//create select for locations
					tmp = "<p>";
					
					for(i = 0; i < returnVal.length; i++)
					{
						if(returnVal[i]["type"])
							tmp += "<h3>"+returnVal[i]["type"]+"</h3><br/>";
						tmp += "Company Name: "+returnVal[i]["name"];
						tmp += "<br />";
						tmp += "Address: "+returnVal[i]["address"];
						if(returnVal[i]["city"])
							tmp += ", "+returnVal[i]["city"];
						if(returnVal[i]["state"])
							tmp += ", "+returnVal[i]["state"];
						if(returnVal[i]["zip"])
							tmp += ", "+returnVal[i]["zip"];
						if(returnVal[i]["country"])
							tmp += ", "+returnVal[i]["country"];	
						
						tmp += "<br />";
						tmp += "Phone Number: "+returnVal[i]["phone"];
						tmp += "<br />";
						tmp += "Fax: "+returnVal[i]["fax"];
						tmp += "<br />";
						tmp += "Email Address: <a href=\"mailto:"+returnVal[i]["email"]+"\">"+returnVal[i]["email"]+"</a>";
						tmp += "<br />";
						if(returnVal[i]["website"])
							tmp += "Website: <a href=\"http://"+returnVal[i]["website"]+"\" target=\"_blank\">"+returnVal[i]["website"]+"</a><br />";
						
					}
					
					$("#company").html(tmp);
					$("#company").slideDown();
					
		});
	});
	
// Set section cookie for shared pages when link is clicked from main menu
// Cookie handled in index.php for submenu clicks	
	$('nav a[href="custom-pumps-and-systems"]').click(function() {
		var sec = $(this).closest(".subnav").prev('.sec').text();
		if ( sec == 'Pumps' ){
			createCookie("Section", 1);
		} else if ( sec == 'Systems' ){
			createCookie("Section", 2);
		}
	});
	$('nav a[href="aftermarket-services"]').click(function() {
		var sec = $(this).closest(".subnav").prev('.sec').text();
		if ( sec == 'Pumps' ){
			createCookie("Section", 1);
		} else if ( sec == 'Systems' ){
			createCookie("Section", 2);
		}
	});	
	
// Trigger Google site search	
	$('.searchSubmit').click(function(){
		var searchTerm = $('#search input.searchBox').val();		
		siteSearch(searchTerm);
	});
	

	
});

// CUSTOM FUNCTIONS
function how_many(){
	var num = $('#slideshow li').length;	
	return num;
}

function start_fade(num){
	// Run it!
	var startup = setInterval(function(){
		// Pass the number of <li>s in the stack
		fade(num);						   
	}, 5000);	
}

function fade(num){
	var current = $('#slideshow li.active');
	var index = current.index();
	
	current.fadeOut('fast');
	// This affects both slides and bullets
	$('.fading-items .active').removeClass('active');
	// If this is the last <li> in the stack, reset stuff (remember that .index() is 0-based)
	if ( index == num - 1 ){			
		$('#slideshow li:first').fadeIn('fast', function(){
			$('#slideshow li').css('display', 'block');		
		});
		$('.fading-items').each(function(){
			$(this).find('li:first').addClass('active');		
		});
	} else {
		// Otherwise just transition to next <li>
		$('.fading-items').each(function(){
			$(this).children().eq(index).next().addClass('active');	
		});
	}	
}

function createBullets(slideshow){
	var num = slideshow.find('ul').children('li').length;
	var i;
	for ( i=0; i<num; i++ ){
		$("#slideshow-bullets ul").append('<li></li>');
	}
	$('#slideshow-bullets ul li:first-child').addClass('active');	
}

// Google site search
function siteSearch(searchTerm){
	window.location='/search?cx=002280292627175214463:gaguf4l3ibk&cof=FORID%3A11&ie=UTF-8&q='+searchTerm+'&sa=Go';
}


// Cookies
// http://www.quirksmode.org/js/cookies.html
function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}






