/*
    js.js
        - JavaScript support for flash client         
*/

// name of the flash movie in the document
var objectName = "main";

// container to store reference to opened windows
var openedWindows = new Array();

function getBrowserType() {
    // default browser type value
    var browserType = 0;

    // set browserType to 1 for MSIE and Opera {{{
    if (navigator.appVersion.indexOf("MSIE") != -1) {
        browserType = 1;
    }
    else if (navigator.userAgent.indexOf("Opera") != -1) {
        browserType = 2;
    }
    // }}}

    // return the browser type
    return browserType;
}

function getOSType() {
    // return 0 for Windows OS {{{
    if (navigator.appVersion.toLowerCase().indexOf('win') != -1) {
        return 0;
    }
    // }}}
    // return 1 for the rest {{{
    else {
        return 1;
    }
    // }}}
}

function openWindow(windowId, uri, windowName, windowOptions) {
    // create the window
    var windowObject = window.open(uri, windowName, windowOptions);

    // notify the flash client if the window has failed to open {{{
    if ((windowObject == undefined) || ((window.opera) && (!windowObject.opera))) {
        // get the flash object
        var flashObject = getFlashMovieObject(objectName);

        // set the windowClosed property
        flashObject.SetVariable("windowDidNotOpen", windowId);
    }
    // }}}
    // otherwise store the reference to the created window {{{
    else {
        // store the ID of opened window
        openedWindows[windowId] = windowObject;
    }
    // }}}
}

function closeWindow(windowId) {
    // close the window specified by the ID
    openedWindows[windowId].close();
    // remove the window from registered windows
    delete openedWindows[windowId];
}

function checkIfWindowIsOpen(windowId) {
    // determine reference to the window object
    var windowObject = openedWindows[windowId];

    // assume the window is not opened
    var windowOpened = false;
    
    // if window is opened, set the windowOpened flag to true {{{
    if ((windowObject != undefined) && (!windowObject.closed)) {
        windowOpened = true;
    }
    // }}}

    // if window was closed, inform the flash client {{{
    if (!windowOpened) {
        // get the flash object
        var flashObject = getFlashMovieObject(objectName);

        // set the windowClosed property
        flashObject.SetVariable("windowClosed", windowId);
    }
    // }}}
}

function onWindowResize() {
    // determine flash overlay corners
    var overlayOffsets = getFlashOverlayOffsets();
    // get the document size
    var dimensions = getDocumentSize();

    // update the movie size
    updateMovieSize(dimensions);

    // update overlayOffsets in flash movie {{{
    var flashObject = getFlashMovieObject(objectName);
    flashObject.SetVariable("windowSize", dimensions.width + "x" + dimensions.height);
    flashObject.SetVariable("overlayOffsets", overlayOffsets);
    // }}}
}

function getFlashMovieObject(movieName) {
    // some older Netscape and Mozilla {{{
    if (window.document[movieName]) {
        return window.document[movieName];
    }
    // }}}
    // Firefox and Opera {{{
    if (navigator.appName.indexOf("Microsoft Internet")==-1) {
        if (document.embeds && document.embeds[movieName]) {
            return document.embeds[movieName]; 
        }
    }
    // }}}
    // Internet Explorer {{{
    else if (navigator.appName.indexOf("Microsoft Internet")!=-1) {
        return document.getElementById(movieName);
    }
    // }}}
}

function getRatio() {
    // get the document size
    var dimensions = getDocumentSize();

    // compute ratios for both sides {{{
    var ratioHeight = dimensions.height / 650;		
    var ratioWidth = dimensions.width / 758;
    // }}}

    // use width ratio if its smaller {{{
    if (ratioHeight > ratioWidth) {
        var ratio = ratioWidth;
    } 
    // }}}
    // otherwise use height ratio {{{
    else {
        var ratio = ratioHeight;
    }
    // }}}
    
    // return the computed content ratio
    return ratio;
}

