function Casino() {

    var casinoFields = ['loggedInCheckURL', 'width', 'height', 'sessionId', 'resizable', 'scrollbars'];

    this.p = new Parameter({});

    Casino.prototype.init = function (p) {
        if (p) {
            for (var i in casinoFields) {
                if (casinoFields[i] in p) {
                    this.p.set(casinoFields[i], p[casinoFields[i]]);
                }
            }
        }
    },Casino.prototype.clone = function () {
        var cloneCasino = new Casino();
        cloneCasino.init(this.p.clone());
        return cloneCasino;
    },Casino.prototype.setSize = function (width, height) {
        this.p.set('width', width);
        this.p.set('height', height);
    },Casino.prototype.openHistiry = function (id, name, p) {
        if (checkUserSession(this.p.get('loggedInCheckURL'))) {
            openCasinoGameWindow(id, "/history/casino-game" + "?sessionId=" + id + "&name=" + name,
                    "width=" + this.p.get('width', p) +
                            ",height=" + this.p.get('height', p) +
                            ",menubar=0,toolbar=0,status=0," +
                            "resizable=" + this.p.get('resizable', p) + ",scrollbars=" +
                            this.p.get('scrollbars', p) + ",fullscreen=0");
        } else {
            window.location.href = '/redirect?to=login';
        }
    },Casino.prototype.openForReal = function (name, p) {
        if (checkUserSession(this.p.get('loggedInCheckURL'))) {
            openCasinoGameWindow(name, "/games/game" + "?name=" + name + "&real=1"
                    , "width=" + this.p.get('width', p) + ",height=" + this.p.get('height', p) +
                    ",menubar=0,toolbar=0,status=0," +
                    "resizable=" + this.p.get('resizable', p) + ",scrollbars=" +
                    this.p.get('scrollbars', p) + ",fullscreen=0");
        } else {
            window.location.href = '/redirect?to=login';
        }
    },Casino.prototype.openForFun = function (name, p) {
        name += ' for Free';
        openCasinoGameWindow(name, "/games/game" + "?name=" + name
                , "width=" + this.p.get('width', p) + ",height=" + this.p.get('height', p) +
                ",menubar=0,toolbar=0,status=0," +
                "resizable=" + this.p.get('resizable', p) + ",scrollbars=" +
                this.p.get('scrollbars', p) + ",fullscreen=0");
    };

    function checkUserSession(url) {
        var checkUrl = url != '' ? '/testxml' + url : '/testxml/casino/index';
        var result = false;
        $.ajax({
            url: checkUrl,
            cache: false,
            async: false,
            dataType: 'xml',
            success: function(data) {
                var $dom = $(data);
                result = $dom.find('LOGGEDIN').length > 0;
            }
        });
        return result;
    }

    function openCasinoGameWindow(_id, url, params) {
        var winName = 'a_' + sid.replace(/-/g, '_') + '_' + _id.replace(/[^\w]/g, '_');
        var gameWin = window.open("", winName, params);
        if (! gameWin || ! gameWin.document.getElementById('flash_container')) {
            gameWin = window.open(url, winName, params);
        }
        if (gameWin)gameWin.focus();
    }

    function Parameter(list) {

        this.list = list;

        this.get = function(name, p) {
            if (name && name != '') {
                if (p && (name in p)) {
                    return p[name];
                } else {
                    return this.list[name];
                }
            }
        },this.set = function(name, value) {
            if (name && name != '') {
                this.list[name] = value;
            }
        },this.clone = function() {
            var cl = {};
            for (var i in this.list) {
                cl[i] = this.get(i);
            }
        }
    }

}

var casino = new Casino();

