var requestSentTime = null;

var savedLinks = Array();

// Set the window load function
jQuery(document).ready(Initialize);

// Page load
function Initialize()
{ 
	// Preload images
	var img;
	img = new Image();
	img.src = "/Images/NewSearchCards/add_to_watchlist_saved_state.gif";
	img = new Image();
	img.src = "/Images/NewSearchCards/add_to_watchlist_saving_state.gif";
	img = new Image();
	img.src = "/Images/NewSearchCards/add_to_watchlist_watchlist_state.gif";
	img = new Image();
	img.src = "/Images/NewSearchCards/add_to_watchlist_over.gif";
			
}

// Watchlist button clicked
function WatchlistButtonClick(variableString)
{
		
	// Check if we can create a XMLHTTP object, otherwise default to postback model
	if ( ! CreateXMLHTTPObj() )
		return true;
		
	var variableArray = variableString.split("~");
	var listingId = variableArray[0];
	var listingType = variableArray[1];
	var linkId = variableArray[2];
	var mcat = variableArray[3];
	
	// Don't click twice
	if( savedLinks[linkId] != null ) return false; 
		
	// Change the style to saving
	document.getElementById(linkId).className = "saveToWatchlistLink linkSaving spriteButton";
				
	// track
	GoogleTracking(mcat);
	
	// Call the AJAX functions
	return SaveToWatchlist(listingId, listingType, linkId);
}

// AJAX call
function SaveToWatchlist(listingId, listingType, linkId) 
{
	
	// Set the AJAX calls URL
    var targetUrl = "/API/Ajax/Watchlist.aspx?useSavedReminderValues=true&id=" + listingId + "&type=" + listingType + "&linkId=" + linkId + "&fromSearchCard=true" + "&initalUrl=" + encodeURIComponent(decodeURIComponent(document.getElementById(linkId).href));
			
	requestSentTime = new Date();
				
	// Execute the AJAX statement
	return SendXMLHTTP( targetUrl, 'CallBack(false,"'+ linkId +'");' );
}

// Call back function
function CallBack( isSuccessful, linkId)
{
	var delay = 0;
	
	// Something went wrong, redirect to postback 
	if( ! isSuccessful )
	{
	    location.href = document.getElementById(linkId).href;
		return;
	}
	
	savedLinks[linkId] = linkId;
						
	// Fake a delay if the request comes back to quickly
	if( requestSentTime != null )
	{
		delay = new Date().getMilliseconds() - requestSentTime.getMilliseconds() - 1000;
		if( delay < 0 )
			delay = delay * -1;
		else
			delay = 0;
	}		
	
	// Wait till at least one second has passed then update UI
	setTimeout('UpdateButtonToSaved("' + linkId + '");', delay);
}

// Change the button to the 'Saved' state
function UpdateButtonToSaved(linkId)
{
	isSaved = true;
	document.getElementById(linkId).className = "saveToWatchlistLink linkSaved spriteButton";
}


function LogIn(redirect)
{
	//alert(redirect);
	location.href = "/Members/Login.aspx?url=" + encodeURIComponent(decodeURIComponent(redirect));
}


function GoogleTracking(mcat) {
    // This will track any Google conversions from AdWords
    var jobs = "5000-";
    var prop = "0350-";
    var moto = "0001-";

    var base = mcat.substr(0, 5);
    
    var value = "";
    var google_conversion_id;
    var google_conversion_label;
  
    if (base == jobs) {

        google_conversion_id = 1065748612;
        google_conversion_label = "gxHTCL6AbRCEkZj8Aw";

    } else if (base == prop) {

        google_conversion_id = 1066491863;
        google_conversion_label = "ksscCKPWZRDXv8X8Aw";

    } else if (base == moto) {

        google_conversion_id = 1066492043;
        google_conversion_label = "_d7oCIe2YRCLwcX8Aw";
        value = "&value=1.0";
        
    } else {

        google_conversion_id = 1039142312;
        google_conversion_label = "x4lZCI7jbBCom8DvAw";

    }

    var tracker = new Image();
    tracker.src = "http://www.googleadservices.com/pagead/conversion/" + google_conversion_id + "/?label=" + google_conversion_label + "&script=0" + value;

}
