/*
 * Author:  Darrin Jahnel
 * Date:    7/28/2011
 *
 * This file is for all javascript popup windows.  These are specifically used to
 * popup the individual game windows.  Make sure you include this file on any page
 * that may popup a window.  It's probably best to include this in the master page.
 * You also need to include this file in the actual game container page so it can
 * do the proper decoding.
 */



/*
 * popupWindow function
 * Example Call:  <a href="http://www.webtoolkit.info" onclick="return popupWindow(this, {width:790,height:450,center:true})">LINK CONTENT</a>
 *
 * Options:  
 *     width
 *     height
 *     left
 *     top
 *     center
 *     name
 *     scrollbars
 *     menubar
 *     locationbar
 *     resizable 
 */
function popupWindow(anchor, options) {

    var args = '';

    if (typeof (options) == 'undefined') { var options = new Object(); }
    if (typeof (options.name) == 'undefined') { options.name = 'win' + Math.round(Math.random() * 100000); }

    if (typeof (options.height) != 'undefined' && typeof (options.fullscreen) == 'undefined') {
        args += "height=" + options.height + ",";
    }

    if (typeof (options.width) != 'undefined' && typeof (options.fullscreen) == 'undefined') {
        args += "width=" + options.width + ",";
    }

    if (typeof (options.fullscreen) != 'undefined') {
        args += "width=" + screen.availWidth + ",";
        args += "height=" + screen.availHeight + ",";
    }

    if (typeof (options.center) == 'undefined') {
        options.x = 0;
        options.y = 0;
        args += "screenx=" + options.x + ",";
        args += "screeny=" + options.y + ",";
        args += "left=" + options.x + ",";
        args += "top=" + options.y + ",";
    }

    if (typeof (options.center) != 'undefined' && typeof (options.fullscreen) == 'undefined') {
        options.y = Math.floor((screen.availHeight - (options.height || screen.height)) / 2) - (screen.height - screen.availHeight);
        options.x = Math.floor((screen.availWidth - (options.width || screen.width)) / 2) - (screen.width - screen.availWidth);
        args += "screenx=" + options.x + ",";
        args += "screeny=" + options.y + ",";
        args += "left=" + options.x + ",";
        args += "top=" + options.y + ",";
    }

    if (typeof (options.scrollbars) != 'undefined') { args += "scrollbars=1,"; }
    if (typeof (options.menubar) != 'undefined') { args += "menubar=1,"; }
    if (typeof (options.locationbar) != 'undefined') { args += "location=1,"; }
    if (typeof (options.resizable) != 'undefined') { args += "resizable=1,"; }

    var win = window.open(anchor, options.name, args);
    return false;

}

/*
 * This is a simple method that returns true if "localhost" is in the URL
 */
function isLocal() {
    var urlString = String(window.location);
    if (urlString.indexOf('localhost') == -1) {
        return false;
    } else {
        return true;
    }
}




function popGameWindow(gameName) {
    var linkToURL;
    var userDataParams;
    var samsonURLPrefix;

    if (isLocal() == true) {
        samsonURLPrefix = "http://localhost/samsonsclassroom";
    } else {
        samsonURLPrefix = "http://www.samsonsclassroom.com";
    }

    userDataParams = document.getElementById('html_user_data_string').value;
    if (userDataParams == "") {
        // If the data params are empty try the demo params
        userDataParams = document.getElementById('html_user_data_string_for_demo').value;
    }

    var windowPropertiesObject = { menubar: false, toolbar: false, status: false, resizable: true, width: 750, height: 540, left: 10, top: 10 };

    if (gameName == 'Showdown') {
        popupWindow("../showdown/secure_showdown_game_container.aspx?" + userDataParams, windowPropertiesObject);
    } else if (gameName == 'Factoids') {
        popupWindow("../factoids/secure_factoids_game_container.aspx?" + userDataParams, windowPropertiesObject);
    } else if (gameName == 'Statewide') {
        popupWindow("../statewide/secure_statewide_game_container.aspx?" + userDataParams, windowPropertiesObject);
    } else if (gameName == 'Sight Words') {
        popupWindow(samsonURLPrefix + "/sight_words/secure_sight_words_game_container.aspx?" + userDataParams, windowPropertiesObject);
    } else if (gameName == 'Spelling') {
        popupWindow(samsonURLPrefix + "/spelling/secure_spelling_game_container.aspx?" + userDataParams, windowPropertiesObject);
    } else if (gameName == 'Reading') {
        popupWindow(samsonURLPrefix + "/reading/secure_reading_game_container.aspx?" + userDataParams, windowPropertiesObject);
    }
}


