var windowsmedia = new Object();
// Set some base values
windowsmedia.installed = false;
windowsmedia.version = '0.0'; // Internet Explorer-only

var detect = navigator.userAgent.toLowerCase();
var isWin = detect.indexOf("win") > -1;

// Check for GeckoActiveXObject and co-inciding Plug-In
var gkoaxwma = false;
if (navigator.plugins && navigator.plugins.length)
{
    for (x = 0; x < navigator.plugins.length; x++)
    {
        if (navigator.plugins[x].name.indexOf('ActiveX') != -1 && window.GeckoActiveXObject)
        {
            gkoaxwma = true;
            break;
        }
    }
    
}

// Create an ActiveX/GeckoActiveX constructor
function AXO(id)
{
    var error;
    var control = null;
    try
    {
        if (window.ActiveXObject && navigator.userAgent.indexOf('Win') != -1) control = new ActiveXObject(id);
        else if (gkoaxwma) control = new GeckoActiveXObject(id);
    }
    catch (error)
    {
    }
    return control;
}

if (window.ActiveXObject || gkoaxwma)
{
    try
    {
        oWMP = new AXO('WMPlayer.OCX.7');
        if (oWMP)
        {
            windowsmedia.installed = true;

			// A wierd bug in the Gecko ActiveX plug-in will return
            // undefined at the first call, but the correct value on the second.
            // This "fix" doesn't seem to hurt IE at all.
            parseFloat(oWMP.versionInfo);

            windowsmedia.version = parseFloat(oWMP.versionInfo);
            if (windowsmedia.version.toString().length == 1) windowsmedia.version += '.0';
        }
    }
    catch(e)
    {
    }
}
else if (navigator.plugins && navigator.plugins.length)
{
    for (x = 0; x < navigator.plugins.length; x++)
    {
        if (navigator.plugins[x].name.indexOf('Windows Media') != -1)
        {
            windowsmedia.installed = true;
            break;
        }
    }
}

// Internet Explorer or GeckoActiveXObject-compatible browsers only.
windowsmedia.ver7 = (windowsmedia.installed && parseInt(windowsmedia.version) >= 7) ? true : false;
windowsmedia.ver8 = (windowsmedia.installed && parseInt(windowsmedia.version) >= 8) ? true : false;
windowsmedia.ver9 = (windowsmedia.installed && parseInt(windowsmedia.version) >= 9) ? true : false;

hasPlayer = false;

if (windowsmedia.installed)
{
    //hasPlayer = true;
    hasPlayer = false;
    embedObject = "<OBJECT id=\"Player\" name=\"Player\" width=\"0\" height=\"0\" style=\"position:absolute; left:0;top:0;\" CLASSID=\"CLSID:";
    if (windowsmedia.ver7)
    {
        classID = "6BF52A52-394A-11D3-B153-00C04F79FAA6";
    }
    else
    {
        classID = "622D6F312-B0F6-11D0-94AB-0080C74C7E95";
    }
    embedObject += classID;
    embedObject += "\" type=\"application/x-oleobject\" VIEWASTEXT>";
    embedObject += "<PARAM NAME=\"SendPlayStateChangeEvents\" VALUE=\"True\">";
    embedObject += "<PARAM name=\"uiMode\" value=\"none\">";
    embedObject += "<PARAM name=\"PlayCount\" value=\"1\">";
    embedObject += "<embed SendPlayStateChangeEvents=\"True\" uiMode=\"none\" pluginspage=\"http://www.microsoft.com/windows/windowsmedia/download/\" PlayCount=\"1\" width=\"0\" height=\"0\"></embed></object>";
}
else
{
    //Commented this out for now due to Shachar saying that this is prompting people every time. We should probably
    //prompt once and cookie it so it does not prompt for that session.
    //alert("No Windows Media Player");
}

//play audio
IsPlaying = 0;
LastPlay = -1;
lastVideoPlay = '';
IsVideoPlaying = 0;
timeoutID = null;

// MP3 player
var MP3playimg = null;
var MP3player = null;
var mp3PlayImg = null;
var IsMP3Playing = 0;

var stopImage = new Image();
stopImage.src = ImageURL + '/Stop.gif'; // predownload the image

var loadingStopImage = false;

