




// ------------ MAIN ORBIS JAVA SCRIPT API ------------

// Opens the game popup and loads the specified game, configured
// to run using an Orbis user account.
function openOrbisGameWindow(organisation, gameType, orbisToken)
{
	var gamePageURL = "https://www.bingo.meccagames.com/oxigames/play/game_page.do?gameType=" + gameType;

	if (orbisToken != null && orbisToken != "")
	{
		gamePageURL += "&token=" + orbisToken;
	}

	_openWindow(gamePageURL, 'GameWindow', 'auto', 'auto', 750, 550, 'no', 'no', 'no', 'no', 'x', 'no', 'no');
}

// Opens the detailed history popup
// remoteAction is of the form 'user id':'game play id'
function openGamePlayDetailWindow(organisation, remoteAccount)
{
	var params = remoteAccount.split(':');

	// staging url
	// var historyURL = 'https://sta.bingo.meccagames.com/oxigames/account/gameshistory/DisplayGameHistory.do';
	
	// live url
	var historyURL = 'https://www.bingo.meccagames.com/oxigames/account/gameshistory/DisplayGameHistory.do';
	historyURL += '?userID=' + params[0];
	historyURL += '&gamePlayID=' + params[1];

	_openWindow(historyURL, 'gamehistory', 'auto', 'auto', 410, 460, 'yes', 'no', 'no', 'no');
}


// ------------ GENERIC WINDOW OPENING SCRIPTS ------------


// For the popupOnly parameter you must supply a value of "true" for each - anything else is ignored
// For the fullScrn if it is not null it will work
function _openWindow(theURL, winName, left, top, wide, high, scroll, menu, tools, reSize, statusBar, fullScrn, popupOnly)
{
	if (winName == "GameWindow" && navigator.appName == "Microsoft Internet Explorer")
	{
		//The GameWindow needs to have 4px deducted in IE - For some reason the inner area is always 4px greater than the requested size.
		wide = (eval(wide) - 4);
		high = (eval(high) - 4);
	}

	window.onerror = _handleError;

	windowDimensionsString  = _checkWindowSize(wide, high, scroll);
	windowScreenLocation    = _centerWindow(left, top);

	features = "resizable=" + reSize +
              windowDimensionsString +
              windowScreenLocation +
              ",scrollbars=" + scroll +
              ",status=" + statusBar +
              ",toolbar=" + tools +
              ",menubar=" + menu;

	// Only the gamepage.jsp needs 'fullscreen=7' - removes the window maxmize & close buttons.
	// Other windows will use it if a true parameter is passed to the constructor
	if ((winName == "GameWindow" && navigator.appName == "Microsoft Internet Explorer") && fullScrn != null)
	{
	    features += ",fullscreen=7";
	}

	if (popupOnly && (popupOnly == "true"))
	{
		popupWindow = window.open(theURL, winName, features);
		popupWindow.opener = self;
		popup.focus();
	}
	else if (winName == "GameWindow")
	{
		if ((this.gameWindow && !this.gameWindow.closed) || (this.parent.gameWindow && !this.parent.gameWindow.closed))
		{
			gameWindow.focus();
			// Call the 'switchGame()' function in the gamePage
			gameWindow.switchGame(theURL);
		}
		else
		{
			gameWindow = window.open(theURL, winName, features);
			gameWindow.focus();
			gameWindow.opener = this;
		}
	}
	else if (winName == "gamehistory")
	{
		if (this.betHistoryWindow && !this.betHistoryWindow.closed)
		{
			// if window already exists, just bring it to the foreground
			betHistoryWindow.focus();
		}
		else
		{
			betHistoryWindow = window.open(theURL, winName, features);
			betHistoryWindow.focus();
			betHistoryWindow.opener = this;
		}
	}
	else
	{
		newWindow = window.open(theURL, winName, features);
		newWindow.focus();

		// If the parent's opener was the Homepage, and it isnt closed..
		if (newWindow.opener.opener != null && !newWindow.opener.opener.closed)
		{
			if (newWindow.opener.name != "download")
			{
				// Set newWindow's opener to be the Homepage
				newWindow.opener = newWindow.opener.opener;
			}
		}
		else
		{
			// Set newWindow's opener to be the its real parent
			newWindow.opener = this;
		}
	}
}

function _checkWindowSize(widthValue, heightValue, scrollBars)
{
	SHeight = screen.availHeight;
	SWidth = screen.availWidth;

	// Enables scrollbars on the newly opened window, if needed
	if (heightValue >= SHeight)
	{
		// Force scrollbars if the screen height has to be changed
		scrollBars = 'yes';
		newHeight = SHeight - 40;
	}
	else
	{
	  newWidth = widthValue;
	  newHeight = heightValue;
	}

	if (scrollBars == 'yes')
	{
	  // 16px added to compensate for added scrollbar
	  newWidth = widthValue + 16;
	}

	// innerWidth + innerHeight are used for netscape to display the window at the correct size
	newWinDimensionsString = ",width=" + newWidth + ",height=" + newHeight + ",innerWidth=" + newWidth + ",innerHeight=" +newHeight;

	return (newWinDimensionsString);
}

function _centerWindow(leftValue, topValue)
{
    SHeight = screen.availHeight;
    SWidth = screen.availWidth;

    // Center the window
    if (leftValue == "auto")
        newLeft = (SWidth - newWidth) / 2;
    else
        newLeft =leftValue;

    if (topValue == "auto")
        newTop = (SHeight - newHeight) / 2;
    else
        newTop = topValue;

    newWinScreenLocation = ",left=" + newLeft + ",top=" + newTop;

    return (newWinScreenLocation);
}

function _handleError()
{
    return true;
}
