hld_documentReady("leadgenPopup");

function leadgenPopup()
{
	$('#loadAjaxMessage').jqm();
	leadgenAddModalTimout();
}

var leadgenLoadSavedPage = function(){
	$("#loadAjaxMessage").jqmShow();
	$("#loadAjaxMessage").load(hld_resource_path + "/html/leadgen.html",
		 function(responseText)
		 {
			fireOffPngFix();
			$("#popupTestDriveImage").attr('src', hld_resource_path + "/images/popups/test_drive.jpg");	  
		 	$("#popupBrochureImage").attr('src', hld_resource_path + "/images/popups/brochure.jpg");	  
		 	$("#popupPriceImage").attr('src', hld_resource_path + "/images/popups/price.jpg");	  
		 	$("#closeWindow").attr('src', hld_resource_path + "/images/popups/closePopup.gif");	  
		 	$("#popupTitle").attr('src', titleImage);	  

		 	$("#popupVehicle").attr('src', leadgenCarImage);	  
		 	$("#popupVehicleName").html(carName);	  
		 	
		 	$("#popupTestDriveLink").attr('href', popupTestDriveLink);	  
		 	$("#popupBrochureLink").attr('href', popupBrochureLink);	  
		 	$("#popupPriceLink").attr('href', popupPriceLink);	  
		 	
		 	//Set the leadgen Cookie
		 	//alert(hld_tracking_id);
		 	$.cookie("Vehicle-"+carName, 'shown', {expires: 7});
		 	
		 	addLeadgenCloseWindowButton();
		 }
	 );
};

function leadgenAddModalTimout()
{
	if(hld_dealer_mode == false)
	{
		//leadgenLoadSavedPage();
		var delay = 1000 * 60 * leadgenDelay;
		// Look for the shown cookie
		//alert('referrer' + document.referrer);
		//alert(carName);
		
		// If the referring document does is not the carName we are to 
		// Reset the timer anyway
		var resetTimer = false;
		if(!(document.referrer.indexOf(carName) > -1))
		{
			var resetTimer = true;
		}
		
		var currentCookie = $.cookie("Vehicle-"+carName);
		//alert(currentCookie);
		
		// If it is not shown or the referring page is not the 
		// same as this vehicle reset
		if((currentCookie != 'shown' && currentCookie == undefined) || (currentCookie != 'shown' && resetTimer))
		{
			//alert("current cookie is undefined set new timestamp");
			var start = new Date();
			$.cookie("Vehicle-"+carName, start.getTime()+'', {expires: 7});
			//alert("current cookie is undefined set new timestamp " + delay);
			var id = setTimeout(leadgenLoadSavedPage, delay);	
		}else if(currentCookie != 'shown')
		{
			// The current cookie timestamp has been set read it
			var end = new Date();
			var difference = end.getTime() - parseInt( currentCookie);
			//alert(delay + ' ' + difference);
			if(difference > 0 &&  delay > difference)
			{
				var newDelay = delay - difference;
				//alert("show after delay - difference " + newDelay);
				var id = setTimeout(leadgenLoadSavedPage, newDelay);	
			}else
			{
				leadgenLoadSavedPage();
			}
		}
		
	}
}

function clearAllCarCookies()
{
	var cookies = document.cookie.split(';');
    for (var i = 0; i < cookies.length; i++) 
    {
		var cookie = jQuery.trim(cookies[i]);
		//alert(cookie);
		// Get the nave portion
		var name = splitValue(cookie, "=", [0]);
		if(name.indexOf("Vehicle-") > -1)
		{
			$.cookie(name, '', {expires: 0});
		}
	}
}

function addLeadgenCloseWindowButton()
{
	$("#closeWindowLink").unbind();
	$("#closeWindowLink").click(function()
	 {
		$("#loadAjaxMessage").jqmHide();
		return false;
	 });
}