var menuElements = [ 'CC', 'WT', 'MC', 'EP', 'NT', 'CP', 'MB', 'EC', 'IC', 'FP', 'IN', 'BC', 'SA' ];
var menuFlashPlayer;
var pageLoaded = false;
var arVersion = navigator.appVersion.split("MSIE");
var version = parseFloat(arVersion[1]);
	
function fixPNG() {}

/**
 * Przechwytujemy obiekt lewego menu przy inicjacji strony
 */
function init () {
	var isIE = navigator.appName.indexOf("Microsoft") != -1;
	menuFlashPlayer = isIE ? window['flashLeftMenu'] : document['flashLeftMenu'];
	
	if (version >= 5.5 && document.body.filters) {
		fixPNG();
	}
	makePngVisible();
	
	pageLoaded = true;
	
	if (menuFlashPlayer) {
		menuFlashPlayer.SetVariable("/:isLoaded", 1);
	}
	
	// no flash player detected
	var flashVer = GetSwfVer();
	var str = window.location.toString();
	if (flashVer < 0 && !str.match ('flashPlayerRequired')) {
		window.location = '/flashPlayerRequired'+window.location.search;
	}
}

/**
 * Blocks out iframes
 */
function blockIframe () {
	if (self != top) {
		top.location.href = self.location.href;
	}
}


/**
 * Shows lobby menu
 */
function showMenu() {
	$('top_menu').setStyle({height : "500px"});
}

/**
 * Hides lobby menu
 */
function hideMenu() {
	$('top_menu').setStyle({height : "28px"});
}
	
/**
 * Zmiana aktywnego elementu lewego menu i pokazanie odpowiedniego kontenera z treścią dla danej pozycji.
 * 
 * @param string	id			Id elementu
 * @param boolean	isDeposit	Flaga oznaczająca, że jesteśmy w depozytach
 */
function changeTo (id, isDeposit) {
	var prefix = isDeposit ? 'd' : 'w';
	
	// unmark menu items
	var menuItems = $$('a.linkMenuItem');
	menuItems.each (function (menuItem) { menuItem.removeClassName ('linkMenuItemActive'); });
	
	// hide all methods
	menuElements.each (function (menuElement) { if ($(menuElement)) { $(menuElement).hide (); } });
			
	// show selected method and mark selected menu item
	if ($(id)) {
		Element.show (id);
		$('menu'+id).addClassName ('linkMenuItemActive');
	}
}

/**
 * Pokazuje wait-diva po czym przekierowuje na wskazany adres
 * 
 * @param String location Adres docelowy
 */
function goTo (location) {
	Wait.show ();
	window.location = location;
}

/**
 * Prosta funkcja podmieniająca pola formularza
 * 
 * @param string sel	Aktualna wartość pola state
 */
function changeState (sel) {
	if (sel.value == 'US') {
		Element.show ('state1');
		Element.hide ('state2');
	}
	
	else {
		Element.hide ('state1');
		Element.show ('state2');
	}
}

/**
 * Odkrywamy wszystkie PNG
 */
function makePngVisible () {
	for (var i = 0; i < document.images.length; i++) {
		var img = document.images[i];
		var imgName = img.src.toUpperCase ();
		if (imgName.substring (imgName.length-3, imgName.length) == "PNG") {
			img.style.visibility = 'visible';
		}
	}
}

/**
 * Zada potwierdzenia operacji i submituje formularz
 */
function savedAccountQuery (query) {
	if (confirm (query)) { 
		$('saForm').action='/cashier/unregisterAccount'; 
		$('saForm').submit (); 
	} 
	
	return false;
}

/**
 * Podmienia graniczne wartosci kwot, odkrywa/ukrywa pole CVV2, pokazuje komunikat o niewystarczajacym stanie konta
 * 
 * @param integer requestId	ID metody platnosci
 */
function savedAccountChangeAmount (requestId) {
	var methodId = map[requestId];
	var amount = amounts[methodId];
	
	try {
		// domyslnie pokazujemy caly formularz
		Element.show ('savedAccountAmount');
		Element.hide ('savedAccountSorry');
		
		Element.update ('minAmount', amount.minimum);
		Element.update ('minAmount2', '$'+amount.minimum);
		Element.update ('maxAmount', amount.maximum);
			
		// jesli za maly amount, to wyswietlamy odpowiedni komunikat
		if (amount.minimum > withdrawableBalance) {
			Element.hide ('savedAccountAmount');
			Element.show ('savedAccountSorry');
		}
		
		else {
			if (methodId == 3)
				Element.show ('cvv2con');
			else
				Element.hide ('cvv2con');
		}
	}
	
	catch (e) {
		exceptionAlert ('savedAccountChangeAmount Error.', e);
	}
}

function ControlVersion()
{
	var version;
	var axo;
	var e;
	
	try {
		axo = new ActiveXObject("ShockwaveFlash.ShockwaveFlash.7");
		version = axo.GetVariable("$version");
	} catch (e) {
	}

	if (!version)
	{
		try {
			axo = new ActiveXObject("ShockwaveFlash.ShockwaveFlash.6");
			version = "WIN 6,0,21,0";		
			axo.AllowScriptAccess = "always";
			version = axo.GetVariable("$version");
		} catch (e) {
		}
	}

	if (!version)
	{
		try {
			axo = new ActiveXObject("ShockwaveFlash.ShockwaveFlash.3");
			version = axo.GetVariable("$version");
		} catch (e) {
		}
	}

	if (!version)
	{
		try {
			axo = new ActiveXObject("ShockwaveFlash.ShockwaveFlash.3");
			version = "WIN 3,0,18,0";
		} catch (e) {
		}
	}

	if (!version)
	{
		try {
			axo = new ActiveXObject("ShockwaveFlash.ShockwaveFlash");
			version = "WIN 2,0,0,11";
		} catch (e) {
			version = -1;
		}
	}
	
	return version;
}