function getFlashOverlayOffsets() {
    // get the document size
    var dimensions = getDocumentSize();
    // get the flash content ratio
    var ratio = getRatio();

    // compute the real dimensions of the movie {{{
    var realMovieWidth = 758 * ratio;
    var realMovieHeight = 650 * ratio;
    // }}}

    // determine the offsets {{{
    var offsetX = Math.round(((realMovieWidth - dimensions.width) / 2) / ratio);
    var offsetY = Math.round(((realMovieHeight - dimensions.height) / 2) / ratio);
    // }}}

    // construct the offsets string for the flash client and return it
    return (offsetX + ":" + offsetY);
}

function updateMovieSize(dimensions) {
    // get the reference to flash object
    var flashObject = getFlashMovieObject(objectName);	

    // resize the flash movie for older IE versions {{{
    if ((document.all) && (!document.getElementById)) {
        flashObject.style.pixelWidth = dimensions.width;
        flashObject.style.pixelHeight = dimensions.height;
    }
    // }}}
    // resize the flash movie for modern browsers {{{
    else {
        flashObject.style.width = dimensions.width;
        flashObject.style.height = dimensions.height;
    }
    // }}}
}

function getDocumentSize() {
    // initialize the width and height variables {{{
    var documentWidth = 0
    var documentHeight = 0;
    // }}}

    // non-IE {{{
    if (window.innerWidth) {
        documentWidth = window.innerWidth;
        documentHeight = window.innerHeight;
    }
    // }}}
    //IE 4+ compatible {{{
    else {
        documentWidth = document.body.clientWidth;
        documentHeight = document.body.clientHeight;
    }
    // }}}

    // if the Flash client footer is visible, subtract its height from the
    // overal Flash client height, so that it fits on screen aswell {{{
    // get the reference to the Flash client footer
    var flashClientFooter = document.getElementById("flashClientFooter");

    // subtract footer's height from the total height {{{
    if ((flashClientFooter != null) && (flashClientFooter.style.display != "none")) {
        documentHeight -= flashClientFooter.offsetHeight;
    }
    // }}}

    // return the computed document dimensions
    return { width: documentWidth, height: documentHeight };
}

function setInitialSettings() {
    // determine reference to the flash movie
    var flashObject = getFlashMovieObject(objectName);
    // get the document size
    var dimensions = getDocumentSize();

    // set client settings in the flash movie {{{
    flashObject.SetVariable("bootURI", "**INSECURE**://**HOSTNAME**/en/shared/default/settings/boot?sid=f668acaa5f3d25b6635e2d8caf8d995f");
    flashObject.SetVariable("bootLoader", "http://199.103.56.174/en/flash/default/boot/bootLoader.swf?build=201003091928");
    flashObject.SetVariable("browserType", getBrowserType());
    flashObject.SetVariable("build", "201003091928");
    flashObject.SetVariable("clientCopyright", "Copyright © 2007-2010 Slotland Entertainment S.A., All Rights Reserved.");
    flashObject.SetVariable("extension", "");
    flashObject.SetVariable("hostname", "www.winaday.com");
    flashObject.SetVariable("swfHostname", "199.103.56.174");
    flashObject.SetVariable("language", "en");
    flashObject.SetVariable("loadingText", "Loading");
    flashObject.SetVariable("preLoadingText", "Win A Day Casino is loading");
    flashObject.SetVariable("loadingFailedText", "We apologize for the inconvenience but there was a problem while communicating with the server. Please check your Internet connection and try again. ==P====LIA|IA_RETRY_LAST_REQUEST==Try again==EL== ==P====FSMALL====B==Error URI==EB====EF====N==");
    flashObject.SetVariable("overlayOffsets", getFlashOverlayOffsets());
    flashObject.SetVariable("theme", "default");
    flashObject.SetVariable("windowSize", dimensions.width + "x" + dimensions.height);
    flashObject.SetVariable("jackpot", "10735375");
    // }}}

    // set flag to inform flash client that all initialization variables were set
    flashObject.SetVariable("initialSettings", 1);
}

