/*jslint browser:true, white:false, nomen:false, plusplus:false */
/*globals window, pageLoaded, YAHOO, alert */
// about_common.js - common script loaded into ALL the about pages

var YY = {};
YY.dbgMsgLn = function(msg) { };

function wait_for_everything_to_be_loaded(loadwaits/*array of things that must be on window before continuing*/)
{
    for ( var i = 0; i < loadwaits.length; i++ )
    {
        if ( !(loadwaits[i] in window) )
        {
            YY.dbgMsgLn("Must wait for component \"" + loadwaits[i] + "\" before we know page is loaded.");
            break;
        }
    }
    if ( i < loadwaits.length )
    {
        setTimeout(function(){wait_for_everything_to_be_loaded(loadwaits);},200);
    }
    else
    {
        try { (new YAHOO.widget.Button('launch-player')).on("click", function() { YY.LaunchRadioPlayer(); } ); } catch(e) { }
        document.getElementById("about-doc").style.display = "block";
    
        if ( "pageLoaded" in window )
        {
            pageLoaded();
        }
    }
}

function about_yui_init( additional_yui_modules, loadwaits )
{
    var require, i, loader;
    require = [ "reset", "base", "fonts", "grids", "container", "button" ]; // equalized css, fonts, and grids across browsers

    for ( i = 0; i < additional_yui_modules.length; i++ )
    {
        require.push(additional_yui_modules[i]);
    }

    loader = new YAHOO.util.YUILoader({
        require: require,
        loadOptional: true,
        onFailure: function(msg, xhrobj) {
            var m = "about_common.js YAHOO.util.YUILoader load failed: " + msg.msg + ", data: " + msg.data;
            alert("Sorry, an internal error has occurred: " + m);
        },
        onSuccess: function() {
            wait_for_everything_to_be_loaded( loadwaits );
        }
    });

    // Load the files using the insert() method. The insert method takes an optional
    // configuration object, and in this case we have configured everything in
    // the constructor, so we don't need to pass anything to insert().
    loader.insert();
    return loader;
}

function setInstructionCookie(value) {
    document.cookie = "cookie-instruction=" + value + "; path=/";
    return true;
}
function getInstructionCookie() {
    var i, c, cname, ca;
    cname = "cookie-instruction=";
    ca = document.cookie.split(';');
    for (i=0; i < ca.length; i++) {
        c = ca[i];
        while (c.charAt(0)===' ') { c = c.substring(1,c.length); }
        if (c.indexOf(cname) === 0) {
            return c.substring(cname.length, c.length);
        }
    }
    return null;
}

YY.LaunchRadioPlayer = function(previewChannel,previewChannelUrl)
{
    setInstructionCookie("Launch" + (previewChannel ? ('_'+previewChannel) : '') );
    setTimeout(function() {
        var instruction = getInstructionCookie();
        if ( !instruction  ||  (instruction !== "DONE") )
        {
            if ( previewChannelUrl )
            {
                window.open(previewChannelUrl,"rwplayer");
            }
            else
            {
                window.open("/v001/","rwplayer");
            }
        }
    }, 2000);
};
