﻿/// <reference path="jquery-1.4.1-vsdoc.js"/>

bet365 = {
    GetBrowser: function () {
        var sBrowser = navigator.userAgent.toLowerCase();

        if (sBrowser.indexOf("chrome") != -1) {
            return 'CHROME';
        }
        else if (sBrowser.indexOf("opera") != -1) {
            return 'OPERA';
        }
        else if (sBrowser.indexOf("netscape") != -1) {
            if (sBrowser.indexOf("msie") != -1) {
                return 'IE';
            } else {
                return 'NETSCAPE';
            }
        }
        else if (sBrowser.indexOf("firefox") != -1) {
            return 'FIREFOX';
        }
        else if (sBrowser.indexOf("chrome") != -1) {
            return 'CHROME';
        }
        else if (sBrowser.indexOf("safari") != -1) {
            return 'SAFARI';
        }
        else if (sBrowser.indexOf("msie 6") != -1) {
            return 'IE6';
        }
        else if (sBrowser.indexOf("msie 8.") != -1) {
            return 'IE8';
        }
        else if (sBrowser.indexOf("msie 7") != -1) {
            return 'IE7';
        }
        else if (sBrowser.indexOf("msie") != -1) {
            return 'IE';
        }
        else {
            return 'IE*';
        }
    },

    GetCookie: function (c_name) {
        if (document.cookie.length > 0) {
            c_start = document.cookie.indexOf(c_name + "=")
            if (c_start != -1) {
                c_start = c_start + c_name.length + 1
                c_end = document.cookie.indexOf(";", c_start)
                if (c_end == -1) c_end = document.cookie.length
                return unescape(document.cookie.substring(c_start, c_end))
            }
        }
        return ""
    },

    CheckType: function (valueToCheck) {
        var returnVal = '';

        if (valueToCheck != undefined) {
            switch (valueToCheck.constructor) {
                case String:
                    returnVal = "String";
                    break;
                case Object:
                    returnVal = "Object";
                    break;
                case Array:
                    returnVal = "Array";
                    break;
                case Function:
                    returnVal = "Function";
                    break;
                case Number:
                    returnVal = "Number";
                    break;
                case Boolean:
                    returnVal = "Boolean";
                    break;
            }
        }

        return returnVal;
    }
}

bet365.Screen = {
    Top: function (control) {
        var top = ((screen.height / 2) - control.height());
        return top;
    },

    Left: function (control) {
        var left = ($(document).width() - control.width()) / 2;
        return left;
    },
   
    VerticalCentre: function (control) {
        control.css("left", bet365.Screen.Left(control) + "px");
        control.css("top", bet365.Screen.Top(control) + "px");
    },


    GetScreenRes: function () {
	    var sHeight = screen.height;
	    switch (bet365.GetBrowser())
	    {
		    case 'OPERA' :
			    sHeight=(sHeight * 0.70);
			    return sHeight;
		    case 'NETSCAPE' :
			    sHeight=sHeight-200;
			    return sHeight;
		    case 'IE' :
			    sHeight=sHeight-250;
			    return sHeight;
			case 'IE6':
			    sHeight = sHeight - 250;
			    return sHeight;
		    case 'FIREFOX' :
			    sHeight=sHeight-250;
			    return sHeight;
		    case 'IE8' :
			    sHeight=sHeight-250;
			    return sHeight;
	    }
    },

    GetScreenWidth: function (){
	    var sWidth = screen.width;
	    sWidth=sWidth - (sWidth * 0.90);
	    return Math.round(sWidth);
    },

    GetScreenTop: function (){
	    var sTop = screen.height;
	    sTop=sTop - (sTop * 0.88);
	    return Math.round(sTop);
    }
}

