﻿/// <reference path="jquery-1.4.1.min.js" />


jQuery(document).ready(function () {
    rtgGame.init();
    //look for query string or init
    if (jQuery.querystring("game") != null)
        setTimeout("rtgGame.changeGameByQueryString()", 4000);

    jQuery(".gamesNavigation a").click(function (e) {
        e.preventDefault();
        var hash = jQuery(this).attr("href");
        if (hash == undefined)
            return;
        if (hash.indexOf("#") < 0) return;
        hash = hash.substring(hash.indexOf("#") + 1);
        rtgGame.changeGameByHash(hash);
    });
    jQuery(".changeGameHandCount").change(function (e) {
        var newValue = jQuery(this).val();
        rtgGame.changeGameHandCount(parseInt(newValue, 10));
    });
    jQuery(".changeGameDenomination").change(function (e) {
        var newValue = jQuery(this).val();
        rtgGame.changeGameDenomination(parseInt(newValue, 10));
    });

    //modal
    jQuery(".gameInfoPanel").hide();
    jQuery(".ui-close-modal").click(function (e) { jQuery(this).parent().hide(); });
    jQuery(".gameInfoButton").click(function (e) { e.preventDefault(); jQuery(".gameInfoPanel").toggle("slow"); });
    jQuery(".cashierButton").click(function (e) {
        e.preventDefault();
        rtgGame.cashier = window.open(jQuery(this).attr("href"), 'cashierwin', 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width=800,height=600,left = 400,top = 300');
        if (rtgGame.cashier.focus)
            rtgGame.cashier.focus();

    });
    jQuery(window).unload(rtgGame.endPlayerSession);
});
var rtgGame = new Object();
rtgGame.movie = null;
rtgGame.cashier = null;

rtgGame.init = function () {
    var movieName = "flash_client";
    var embedMovieName = "embed_flash_client";
    if (document.getElementById)
        rtgGame.movie = document.getElementById(movieName);
    else
        rtgGame.movie = document[movieName];
    if (rtgGame.movie == null)
        alert("movie not found");
    if (typeof (rtgGame.movie.SetVariable) == "undefined") {
        if (document.getElementById)
            rtgGame.movie = document.getElementById(embedMovieName);
        else
            rtgGame.movie = document[embedMovieName];
    }
    if (rtgGame.movie == null)
        alert("embed not found");

};


rtgGame.debug = function (text) { jQuery("#debug").html(text); }
rtgGame.data = null;
rtgGame.instantPlayGameSettings = [];
rtgGame.refreshGameBalance = function () {

    if (this.movie.refreshBalance != "undefined")
        this.movie.refreshBalance();
};
rtgGame.changeGameByQueryString = function () {
    var hash = jQuery.querystring("game")
    if (hash != null && hash.length > 0)
        this.changeGameByHash(hash);
};
rtgGame.changeGameByHash = function (hash) {
    if (hash == undefined || hash == null)
        return;
    if (hash != null && hash.length > 0) {
        var hashArgs = hash.split("_");
        var gameId = 0;
        var machId = 0;
        var denom = null;
        var hands = null;
        if (hashArgs.length > 0)
            gameId = parseInt(hashArgs[0], 10);
        if (hashArgs.length > 1)
            machId = parseInt(hashArgs[1], 10);
        if (hashArgs.length > 2)
            denom = parseInt(hashArgs[2], 10);
        if (hashArgs.length > 3)
            hands = parseInt(hashArgs[3], 10);
        this.changeGame(gameId, machId, denom, hands);
    }
};
rtgGame.refreshDenominationList = function (gameId, machineId, selected) {
    var settings = rtgGame.getGameSettings(gameId, machineId);
    var dropdown = jQuery(".changeGameDenomination");
    jQuery("option", dropdown).remove();
    if (settings.d.length > 1) {
        dropdown.append("<option>Change Denomination</option>");
        for (var i = 0; i < settings.d.length;i++){
            var d = settings.d[i];
            var label = d < 100 ? d + "&cent;" : "$" + (d / 100);
            dropdown.append("<option value='"+ d +"'" + (d==selected ? " selected" : "") + ">" + label + "</option>");
        }
        dropdown.show(200);
    }
    else
        dropdown.hide(200);
};
rtgGame.refreshHandCountList = function (gameId, machineId, selected) {
    var settings = rtgGame.getGameSettings(gameId, machineId);
    var dropdown = jQuery(".changeGameHandCount");
    jQuery("option", dropdown).remove();
    if (settings.h.length > 1) {
        dropdown.append("<option>Change Hand Count</option>");
        for (var i = 0; i < settings.h.length; i++) {
            var h = settings.h[i];
            var label = h + " hands";
            dropdown.append("<option value='" + h + "'" + (h == selected ? " selected" : "") + ">" + label + "</option>");
        } 
        dropdown.show(200);
    }
    else
        dropdown.hide(200);
};
Array.prototype.contains = function (i) { for (var n = 0; n < this.length; n++) { if (this[n] == i) return true; } return false; };
rtgGame.changeGame = function (gameId, machineId, denomination, handCount) {    
    var settings = this.getGameSettings(gameId, machineId);
    if (settings != null) {
        if ((denomination == null || denomination == 0) && settings.d.length > 0) {
            if (this.data != null && this.data.denomination != null && settings.d.contains(this.data.denomination))
                denomination = this.data.denomination;
            else
                denomination = settings.d[0];
        }
        if ((handCount == null || handCount == 0) && settings.h.length > 0) {
            if (this.data != null && this.data.handCount != null)
                handCount = this.data.handCount;
            else
                handCount = settings.h[0];
        }
    }
    if (typeof (rtgGame.movie.changeGame) == undefined)
        alert("your browser is not supported");
    if (handCount == 0)
        handCount = 3;
    if (denomination == 0)
        denomination = 100;
    //alert("change " + gameId + "," + machineId + "," + denomination + "," + handCount);
    
    rtgGame.movie.changeGame(gameId, machineId, denomination, handCount);

    rtgGame.refreshDenominationList(gameId, machineId, denomination);
    rtgGame.refreshHandCountList(gameId, machineId, handCount);       
    this.refreshGameInfo(gameId, machineId);
    this.data = {
        gameId: gameId,
        machineId: machineId,
        denomination: denomination,
        handCount: handCount
    };
};
rtgGame.refreshGameInfo = function (gameId, machineId) {
    var infoData = jQuery(".gameInfo_" + gameId + "_" + machineId);
    if (infoData.length == 0)
        jQuery(".gameInfoButton").hide();
    else {
        jQuery(".gameInfoButton").show();
        jQuery(".gameInfoText").html(infoData.html());
    }
    var settings = this.getGameSettings(gameId, machineId);
    if (settings != null) {
        document.title = settings.t;
    }    
};
rtgGame.getGameSettings = function (gameId, machineId) {
    if (rtgGame.instantPlayGameSettings != null) {
        for (var i = 0; i < rtgGame.instantPlayGameSettings.length; i++) {
            var n = rtgGame.instantPlayGameSettings[i];
            if (n.g == gameId && n.m == machineId) {
                return n;
            }
        }
    }
}
rtgGame.changeGameHandCount = function (handCount) {
    if (this.data != null)
        this.changeGame(this.data.gameId, this.data.machineId, null, handCount);
};
rtgGame.changeGameDenomination = function (denomination) {
    if (this.data != null)
        this.changeGame(this.data.gameId, this.data.machineId, denomination, null);
};


rtgGame.endPlayerSession = function () {
    //alert("--------------------> endSessionPlayer")
    try {
        jQuery(".disconnecting").show();
        if (rtgGame.movie != null) {
            if (rtgGame.movie.closeSessionAS2 != undefined)
                rtgGame.movie.closeSessionAS2();    // Call function close session in flash wrapper
            if (rtgGame.movie.closePlayerSession != undefined)
                rtgGame.movie.closePlayerSession(); // Call function close session in flash client
        }
    } catch (e) { }
};

jQuery.querystring = function (name) {
    var results = new RegExp('[\\?&]' + name + '=([^&#]*)').exec(window.location.href);
    if (!results) { return null; }
    return results[1];
} 

