/// <reference path="/js/jquery-1.6.1.js" />
function SetOmnitureData_DealerDetails(linkName, code){
	$.ajax({
	    type: "POST",
	    contentType: "application/json; charset=utf-8",
	    url: "/Services/Omniture.asmx/SetOmnitureData_DealerLinks",
	    data: '{linkName: "' + linkName + '", paCode: "' + code + '"}',
	    dataType: "json",
	    success: GetOmnitureDataFromSession_Success,
	    error: GetOmnitureDataFromSession_Fail
	});
}

function SetOmnitureData_DealerLanding_Click(dealerName, dealerPACode, clickEvent) {
    $.ajax({
        type: "POST",
        contentType: "application/json; charset=utf-8",
        url: "/Services/Omniture.asmx/SetOmnitureData_DealerLanding_Click",
        data: '{dealerName: "' + dealerName + '", dealerPACode: "' + dealerPACode + '", clickEvent: "' + clickEvent + '"}',
        dataType: "json",
        success: GetOmnitureDataFromSession_Success,
        error: GetOmnitureDataFromSession_Fail
    });
}

function SetOmnitureData_DealerDetailsMap(code) {
	$.ajax({
		type: "POST",
		contentType: "application/json; charset=utf-8",
		url: "/Services/Omniture.asmx/SetOmnitureData_HomeDealerDetailsMap",
		data: '{paCode: "' + code + '"}',
		dataType: "json",
		success: GetOmnitureDataFromSession_Success,
		error: GetOmnitureDataFromSession_Fail
	});
}

function DownloadCreditApp() {
	try {
		if (typeof(paCode) != 'undefined')
		{
			if (typeof(clickName) != 'undefined') {
				SetOmnitureData_DealerDetails(clickName, paCode); //click name set by control
			}else {
				SetOmnitureData_DealerDetails('fpol: register: dealer credit app: pdf', paCode);
			}
		}
		
		if (typeof(creditAppUrl) != 'undefined')
		{
			var frmCreditApp = $get('frmCreditApp');
			if (frmCreditApp!= null)
			{
				frmCreditApp.src = creditAppUrl;
			} else {
				alert('Credit App IFrame is unavailable.');
			}
		}
	}
	catch(ex){
		alert("Errors occured attempting to download Credit Application.");
	}
}

//this function will bind the mouseleave events which call the
//OnGetSpecialsCloseUp to close the popup
function SetupShopWithPopup()
{
	$(document.body).children('#dvShopWith').remove();
	$('#divGetSpecials').bind('mouseleave', function(e){ OnGetSpecialsCloseUp(e); });
	$('#dvShopWith').bind('mouseleave', function(e) { OnGetSpecialsCloseUp(e); });
	$('#dvShopWith').width($('#divGetSpecials').width());
}

//this function is called when divGetSpecials is clicked.  divGetSpecials
//is a container element for the cms managed special offers image	
function OnGetSpecialsClick(link)
{
	//grab boolean value from hidden field
	var isCurrentDealerSelected = $('#' + hdnIsCurrentDealerSelectedID).val();
	
	//if the current dealer for the dealer details is the
	//selected dealer then just redirect the user to garage
	//sale otherwise show the ShopWith popup
	if(isCurrentDealerSelected == 'true')
		RedirectToGetSpecials();
	else
		ShowShopWithPopup(link);
}

//this function is called when divGetSpecials is clicked.  divGetSpecials
//is a container element for the cms managed special offers image
function OnDealerPromotionClick(link) {
    //grab boolean value from hidden field
    var isCurrentDealerSelected = $('#' + hdnIsCurrentDealerSelectedID).val();

    //if the current dealer for the dealer details is the
    //selected dealer then just redirect the user to garage
    //sale otherwise show the ShopWith popup
    if (isCurrentDealerSelected == 'true') {
        //return true;
        RedirectToGetSpecials();
    } else {
        ShowShopWithPopup($(link).parent("div"));
        //return false;
    }
}

//this function redirects to garage sale
function RedirectToGetSpecials() {
	DealerPromoOmnitureGarageSale();
	location.href = '/commerce/GarageSale.aspx';
}

//a function to reposition and show the ShopWith popup
function ShowShopWithPopup(link)
{
	//get the div that contains the popup
	var elm = document.getElementById('dvShopWith');
	
	//only position and adjust the popups DOM location
	//if it wasn't already done before
	if(elm.parentNode.tagName != 'body')
	{
		//grab link position coordinates to assist in positioning popup
		var pos = $(link).offset();
		var eHeight = $(link).outerHeight();	
			
		//remove the div which is currently nested deep within the DOM and
		//append it to the body -- this helps with z-index and positioning bugs
		document.getElementById('dvShopWith').parentNode.removeChild(document.getElementById('dvShopWith'));
		document.body.appendChild(elm);

		//finally, position the div
		document.getElementById('dvShopWith').style.left = parseInt(pos.left) + 'px';
		document.getElementById('dvShopWith').style.top = parseInt(pos.top) + eHeight + 'px';
	}
		
	//show the popup using jquery slide technique		
	$('#dvShopWith').slideDown('fast');	
}

//an event handler for closing the popup depending
//on the event target
function OnGetSpecialsCloseUp(e)
{	
	if(e.relatedTarget != null && e.relatedTarget.id != 'dvShopWith' && !$(e.relatedTarget).parents().is("#divGetSpecials") && !$(e.relatedTarget).parents().is("#dvShopWith")){	
		$('#dvShopWith').slideUp('fast'); 
	}
	else
		e.stopPropagation();
}

function LoadFedExPopup() {
    var title = $('span#spanFedExFlatRateContentTitle').text();
    if (title == "") title = 'FedEx Flat Rate Shipping Rates';

    LoadLazyContentPopup('Dealer Landing', 'FedEx Flat Rate Content', 'FedExPopup', title);

    //call omniture event
    $.ajax({
    	type: "POST",
    	contentType: "application/json; charset=utf-8",
    	url: "/Services/Omniture.asmx/SetOmnitureData_DealerLanding_FedEx",
    	data: '{dealerName: "' + escape($('a.DealerName').text()) + '", dealerPACode: "' + paCode + '"}',
    	dataType: "json",
    	success: GetOmnitureDataFromSession_Success,
    	error: GetOmnitureDataFromSession_Fail
    });
}

function DealerPromoOmniture(promoType) {
	var sProp5 = escape("fpol: dealer promo: " + $('a.DealerName').text() + ": " + promoType);
		
	$.ajax({
		type: "POST",
		contentType: "application/json; charset=utf-8",
		url: "/Services/Omniture.asmx/SetOmnitureData_DealerLanding_DealerPromo",
		data: '{sProp5: "' + sProp5 + '", dealerName: "' + escape($('a.DealerName').text()) + '", dealerPACode: "' + paCode + '"}',
		dataType: "json",
		success: GetOmnitureDataFromSession_Success,
		error: GetOmnitureDataFromSession_Fail
	});
}

function DealerPromoOmnitureGarageSale() {
	$.ajax({
		type: "POST",
		contentType: "application/json; charset=utf-8",
		url: "/Services/Omniture.asmx/SetOmnitureData_DealerLanding_DealerPromoGarageSale",
		data: '{dealerName: "' + escape($('a.DealerName').text()) + '", dealerPACode: "' + paCode + '"}',
		dataType: "json",
		success: GetOmnitureDataFromSession_Success,
		error: GetOmnitureDataFromSession_Fail
	});
}