bet365.flash = {
    EmbedFlashObject: function (flashVersion, flashContainerID, flashMovieUrl, flashWidth, flashHeight, flashVars, flashParams, flashAttributes, noFlash) {
        var showHtml = function () {
            var noFlashElement = document.getElementById(noFlash);
            var flashContainerElement = document.getElementById(flashContainerID);

            if (noFlashElement) {
                noFlashElement.style.display = '';
            }

            if (flashContainerElement) {
                flashContainerElement.style.display = 'none';
            }
        }

        var flashSuccess = false

        //only hide the no flash container if flash is available
        if (swfobject.hasFlashPlayerVersion(flashVersion)) {
            swfobject.embedSWF(
                flashMovieUrl,
                flashContainerID,
                flashWidth,
                flashHeight,
                flashVersion,
                false,
                flashVars,
                flashParams,
                flashAttributes,
                function (e) {
                    if (!e.success) {
                        showHtml();
                    }
                }
            );
        } else {
            showHtml();
        }
    },

    ShouldShowMsg: function (attempt) {
        var fc = document.getElementById("flshckie");

        if (fc == null) {
            fc = document.getElementById("flshckie2");
        }
        if (fc == null) {
            return false;
        }

        var fC;
        if (typeof fc.getCookie == 'function' && typeof fc.getCookie("fts") !== 'undefined' && typeof fc.getCookie("fts") !== null) {
            fC = fc.getCookie("fts");
        } else {
            if (attempt !== 0) {
                attempt = Number(attempt);
                attempt = (isNaN(attempt) ? 1 : attempt++);

                if (attempt < 10) {
                    setTimeout(function () { bet365.flash.ShouldShowMsg(attempt); }, 100);
                }
            }
            fC = "";
        }

        var bC = bet365.GetCookie("ft");

        if ((typeof fC !== 'undefined') && (fC !== null) && (fC.length > 0)) {
            if (Number(fC) == 1) {
                document.cookie = 'ft=1; expires=Wed, 1 Jan 2070 23:59:00 UTC; path=/';
                return false;
            }
        }

        if ((typeof bC !== 'undefined') && (bC !== null) && (bC.length > 0)) {
            if (Number(bC) == 1) {
                if (typeof fc.setCookie === 'function') {
                    fc.setCookie("fts", bC);
                }
                return false;
            }
        }

        return true;
    },

    showNoFlashPrompt: function () {
        $(document).ready(function () {
            bet365.flash.noFlashSetup();
            if (bet365.flash.ShouldShowMsg()) {
                bet365.popups.showDialog(".NoFlashLogoContainer");
            }
        });
    },

    hideNoFlashPrompt: function () {
        bet365.popups.hideDialog(".NoFlashLogoContainer");

        $(".NoFlashLauncherContainer").hide();
        $('#mask').fadeTo("slow", 0);
        $('#mask').css("display", "none");
    },

    AttachNoFlashCloseEvent: function () {
        $(".Close, .NoFlashCloseIcon").click(function (e) {
            bet365.flash.hideNoFlashPrompt();
            e.preventDefault();
        });
    },

    //check whether the user has flash if not show the popup
    checkHaveFlash: function () {
        //version does not matter - user just needs flash
        if (!swfobject.hasFlashPlayerVersion("1.0.0.0")) {
            bet365.flash.showNoFlashPrompt();
        } else {
            bet365.flash.hideNoFlashPrompt();
        }
    },

    noFlashSetup: function () {
        $(".butLeft").click(function (e) {
            bet365.flash.CanSeeFlashLogo();
            bet365.flash.hideNoFlashPrompt();
            e.preventDefault();
        });
    },

    CanSeeFlashLogo: function () {
        var fc = document.getElementById("flshckie");
        if (fc == null) {
            fc = document.getElementById("flshckie2");
        }

        if (typeof fc.setCookie == 'function') {
            fc.setCookie("fts", "1");
        }
        document.cookie = 'ft=1; expires=Wed, 1 Jan 2070 23:59:00 UTC; path=/';
    },

    showNoFlashLauncherPrompt: function () {
        var message = $(".NoFlashLauncherContainer");

        // Move the popup to in front of the mask.
        $('#mask').after(message);

        //Get the screen height and width
        var maskHeight = $(document).height();
        var maskWidth = $(document).width();

        //Set height and width to mask to fill up the whole screen
        $('#mask').css({ 'width': maskWidth, 'height': maskHeight });

        //transition effect
        $('#mask').fadeTo(2000, 0.3);

        // Follwing generic function is not horizentally aligning this popup.
        //var x = (message.parent().width() / 2) - (message.outerWidth() / 2);
        message.css("top", bet365.Screen.Top(message) + "px");
        message.css("left", "225px");
        message.fadeTo(1000, 1);
    },

    ProgressiveScript: function (id) {
        if (($('body').data(id) !== null) && ($('body').data(id) !== undefined)) {
            var gamePod = $("#" + id);
            var dataValues = $('body').data(id);
            var pageAttributes = $('body').data('pageAttributes');
            var hasRequestedVersion = swfobject.hasFlashPlayerVersion(pageAttributes.flashVersion);
            var ProgressiveTextDiv = $("[id$=ProgressiveTextDiv]", gamePod);
            var ProgressiveFlashDiv = $("[id$=ProgressiveFlashDiv]", gamePod);
            var newProgressiveDiv;

            //if the progressive divs are not in the object then we need to create them. Typically this should only be the case after GMT has run.
            if (!$("[id$=ProgressiveDiv]", gamePod).length > 0) {
                newProgressiveDiv = $("<div />", { 'id': 'ProgressiveDiv', 'class': 'ProgressiveDiv' });
                ProgressiveFlashDiv = $("<div />", { 'id': 'ProgressiveFlashDiv' });
                ProgressiveTextDiv = $("<div />", { 'id': 'ProgressiveTextDiv' });

                newProgressiveDiv.append(ProgressiveFlashDiv).append(ProgressiveTextDiv);
                gamePod.append(newProgressiveDiv);
            }

            if (!hasRequestedVersion) {
                ProgressiveTextDiv.css('display', 'block');
                ProgressiveFlashDiv.css('display', 'none');
            } else {
                ProgressiveTextDiv.css('display', 'none');
                ProgressiveFlashDiv.css('display', 'block');
                swfobject.embedSWF('' + pageAttributes.flashProgressive + '', ProgressiveFlashDiv.attr("id"), '191', '18', '' + pageAttributes.flashVersion + '', false,
                    { height: '18', width: '191', fontSize: '10', align: 'center', bold: 'true', fontColour: 'FFFFFF', periodSymbol: '' + dataValues.periodSymbol + '', dividerSymbol: '' + dataValues.dividerSymbol + '',
                        initialValue: '' + dataValues.initalValue + '', stepValue: '' + dataValues.stepValue + '',
                        displayFormat: '' + dataValues.displayFormat + '', appPath: '' + pageAttributes.appPath + '', gameCode: '' + dataValues.gameCode + '', currencyCode: '' + dataValues.currencyCode + '',
                        pollInterval: '' + pageAttributes.flashProgressivePollInterval + ''
                    },
                    { align: 'middle', wmode: 'transparent', quality: 'high', allowScriptAccess: 'true', type: 'application/x-shockwave-flash' },
                    { id: '' + id + '_flashObject', name: '' + id + '_flashObject' }
                );

                if ((newProgressiveDiv !== undefined) && (newProgressiveDiv.length > 0)) {
                    newProgressiveDiv.removeAttr("style");
                }
            }
        }
    }
};