function showFlashClientFooter() {
    // get reference to the Flash client footer
    var flashClientFooter = document.getElementById("flashClientFooter");

    // initial display type for all browsers
    var displayStyle = "table";

    // IE does not support 'table' display properly, so use block {{{
    if (getBrowserType() == 1) {
        displayStyle = "block";
    }
    // }}}

    // show the footer table {{{
    if (flashClientFooter.style.display != displayStyle) {
        // show the footer table
        flashClientFooter.style.display = displayStyle;

        // resize the Flash client size
        onWindowResize();
    }
    // }}}
}

function hideFlashClientFooter() {
    // get reference to the Flash client footer
    var flashClientFooter = document.getElementById("flashClientFooter");

    // if the footer table is visible, hide it and update client size {{{
    if (flashClientFooter.style.display != "none") {
        // hide the footer table
        flashClientFooter.style.display = "none";

        // update the Flash client size
        onWindowResize();
    }
    // }}}
}

function insertFlashClient() {
    // get document dimensions
    var windowDimensions = getDocumentSize();

    // use 'transparent' wmode to enable z-indexing of the Flash object {{{
    // NOTE: enabled for all Windows browsers but Opera which freezes with wmode enabled
    if ((getOSType() == 0) && (getBrowserType() != 2)) {
        var wmode = "transparent";
    }
    // }}}
    // Firefox on Linux has weak support for wmode (not working, crashing the
    // browser or enormous slow downs of the Flash content) {{{
    else {
        var wmode = "normal";
    }
    // }}}

    // create the content string to be written into the document {{{
    var content = '<object style="z-index: 1;" classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000"\n' + 
                  '  codebase="https://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=10,0,0,0"\n' +
                  '  width="' + windowDimensions.width + '" ' +
                  '  height="' + windowDimensions.height + '" ' +
                  '  id="main" align="top">\n' +
                  '  <param name="allowScriptAccess" value="always" />\n' +
                  '  <param name="swLiveConnect" value="true" />\n' +
                  '  <param name="movie" value="http://199.103.56.174/en/flash/default/boot/preLoader.swf?build=201003091928" />\n' +
                  '  <param name="quality" value="best" />\n' +
                  '  <param name="wmode" value="' + wmode + '" />\n' +
                  '  <param name="bgcolor" value="#000000" />\n' +
                  '  <embed src="http://199.103.56.174/en/flash/default/boot/preLoader.swf?build=201003091928" quality="best" bgcolor="#000000"' +
                  '     swLiveConnect="true" wmode="' + wmode + '" width="' + windowDimensions.width + '" '+
                  '     height="' + windowDimensions.height + '" ' + 
                  '     name="main" align="top"\n' +	
                  '     allowScriptAccess="always" type="application/x-shockwave-flash"\n' +
                  '     pluginspage="http://www.macromedia.com/go/getflashplayer"/>\n' +	
                  '</object>\n';
    // }}}

    // write the content for mozilla based browsers {{{
    if (getBrowserType() == 0) {
        document.write(content);
    } 
    // }}}
    // write the content for other browsers {{{
    else {
        window.document.write(content);
    }
    // }}}
}

function insertImagePageHeader() {
    // create the content string to be written into the document
    var content = '<img src="/en/file/default/image/pageHeader.gif" border="0" width="573" height="141" alt="Win A Day Casino" />';
    // write the content
    document.getElementById("pageHeader").innerHTML = content;
}

function insertImagesInstallFlash() {
    // create the content string to be written into the document {{{
    var content = '<img id="offImage" src="/en/file/default/image/flashOff.gif" width="391" height="176" alt="Click here to install Adobe Flash Player" />' +
                  '<img src="/en/file/default/image/flashOn.gif" width="391" height="176" alt="Click here to install Adobe Flash Player" />';
    // }}}
    
    // write the content
    document.getElementById("installFlashLink").innerHTML = content;
}

