/**
 * Spawn Library: 
 * Spawns popup windows, closes them and keeps track of them.
 * 
 * Note on JSON Style script library:
 * - Each function should be called in context: like top.Spawn.spawn("myaccount");
 *   to ensure the object is complete and the function is not detached.
 * 
 * To add new popup window definitions, first try adding them into
 * SpawnConfig.js using template values.  If there's absolutely no way you can
 * make the url a string with template variables, 
 * add them into spawn() below as a last resort.
 */

var Spawn = {
	windowCollection: new Array()
};

Spawn.spawn = function(windowName) {
	//Template Values for URLS - so that SpawnConfig can have URLs like
	// "http://${contentURL}folder/Action.do"
	var valueMap = new Array();
	valueMap.cookieInfo = this.getCookieInfo(false);
	valueMap.singlesDomain = sSinglesDomain;
	valueMap.portalDomain = sPortalDomain;
	valueMap.contentURL = content_URL;
	valueMap.radioURL = radio_URL;
	valueMap.videoURL = video_URL;
	valueMap.timeFormURL = timeForm_URL;
	valueMap.region = GetCookie("betexRegionSessionCookie", "");
	valueMap.locale = GetCookie("betexLocaleSessionCookie", "");
	valueMap.brand =  GetCookie("betexBrand", "");
	valueMap.timeZoneId = GetCookie("betexTimeZoneSessionCookie", "");
	valueMap.currency = GetCookie("betexCurrencySessionCookie", "");
	valueMap.domain = document.domain;

	for (var i=0; i<arguments.length; i++) {
		valueMap["" + i] = arguments[i];
	}
	
	var windowConfig = SpawnConfig[windowName];
	if (windowConfig != null) {//popup is defined properly in config object.
		var url = null;
		
		if (windowConfig.url) {	
			//replace url template variables with values
			url = this.replaceUrlTemplateValues(windowConfig.url, valueMap);
		}
		else if (windowConfig.constructUrl) {
			url = top.domainManager.constructUrl(windowConfig.constructUrl.appName, windowConfig.constructUrl.isSecure);
		}
		else if (windowConfig.constructUrlWithPath) {
			url = top.domainManager.constructUrlWithPath(
				windowConfig.constructUrlWithPath.appName,
				windowConfig.constructUrlWithPath.pathName,
				windowConfig.constructUrlWithPath.isSecure);
		}
		else {
			return;
		}
		if (windowConfig.extraParams) {
			url += this.replaceUrlTemplateValues(windowConfig.extraParams, valueMap);
		}
		
		this.getWindow(
			windowName, 
			url, 
			windowConfig.autoscroll,
			windowConfig.height,
			windowConfig.width,
			windowConfig.statusBar,
			windowConfig.menuBar,
			windowConfig.toolbar,
			windowConfig.locationBar
		);

		if (windowConfig.logging == true) {
			FILogger(arguments[1]);
		}		
	}	
	else { //we have to use a function to generate the url.
		switch (windowName) {
			case "betBreakdown":
				var queryStringInfo = "?betId=" + arguments[1];
				if(arguments[2] != null) {
					queryStringInfo = queryStringInfo + 
						"&subAccountID=" + arguments[2];
				}
				this.getWindow(windowName, "/reporting/bethistory/LoadBetBreakdownAction.do" + queryStringInfo, 0, 300, 620);
			break;
			case "help":
				if ((arguments[1] == 'Help.Contact.Us') && !arguments[2]) {
					spawn("cmsContactUs");
				} else {				
					var queryStringInfo = "help/?product=exchange" +
						this.getCookieInfo(false);
					if (valueMap.locale == "cz" && arguments[1] == "Help.Betfair.Internet.Betting") {
						content_URL = "http://help.betfairinfo.cz/?p=61";
						queryStringInfo = "";
					} else if (arguments[1] != "GHL") {
						queryStringInfo = queryStringInfo +
						"&sWhichKey=" + arguments[1] +
						"&sWhichAnchor=" + arguments[2];
					} else if (valueMap.locale == "cz") {
						content_URL = "http://help.betfairinfo.cz";
						queryStringInfo = "";
					} else {
						queryStringInfo = queryStringInfo + "&origin=" + arguments[1];
					}
					var url = content_URL + queryStringInfo;
					this.getWindow(windowName, url, 0, 500, 788);			
					FILogger(arguments[1]);
				}
			break;
			case "joinNow":
				var url = joinNow_URL;
	
				if (arguments[1] && arguments[1] != '') {
					url = url + "?" + arguments[1] + "=" + arguments[2];
				} 
				this.getWindow(windowName, url, 1, 700, 738, true);
			break;
			case "moreInfoChart":
				var queryStringInfo = 
					"&timeZone=" + GetCookie("betexTimeZoneSessionCookie", "") +
					this.getCookieInfo(false) +
					"&currency=" + GetCookie("betexCurrencySessionCookie", "");	
				var baseURL = ((arguments[2]) ? arguments[2] : "");
				this.getWindow(windowName, baseURL + "/betting/LoadRunnerInfoAction.do" + arguments[1] + queryStringInfo + "", 0, 500, 700);
			break;
			case "payments":
				var url = "http://payments.betfair.com";
				if (arguments[1] && arguments[2] && arguments[1] != '' && arguments[2] != '') {
					url = url + "?" + arguments[1] + "=" + arguments[2];
				}
				if (arguments.length > 3 && typeof(arguments[3]) != "undefined") {
					this.getWindow(windowName, arguments[3],1,600,800);
				} else {
					var region = GetCookie("betexRegionSessionCookie", "");
					this.getWindow(windowName, url,1,600,800);			
				}
			break;
	 		case "placingABet":
				switch(arguments[1]) {
					case "bg":
						this.getWindow(windowName, "http://promo.betfair.com/demo/index.asp?locale=bg",1,460,727);
					break;
					case "cz":
						this.getWindow(windowName, "http://promo.betfair.com/demo/index.asp?locale=cz",1,460,727);
					break;
					case "da":
						this.getWindow(windowName, "http://promo.betfair.com/demo/index.asp?locale=da",1,460,727);
					break;
					case "de":
						this.getWindow(windowName, "http://promo.betfair.com/demo/index.asp?locale=de",1,460,727);
					break;
					case "el":
						this.getWindow(windowName, "http://promo.betfair.com/demo/index.asp?locale=el",1,460,727);
					break;
					case "en":
						this.getWindow(windowName, "http://www.betfairpromo.com/learnbetfair",1,581,650);
					break;
					case "es":
						this.getWindow(windowName, "http://promo.betfair.com/demo/index.asp?locale=es",1,460,727);
					break;
					case "fi":
						this.getWindow(windowName, "http://promo.betfair.com/demo/index.asp?locale=fi",1,460,727);
					break;
					case "fr":
						this.getWindow(windowName, "http://promo.betfair.com/demo/index.asp?locale=fr",1,460,727);
					break;
					case "it":
						this.getWindow(windowName, "http://www3.betfairpromo.com/demo/index.asp?locale=it",1,460,727);
					break;
					case "no":
						this.getWindow(windowName, "http://promo.betfair.com/demo/index.asp?locale=no",1,460,727);
					break;
					case "pl":
						this.getWindow(windowName, "http://promo.betfair.com/demo/index.asp?locale=pl",1,460,727);
					break;
					case "pt":
						this.getWindow(windowName, "http://promo.betfair.com/demo/index.asp?locale=pt",1,460,727);
					break;
					case "ru":
						this.getWindow(windowName, "http://promo.betfair.com/demo/index.asp?locale=ru",1,460,727);
					break;
					case "sv":
						this.getWindow(windowName, "http://promo.betfair.com/demo/index.asp?locale=se",1,460,727);
					break;
					case "tr":
						this.getWindow(windowName, "http://promo.betfair.com/demo/index.asp?locale=tr",1,460,727);
					break;
					case "zh":
						this.getWindow(windowName, "http://promo.betfair.com/demo/index.asp?locale=zh",1,460,727);
					break;
					case "zh_TW":
						this.getWindow(windowName, "http://promo.betfair.com/demo/index.asp?locale=cn",1,460,727);
					break;
					default:
						this.getWindow(windowName, "http://www.betfairpromo.com/learnbetfair",1,581,650);
					break;
				}
	 		break;
			case "priceComparison":
				var url = "/PriceComparison.do";
				if (arguments[1] && arguments[2] && arguments[1] != '' && arguments[2] != '') {
					url = url + "?" + arguments[1] + "=" + arguments[2];
				}
				this.getWindow(windowName, url, 0, 600, 850);
			break;
			case "privacyPolicy":
				var url = 'aboutus/?product=exchange' + this.getCookieInfo(false);
				if (arguments[1] && arguments[1] != '') {
					url = url + "&jumpTo=" + arguments[1];
				}
				if (arguments[2] && arguments[3] && arguments[2] != '' && arguments[3] != '') {
					url = url + "&" + arguments[2] + "=" + arguments[3];
				}
				url = url + "&sWhichKey=Privacy Policy";
				this.getWindow(windowName, content_URL + url, 0, 500, 788);
			break;
	 		case "transfer":
				//set default to "portal" for transfers
				var url = top.domainManager.constructUrlWithPath('portal', 'wallettx', true);
				if (top.domainManager.getApplication(top.productName) != null
						&& top.domainManager.getApplication(top.productName).getPath("wallettx") != null) {
					url = top.domainManager.constructUrlWithPath(top.productName, 'wallettx', true);
				}
				this.getWindow(windowName, url, 0, this.getXPosition(url), this.getYPosition(url));;
	 		break;
	 		case "onCasinoLogin":
	 			var url = top.domainManager.constructUrlWithPath("casino","wallettx",false);
	 			this.getWindow(windowName, url, 0, this.getXPosition(url), this.getYPosition(url));;
	 		break;
	 		case "onCasinoLogout":
	 			var url = top.domainManager.constructUrlWithPath("casino","popLogoutTx",false);
	 			this.getWindow(windowName, url, 0, this.getXPosition(url), this.getYPosition(url));;
	 		break;
		}
	}
};

