var B = { init: function () { this.browser = this.sS(this.dB) || "unknown"; this.version = this.sV(navigator.userAgent) || this.sV(navigator.appVersion) || "unknown";this.OS = this.sS(this.dO) || "unknown";}, sS: function (d) { for (var i=0;i<d.length;i++){ var dStr = d[i].str; var dPp = d[i].p; this.vSS = d[i].vSch || d[i].id; if (dStr){ if (dStr.indexOf(d[i].substr) != -1) return d[i].id; }else if (dPp) return d[i].id; }}, sV: function (dStr) { var i = dStr.indexOf(this.vSS); if (i == -1) return; return parseFloat(dStr.substring(i+this.vSS.length+1)); }, dB: [{str: navigator.userAgent,substr: "OmniWeb",vSch: "OmniWeb/",id: "OmniWeb"},{str: navigator.vendor,substr: "Apple",id: "Safari"},{p: window.opera,id: "Opera"},{str: navigator.vendor,substr: "iCab",id: "iCab"},{str: navigator.vendor,substr: "KDE",id: "Konqueror"},{str: navigator.userAgent,substr: "Firefox",id: "Firefox"},{str: navigator.vendor,substr: "Camino",id: "Camino"},{str: navigator.userAgent,substr: "Netscape",id: "Netscape"},{str: navigator.userAgent,substr: "MSIE",id: "IE",vSch: "MSIE"},{str: navigator.userAgent,substr: "Gecko",id: "Mozilla",vSch: "rv"},{ str: navigator.userAgent,substr: "Mozilla",id: "Netscape",vSch: "Mozilla"}], dO : [{str: navigator.platform,substr: "Win",id: "Windows"},{str: navigator.platform,substr: "Mac",id: "Mac"},{str: navigator.platform,substr: "Linux",id: "Linux"}] }; 
B.init();


/* -------------------------------------------
	Document Ready
	------------------------------------------- */
$(document).ready(function(){

	// Let's preload the menu2010 background image
	$.preLoadImages("/images/menu2010.png");


	// Show the Menu 2010 when mouse enters one of the 3 global nav items
	$('#GlobalNavigation li').mouseenter(function(){	
			$('.menu').hide(); // hide all the ULs but not the bkg image
			$('#GlobalNavigation li').removeClass('selected');
			$('#menu2010').show(); // show the bkg image
			var linkID = $(this).attr('id');
			var menuID = '#' + linkID.slice(4); // without the 'LINK' part
			$(menuID).show(); // show the appropriate UL
			$(this).addClass('selected');
	}).mouseleave(function(){
		$('#menu2010').hide();
	});
   	
	
	// Hide the Menu 2010 when mouse leaves the Menu 2010
	$('#menu2010').mouseleave(function(){	
		$('#GlobalNavigation li').removeClass('selected');
		$('#menu2010').hide();
		$('.menu').hide();
	}); 	
	
	// Make the whole LI inside the Menu 2010 clickable instead of just its text
	$('#menu2010 li').mouseenter(function(){
		var hrf = $(this).find('a').attr('href');
		$(this).click(function(){window.location=hrf});
		//console.log ('hrf = ' + hrf);
	});
	
	// To prevent the menu from hiding when exiting the Global Nav from the bottom
	$('#menu2010').mouseenter(function(){$('#menu2010').show();});
		
	
		
	// Prepare the callout box for submittal	
	$('#CalloutForm').submit(function(){
		//if (!already_submitted) {
			sendCalloutRequest();
			//already_submitted = true;
		//}
		return false;	
	});
	
}); // end document ready



/* -------------------------------------------
	jQuery plugin to preload images
		Arguments are image paths relative to the current page.
		Call like this: 	$.preLoadImages("image1.gif", "/path/to/image2.png");
	------------------------------------------- */ 
	
(function($) {
	var cache = [];
	$.preLoadImages = function() {
		//console.log ('preloading images');
		var args_len = arguments.length;
		for (var i = args_len; i--;) {
			var cacheImage = document.createElement('img');
			cacheImage.src = arguments[i];
			cache.push(cacheImage);
		}
	}
})(jQuery)



/* set a cookie */
	function setCookie(c_name,value,expiredays)
	{
	var exdate=new Date();
	exdate.setDate(exdate.getDate()+expiredays);
	document.cookie=c_name+ "=" +escape(value)+
	((expiredays==null) ? "" : ";expires="+exdate.toGMTString())+
	";path=/";
	}


