/*
 * Affiliate Lounge / Casino Euro free games script. 
 * Allows Casino Euro Free Games to be displayed and tracked
 * via the Affiliate Lounge tracking system
 *
 * dmaworsley 2009
 */

/*
 * These REALLY can't be defined in a function - if they are the whole thing doesn't work - go figure.
 */
var script=document.getElementsByTagName('script');
var mS=script[ script.length - 1 ];
var querySring=mS.src.replace(/^[^\?]+\??/,'');

/*
 * functions to get the params from the js query string
 */
function getParamArray()
{
	var param = parseJSQuery(querySring);

	return param;
}

function parseJSQuery(query)
{
	var parameters=new Object();
	if (!query) return parameters;
	
	var pieces=query.split(/[;&]/);
	for (var i=0; i<pieces.length; i++)
	{
		var pairVals=pieces[i].split('=');
		if (!pairVals||pairVals.length!=2) continue;
		var key=unescape(pairVals[0]);
		var val=unescape(pairVals[1]);
		val=val.replace(/\+/g, ' ');
		parameters[key]=val;
	};
	
	return parameters;
}


/*
 * To stop this being declared everywhere make it global
 */
var params = getParamArray();

function showGameForAffiliateLounge()
{
	//get the params from the js query string supplied by AL	
	var lg = params['lang'].toString();
	var h = params['height'].toString();
	var w = params['width'].toString();
	var game = params['gameNAME'].toString();
	var clickTag = params['clickTAG'].toString();
	var divId = params['divID'].toString();

	//divID and gameID and clickTag have to be set, the rest can be defaulted
	if((divId != null || divId != "") && (game != null || game != "") && (clickTag != null || clickTag != ""))
	{
		/* defaults */
		if(lg == null){ lg ="en";}
		if(h == null){ h = 400;}
		if(w == null) { w = 500;}

		getAffGameScript(game, lg, h, w, 4.0, "false", clickTag, "ce-freeplay-site-prompt.swf");
	}
	else
	{
		window.alert("Unable to load game. divId or gameId not set");
	}
}

