/**
 * iTracker.js
 * 
 * Handles the dynamic tracking of any javascript events or ajax calls.
 * 
 * @author Chris Willmott <chris.willmott@gforces.co.uk>
 * @copyright G-Forces Web Management Ltd 2009
 * @see /classes/Util/ITracker.php
 * @see /templates/iTracker.html
 * @see /www/ajax.php
 */

/**
 * Sends a request to intellitracker to log dynamic events.
 * @param {String} itAction
 */
function itEvent( itAction, itParams ) {
	
	
//	if ( typeof itAutoCarDetailId != 'undefined' ) {
//		// Gather vehicle information
//		
//		if ( isFinite( itAutoCarDetailId ) ) {
//			var target = '../ajax.php';
//		    var params = 'type=itGetVehicleDetails&itAutoCarDetailId='+itAutoCarDetailId;
//			var getVehicleDetails = new Ajax.Request(target, {
//		        method: 'post',
//		        parameters: params,
//		        onSuccess: function( request ){
//					// Evaluate JSON
//					var script = 'var vehicle = ' + request.responseText;
//					eval( script );
//					
//					// Encode PQRY
//					/** @todo iterate instead */
//					var pqry = encodeURIComponent(
//						'bodyStyle='		+ vehicle.bodyStyle + '&' +
//						'fuelType='			+ vehicle.fuelType + '&' +
//						'marque='			+ vehicle.marque + '&' +
//						'model='			+ vehicle.model + '&' +
//						'price='			+ vehicle.price + '&' +
//						'reg='				+ vehicle.reg + '&' +
//						'transmission='		+ vehicle.transmission + '&' +
//						'leadDestination='	+ vehicle.leadDestination + '&' +
//						'siteArea='	        + vehicle.siteArea + '&' +
//						'vehicleId='		+ itAutoCarDetailId
//					);
//					
//					// Track with intellitracker
//					itTrack( pqry, itAction, itParams );
//		        }
//				// No onFailure, simply ignore failures
//		    });
//		}	
//		
//	} else if ( typeof itCmsPageContentId != 'undefined' ) {
//		// Gather page information
//	
//		if ( isFinite( itCmsPageContentId ) ) {
//			
//			// AJAX
//			var target = '../ajax.php';
//		    var params = 'type=itGetPageDetails&itCmsPageContentId='+itCmsPageContentId;
//			var getPageDetails = new Ajax.Request(target, {
//		        method: 'post',
//		        parameters: params,
//		        onSuccess: function( request ){
//					// Evaluate JSON
//					var script = 'var page = ' + request.responseText;
//					eval( script );
//					
//					// Encode PQRY
//					/** @todo iterate instead */
//					var pqry = encodeURIComponent(
//						'pageTitle=' + page.title + '&' +
//						'siteArea='	 + page.siteArea
//					);
//					
//					// Track with intellitracker
//					itTrack( pqry, itAction, itParams );
//					
//		        }
//		    });
//			
//		}
//		
//	} else {
		// No car detail or page detail, just call the itTrack
		// This is more of a failsafe and will never normally be fired
		
		var pqry = '';
		
		itTrack( pqry, 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( baseHref + '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/?'
				+ $F( 'itAccountId' )
				+ '&'
				+ rand
				+ '&%20&'
				+ pqry
				+ '&iREGQry&iSale&0&0&0&0&0&0&%20&1500&'
				+ virtualUri
				+ '&2';
	
}

/**
 * Generate GUID
 * The GUID needs to be re-generated on each click to prevent duplicates
 */
function itGenerateGuid(){
	
	var target = '../ajax.php';
    var params = 'type=itGenerateGuid';
	var generateGuid = new Ajax.Request(target, {
        method: 'post',
		asynchronous: false,
        parameters: params
    });
	
	return generateGuid.transport.responseText;
	
}