Spawn.closeSpawn = function(windowName) {
	try {
		this.windowCollection[windowName].close();	
	}
	catch(x) {
	}
};

Spawn.getWindow = function(windowName, sURL, bAutoScroll, iHeight, iWidth, bStatusBar, bMenuBar, bToolBar, bLocationBar) {
	try {
		this.windowCollection[windowName].document.location = sURL;
		this.windowCollection[windowName].focus();
		return this.windowCollection[windowName];
	}
	catch(x) {
		try {
			this.windowCollection[windowName] = top.window.open(sURL, windowName,'height='+ iHeight +',width='+ iWidth +',top=' + ((screen.availHeight - iHeight) / 2) + ",screenY=" + ((screen.availHeight - iHeight) / 2) + ",left=" + ((screen.availWidth - iWidth) / 2) + ",screenX=" + ((screen.availWidth - iWidth) / 2) +",scrollbars="+((bAutoScroll)? "yes":"no")+",status="+((bStatusBar)? "yes":"no")+",resizable=yes,menubar="+((bMenuBar)? "yes":"no")+",toolbar="+((bToolBar)? "yes":"no")+",location="+((bLocationBar)?"yes":"no")+",titlebar=yes')");
			return this.windowCollection[windowName];
		}
		catch(x) {
			if (loginMsg_SpawnBlocker != "") {
				alert(loginMsg_SpawnBlocker);
				return null;
			}
		}
	}
};

