/********************************
 *Intellitracker
 *///////////////////////////////



/**
 * Submits form data to Intellitracker.
 */
function itForm(itData){

	var src = itData.protocol + "://www.intelli-direct.com/e/t3.dll?"
	        + netdirector.intellitracker.id + "&0&%20&" + itData.pqry + "&"
			+ itData.rqry+"&"+itData.sqry+"&0&0&0&0&0&"
			+ "0&%20&1500&%20&0";


	var image = new Image();
	image.src = src;

}


/**
 * Sends a request to intellitracker to log dynamic events.
 * @param {String} itAction
 */
function itEvent( itAction, itParams ) {

	if ( typeof netdirector.intellitracker.stockDetail != 'undefined' ) {
		// Gather vehicle information

		var stockDetail = netdirector.intellitracker.stockDetail;

		var pqry = encodeURIComponent(
			'bodyStyle='		+ stockDetail.bodyStyle + '&' +
			'fuelType='			+ stockDetail.fuelType + '&' +
			'marque='			+ stockDetail.marque + '&' +
			'model='			+ stockDetail.model + '&' +
			'price='			+ stockDetail.price + '&' +
			'reg='				+ stockDetail.reg + '&' +
			'transmission='		+ stockDetail.transmission + '&' +
			'leadDestination='	+ stockDetail.leadDestination + '&' +
			'siteArea='	        + stockDetail.siteArea + '&' +
			'vehicleId='		+ stockDetail.vehicleId
		);

		itTrack( pqry, itAction, itParams );

	} else if ( typeof netdirector.intellitracker.pageDetail != 'undefined' ) {
		// Gather page information

		var page = netdirector.intellitracker.pageDetail;

		var pqry = encodeURIComponent(
			'pageTitle=' + page.title + '&' +
			'siteArea='	 + page.siteArea
		);

		itTrack( pqry, itAction, itParams );

	} else {
		// No car detail or page detail, just call the itTrack
		// This is more of a failsafe, we hope it won't be fired, maybe from a
		// list page.

		// Try and extract siteArea, medium and device (if available) from page pqry

		try {

			// Defaults
			var pqry = {
				siteArea : 'GROUP',
				medium   : 'web'
			};

			var pqryFromPage = $.unserialise( unescape( window.pqry ) );

			if ( typeof pqryFromPage.siteArea != 'undefined' ) {
				pqry.siteArea = pqryFromPage.siteArea;
			}
			if ( typeof pqryFromPage.medium != 'undefined' ) {
				pqry.medium = pqryFromPage.medium;
			}
			if ( typeof pqryFromPage.device != 'undefined' ) {
				pqry.device = pqryFromPage.device;
			}

			itTrack( escape( $.param( pqry ) ), itAction, itParams );

		} catch ( e ) {

			// Otherwise just pass through with a default site area of group
			itTrack( 'siteArea%3DGROUP', itAction, itParams );

		}


	}

}

/**
 * Get the tracking image from intellitracker
 * @param {string} pqry
 * @param {string} itAction
 * @param {object} itParams
 */
function itTrack( pqry, itAction, itParams ) {

	// Add the action
	pqry += encodeURIComponent( '&action=' + itAction );

	// Add any additonal parameters
	if ( itParams != null ) {
		for ( var key in itParams ) {
			if ( itParams.hasOwnProperty( key ) ) {
				pqry += encodeURIComponent( '&' + key + '=' + itParams[key] );
			}
		}
	}

	// Encode Virtual URI
	var virtualUri = encodeURIComponent( netdirector.baseUrl + '/clickEvent' );

	// Get tracking image from intellitracker
	var rand = Math.ceil( Math.random() * 1000000 );

	var iTrack = new Image();
	iTrack.src	= 'http://www.intelli-direct.com/cr/?'
				+ netdirector.intellitracker.id
				+ '&'
				+ rand
				+ '&%20&'
				+ pqry
				+ '&iREGQry&iSale&0&0&0&0&0&0&%20&1500&'
				+ virtualUri
				+ '&2	';


    // Allows links to be tracked before going to the link
	if ( typeof window.gohere == 'string' ) {
        iTrack.onload = function(){
            window.open( window.gohere, '_blank' );
        }
    }

    // Allows links to be tracked before going to the link and open in the same window
	if ( typeof window.goherecustom == 'string' ) {
        iTrack.onload = function(){
            window.location = window.goherecustom;
        }
    }

}

/**
 * Generate GUID
 * The GUID needs to be re-generated on each click to prevent duplicates
 */
function itGenerateGuid(){

	var target = netdirector.baseUrl + '/' + netdirector.franchiseUrl + 'frontend-operations/it-generate-guid';

	$.ajax({
		url : target,
		success : function (guid){
			itGuid = guid;
		}
	});
}



// JQuery Unserialize v1.0 by James Campbell
(function($){
	$.unserialise = function(Data){
        var Data = Data.split("&");
        var Serialised = new Array();
        $.each(Data, function(){
            var Properties = this.split("=");
            Serialised[Properties[0]] = Properties[1];
        });
        return Serialised;
    };
})(jQuery);