bet365.games = {
    CheckFlashAvailableAndLaunchGame: function (requiredFlashVersion, destinationURL, targetName, height, width, isLI) {
        var hasRequestedVersion = swfobject.hasFlashPlayerVersion(requiredFlashVersion);

        if (!hasRequestedVersion) {
            //flash not available so trigger modal popup
            bet365.flash.showNoFlashLauncherPrompt();
        }
        else {
            //flash available so launch game popup
            var sHeight = bet365.Screen.GetScreenRes();
            var sLeft = bet365.Screen.GetScreenWidth();
            var sTop = bet365.Screen.GetScreenTop();

            if (isLI == 'False') {
                if (persistantGameWindow != null) {
                    try {
                        persistantGameWindow.close();
                    }
                    catch (err) { };
                };

                var gamesWindow = null;

                try {
                    gamesWindow = window.open(destinationURL, targetName, 'height=' + height + ',width=' + width + ',status=yes,toolbar=no,menubar=no,location=no,left=' + sLeft + ',top=' + sTop + ',scrollbars=yes,resizable=yes');
                }
                catch (err) {
                    gamesWindow = window.open(destinationURL, '_blank', 'height=' + height + ',width=' + width + ',status=yes,toolbar=no,menubar=no,location=no,left=' + sLeft + ',top=' + sTop + ',scrollbars=yes,resizable=yes');
                }

                if (settings.createnew == 0 && name == "GGG") {
                    persistantGameWindow = winObj;
                }

                if (persistantGameWindow != null) {
                    var browser = bet365.GetBrowser();
                    if (browser == 'IE8') {
                        setTimeout('persistantGameWindow.focus();', 500);
                    }
                    else {
                        setTimeout('persistantGameWindow.blur(); persistantGameWindow.focus();', 500);
                    }
                }
            }
            else {
                window.open(destinationURL, targetName, 'height=' + height + ',width=' + width + ',status=yes,toolbar=no,menubar=no,location=no,left=' + sLeft + ',top=' + sTop + ',scrollbars=yes,resizable=yes');
            }

        }

        return false;
    }
}


