//=============================================================
// POPUP UTILS
//-------------------------------------------------------------
/**
 * opens a schreenshot popup from Game-page
 *
 * @param {number} gCode
 * The product-code of the product of the screenshot. Used to present the name of the game.
 *
 * @param {string} imgName
 * The image name of the screen-shot - used on embedded as src of IMG
 *
 * @param {number} imgWidth
 * The width of the popped up window
 *
 * @param {number} imgHeight
 * The height of the popped up window
 *
 */
function popScreenshot(gCode,imgName, imgWidth, imgHeight)
{
	window.open("screenshot.aspx?code=" + gCode + "&src="+imgName, "screenshot", "toolbar=no, status=no, resize, width=" + imgWidth + ", height=" + imgHeight + " ")
	return;
}
/**
 * opens an online-game popup
 *
 * @param {string} gameUrl
 * The url of the play-online game page with no parameters
 *
 * @param {number} gameCode
 * The product-code of the product of played game
 *
 * @param {string} origin
 * orign - indicator of the pressed link (for means tracking)
 *
 * @param {number} imgWidth
 * The width of the popped up window
 *
 * @param {number} imgHeight
 * The height of the popped up window
 *
 */
function popOnlineExt(gameUrl, gameCode, origin, width, height) {    
    window.open(gameUrl + "?code=" + gameCode + "&origin=" + origin, "online", "width=" + width + ", height=" + height);
    return;
}
/**
 * opens an online-game popup
 *
 * @param {number} gCode
 * The product-code of the product of played game
 *
 * @param {number} imgWidth
 * The width of the popped up window
 *
 * @param {number} imgHeight
 * The height of the popped up window
 *
 */
 function popPlayOnline(gCode, gWidth, gHeight){
	if (window != top)
		top.popPlayOnline(gCode, gWidth, gHeight);

	var oWidth, oHeight, oWindow
	oWidth = parseInt(gWidth) +80
	oHeight = parseInt(gHeight) - 250
	oWindow=window.open("/PlayOnline.aspx?code="+gCode,'null',"width=" + oWidth + ",Height=" + oHeight + ",status=no,toolbar=no,menubar=no,location=no,resizeable=yes")
	oWindow.focus()
}
/**
 * opens an online-game popup
 *
 * @param {string} gameUrl
 * The url of the play-online game page with no parameters
 *
 * @param {number} gCode
 * The product-code of the product of played game
 *
 * @param {number} imgWidth
 * The width of the popped up window
 *
 * @param {number} imgHeight
 * The height of the popped up window
 *
 */
 function popPlayOnline2(gameUrl, gCode, gWidth, gHeight){
	if (window != top)
		top.popPlayOnline2(gameUrl, gCode, gWidth, gHeight)
		
	var oWidth, oHeight, oWindow
	oWidth = parseInt(gWidth) +80
	oHeight = parseInt(gHeight) - 250
	oWindow=window.open(gameUrl + "?code=" + gCode,'null',"width=" + oWidth + ",Height=" + oHeight + ",status=no,toolbar=no,menubar=no,location=no,resizeable=yes")
	oWindow.focus()
}
//=============================================================
// IFRAME UTILS
/**
 * returns true if the provided window does not exist or closed.
 *
 * @param {object} w
 * the tested window instance
 *
 * @returns if the provided window/iframe is closed or not exists
 * @type Boolead
 */
function isWindowClosed(w){
    if(!w) return true;
    try{
        return w.closed;
    }catch(ex){
        return true;
    }
}
/**
 * sends the opener window/iframe to the provided url
 * used to manipulate the main window of a popup
 *
 * @param {string0} url
 * the URL to send the opener window to 
 *
 */
function sendOpenerToUrl(url)
{
	if( null == window.opener || isWindowClosed(window.opener) )
	{
		var iWidth = screen.availWidth - 10;
        var iHeight = screen.availHeight - 150;
        window.open(url,'_blank',"width=" + iWidth + ",Height=" + iHeight + ",top=0,left=0,status=yes,toolbar=yes,menubar=yes,location=yes,resizable=yes,scrollbars=yes,maximize=yes");
    }
    else
    {
		
		opener.location=url;
        opener.focus();
    }
}
/**
 * sends the opener of the opener window/iframe to the provided url
 * used to manipulate the main window of a 2nd generation popups
 *
 * @param {string0} url
 * the URL to send the opener window to 
 *
 */
function sendOpenerOpenerToUrl(url)
{

	if( null == window.opener || isWindowClosed(window.opener) || null == window.opener.opener || isWindowClosed(window.opener.opener) )
	{
		var iWidth = screen.availWidth - 10;
        var iHeight = screen.availHeight - 150;
        window.open(url,'_blank',"width=" + iWidth + ",Height=" + iHeight + ",top=0,left=0,status=yes,toolbar=yes,menubar=yes,location=yes,resizable=yes,scrollbars=yes,maximize=yes");
    }
    else
    {
		
		opener.opener.location=url;
        opener.opener.focus();
    }
    return undefined;
}