function playAudio(TrackURL, CurrentPlay)
{
    thePlayer = document.getElementById("Player");


    if (!hasPlayer || thePlayer == null || thePlayer.controls == null)
    {
        //if( TrackURL.toLowerCase().indexOf("mms:") > -1 )
        //{

        if (isWin)
            o = window.open("../stream.aspx?URL=" + escape(TrackURL), "stream");
        else
            document.location.href = "../stream.aspx?URL=" + escape(TrackURL);
		//}


    }
    else
    {
        CurrentButton = 'PlayerButton' + CurrentPlay;
        var playButton = document.getElementById(CurrentButton);
		//playButton.onload = fnPlayButtonIsReady;
        //playButton.onerror = fnPlayButtonOnError;

        if (IsPlaying != -1)
        {

            if (LastPlay == CurrentPlay)
            {
                //document[CurrentButton].src = ImageURL + '/Play.gif';
                playButton.src = ImageURL + '/Play.gif';
                thePlayer.controls.stop();
                LastPlay = -1;
                IsPlaying = 0;
                if (timeoutID != null) clearTimeout(timeoutID);
                timeoutID = null;
            }
            else
            {
                setTimeout('thePlayer.URL=\'' + TrackURL.replace("\'", "\\'") + '\';', 0); //this needs to be a setTimeout to prevent the purple box error
                //playButton.src = ImageURL + '/Stop.gif';
                playButton.src = stopImage.src;

                if (LastPlay != -1)
                {
                    LastButton = 'PlayerButton' + LastPlay;
                    var lastButton = document.getElementById(LastButton);
					//document[LastButton].src = ImageURL + '/Play.gif';
                    lastButton.src = ImageURL + '/Play.gif';
                    if (timeoutID != null) clearTimeout(timeoutID);
                }

                LastPlay = CurrentPlay;
                IsPlaying = 1;
                timeoutID = setTimeout('playAudio(\'\', \'' + CurrentPlay + '\')', 35000);
            }
        }
        else
        {
            if (andyDebug)alert('8');
            setTimeout('thePlayer.URL=\'' + TrackURL.replace("\'", "\\'") + '\';', 0);
			//playButton.src = ImageURL + '/Stop.gif';
            playButton.src = stopImage.src;
            LastPlay = CurrentPlay;
            IsPlaying = 1;
            timeoutID = setTimeout('playAudio(\'\', \'' + CurrentPlay + '\')', 35000);
        }
    }

    if (lastVideoPlay != '' && IsVideoPlaying == 1)
    {
        toggleLayer(lastVideoPlay, '', '', '');
        lastVideoPlay = '';
    }

    if (MP3player && IsMP3Playing == 1)
    {
        stopMP3(mp3PlayImg);
    }
}

//function fChangeImage(playButton, stopImage)
//{
//    playButton.src = stopImage.src;
//    playButton.onload = fnPlayButtonIsReady;
//}

function fnPlayButtonIsReady()
{
    alert('button onload');
}

function fnPlayButtonOnError()
{
    alert('button error');
}

if (hasPlayer == true && isWin)
{
    document.write(embedObject);
}

LastClientID = '';

function f_scrollTop()
{
    return f_filterResults(
            window.pageYOffset ? window.pageYOffset : 0,
            document.documentElement ? document.documentElement.scrollTop : 0,
            document.body ? document.body.scrollTop : 0
            );
}

function f_filterResults(n_win, n_docel, n_body)
{
    var n_result = n_win ? n_win : 0;
    if (n_docel && (!n_result || (n_result > n_docel)))
        n_result = n_docel;
    return n_body && (!n_result || (n_result > n_body)) ? n_body : n_result;
}


