//*****************************
// iDream Sample Player
// www.alltern8.com
//*****************************
var intSampleID
intSampleID = 0;
var strIDVal
strIDVal = "";

//*****************************
//Initialise the function
//*****************************
function iDreamPlayer()
{   
    iDreamPlayer.prototype.Initialise();
}

//*****************************
//Setup the player and append to the body
//*****************************
iDreamPlayer.prototype.Initialise = function ()
{
    var iDreamSamplePlayerObject = "";
    if (jQuery.browser.msie) {
        iDreamSamplePlayerObject = ('<OBJECT id="iDreamSamplePlayer" name="iDreamSamplePlayer" classid="clsid:6BF52A52-394A-11d3-B153-00C04F79FAA6" width="0" height="0"><param name="uiMode" src="" value="invisible"/></OBJECT>');
    } else if (jQuery.browser.mozilla) {
        iDreamSamplePlayerObject = ('<OBJECT id="iDreamSamplePlayer" name="iDreamSamplePlayer" type="application/x-ms-wmp" width="0" height="0"><param name="uiMode" src="" value="invisible"/><param name="SendPlayStateChangeEvents" value="True"/></OBJECT>');
    } else {
        iDreamSamplePlayerObject = ('<OBJECT id="iDreamSamplePlayer" name="iDreamSamplePlayer" type="application/x-ms-wmp" width="0" height="0"><param name="uiMode" src="" value="invisible"/><param name="SendPlayStateChangeEvents" value="True"/></OBJECT>');
    }
    $(document).ready(function(){$("BODY").prepend(iDreamSamplePlayerObject)});
}

//*****************************
//Get the player instance
//*****************************
iDreamPlayer.prototype.GetiDreamSamplePlayerInstance = function ()
{
    return document.getElementById('iDreamSamplePlayer');    
}

iDreamPlayer.prototype.IsFirefoxMPInstalled = function ()
{
    var isPluginInstalled = false;;

    for (var i in navigator.plugins) 
    {
        var plugin = navigator.plugins[i].name;

        if (plugin != null && plugin.indexOf("Windows Media Player Firefox Plugin") >= 0 && plugin.indexOf("Microsoft") >= 0) 
        {
            isPluginInstalled = true;
            break;
        }
    }
    
    return isPluginInstalled;
}


//*****************************
//Play the sample
//*****************************
iDreamPlayer.prototype.PlaySample = function(IDVal,trackID)
{
    var canPlay;
    canPlay = true;
    if (jQuery.browser.mozilla==true)
    {
        if (!iDreamPlayer.prototype.IsFirefoxMPInstalled()) 
        {
            canPlay = false;
            alert ("Unfortunately your browser doesnt support our sample buttons\n\nTo play samples within simply install the Windows Media Player Firefox Plugin from \n http://port25.technet.com/pages/windows-media-player-firefox-plugin-download.aspx");
        }
    }

    if (strIDVal != "" )
    {
        $('#'+strIDVal).html('<div class="playerControl play" onclick="iDreamSampPlayer.PlaySample(\''+ strIDVal + '\',\'' + intSampleID + '\');return false;" title=\'Play Sample\'></div>')
        strIDVal = "";
    }
    var idsp = iDreamPlayer.prototype.GetiDreamSamplePlayerInstance();
    
    if (canPlay) {
        //set the volume to full
        idsp.URL = "http://www.alltern8.com/idream/getsample.asp?D=" + trackID;
        intSampleID = trackID;
        strIDVal = IDVal;
        $('#'+strIDVal).html('<div class="playerControl load" onclick="iDreamSampPlayer.StopSample();return false;" title=\'Loading Sample\'></div>');
        if (jQuery.browser.msie==true)
        {
            idsp.settings.volume = 100;
        }
    }        
 }
 

//*****************************
//Stop the sample
//*****************************
iDreamPlayer.prototype.StopSample = function()
{
    $('#'+strIDVal).html('<div class="playerControl play" onclick="iDreamSampPlayer.PlaySample(\''+ strIDVal + '\',\'' + intSampleID + '\');return false;" title=\'Play Sample\'></div>')
    strIDVal = "";
    var idsp = iDreamPlayer.prototype.GetiDreamSamplePlayerInstance();
    //wipe the URL
    idsp.URL = "http://www.alltern8.com/idream/stopsample.mp3";
}

//*****************************
//Display the play button
//*****************************
iDreamPlayer.prototype.PlayButton= function()
{
    $('#'+strIDVal).html('<div class="playerControl stop" onclick="iDreamSampPlayer.StopSample();return false;" title=\'Stop Sample\'></div>')
}

//*****************************
//Display the load button
//*****************************
iDreamPlayer.prototype.LoadButton = function()
{
    $('#'+strIDVal).html('<div class="playerControl load" onclick="iDreamSampPlayer.StopSample();return false;" title=\'Stop Sample\'></div>')
}

// 
iDreamPlayer.prototype.IDSPEventHandler = function(eventMPState)
{
    var state;
    //alert(eventMPState);
    switch(eventMPState)
    {
        case 0:
         state = "Undefined";
         break;
        case 1:
         state = "Stopped";
         iDreamPlayer.prototype.StopSample();
         break;
        case 2:
         state = "Paused";
         break;
        case 3:
         state = "Playing";
         iDreamPlayer.prototype.PlayButton();
         break;
        case 4:
         state = "ScanForward";
         break;
        case 5:
         state = "ScanReverse";
         break;
        case 6:
         state = "Buffering";
         iDreamPlayer.prototype.LoadButton();
         break;
        case 7:
         state = "Waiting";
         break;
        case 8:
         state = "MediaEnded";
         iDreamPlayer.prototype.StopSample();
         break;
        case 9:
         state = "Transitioning";
         break;
        case 10:
         state = "Ready";
         //iDreamPlayer.prototype.StopSample();
         break;
        case 11:
         state = "Reconnecting";
         break;
    }    
    
}

