/* Click tracking parser */
var regEx = /(goto|transfer)\/(.*)\.html/i;
var featuredRegEx = /(featured)\/(.*)\.html/i;

$(document).ready(function() {
	
	$("a[href*='/goto/'], a[href*='/transfer/'], a[href*='/featured/']").click(function() { 
		if( featuredRegEx.exec( this.href ) != null )
		{
			p = featuredRegEx.exec( this.href )[2];

			return cLT( this, 2, p );
		} else { 

			if( regEx.exec( this.href ) != null )
			{
				p = regEx.exec( this.href )[2];
	
				return cLT( this, 1, p );
			}
		}
	});
	$("#conv-slot-toplist a, #conv-slot-banner a").click(function() { 
		return trackMboxClick( this, 'slots-tournaments' )
	});

});

function trackMboxClick( obj, tracker )
{
	
	href = $(obj).attr('href');

	if( href.indexOf( '#' ) != -1 )
	{
		return true;
	}
		
	href = href.substr( href.indexOf( obj.pathname ) );

	if( href.substr( 0, 1 ) != '/' )
	{
		href = '/' + href;
	}

	var mboxType = '-review';
	
	if( regEx.exec( href ) != null )
	{
		var mboxType = '-lead';
	}
	else if ( href == '/us-online-casinos.html' || href == '/best-rated-casinos.html' )
	{
		var mboxType = '-view-all';
	}
	
	var sessID = mboxFactoryDefault.getSessionId().getId();
	href = '' + tracker + mboxType + '&mboxSession=' +sessID +'' + href;

	if ( mboxType == 'lead' )
	{
		window.open( href );
	}
	else
	{
		window.location = href;
	}

	return false;
}

function URLEncode( plaintext )
{
	// The Javascript escape and unescape functions do not correspond
	// with what browsers actually do...
	var SAFECHARS = "0123456789" +					// Numeric
					"ABCDEFGHIJKLMNOPQRSTUVWXYZ" +	// Alphabetic
					"abcdefghijklmnopqrstuvwxyz" +
					"-_.!~*'()";					// RFC2396 Mark characters
	var HEX = "0123456789ABCDEF";

	var plaintext = plaintext;
	var encoded = "";
	for (var i = 0; i < plaintext.length; i++ ) {
		var ch = plaintext.charAt(i);
	    if (ch == " ") {
		    encoded += "+";				// x-www-urlencoded, rather than %20
		} else if (SAFECHARS.indexOf(ch) != -1) {
		    encoded += ch;
		} else {
		    var charCode = ch.charCodeAt(0);
			if (charCode > 255) {
			    alert( "Unicode Character '" 
                        + ch 
                        + "' cannot be encoded using standard URL encoding.\n" +
				          "(URL encoding only supports 8-bit characters.)\n" +
						  "A space (+) will be substituted." );
				encoded += "+";
			} else {
				encoded += "%";
				encoded += HEX.charAt((charCode >> 4) & 0xF);
				encoded += HEX.charAt(charCode & 0xF);
			}
		}
	} // for

	return encoded;
};
	
originalPageName = '';
	