Spawn.getCookieInfo = function(bFirstParam) {
	var url = "";
	if (bFirstParam) {
		url = "?";
	} else {
		url = "&";
	}
	
	if(GetCookie("betexRegionSessionCookie", "")) {
		url = url + "region=" + GetCookie("betexRegionSessionCookie", "");
	}
	if(GetCookie("betexLocaleSessionCookie", "")) {
		url = url + "&locale=" + GetCookie("betexLocaleSessionCookie", "");
	}
	if(GetCookie("betexBrand", "")) {
		url = url + "&brand=" + GetCookie("betexBrand", "");
	}
	return url;
}

Spawn.replaceUrlTemplateValues = function(url, valueMap) {
	var output = url;
	for (var i in valueMap) {
		var template = "${" + i + "}";
		while (output.indexOf(template) > -1) {
			output = output.replace(template, valueMap[i]);	
		}
	}
	
	output = output.replace(/\$\{.*\}/, "");
	
	return output;
}

Spawn.getXPosition = function(url) {
	var sizeX = 460;
	var xRegex = /sizeX=([\d]+)/;
	var xResult = xRegex.exec(url);
	if (xResult != null && xResult.length == 2 ){
		sizeX = xResult[1];
	}
	return sizeX;
}

Spawn.getYPosition = function(url) {
	var sizeY = 712;
	var yRegex = /sizeY=([\d]+)/;
	var yResult = yRegex.exec(url);
	if (yResult != null && yResult.length == 2 ){
		sizeY = yResult[1];
	}
	return sizeY;
}
