function writeWMPlayer(theURL)
{
    var width = 376;
    var height = 210;
    var adjustedHeight = height + 65;
    var divid = "divMediaPlayer";
    var autoplay = true;
    
    var playerString = "<object id=\"MediaPlayer\" name=\"MediaPlayer\" width=\"" + width + "\" height=\"" + adjustedHeight + "\" classid=\"CLSID:6BF52A52-394A-11D3-B153-00C04F79FAA6\" standby=\"Loading Microsoft® Windows® Media Player components...\" type=\"application/x-oleobject\">"
    + " <param name=\"url\" value=\"" + theURL + "\" id=\"Param1\" />";
    if( autoplay ) {
        playerString += "<param name=\"autostart\" value=\"true\" />   ";
    } else {
        playerString += "<param name=\"autostart\" value=\"false\" />   ";
    }
    playerString += " <param name=\"ShowControls\" value=\"1\" />"
    + " <param name=\"ShowPositionControls\" value=\"0\" />"
    + " <param name=\"ShowDisplay\" value=\"0\" />"
    + " <param name=\"ShowStatusBar\" value=\"1\" />"
    + " <param name=\"EnableContextMenu\" value=\"1\" />"
    + " <param name=\"SendPlayStateChangeEvents\" value=\"1\" />"
    + " <param name=\"DefaultFrame\" value=\"slides\" />"
    + " <param name=\"DisplayBackColor\" value=\"white\" />"
    + " <param name=\"AutoSize\" value=\"true\" /> "
    + " <embed type=\"application/x-mplayer2\" "
    + " id=\"MediaPlayerEmbed\" name=\"MediaPlayerEmbed\" "
    + "   pluginspage=\"http://www.microsoft.com/Windows/MediaPlayer/\" "
    + "   src=\"" + theURL + "\" "
    + "   url=\"" + theURL + "\" "
    + "   name=\"MediaPlayer\" "
    + "   width=\"" + width + "\" "
    + "   height=\"" + adjustedHeight + "\" ";
    
    if( autoplay ) {
        playerString += "   autostart=\"1\" ";
    } else {
        playerString += "   autostart=\"0\" ";
    }
    
    playerString += "   showcontrols=\"1\" "
    + "   showpositioncontrols=\"0\" "
    + "   showdisplay=\"0\" "
    + "   showstatusbar=\"1\" "
    + "   sendplaystatechangeevents=\"1\" "
    + "   defaultframe=\"slides\" "
    + "   displaybackcolor=\"white\"> "
    + "   </embed>"
    + "</object>";  

    document.getElementById( divid ).innerHTML = playerString;
}

var isIE = (navigator.appName == "Microsoft Internet Explorer") ? true : false;

var videoTitles = new Array
(
    "The Sarnelli House story",
    "The volunteers' journey", 
    "Coming home ...and how you can help",
    "Behind the scenes"
);
var videoSummaries = new Array
(

    "Peter Jones, BUPA's Director of Corporate Communications meets some of the children of Sarnelli and hears their stories from Father Mike Shea, founder of the orphanage. ",
    "The first group of BUPA volunteers are welcomed by the children of  Sarnelli House and give us their first impressions.", 
    "Some of the volunteers talk about how the Challenge has affected them and their determination to continue to help the children of Sarnelli.", 
    "A short sequence of footage shot by the BUPA team captures some of the highlights of the Challenge and shows some of the improvement works carried out by the volunteers"
);

function videoFirstLoad()
{
    if (isIE)
    {
        // IE, load full playlist
        writeWMPlayer("media/playlist.asx");
    }
    else
    {
        // Non-IE, load individual video
        changeVideo(0);
    }
}

function mediaPlayer_CurrentItemChange()
{
    if (!isIE) return;
      
    var player = document.getElementById("MediaPlayer");
    var newVideo = player.currentMedia;
    // Get new video index
    var videoIndex;
    


    for (n = 0; n < player.currentPlaylist.count; n++)
    {
    
 
        if (player.currentPlaylist.item(n).sourceURL == newVideo.sourceURL)
        {
           // alert(player.playState)

    
 
            videoIndex = n;
             
            break;
        }
    
}

    // Set summary area
   
    displaySummary(videoIndex);
     
     //if the video is the last vid on the list  - stop playing
     
      if (player.playState == 3 &&videoIndex == player.currentPlaylist.count-1 ){
       player.controls.Stop();
   
       }
  
}

function changeVideo(videoIndex)
{
    if (isIE)
    {
        // IE, move to next video in playlist --->
     
        var player = document.getElementById("MediaPlayer");
        var newVideo = player.currentPlaylist.item(videoIndex);

        // Load new video (raises CurrentItemChange() event)
        player.controls.playItem(newVideo);
        

    
    }
    else
    {
        // Not IE, reload media player with different video --->
        
        writeWMPlayer("media/clip_" + videoIndex + ".asx");
        displaySummary(videoIndex);

    }
    
}





function displaySummary(videoIndex)
{
    // Set title
    document.getElementById("lblSummary_Title").innerHTML = videoTitles[videoIndex] + ": ";
    
    
    // Set description
    document.getElementById("lblSummary_Text").innerHTML = videoSummaries[videoIndex];
   
}

function videoLink_MouseOver(linkId)
{
    var link = document.getElementById(linkId);
    link.src = link.src.replace(".gif", "_on.gif");
}

function videoLink_MouseOut(linkId)
{
    var link = document.getElementById(linkId);
    link.src = link.src.replace("_on", "");
}