bet365.popups = {
    showDialog: function (dialogSelector, backgroundcolour, backgroundOpacity, fadeTime, popupMask) {
        if (backgroundcolour == undefined) {
            backgroundcolour = "white";
        }

        if (backgroundOpacity == undefined) {
            backgroundOpacity = 0.4;
        }

        if (fadeTime == undefined) {
            fadeTime = 700;
        }

        if (popupMask == undefined) {
            popupMask = '#popupmask';
        }

        var dialogJQuery = $(dialogSelector);

 

        if (popupMask == '#popupmask') {

            if ($.browser.msie && $.browser.version <= 7) {
                dialogJQuery.css('position', 'absolute');
                $(popupMask).css('position', 'relative');
            } else {
                dialogJQuery.css('position', 'fixed');
                $(popupMask).css('position', 'fixed');
            }

            // Move the popup to in front of the mask.
            $(popupMask).after(dialogJQuery);

            //Get the screen height and width
            var maskHeight = $(document).height();
            var maskWidth = '100%'; // $(document).width();

            //Set height and width to mask to fill up the whole screen
            $(popupMask).css({ 'width': maskWidth, 'height': maskHeight, 'background-color': backgroundcolour, 'z-index': 9999, 'top': '0px', 'left': '0px' });
        } else {
            
            //Get the screen height and width
            var maskHeight = $(document).height();
            $(popupMask).css({ 'height': maskHeight, 'background-color': backgroundcolour, 'z-index': 9999 });
        }
        //transition effect
        $(popupMask).fadeTo(fadeTime, backgroundOpacity);

        //Get the window height and width
        var winH = $(window).height();
        var winW = $(window).width();

        //Set the popup window to center

        dialogJQuery.css('z-index', 10000);
        dialogJQuery.css('top', (winH / 2) - ($(dialogSelector).height() / 2));
        dialogJQuery.css('left', (winW / 2) - ($(dialogSelector).width() / 2));
        dialogJQuery.fadeTo(fadeTime, 1.0, function () {
            dialogJQuery.css("opacity", "");
        });
    },

    hideDialog: function (dialogSelector, fadeTime, popupMask) {
        if (fadeTime == undefined) {
            fadeTime = "slow";
        }
        if (popupMask == undefined) {
            popupMask = '#popupmask';
        }

        $(dialogSelector).hide();
        $(popupMask).fadeTo(fadeTime, 0);
        $(popupMask).css("display", "none");
    },

    setFocus: function () {
        setTimeout(function () {
            this.focus();
        }, 500);
    },

    ReloadParent: function () {
        window.opener.location.reload();
    }
}