function getAffGameScript(gameName, lang, height, width, timeout, branded, url, replaceFlash)
{	
	var objectId = getUniqueName("game", gameName);	
	var tableName = getUniqueName("table", gameName);

	var ruleServer = "http://casinoeuro.casinomodule.com";
	var gameServer = "http://casinoeuro-proxy.casinomodule.com/";
        //var gameServer = "http://casinoeuro-api.casinomodule.com/";
	var unbrandedPath = branded == "false" ? "unbranded/" : "";
	
	var gameURL = gameServer + "flash/" + unbrandedPath + gameName + "/"
			+ lang + "/loader.swf";

        var flashVars = "";

        if(gameName == 'goldtimer')
        {
             flashVars = "helpURL=javascript%3Arules%28%27%2Fen%2Fgamerules.jsp%3Fgame%3Dgoldtimer_sw%26gameTitle%3Dgoldtimer%26countryCode%3D" + lang + "%27%29&historyURL=javascript%3Ahistory%28%27%2Fen%2Flast20.jsp%27%29&closeURL=javascript%3Awindow.close%28%29&lang=" + lang + "&gameId=goldtimer_sw&currencyChar=â‚¬&symbol1=â‚¬&symbol2=Â¢&gameurl=goldtimer.swf&server=http%3A%2F%2Fcasinoeuro.casinomodule.com%3A80%2F&fullscreen=false";
        }
        else if(gameName == 'fruits')
        {
            flashVars = "helpURL=javascript%3Arules%28%27%2Fen%2Fgamerules.jsp%3Fgame%3Dfruits_sw%26gameTitle%3Dfruits%26countryCode%3Den%27%29&amp;historyURL=javascript%3Ahistory%28%27%2Fen%2Flast20.jsp%27%29&amp;closeURL=javascript%3Awindow.close%28%29&amp;lang=en&amp;gameId=fruits_sw&amp;currencyChar=â‚¬&amp;symbol1=â‚¬&amp;symbol2=Â¢&amp;gameurl=fruits.swf&amp;server=http%3A%2F%2Fcasinoeuro.casinomodule.com%3A80%2F&amp;fullscreen=false";
        }
        else if(gameName == 'alienrobots')
        {
            flashVars = "helpURL=javascript%3Arules%28%27%2Fen%2Fgamerules.jsp%3Fgame%3Dalienrobots_sw%27%29&historyURL=javascript%3Ahistory%28%27%2Fen%2Flast20.jsp%27%29&closeURL=javascript%3Awindow.close%28%29&lang=en&gameId=alienrobots_sw&currencyChar=€&symbol1=€&symbol2=¢&gameurl=alienrobots.swf&server=http%3A%2F%2Fcasinoeuro.casinomodule.com%3A80%2F&fullscreen=false";
        }
        else
        {
         flashVars = "gameId=" + gameName + "_sw" + "&" + "lang=" + lang + "&"
			+ "currencyChar=EUR&" + "symbol1=EUR&" + "symbol2=c&"
			+ "gameurl=" + gameName + ".swf&" + "server=" + ruleServer
			+ "&fullscreen=false&disableAudio=true";
        }

	var strFlash = "<object "
			+ "classid='clsid:D27CDB6E-AE6D-11cf-96B8-444553540000'"
			+ " codebase='http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,0,0'"
			+ " WIDTH='"
			+ width
			+ "'"
			+ " HEIGHT='"
			+ height
			+ "'"
			+ " id='"
			+ objectId
			+ "'"
			+ " ALIGN=''>"
			+ "<param name=\"allowScriptAccess\" value=\"always\">"
			+ "<param name=\"movie\" value=\""
			+ gameURL
			+ "\">"
			+ "<param name=\"base\" value=\".\">"
			+ "<param name=\"scale\" value=\"exactfit\">"
			+ "<param name=\"quality\" value=\"high\">"
			+ "<param name=\"bgcolor\" value=\"#002678\">"
			+ "<param name=\"FlashVars\" value=\""
			+ flashVars
			+ "\">"
			+ " <embed"
			+ " src='"
			+ gameURL
			+ "'"
			+ " quality='high'"
			+ " bgcolor='#002678'"
			+ " ALLOWSCRIPTACCESS='ALWAYS'"
			+ " base='.'"
			+ " width='"
			+ width
			+ "'"
			+ " height='"
			+ height
			+ "'"
			+ " name='"
			+ objectId
			+ "'"
			+ " align=''"
			+ " scale='exactfit'"
			+ " type='application/x-shockwave-flash'"
			+ " pluginspage='http://www.macromedia.com/go/getflashplayer'"
			+ " FlashVars='" + flashVars + "'></EMBED>" + "</OBJECT>";
	var div = params['divID'].toString();	
	var obj = document.getElementById(div);	

	obj.innerHTML = strFlash;

	//replace game provided the timeout is not infinite
	if (parseFloat(timeout) != 0)
	{		
		var newFlashId = getUniqueName ("flash", "CE");        
		var newFlashURL = "http://static1.casinoeuro.com/skins/casinoeuro/flash/freegame-prompt/button-register-free-prompt-black-" + lang + ".swf";
		var clickTag = url;

		var newFlash = "<object " +
		 "classid=\"clsid:D27CDB6E-AE6D-11cf-96B8-444553540000\"" + 
			" codebase=\"http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,0,0\"" +
			" WIDTH=\"" + width + "\" " +
			" HEIGHT=\"" + height + "\" " + 
			" id=\"" +newFlashId + "\">" +
			"<param name=\"allowScriptAccess\" value=\"always\">" +
			"<param name=\"movie\" value=\"" + newFlashURL + "?clickTAG=" + clickTag + "\">" +
			"<param name=\"base\" value=\".\">" +
			"<param name=\"scale\" value=\"exactfit\">" +
			"<param name=\"quality\" value=\"high\">" +
			"<param name=\"bgcolor\" value=\"#002678\">" +

			" <embed" +
			" src=\"" + newFlashURL + "?clickTAG=" + clickTag + "\" " +
			" quality=\"high\" " +
			" bgcolor=\"#002678\" " +
			" ALLOWSCRIPTACCESS=\"ALWAYS\" " +
			" base=\".\" " +
			" width=\"" + width + "\" " +
			" height=\"" + height + "\" " +
			" name=\"" + newFlashId + "\" " + 
			" scale=\"exactfit\" " +
			" type=\"application/x-shockwave-flash\" " +
			" pluginspage=\"http://www.macromedia.com/go/getflashplayer\" />" +
			"</object>";
		
		//setTimeout("alert('"+newFlash+"')", parseFloat(timeout) * 60000);
		setTimeout("document.getElementById ('"+div+"').innerHTML = '"+newFlash+"';", parseFloat(timeout) * 60000);
	}
}

function getNewFlashLang(currentLang)
{
	if (currentLang.toLowerCase() == "de"
			|| currentLang.toLowerCase() == "en"
			|| currentLang.toLowerCase() == "fi"
			|| currentLang.toLowerCase() == "fr"
			|| currentLang.toLowerCase() == "nl"
			|| currentLang.toLowerCase() == "no"
			|| currentLang.toLowerCase() == "sv") {
		return currentLang;
	}

	return "en";
}

function getUniqueName(prefix, gameName)
{
	var found = 1;
	var count = 0;

	while (true)
	{
		var x = document.getElementById(prefix + gameName + count);
		if (x == null) {
			return prefix + gameName + count;
		}
		count++;
	}
}

/*
 * Load the Game
 */
  showGameForAffiliateLounge();

  
