/*jslint browser:true, white:false, nomen:false, plusplus:false */
/*globals YY, AC_FL_RunContent, traqCreatePytime */

function pytime_to_jstime(nodename,jsDate)
{
    var months, minutes, hours, datestr, el;
    months = [ 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec' ];
    minutes = "" + jsDate.getMinutes();
    if ( minutes.length === 1 )
    {
        minutes = "0" + minutes;
    }
    hours = jsDate.getHours() % 12;
    if ( hours === 0 )
    {
        hours = 12;
    }
    datestr = months[jsDate.getMonth()] + " " + jsDate.getDate() + ", " + jsDate.getFullYear() + " " + hours +
              ":" + minutes + " " + (jsDate.getHours() < 12 ? "AM" : "PM");
    el = document.getElementById(nodename);
    el.firstChild.nodeValue = datestr;
}

function unsupportedBrowser()
{
    var ua, unsupported_browser = null;
    try
    {
        ua = navigator.userAgent.toLowerCase();
        if (ua.indexOf("iphone") !== -1)
        {
            unsupported_browser = "iPhone+Safari";
        }
        else if (ua.indexOf("ipad") !== -1)
        {
            unsupported_browser = "iPad+Safari";
        }
        else if (ua.indexOf("webos") !== -1)
        {
            unsupported_browser = "webos";
        }
        else if (ua.indexOf("android") !== -1)
        {
            unsupported_browser = "android";
        }
        else if (ua.indexOf("up.browser") !== -1)
        {
            unsupported_browser = "Openwave/Purple+Labs";
        }
        // add more unsupported browsers here...
    }
    catch(e)
    {
        unsupported_browser = "Unknown";
    }
    return unsupported_browser;
}

function mbedplayerIsRunning() // player tells us when it's ready
{
    YY.mbedplayer = document.getElementById("mbedplayer");
}

var load_external_player = false;
var waited_way_too_long = Math.floor(45 * 1000 / 333);
function pubpageLoaded2()
{
    var i, neededInYY, needed;
    // make sure everything we need is loaded
    neededInYY = [ 'mbedplayer' ];
    for ( i = 0; i < neededInYY.length; i++ )
    {
        needed = neededInYY[i];
        if ( !(needed in YY) )
        {
            if ( 0 === (--waited_way_too_long) )
            {
                document.getElementById("loadingflash").innerHTML =
                    '<p>Flash does not appear to be installed.  You may download<br/>' +
                    'the current Flash player from the <a href="http://www.adobe.com/go/getflashplayer" target="_blank">Adobe Download Site</a>.</p>';
            }

            setTimeout(pubpageLoaded2,200);
            return;
        }
    }

    document.getElementById("play-row").style.display = "none";
    try { document.getElementById("loadedflash").style.display = "block"; } catch(e0) { }
    try { document.getElementById("playbtn").style.display = "block"; } catch(e1) { }
}

function pubpageLoaded(validTraq)
{
    var ub, html5audio;

    function i_Safari()
    {
        document.getElementById("iphonehint").style.display = "block";
        html5audio = document.getElementById("html5audio");
        html5audio.innerHTML = '<audio src="' + YY.redirectUrl + '" controls style="width:200px;height:20px;"></audio>';
        html5audio.style.display = "block";
        document.getElementById("loadingflash").style.display = "none";
    }

    document.getElementById("allhint").style.display = "block";
    if ( document.getElementById("local-create-time") )
    {
        pytime_to_jstime("local-create-time",traqCreatePytime);
    }
    ub = unsupportedBrowser();
    switch (ub)
    {
    case null:
        document.getElementById("browserhint").style.display = "block";
        if (document.getElementById("embedmap"))
        {
            document.getElementById("embedmap").style.display = "block";
            document.getElementById("embedmap").src = YY.mapEmbedUrl;
        }
        break;
    case "iPad+Safari":
        if (document.getElementById("embedmap"))
        {
            document.getElementById("embedmap").style.display = "block";
            document.getElementById("embedmap").src = YY.mapEmbedUrl;
        }
        i_Safari();
        return;
    case "iPhone+Safari":
        i_Safari();
        return;
    /*case "webos":
    case "android":*/
    default:
        document.getElementById("play-row").style.display = "none";
        document.getElementById("loadedflash").style.display = "block";
        document.getElementById("playbtn").style.display = "block";
        load_external_player = true;
        return;
    }

    if (!validTraq)
    {
        return;
    }

    /*jslint newcap:false */
    document.getElementById('play-img').innerHTML =
    AC_FL_RunContent( 'codebase','http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,28,0',
                      'flashVars','l='+YY.duration+'&e='+YY.create_epoch_code+'&i='+YY.id_code+(YY.JSESSIONID?('&j='+YY.JSESSIONID):'')+'&h='+YY.imageLength+(YY.nsfw?'':'&auto')+"&scracc"+(YY.local?'&local':''),
                      'width',String(YY.imageLength),
                      'height',String(YY.imageLength),
                      'title','mbedplayer','id','mbedplayer',
                      'src','/WebMedia/swf/v001/mbedtrackplay.swf?vr=5',
                      'quality','high','allowScriptAccess','always',
                      'pluginspage','http://www.adobe.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash',
                      'movie','/WebMedia/swf/v001/mbedtrackplay.swf?vr=5' );
    /*jslint newcap:true */

    setTimeout(pubpageLoaded2,200);
}

function PlayBtn(el,clipID)
{
    if (load_external_player)
    {
        // redirect to MP3 file directly (doesn't work for the iPhone for some reason)
        document.location.href = YY.redirectUrl;
    }
}