function cLT(obj,type,product, trackingObject) {
	
	product = product.replace(/-review/g, "" );
	
	var tt = new Array();
	tt[0] = 'Casino Room';
	tt[1] = 'Poker Room';
	tt[2] = 'Featured Content';
	tt[3] = 'Progressive Jackpot';
	
	var score = "0.00";
	
	var ws= new Array();
	ws["24hpoker"]="0";
	ws["action-poker"]="0";
	ws["bet365poker"]="0";
	ws["betfair-poker"]="8.33";
	ws["bodog-poker"]="2.24";
	ws["bugsys-club"]="0";
	ws["bwin-poker"]="0";
	ws["carbon-poker"]="7.5";
	ws["cd-poker"]="0";
	ws["doyles-room"]="0";
	ws["dreampoker"]="0";
	ws["empire-poker"]="0";
	ws["everest-poker"]="0";
	ws["fair-poker"]="0";
	ws["full-tilt-poker"]="3.42";
	ws["gnuf-poker"]="7";
	ws["hollywoodpoker.com"]="1.56";
	ws["inter-poker"]="0";
	ws["kspoker"]="0";
	ws["ladbrokes-poker"]="0";
	ws["mansion-poker"]="3.74";
	ws["noble-poker"]="0";
	ws["nuts-poker"]="0";
	ws["pacific-poker"]="5.67";
	ws["paddy-power-poker"]="0";
	ws["paradise-poker"]="0";
	ws["party-poker"]="2.96";
	ws["pkr"]="0";
	ws["playersonly-poker"]="2.2";
	ws["poker-nordica"]="0";
	ws["poker-ocean"]="0";
	ws["pokerroom"]="0";
	ws["poker-stars"]="0";
	ws["prime-poker"]="0";
	ws["purple-lounge"]="0";
	ws["red-kings-poker"]="0";
	ws["sportsbook-poker"]="5.74";
	ws["titan-poker"]="3.3";
	ws["unibet"]="0";
	ws["william-hill-poker"]="0";
	ws["wingows"]="0";

	/*casino*/
	ws["europa-casino"]="17.56";
	ws["casino-bellini"]="9.02";
	ws["euro-Grand-casino"]="0.00";
	ws["swiss-casino"]="10.26";
	ws["888-casino"]="6.34";
	ws["casino-tropez"]="12.04";
	ws["vegas-red"]="6.8";
	ws["spin-palace-casino"]="2.64";
	ws["golden-casino"]="22.28";
	ws["cameo-casino"]="0.00";
	ws["online-casino"]="14.21";
	ws["party-casino"]="0.00";
	ws["platinum-play-casino"]="12.48";
	ws["golden-palace-casino"]="4.7";
	ws["skykings-casino"]="6.43";
	ws["casino-del-rio"]="31.25";
	ws["casino-las-vegas"]="5.42";
	ws["mansion-casino"]="6.62";
	ws["casino-king"]="22.88";
	ws["reef-club-casino"]="0.00";
	ws["50-stars-casino"]="10.89";
	ws["piggs-peak-casino"]="0.00";
	ws["rushmore-casino"]="42.12";
	ws["32vegas-casino"]="14.84";
	ws["all-slots"]="0.00";
	ws["millionaire-casino"]="17";
	ws["gnuf-casino"]="7.55";
	ws["all-jackpots"]="3.18";
	ws["golden-riviera-casino"]="2.13";
	ws["ruby-fortune-casino"]="3.98";
	ws["super-slots"]="1.3";
	ws["one-club-casino"]="0.00";
	ws["prime-casino"]="0.00";
	ws["riverbelle-casino"]="0.00";
	ws["jackpot-city"]="0.00";
	ws["gaming-club-casino"]="0.00";
	ws["lucky-nugget-casino"]="0.00";
	ws["joyland-casino"]="5.59";

	if( ws[product] )
	{
		score = ws[product];
	}
		
	var s=s_gi(s_account);
	
	if( originalPageName != '' )
	{
		s.pageName = originalPageName;
	}
	
	s.referrer=document.location.href;
	s.linkTrackVars="eVar1,eVar2,eVar7,eVar8,eVar9,eVar10,eVar11,eVar16,prop16,events,products,transactionID";
	s.linkTrackEvents="purchase,event1";
	s.eVar1 = 1;
	s.eVar2 = tt[type];
	s.eVar11 = s.pageName;
	
	/* Keeps track of mbox_version for one day */
	if( pageNameAdded )
	{
		s.eVar7 = s.getAndPersistValue(null,'mbox_prop',1);
	}

	dateObj = new Date();
	currTime = dateObj.getTime();
	trID = 's28l35t' + currTime + product.replace(/\W|casino/g,'');
		
	if( type == 2 )
	{
		trID = trID + 'f'; //Featured
	}
	
	s.transactionID = trID;
	s.eVar16 = trID;
	s.prop16 = trID;
	
	s.events="purchase,event1";
	s.products="" + tt[type] + ";" + product + ";1;" + score;
		
	/* Track Custom Link */
	s.tl(obj,'e',product);
	
	/* Track mbox click - Disabled for now, not testing
	if ( mboxFactoryDefault.getCookieManager().getCookie('PC') != null )
	{	
		currentValue = s.prop1;
		if( trackingObject ){
			s.prop1 = "Conversion: casinoguide" + trackingObject;
		}
		trackScEvent( s );
		s.prop1 = currentValue;
	}
	*/
	
	/*Reset purchase variables*/
	s.linkTrackVars = '';
	s.linkTrackEvents = '';     
	s.eVar1 = '';
	s.eVar2 = '';
	s.eVar7 = '';
	s.eVar11 = '';
	s.eVar16 = '';
	s.prop16 = '';
	s.transactionID = '';
	s.events = '';
	s.products= '';

	/* Save original page name */
	originalPageName = s.pageName;
	
	/* Transition page name */
	s.pageName = '/landing-page/' + product;
	
	/* Keeps track of mbox_version for one day */
	if( pageNameAdded )
	{
		s.prop7 = s.getAndPersistValue(null,'mbox_prop',1) + s.pageName;
	}
	
	void(s.t());
	
	if( trID != false )
	{
		if( type != 3 ) {
			window.open( obj.href.replace( '.html', ';' + trID + '.html' ) );
		}
		return false;
	}
	
	trID = false;
	
}

function trackScEvent( sc ) { 
	var s = sc;
	var tc = s.events && s.events.indexOf('purchase') != -1;
	var T = 'SiteCatalyst: ' + (tc ? 'purchase' : 'event');
	var pb = document.createElement('DIV');
	Q = mboxFactoryDefault.create(T, new Array(), pb );
	Q.setFetcher( new mboxAjaxScPluginFetcher('function', s ) ); 
	Q.load();
}