function insertImagesUpgradeFlash() {
    // create the content string to be written into the document {{{
    var content = '<img id="offImage" src="/en/file/default/image/flashUpgradeOff.gif" width="391" height="176" alt="Click here to upgrade Adobe Flash Player" />' +
                  '<img src="/en/file/default/image/flashUpgradeOn.gif" width="391" height="176" alt="Click here to upgrade Adobe Flash Player" />';
    // }}}
    
    // write the content
    document.getElementById("upgradeFlashLink").innerHTML = content;
}

function insertImagesScreenshot() {
    // create the content string to be written into the document {{{
    var content = '<img src="/en/file/default/image/wadEntrance.jpg" width="282" height="200" alt="Win A Day Casino Screenshot of Entrance" />' +
                  '<img src="/en/file/default/image/wadGames.jpg" width="215" height="200" alt="Win A Day Casino Screenshot of Games" />';
    // }}}
    
    // write the content
    document.getElementById("screenshots").innerHTML = content;
}

function showTransactionButton(buttonText, x, y) {
    // get the reference to the transacttion button
    var buttonDiv = document.getElementById("transactionButton");
 
    // tell the button to ignore document flow for positioning it
    // (must be done here otherwise IE will not display the DIV correctly)
    buttonDiv.style.position = "absolute";

    // determine the current ratio of the content
    var ratio = getRatio();

    // position the button applying the ratio on both axis to achieve correct
    // placement according to the flash movie {{{
    buttonDiv.style.left = x * ratio;
    buttonDiv.style.top = y * ratio;
    // }}}
    
    // set the fontSize also applying the ratio to match font size in the flash client
    buttonDiv.style.fontSize = Math.round(15 * ratio);

    // insert the button text into the div
    buttonDiv.innerHTML = buttonText;

    // show the transaction button
    document.getElementById("transactionButton").style.display = "block";

    // Firefox on Linux does not support proper wmode and z-indexing so use the
    // iframe hack to display the button {{{
    if (getOSType() == 1) {
        // get the reference to the iframe
        var iframe = document.getElementById("transactionIframe");

        // position and resize the iframe according to the button's dimensions {{{
        iframe.style.left = buttonDiv.style.left;
        iframe.style.top = buttonDiv.style.top;
        iframe.style.width = buttonDiv.offsetWidth;
        iframe.style.height = buttonDiv.offsetHeight;
        // }}}       

        // show the iframe
        iframe.style.display = "block";
    }
    // }}}
}

function hideTransactionButton() {
    // get the reference to the transacttion button
    var buttonDiv = document.getElementById("transactionButton");

    // reset the content of the div
    buttonDiv.innerHTML = "";
    // hide the transaction button
    buttonDiv.style.display = "none";

    // hide the iframe in Linux hack {{{
    if (getOSType() == 1) {
        // get the reference to the iframe
        var iframe = document.getElementById("transactionIframe");

        // hide the iframe
        iframe.style.display = "none";
    }
    // }}}
}

function hasFlash(requiredFlashVersion) {
    // get the version numbers {{{
    // get version numbers for the required flash version {{{
    var requiredVersion = new String(requiredFlashVersion).split(".");
    var requiredMajor = Number(requiredVersion[0]);
    var requiredMinor = Number(requiredVersion[1]);
    var requiredRevision = Number(requiredVersion[2]);
    // }}}

    // get version numbers for the client flash version {{{
    var clientVersion = new String(getFlashVersion()).split(".");
    var clientMajor = Number(clientVersion[0]);
    var clientMinor = Number(clientVersion[1]);
    var clientRevision = Number(clientVersion[2]);
    // }}}
    // }}}

    // if major version is higher any other checks are not necessary so skip them and return true {{{
    if (clientMajor > requiredMajor) {
        return true;
    }
    // }}}
    // if same major versions, check the minor and revision numbers {{{
    else if (clientMajor == requiredMajor) {
        // if minor version is higher any other checks are not necessary so return true {{{
        if (clientMinor > requiredMinor) {
            return true;
        }
        // }}}
        // otherwise check revision {{{
        else if (clientMinor == requiredMinor) {
            // if revision equals or is higher, return true {{{
            if (clientRevision >= requiredRevision) {
                return true;
            }
            // }}}
        }
        // }}}
    }
    // }}}

    // otherwise return false as the client version is too low 
    return false;
}