function toggleLayer(whichLayer, url, homeurl, clientID)
{
    var elem, vis;
    if (document.getElementById) // this is the way the standards work
        elem = document.getElementById(whichLayer);
    else if (document.all) // this is the way old msie versions work
        elem = document.all[whichLayer];
    else if (document.layers) // this is the way nn4 works
        elem = document.layers[whichLayer];
    vis = elem.style;  // if the style.display value is blank we try to figure it out here
    if (vis.visibility == 'visible' && elem.offsetWidth != undefined && elem.offsetHeight != undefined)
        vis.visibility = (elem.offsetWidth != 0 && elem.offsetHeight != 0) ? 'visible' : 'hidden';
    vis.visibility = (vis.visibility == 'visible') ? 'hidden' : 'visible';

    clientID = clientID == '' ? whichLayer.substring(12) : clientID;

    if (!clientID)
        clientID = LastClientID;

    if (vis != null)
        vis.top = f_scrollTop() + 200 + 'px';

    LastClientID = clientID;

    CurrentVideoButton = 'PlayerButton' + clientID;

    if (AC_FL_RunContent == 0)
    {
        alert("This page requires AC_RunActiveContent.js.");
    }
    else
    {
        elem.innerHTML = AC_FL_RunContent(
                'codebase', 'http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,0,0',
                'width', '336',
                'height', '297',
                'src', homeurl + '/scrubber' + '?file=' + url + '&bufferTime=3&autoStart=true',
                'quality', 'high',
                'pluginspage', 'http://www.macromedia.com/go/getflashplayer',
                'align', 'middle',
                'play', 'true',
                'loop', 'true',
                'scale', 'showall',
                'wmode', 'window',
                'devicefont', 'false',
                'id', 'scrubber',
                'bgcolor', '#ffffff',
                'name', 'scrubber',
                'menu', 'true',
                'allowFullScreen', 'false',
                'allowScriptAccess', 'always',
                'movie', homeurl + '/scrubber' + '?file=' + url + '&bufferTime=3&autoStart=true',
                'salign', ''
                ); //end AC code
    }

    if (vis.visibility == 'visible')
    {
        IsVideoPlaying = 1;

        document[CurrentVideoButton].src = ImageURL + '/Stop.gif';

        if (lastVideoPlay != '' && lastVideoPlay != whichLayer)
        {
            var elemLast, visLast;
            if (document.getElementById) // this is the way the standards work
                elemLast = document.getElementById(lastVideoPlay);
            else if (document.all) // this is the way old msie versions work
                elemLast = document.all[lastVideoPlay];
            else if (document.layers) // this is the way nn4 works
                elemLast = document.layers[lastVideoPlay];
            visLast = elemLast.style;
            if (visLast.visibility == 'visible')
                visLast.visibility = 'hidden';
        }

        lastVideoPlay = whichLayer;

        if (LastPlay != -1)
        {
            thePlayer = document.getElementById("Player");
            document[CurrentButton].src = ImageURL + '/Play.gif';
            thePlayer.controls.stop();
            LastPlay = -1;
            IsPlaying = 0;
            if (timeoutID != null) clearTimeout(timeoutID);
            timeoutID = null;
        }

        if (MP3player && IsMP3Playing == 1)
        {
            stopMP3(mp3PlayImg);
        }
    }
    else
    {
        if (document[CurrentVideoButton])
            document[CurrentVideoButton].src = ImageURL + '/Play.gif';

        elem.innerHTML = '';

        IsVideoPlaying = 0;
    }
}

function stopMP3(playimgurl)
{
    MP3playimg.src = playimgurl;
    MP3playimg.style.marginLeft = '0.0em';
    MP3playimg = null;
    MP3player.removeChild(MP3player.firstChild);
    var divPlayer = document.getElementById("divPlayer");
    divPlayer.innerHTML = "";
    MP3player = null;
}

function playMP3(imgid, playimgurl, stopimgurl, homeurl, url)
{
    var img = document.getElementById(imgid);
    mp3PlayImg = playimgurl;

    if (MP3playimg == img)
    {
        IsMP3Playing = 0;
        stopMP3(playimgurl);
    }
    else
    {
        if (MP3playimg)
            stopMP3(playimgurl);

        IsMP3Playing = 1;

        var a = img.nextSibling;

        img.src = stopimgurl;
        //img.style.marginLeft = '0.1em';
        MP3playimg = img;
        MP3player = document.createElement('div');
        MP3player.innerHTML = AC_FL_RunContent(
                'codebase', 'http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,0,0',
                'width', '0',
                'height', '0',
                'src', homeurl + '/playMp3' + '?theLink=' + url + '&bufferTime=3&autoStart=true',
                'quality', 'high',
                'pluginspage', 'http://www.macromedia.com/go/getflashplayer',
                'align', 'middle',
                'play', 'true',
                'loop', 'true',
                'scale', 'showall',
                'wmode', 'window',
                'devicefont', 'false',
                'id', 'mp3player',
                'bgcolor', '#ffffff',
                'name', 'scrubber',
                'menu', 'true',
                'allowFullScreen', 'false',
                'allowScriptAccess', 'always',
                'movie', homeurl + '/playMp3' + '?theLink=' + url + '&bufferTime=3&autoStart=true',
                'salign', ''
                ); //end AC code
        // this dive can be find on page.aspx near the end of the page.

        if (!isExplorer())
        {
            var divPlayer = document.getElementById("divPlayer");
            divPlayer.innerHTML = MP3player.innerHTML;
        }



		//alert(divPlayer.innerHTML);

        // commented this code to remove the shaking/moving of action button by 2px to the right.
        //img.parentNode.insertBefore(MP3player, img.nextSibling);
        //alert(player.innerHTML);

        // Stop Video Player
        if (lastVideoPlay != '' && IsVideoPlaying == 1)
        {
            toggleLayer(lastVideoPlay, '', '', '');
            lastVideoPlay = '';
        }

	    // Stop WMA Player
        if (LastPlay != -1)
        {
            thePlayer = document.getElementById("Player");
            document[CurrentButton].src = ImageURL + '/Play.gif';
            thePlayer.controls.stop();
            LastPlay = -1;
            IsPlaying = 0;
            if (timeoutID != null) clearTimeout(timeoutID);
            timeoutID = null;
        }
    }
}

function isExplorer()
{
    return /msie/i.test(navigator.userAgent) && !/opera/i.test(navigator.userAgent);
}