/**
 * Flash Player version detector
 */
function GetSwfVer(){
	var isIE  = (navigator.appVersion.indexOf("MSIE") != -1) ? true : false;
	var isWin = (navigator.appVersion.toLowerCase().indexOf("win") != -1) ? true : false;
	var isOpera = (navigator.userAgent.indexOf("Opera") != -1) ? true : false;	
	
	var flashVer = -1;
	
	if (navigator.plugins != null && navigator.plugins.length > 0) {
		if (navigator.plugins["Shockwave Flash 2.0"] || navigator.plugins["Shockwave Flash"]) {
			var swVer2 = navigator.plugins["Shockwave Flash 2.0"] ? " 2.0" : "";
			var flashDescription = navigator.plugins["Shockwave Flash" + swVer2].description;			
			var descArray = flashDescription.split(" ");
			var tempArrayMajor = descArray[2].split(".");
			var versionMajor = tempArrayMajor[0];
			var versionMinor = tempArrayMajor[1];
			if ( descArray[3] != "" ) {
				tempArrayMinor = descArray[3].split("r");
			} else {
				tempArrayMinor = descArray[4].split("r");
			}
			var versionRevision = tempArrayMinor[1] > 0 ? tempArrayMinor[1] : 0;
			var flashVer = versionMajor + "." + versionMinor + "." + versionRevision;
		}
	}
	else if (navigator.userAgent.toLowerCase().indexOf("webtv/2.6") != -1) flashVer = 4;
	else if (navigator.userAgent.toLowerCase().indexOf("webtv/2.5") != -1) flashVer = 3;
	else if (navigator.userAgent.toLowerCase().indexOf("webtv") != -1) flashVer = 2;
	else if ( isIE && isWin && !isOpera ) {
		flashVer = ControlVersion();
		if (typeof flashVer == 'string')
			flashVer = flashVer.replace(/WIN /,"").replace(/,/g, ".");
	}	
	return flashVer;
}


Event.observe (window, 'load', init);
if (!isLobby) {
	Event.onDOMReady(blockIframe);
}

// otwiera okienko live chat'a
function showChat (login) {
	window.open ('https://server.iad.liveperson.net/hc/58353834/?cmd=file&file=visitorWantsToChat&site=58353834&byhref=1&SESSIONVAR!visitor_profile=Slots%20Oasis%20Casino&SESSIONVAR!skill=Slots%20Oasis%20CS&SESSIONVAR!casinoTitle=Slots%20Oasis%20Casino&SESSIONVAR!casinoId=1&SESSIONVAR!skinId=1&SESSIONVAR!identifier='+login+'&SESSIONVAR!survey_pre-chat_enable=off&SESSIONVAR!eventType=PlayerRequest&SESSIONVAR!source=LobbyMainPage&SESSIONVAR!mode=real&SESSIONVAR!game=&SESSIONVAR!subGame=&SESSIONVAR!gameID=&SESSIONVAR!subGameID=&SESSIONVAR!reason=&SESSIONVAR!skill=&SESSIONVAR!operator=', '', 'scrollbars=no,width=475,height=400, resizable=yes');
}

// resizes window to full screen
function maxWindow() {
	if (document.all) {
		window.moveTo (0,0);
		top.window.resizeTo(screen.availWidth, screen.availHeight);
	}
	
	else if (document.layers || document.getElementById) {
		if (top.window.outerHeight < screen.availHeight || top.window.outerWidth < screen.availWidth) {
			window.moveTo (0,0);
	    	top.window.outerHeight = screen.availHeight;
	    	top.window.outerWidth = screen.availWidth;
		}
	}
}

/**
 * Otwiera cashiera RTG w nowym oknie.
 */
function cashierOpen () {
	window.open('/cashier/open', 'cashier', 'toolbar=0,location=0,direction=0,status=0,menubar=0,scrollbars=0,width=800,height=600,left=100,top=100');
}




/**
 * Klasa obsługi pełnoekranowego wait div'a
 */
var Wait = Class.create();
Wait.prototype = {};
Wait.show = function () {
	this.delay = 250;		// próg czasowy, po którego przekroczeniu ma zostać pokazany wait div
	this.duration = 800;	// minimalny czas wyświetlania wait diva w milisekundach
		
	var date = new Date();
	this.start = date.getTime ();
	this.timer = setTimeout (
		function () {
			vLayer (true);
			Element.center ('waitDiv', document.body);
			Element.show ('waitDiv');		
		}, this.delay
	);
	
	// po 10sek. automatycznie ukrywamy waitdiva - zapobiega zablokowaniu interfejsu w sytuacjach nietypowych
	setTimeout (
		function () {
			vLayer (false);
			Element.hide ('waitDiv');		
		}, 10000
	);
}

Wait.hide = function () {
	// czyscimy timer; jesli nie zostal przekroczony "delay", to waitdiv sie w ogole nie pokaze
   	clearTimeout (this.timer);
   	delete this.timer;

	// ukrycie waitdiva, ale nie wczesniej niz po uplywie okreslonego interwalu - unikniecie efektu migniecia
	var date = new Date();
	var time = date.getTime () - this.start;
	var duration = time < this.duration ? this.duration - time : 1;
	setTimeout (
		function () {
			vLayer (false);
			Element.hide ('waitDiv');
		}, duration
	);			
}

function vLayer (show) {
	try {
		Element.setStyle ('vLayer', { width: '800px', height: '600px', top: 0, left: 0 });
		$('vLayer').style.display = (show ? 'block' : 'none');
	}
	
	catch (e) {
		exceptionAlert ('Virtual layer error', e);
	}
}