﻿/*
 * Implementation of Game Facade
 */

function ReadyForInitData() {
//    alert('ReadyForInitData');
    InstantGames.Manager.executeCommand('ReadyForInitData', null);
}

function GameReady() {
//    alert('GameReady');
    InstantGames.Manager.executeCommand('GameReady', null);
}

function ExitToLobby() {
    InstantGames.Manager.executeCommand('ExitToLobby', null);
}


function NavigationButtonsState(state) {
    //InstantGames.Manager.executeCommand('NavigationButtonsState', state);
}


function CreditManagerUpdate(xml) {
    //alert('CreditManagerUpdate: ' + xml);
    var creditDoc = null;

    if (window.ActiveXObject) {
        creditDoc = new ActiveXObject("Microsoft.XMLDOM");
        creditDoc.async = "false";
        creditDoc.loadXML(xml);
    }
    else {
        parser = new DOMParser();
        creditDoc = parser.parseFromString(xml, "text/xml");
    }

    var info = {
        operation: parseInt(creditDoc.getElementsByTagName('Operation')[0].firstChild.nodeValue),
        credit: parseInt(creditDoc.getElementsByTagName('Credit')[0].firstChild.nodeValue),
        bet: parseInt(creditDoc.getElementsByTagName('Bet')[0].firstChild.nodeValue)
    };
    InstantGames.Manager.executeCommand('CreditManagerUpdate', info);
//    alert('CreditManagerUpdate: operation=' + info.operation+', credit='+info.credit+', bet='+info.bet);
}

function RemoveFromFavorite(id, name) {
	if (!userAuthenticated) {
		ShowRequired();
		return false;
	}
	$('removing-game-name').innerHTML = name;
	$('remove-favorite-button').onclick = function () {
		InstantGames.Manager.executeCommand('RemoveFromFavorite', id);
		$('remove-favorite-dialog').hide();
		//window.location = '/Default.aspx?CategoryId=Favorites';
	};
	(new Dialog.Box('remove-favorite-dialog')).show();
	return false;
}

function GotoHelp(id) {
	window.location = '/help/GameHelp.aspx?Id=' + id;
}

function ChangeSort(select) {
	var query = select.options[select.selectedIndex].value;
	window.location.href = '/Default.aspx?' + query;
}

function showLangPanel() {
	var langPanel = document.getElementById("lang-panel");
	langPanel.style.display = "block";
	setTimeout("closeLangPanel()", 3000);
}
function closeLangPanel() {
	document.getElementById("lang-panel").style.display = "none";
}

//////////// footer text toggle /////////

function hasClass(ele, cls) {
	return ele.className.match(new RegExp('(\\s|^)' + cls + '(\\s|$)'));
}

function addClass(ele, cls) {
	if (!this.hasClass(ele, cls)) ele.className += "" + cls;
}

function removeClass(ele, cls) {
	if (hasClass(ele, cls)) {
		var reg = new RegExp('(\\s|^)' + cls + '(\\s|$)');
		ele.className = ele.className.replace(reg, '');
	}
}

function toggleFooterText() {
	if (hasClass(document.getElementById("plus-minus"), "plus")) {
		removeClass(document.getElementById("plus-minus"), "plus");
		addClass(document.getElementById("plus-minus"), "minus");
		document.getElementById("footer-text").style.display = "block";
	} else {
		removeClass(document.getElementById("plus-minus"), "minus");
		addClass(document.getElementById("plus-minus"), "plus");
		document.getElementById("footer-text").style.display = "none";
	}
}

function removeParamFromURL(URL, param) {
	URL = String(URL);
	var regex = new RegExp("\\?" + param + "=[^&]*&?", "gi");
	URL = URL.replace(regex, '?');
	regex = new RegExp("\\&" + param + "=[^&]*&?", "gi");
	URL = URL.replace(regex, '&');
	URL = URL.replace(/(\?|&)$/, '');
	regex = null;
	return URL;
}
function gup(name,url) {
	name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
	var regexS = "[\\?&]" + name + "=([^&#]*)";
	var regex = new RegExp(regexS);
	var results = regex.exec(url);
	if (results == null)
		return "";
	else
		return results[1];
}

function SendAnalyticsEvent(i_category, i_action, i_label, i_value) {
    var category = typeof i_category == 'string' ? i_category : '';
    var action = typeof i_action == 'string' ? i_action : '';
    var label = typeof i_label == 'string' ? i_label : '';
    try {
        value = parseInt(i_value);
        value = isNaN(value) ? 0 : value;
    }
    catch (err) {
        value = 0;
    }
    pageTracker._trackEvent(category, action, label, value);
}