/* get a cookie value */
	function getCookie(c_name)
	{
	if (document.cookie.length>0)
	  {
	  c_start=document.cookie.indexOf(c_name + "=");
	  if (c_start!=-1)
		 { 
		 c_start=c_start + c_name.length+1; 
		 c_end=document.cookie.indexOf(";",c_start);
		 if (c_end==-1) c_end=document.cookie.length;
		 return unescape(document.cookie.substring(c_start,c_end));
		 } 
	  }
	return "";
	}
	

/* Set user's Switcher pref in a cookie */
	function SetSwitcherPref (state) {
		setCookie("IntegraSwitcher", state, 365);	
	}
	
/* Get user's Switcher pref from the cookie */
	function GetSwitcherPref() {
		return getCookie ("IntegraSwitcher");	
	}


/* Set the user's market ID in a cookie */
	function SetMarketId (newmarketId){
		setCookie("IntegraMarketId", newmarketId, 365);	
	}

/* Set the user's market Name in a cookie */
	function SetMarketName (newmarketName){
		setCookie("IntegraMarketName", newmarketName, 365);	
	}
	
/* Get the user's market ID from the cookie */
	function GetMarketId() {
		return getCookie ("IntegraMarketId");
	}
	
	
/* Get the user's market Name from the cookie */
	function GetMarketName() {
		return getCookie ("IntegraMarketName");
	}	


/* Show lightbox to let user select market. */
	function DoLightbox(){
		document.getElementById("usermarket").value = GetMarketId();
		Lightbox.init();	
		Lightbox.showBoxByID('market-selector', 350, 230);
	}


/* Send to Prior Lake 
	Redirects to getintegra.com homepage.
	Used by the Solution Finder when someone chooses Prior Lake.
*/
function SendToPL() {
	location.href = '/priorlake.php';
}
	
	
function AjaxMarketName() {
	var xmlHttp;
	try {
		// Firefox, Opera 8.0+, Safari
		xmlHttp=new XMLHttpRequest();
	} catch (e)	{
		// Internet Explorer
		try {
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e)	{
			try {
				xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e)	{
				alert("Your browser does not support AJAX!");
				return false;
			}
		}
	}
	
	xmlHttp.onreadystatechange=function() {
		if(xmlHttp.readyState==4) {
			var newhtml = 'Your Service Area: <a href="javascript:DoLightbox();">' + xmlHttp.responseText + '</a>';
			document.getElementById("YourMarketName").innerHTML = newhtml;
		}
	}
	xmlHttp.open("GET","/includes/ajax_market_name.php",true);
	xmlHttp.send(null);
 }



/* Popup
	Use to make a popup window
*/
function Popup(strURL,strHeight,strWidth) {
	var strOptions = "toolbar,menubar,scrollbars,resizable,location,height="+strHeight+",width="+strWidth;
	window.open(strURL, 'newWin', strOptions);
}



/*-------------------------
	CALLOUT BOX (was calloutbox.js) */
	
	
function ValidateCallout() {
	
	// for debugging
	//return true;
	
	
	// Make sure all fields are filled in
	var name = $("#CalloutBoxName").val();
	var contact = $("#CalloutBoxContact").val();
	var market = $("#CalloutBoxMarket").val();
	var err = "";
	if (name == "" || contact == "" || market == "") {
		alert("Please complete all fields to submit this form.");
		return false;
	}
	

	// Check the phone number on the server side
	$.ajax({
		type: "POST",
		async: false,
		url: "/includes/functions_form_validation.php", 
		data: {validation : "phone", test_str : $("#CalloutBoxContact").val()},
		success: function(data){
			if (data == '0') {
				alert("Invalid phone number");
				retval = false;
			} else {
				retval = true;
			}
		}
	});
	
	return retval;
	
}



	
function sendCalloutRequest(){
	
	if (ValidateCallout()) {

		var parms = {};
		
		parms.CalloutBoxName = $("#CalloutBoxName").val();
		parms.CalloutBoxContact = $("#CalloutBoxContact").val();
		parms.CalloutBoxMarket = $("#CalloutBoxMarket").val();
		parms.CalloutBoxReferringUrl = $('#CalloutBoxReferringUrl').val();
		parms.PageTitle = $('#PageTitle').val();
		parms.adwordsReporter = $('#adwordsReporter').val();
		
		$.post('/contact/order_service_processor.php', parms, function(){
			window.location.replace("/services/contact_confirm.php");
		});
	}		
}