function getFlashVersionString(pluginVersionString) {
    // get flash version out of the version string
    var version = pluginVersionString.match(/[\d]+/g);

    // limit the version length to 3 digits 
    // (compatibility between IE and FF)
    version.length = 3;

    // return the version
    return version.join(".");
}

function getFlashVersion() {
    // initialize variable which will hold the flash version
    var flashVersion = '';

    // every browser except IE supports the plugins container {{{
    if ((navigator.plugins) && (navigator.plugins.length)) {
        // get the plugin object
        var plugin = navigator.plugins['Shockwave Flash'];

        // if the plugin is set, look for description and get the flash version {{{
        if (plugin) {
            if (plugin.description) {
                flashVersion = getFlashVersionString(plugin.description);
            }
        }
        // }}}
    }
    // }}}
    // if the first failed, next solution is to look up the mimeTypes container {{{
    else if ((navigator.mimeTypes) && (navigator.mimeTypes.length)) {
        // check if the flash MIME type can be found
        var mimeType = navigator.mimeTypes['application/x-shockwave-flash'];

        // if the MIME type was found and plugin is enabled, access the version
        // through MIME type plugin object {{{
        if (mimeType && mimeType.enabledPlugin) {
            flashVersion = getFlashVersionString(mimeType.enabledPlugin.description);
        }
        // }}}
    }
    // }}}
    // get version from Internet Explorer {{{
    else {
        // get the version from the ActiveX object for Flash plugin 7 and up {{{
        try {
            // try 7 first, since we know we can use GetVariable with it
            var ax = new ActiveXObject('ShockwaveFlash.ShockwaveFlash.7');
            flashVersion = getFlashVersionString(ax.GetVariable('$version'));
        }
        // }}}
        // no valid version so try the default ActiveX object {{{
        catch (e) {
            // try the default activeX {{{
            try {
                var ax = new ActiveXObject('ShockwaveFlash.ShockwaveFlash');
                flashVersion = getFlashVersionString(ax.GetVariable('$version'));
            }
            // }}}
            // no flash {{{
            catch (e) {
                // No flash
            }
            // }}}
        }
        // }}}
    }
    // }}}

    // return the flash version
    return flashVersion;
}

// if the client's Flash Plugin version meets the requirements, initialize the main flash movie {{{
if (hasFlash("10.0.0")) {
    // make sure the browser scrollbars are always hidden
    document.body.style.overflow='hidden';
    // draw the Flash client
    insertFlashClient();
}
// }}}
// otherwise display HTML message with instructions how to get the required version of the Flash Plugin {{{
else {
    // draw the page header image
    insertImagePageHeader();

    // for users with old version of Flash plugin display a message that they need to update it {{{
    if (hasFlash("0.0.0")) {
        // draw the upgrade flash images
        insertImagesUpgradeFlash();
        // show the div container which will display the message
        document.getElementById("upgradeFlash").style.display = "block";
    }
    // }}}
    // for users without Flash plugin display a message that they need to install it {{{
    else {
        // draw the install flash images
        insertImagesInstallFlash();
        // show the div container which will display the message
        document.getElementById("installFlash").style.display = "block";
    }
    // }}}

    // draw the screenshot images
    insertImagesScreenshot();
    // display the div with instructions to install the Flash plugin
    document.getElementById("noFlash").style.display = "block";
    // make the div container with the SEO links position relative
    document.getElementById("flashClientFooter").style.position = "static";
}
// }}}
