// Basic javascript to be used on every page.
// ACS - 7/9/10

var okToSubmit = false;

(function($) {
  var cache = [];
  // Arguments are image paths relative to the current page.
  $.preLoadImages = function() {
    var args_len = arguments.length;
    for (var i = args_len; i--;) {
      var cacheImage = document.createElement('img');
      cacheImage.src = arguments[i];
      cache.push(cacheImage);
    }
  }
})(jQuery)

jQuery.preLoadImages("/images/structure/IIHS_logo.gif", "/images/structure/cartSecure.gif");

var popupStatus = 0;
function loadPopup(){  
	//loads popup only if it is disabled  
	if(popupStatus==0){  
		$("#backgroundPopup").css({"opacity": "0.7"});  
		$("#backgroundPopup").fadeIn("slow");  
		$("#popupContact").fadeIn("slow");  
		popupStatus = 1;  
	}  
}  

function disablePopup(){
	//disables popup only if it is enabled
	if(popupStatus==1){
		$("#backgroundPopup").fadeOut("slow");
		$("#popupContact").fadeOut("slow");
		popupStatus = 0;
	}
}

//centering popup
function centerPopup(){
	var myWidth = 0, myHeight = 0;
	if( typeof( window.innerWidth ) == 'number' ) {
		//Non-IE
		myWidth = window.innerWidth;
		myHeight = window.innerHeight;
	} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
		//IE 6+ in 'standards compliant mode'
		myWidth = document.documentElement.clientWidth;
		myHeight = document.documentElement.clientHeight;
	} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
		//IE 4 compatible
		myWidth = document.body.clientWidth;
		myHeight = document.body.clientHeight;
	}

	//request data for centering
	//var windowWidth = $(window).width();
	var windowWidth = myWidth;
	var windowHeight = myHeight;
	var popupHeight = $("#popupContact").outerHeight();
	var popupWidth = $("#popupContact").outerWidth();
	//centering
	$("#popupContact").css({
	"position": "absolute",
	"top": (windowHeight/2-popupHeight/2) + $(window).scrollTop(),
	"left": (windowWidth/2-popupWidth/2)
	});
	//only need force for IE6
	
	$("#backgroundPopup").css({
	"height": $(document).height()
	});
}

function displayPopup() {
	//var randomnumber=Math.floor(Math.random()*3);
	//if ($.cookie('popNewsDay') == 1 || $.cookie('popNewsMonth') == 1 || $.cookie('newsSignedUp') == 1 || randomnumber > 0) {
	// do nothing
	//} else  {
		centerPopup();
		loadPopup();
		$(window).scroll(function () { 
			centerPopup();
		});
		//date = new Date();
        //date.setTime(date.getTime() + (20 * 60 * 1000));
		//$.cookie('popNewsDay', '1', { expires: date, path: '/' });
	//}
}

function readCookie(name) {
	var cookiename = 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(cookiename) == 0) return c.substring(cookiename.length,c.length);
	}
	return null;
}

$(document).ready(function() {
	$("ul#mainNav li").mouseenter(function(){
		$(this).addClass("sfhover");
	});
	$("ul#mainNav li").mouseleave(function(){
		$(this).removeClass("sfhover");
	});
	
	$("a.iihsLink").click(function() {
		displayPopup();
		var temp_link = $(this).attr("href");
		setTimeout(function(){
			disablePopup();
			window.location.href = temp_link;
		}, 3000);
		//setTimeout('alert(\'Blah!\')', 2000);
		return false;
	});
	$('form.iihsLink').submit(function() {
		if (okToSubmit == true) {
			return true;
		} else {
			displayPopup();
			var $this = $(this);
			setTimeout( function(){
				disablePopup();
				$this.submit();
			}, 3000);
			okToSubmit = true;
			return false;
		}
	});
	$("#popupContactClose").click(function(){
		disablePopup();
	});
	$("input.searchTerm").focus(function() {
		$(this).val("");
	});
	$("input.searchTerm").blur(function() {
		if ($(this).val() == "") {
			$(this).val("Enter search term");
		}
	});
	$("a.printPage").click(function() {
		window.print();
	});
	$('a.pop').colorbox({transition:'elastic', speed:500, initialWidth:'300px', initialHeight:'200px', width:'600px', height:'450px', href:function() {
		var url = $(this).attr('href') + "?display_type=popup";
		return url;
	}});
	$('a.testOnline').colorbox({transition:'elastic', speed:500, initialWidth:'300px', initialHeight:'200px', width:'550px', height:'550px'});
	
	// Trk
	trkv_temp = readCookie("trkv");
	trks_temp = readCookie("trks");
	if (trkv_temp != null & trks_temp != null ) {
		//alert("Referral cookie found: " + trkv_temp);
		$('#mainNav a.iihsLink, #mainFooter .otherSites a, #mainContent a[href^="http"]').each(function() {
			href_temp = $(this).attr('href');
			if (href_temp.indexOf('?') != -1) {
				$(this).attr('href',href_temp + '&trkv=' + trkv_temp + '&trks=' + trks_temp);
			} else {
				$(this).attr('href',href_temp + '?trkv=' + trkv_temp + '&trks=' + trks_temp);
			}
		});
		$('#mainContent form.iihsLink').each(function() {
			$(this).append('<input type="hidden" name="trkv" value="' + trkv_temp + '"/><input type="hidden" name="trks" value="' + trks_temp + '"/>');
		});
	}
});