//=============================================================
// OMNITURE URILS
//-------------------------------------------------------------
 /**
  * @private
  * This function is used by the output of Obeorn.BL.Playable.downloadEvents()
  * Logs an omniture event - wrapper function
  *
  * @param {number} gameCode
  * The product code of the downloaded game
  *
  * @param {string} omnitureChannel
  * The omniture-cannel code
  *
  * @param {string} gameName
  * The report-name of the downloaded game
  *
  * @param {string} catName
  * The report-name of the category
  *
  * @param {string} dlDataTime
  * The ISO date string-representation of the download universal time
  */
function omnitureLogDownload(gameCode, omnitureChannel,gameName,catName,dlDateTime) {
	try{
	omnitureLogDownload_Real(gameCode, omnitureChannel,gameName,catName,dlDateTime);
	}
	catch(e){}
}
/**
  * @private
  * This function is used by the output of Obeorn.BL.Playable.downloadEvents()
  * Logs an omniture event - call to omniture API s_gs(omnitureChannel)
  *
  * @param {number} gameCode
  * The product code of the downloaded game
  *
  * @param {string} omnitureChannel
  * The omniture-cannel code
  *
  * @param {string} gameName
  * The report-name of the downloaded game
  *
  * @param {string} catName
  * The report-name of the category
  *
  * @param {string} dlDataTime
  * The ISO date string-representation of the download universal time
  */
function omnitureLogDownload_Real(gameCode, omnitureChannel,gameName,catName,dlDateTime) {
	var expirationPeriod = 180; //the number of days before expiration
	var today = new Date();
	try{		
	setCookie(gameCode,today,expirationPeriod);

	s_linkType="d";
	s_linkName=gameName;
	s_events="event1";
	s_products=catName + ";" + gameName;
	s_lnk=s_co(this);		
	s_eVar11=dlDateTime;
	s_gs(omnitureChannel);
	}
	catch(e){}
	return true;
}

//=============================================================
// MISC UTILS
//-------------------------------------------------------------

/**
 * If the game of the provided sku is installed - it invokes its launcher using the Oddesy active-X
 *
 * @param {numner} sku
 * the SKU of the game to launch
 */
function runApp(sku)
{
	objActiveX = document.getElementById('objActiveX');
    if(ODCinstalled==1) {
        objActiveX.launchGame(sku);
        self.location.reload()
    }
    return
}
/**
 * Calls the code provided as string untill it passes with no errors.
 * If error occured - sets a timeout to call it again.
 * Usefull to make sure that a code line that depends on DOM elements will gets executed by the nearest interval that its DOM is available.
 *
 * @param {string} sCode
 * The dangerous code line to call
 */
function safeCall(sCode,iTimeout)
{
    if ( !iTimeout ) iTimeout = 250;
    try
    {
        (new Function(sCode))();
        return true;
    }
    catch(ex)
    {
        setTimeout("safeCall(\"" + sCode.replace(/\"/g,"\\\"") + "\"," + iTimeout + ");", iTimeout);
    }
    return false;
}
//=============================================================
// COOKIES UTILS
//-------------------------------------------------------------
/**
 * returns the cookie by the provided name
 *
 * @param {string} name
 * The name of the sought cookie
 *
 * @returns the cookie by the provided name
 * @type string
 */
function getCookie(name) 
{ 
	var index = document.cookie.indexOf(name + "=");
	if (index == -1) return null;
	index = document.cookie.indexOf("=", index) + 1;
	var endstr = document.cookie.indexOf(";", index);
	if (endstr == -1) endstr = document.cookie.length;
	return unescape(document.cookie.substring(index, endstr));
}

/**
* @param {string} name 
* the name of cookie
*
* @param {string} value 
* the value of the cookie
*
* @param {number(optional)} expiration 
* the expiration period of the cookie (in days)
*
* @param {string(optional)} domain
* the domain to write the cookie in
*/
function setCookie(name, value, expiration, domain) 
{ 
	if(domain == null)
		domain = location.host;	
	
	try {
		var strValue = asCookieValue(value);
		var today = new Date();
		var expiry = new Date(today.getTime() + expiration * 24 * 60 * 60 * 1000); 
		if (strValue != null && strValue != "") {
			document.cookie=name + "=" + strValue + "; domain=" + domain+ "; expires=" + expiry.toGMTString();
		}
	}
	catch(e){}
}
/**
 * returns the supplied value, encoded as cookie value
 * 
 * @param {string} value
 * the value to be encoded
 * 
 * @returns the supplied value, encoded as cookie value
 * @type string
 */
function asCookieValue(value){
	var strValue = new String();
	strValue = value.toString();
	//replace all chars with URL encoding
	while (strValue.indexOf(" ") != -1) {
		strValue = strValue.replace(" ","%20");
	}
	strValue = strValue.replace(/\:/g,"%3A");
	strValue = strValue.replace(/\+/g,"%2B");
	strValue = strValue.replace(/\-/g,"%2D");
	return strValue;
}
