﻿function SetFocus() {
    var gameElement = document.getElementById("flashGame");

    if (!gameElement) {
        gameElement = document.getElementById("silverlightGame");
    }

    gameElement.focus();
}

function NavigateTo(url, close) {
    // We expect to be in a dialog window, so try to 
    // close the window and launch the window that opened 
    // this dialog to the specified location.  If we get an 
    // error doing so (security exception; window does not exist, etc.)
    // we will fall back to use the current window.
    if (window.opener) {
        try {
            window.opener.location.href = url;

            if (close) {
                window.close();
            }

            return;
        }
        catch (ex) { }
    }

    window.location.href = url;
}

var gameInProgress = false;

var GameShowAPI = {};
GameShowAPI = function() { }

GameShowAPI.Search = function(termToSearch, typeOfSearch, formCode, id) {
    if (id === undefined || id == null) {
        id = "";
    }
    else {
        id = escape(id).replace("+", "%2B").replace("=", "%3D").replace("/", "%2F");
    }

    if (formCode == null) {
        var formCode = "MICC11";
    }

    var ifSearchResult = document.getElementById("ifSearchResult");

    var url = "";

    if (typeOfSearch == "images") {
        url = "http://www.bing.com/images/search?q=" + escape(termToSearch) + "&mkt=en-us&adlt=strict&FORM=" + formCode + "&id=" + id;
    }
    else if (typeOfSearch == "news") {
        url = "http://www.bing.com/news/search?q=" + escape(termToSearch) + "&mkt=en-us&adlt=strict&FORM=" + formCode + "&id=" + id;
    }
    else if (typeOfSearch == "videos") {
        url = "http://www.bing.com/videos/search?q=" + escape(termToSearch) + "&mkt=en-us&adlt=strict&FORM=" + formCode + "&id=" + id;
    }
    else if (typeOfSearch == "books") {
        url = "http://www.bing.com/search?q=" + escape(termToSearch) + "&scope=books&mkt=en-us&adlt=strict&FORM=" + formCode + "&id=" + id;
    }
    else if (typeOfSearch == "products") {
        url = "http://www.bing.com/shopping/search?q=" + escape(termToSearch) + "&mkt=en-us&adlt=strict&FORM=" + formCode + "&id=" + id;
    }
    else if (typeOfSearch == "maps") {
        url = "http://www.bing.com/maps/default.aspx?q=" + escape(termToSearch) + "&mkt=en-us&FORM=" + formCode + "&id=" + id;
    }
    else if (typeOfSearch == "local") {
        url = "http://www.bing.com/local/default.aspx?what=" + escape(termToSearch) + "&mkt=en-us&adlt=strict&FORM=" + formCode + "&id=" + id;
    }
    else if (typeOfSearch == "advanced") {
        url = "http://www.bing.com/search?q=" + escape(termToSearch) + "&qb=1&mkt=en-us&adlt=strict&FORM=" + formCode + "&id=" + id;
    }
    else if (typeOfSearch == "academic") {
        url = "http://www.bing.com/search?scope=academic&q=" + escape(termToSearch) + "&mkt=en-us&adlt=strict&FORM=" + formCode + "&id=" + id;
    }
    else if (typeOfSearch == "url") {
        // not actually searching, just passing a URL through. Use this for any pages MS wants the user to go to directly.
        url = termToSearch;
    }
    else {
        url = "http://www.bing.com/search?q=" + escape(termToSearch) + "&mkt=en-us&adlt=strict&FORM=" + formCode + "&id=" + id;
    }

    ifSearchResult.src = url.replace("#", "");

    setTimeout('SetFocus();', 500);

    uet.fls();
}

GameShowAPI.GameStarted = function(game) {

    var multiplayer = (typeof game === "object" && typeof game.multiplayer === "boolean" && game.multiplayer === true);

    if (game === null || !multiplayer) {
        CheckAsirraStatus();
    }

    gameInProgress = true;
    uet.fgs();
};

GameShowAPI.GameEnded = function(game) {
    gameInProgress = false;
    RefreshTickets();
    uet.fge();
}

GameShowAPI.RefreshModules = function() {
    RefreshTickets();
}

GameShowAPI.SignIn = function() {
    window.location.href = '../../SignIn.aspx?rg=' + window.location.href;
}

GameShowAPI.GetPrizes = function() {
    NavigateTo('../Prizes/Prize_Mall.aspx', true);
}

GameShowAPI.get_SoundEnabled = function() {
    var cookie = GetCookie("SoundEnabled");
    var result = true;

    if (cookie) {
        result = GetCookieValue(cookie) == 1;
    }

    return result;
}

GameShowAPI.ShowCurtains = function(show) {
    var curtains = $("div[id='GameRight'], div[id='GameLeft']");

    if (show) {
        curtains.show();
    }
    else {
        curtains.hide();
    }
}

GameShowAPI.SetGameWidth = function(pixels) {
    var game = $("object[id='flashGame'], object[id='silverlightGame']");
    var embed = $("embed", game);

    if ((pixels) && (!isNaN(pixels))) {
        game.attr("width", pixels);
        embed.attr("width", pixels);
    }
}

GameShowAPI.set_SoundEnabled = function(value) {
    var enabled = (value ? 1 : 0);
    var date = new Date();

    // Expire in 24 hours
    date.setTime(date.getTime() + 24 * 60 * 60 * 1000);

    var cookie = "SoundEnabled=" + enabled +
                "; expires=" + date.toGMTString() +
                "; path=/" +
                "; domain=." + document.location.host;

    document.cookie = cookie;
}

function GetCookie(name) {
    var cookies = document.cookie.split('; ');
    var result = null;

    if (name) {
        for (var i = 0; i < cookies.length; i++) {
            if (cookies[i].split('=')[0] == name) {
                result = cookies[i];
                break;
            }
        }
    }

    return result;
}

function GetCookieValue(cookie) {
    var result = null;

    if (cookie) {
        var keyValuePair = cookie.split('=');

        if (keyValuePair.length == 2) {
            result = keyValuePair[1];
        }
    }

    return result;
}

function RefreshTickets() {
    if (opener && opener.RefreshTickets) {
        opener.RefreshTickets();
    }
}

function CheckAsirraStatus() {    
    if (typeof asirraRequired === "function" && asirraRequired()) {
        var div = $("#asirra-challenge");
        createSession(div);
    }
}