﻿// Setup.
var vi_windowsMP = new Object();
var vi_xmlPlayList;
var vi_xmlPlayItem;
var vi_playerContainerDiv;

// used to set the volume of the player and the position of the volume slider at load.
var vi_initialVolume = 50;

var fireFoxPlayBackState = 0;

// Setting up an event handler for the FLV player.

// The function vi_playerReady is called from swfobjects.swf.
function playerReady(thePlayer)
{
	var player = document.getElementById(thePlayer.id);
	// Flash Player Event Handler.
	if (player)
	{ 
		player.addModelListener("STATE", "vi_genericFlashPlayerStateListener");
	}
	else
	{
		setTimeout("vi_genericFlashPlayerStateListener()",100);
	}
} // EOF

function vi_genericFlashPlayerStateListener(obj) 
{ 
	var currentState = obj.newstate;
	var previousState = obj.oldstate; 

	if ((currentState == "COMPLETED")&&(previousState == "PLAYING")) 
	{	
		vi_destroyPlayers();
	    vi_xmlPlayItem = vi_xmlPlayItem.nextSibling;
	    if (vi_xmlPlayItem != null)
	    {
	        vi_playItem(vi_xmlPlayItem);
        }
        else
        {
            var newElement = document.createElement('div');
            var oldElement = document.getElementById('vi_playerContainer');
            
            newElement.id = "vi_playerContainer";
            vi_playerContainerDiv = "background-image:url(" + vi_contentListImage + ");";
            newElement.setAttribute('style',vi_playerContainerDiv);
            
            document.getElementById("vi_outerPlayerContainer").replaceChild(newElement, oldElement);
            
                  
        }
	}
	    
} // EOF

// Event handler to test if Windows Media Player is in the MediaEnded State.
function vi_genericWindowsMediaPlayerListeners()
{   

    if (vi_windowsMP.playstate == 8)
    {
        
        vi_destroyPlayers();
	    vi_xmlPlayItem = vi_xmlPlayItem.nextSibling;
	    if (vi_xmlPlayItem != null)
	    {
	        vi_playItem(vi_xmlPlayItem);
	    }
	    else
        {
            var newElement = document.createElement('div');
            var oldElement = document.getElementById('vi_playerContainer');
            
            newElement.id = "vi_playerContainer";
            var vi_playerContainerDiv = "background-image:url(" + vi_contentListImage + ");";
            newElement.setAttribute('style',vi_playerContainerDiv);
            
            document.getElementById("vi_outerPlayerContainer").replaceChild(newElement, oldElement);         
        }
    }
    
    // For IE.
    // Set up timer. This is used to poll the vi_moveScrubControl function which moves the scrub control.  This will also be used to display a timer.
    
    if (vi_windowsMP.playstate == 3)
    {
        idTmr = window.setInterval("vi_setSliderPosition()",100);
        vi_enableControls();  
    }
    
} // EOF

function vi_jukeboxPlay(theXML)
{

    // Load XML data.
    
    // code for IE
    if (window.ActiveXObject)
    {
        vi_xmlPlayList=new ActiveXObject("Microsoft.XMLDOM");
        vi_xmlPlayList.async=false;
        vi_xmlPlayList.loadXML(theXML);
    }
    // code for Mozilla, Firefox, Opera, etc.
    else if (document.implementation.createDocument)
    {
        //vi_xmlPlayList=document.implementation.createDocument("","",null);
        parser=new DOMParser();
        vi_xmlPlayList=parser.parseFromString(theXML,"text/xml");
    }
    else
    {
        alert('Your browser cannot handle this script');
    }
    
    var listItems = vi_xmlPlayList.documentElement.childNodes;
    vi_xmlPlayItem = listItems[0];
    vi_playItem(vi_xmlPlayItem);
}


function vi_playItem(vi_xmlPlayItem)
{

    var wmp = document.getElementById('vi_VIDEO');
    var flash = document.getElementById('FlashPlayer');

    var url = vi_xmlPlayItem.getElementsByTagName("URL")[0].childNodes[0].data;
    var format = vi_xmlPlayItem.getElementsByTagName("Player")[0].childNodes[0].data;
    //var canSkip = vi_xmlPlayItem.getElementsByTagName("CanSkip")[0].childNodes[0].data;
    
   vi_disableControls();
//    
//    if (document.getElementById('pausePlay').value == 'play')
//    {
//        vi_continue();
//    }
//        
//    //vi_play(url, format);
//    


    
    if (format == "GenericFlash")
    {
        vi_destroyPlayers();
        vi_createGenericFlashPlayer(vi_xmlPlayItem);
    }
    else if (format == "GenericWindowsMedia")
    {
        vi_destroyPlayers();
        vi_createGenericWindowsMediaPlayer(url);
    }
    else if (format == "GenericImage")
    {
        vi_destroyPlayers();       
        vi_createGenericImagePlayer(url);
    }
    else if (format == "Utarget")
    {
        vi_destroyPlayers();       
        vi_createUtargetPlayer(url);
    }
    else if (format == "Advertising_com")
    {
        vi_destroyPlayers();       
        vi_createAdvertising_comPlayer(url);
    }
    else if (format == "LightningCast")
    {
        vi_destroyPlayers();       
        vi_createLightningCastPlayer(url);
    }
    else if (format == "AOL")
    {
        vi_destroyPlayers();       
        vi_createAOLPlayer(url);
    }
    else if (format == "ADTECH")
    {
        vi_destroyPlayers();       
        vi_createADTECHPlayer(url);
    }
    
    
//    if (wmp != null)
//    {
//        vi_enableControls();
//    }
    
} // EOF

// Create a Utarget player.
function vi_createUtargetPlayer(url)
{
    // ToDo.
}

// Create a Advertising_com player.
function vi_createAdvertising_com(url)
{
    // ToDo.
}

// Create a LightningCast player.
function vi_createLightningCast(url)
{
    // ToDo.
}

// Create a AOL player.
function vi_createAOL(url)
{
    // ToDo.
}

// Create a ADTECH player.
function vi_createADTECH(url)
{
    // ToDo.
}

// Create Windows Media Player as fallback if the Silverlight Plugin isn't available.
     function vi_wmpCreate(url, width, height) {
         //var d = document.getElementById("WMEPlay");
         
            var d = "";
            var is_ie = navigator.appName == 'Microsoft Internet Explorer';
            var is_op = navigator.appName == 'Opera' ? true : false;
            var is_ns = !is_ie && !is_op ? true : false;
            var str = "";
            if (is_ie) {
                // create the WMP for IE onmouseover="JavaScript: alert( 'onmouseover event' )"
                str = '<OBJECT  id="vi_VIDEO" type="application/x-oleobject" CLASSID="CLSID:6BF52A52-394A-11d3-B153-00C04F79FAA6" width="' + width + '" height="' + height + '">';
            } else {
                // create it for FF.
            str = '<object id="vi_VIDEO" type="application/x-ms-wmp" data="' + url + '" width="' + width + '" height="' + height + '" uiMode="none" stretchToFit="true" enableContextMenu="false">'; //  had to add uiMode to the object tag as otherwise Firefox ignores it.  Also, 'stretchToFit' and 'enableContextMenu'.
            }
            str += '<PARAM NAME="URL" value="' + url + '" />';
            str += '<PARAM NAME="SRC" value="' + url + '" />';
            str += '<PARAM NAME="uiMode" value="none">';
            str += '<PARAM NAME="repeat" VALUE="false">';
            str += '<PARAM NAME="mute" VALUE="False">';
            str += '<PARAM NAME="AutoRewind" VALUE="false">';
            str += '<PARAM NAME="ClickToPlay" VALUE="false">';
            str += '<PARAM NAME="AnimationAtStart" VALUE="false">';
            str += '<PARAM NAME="TransparentAtStart" VALUE="false">';
            str += '<PARAM NAME="SendPlayStateChangeEvents" VALUE="True">';
            str += '<PARAM NAME="AutoStart" VALUE="True">';
            str += '<PARAM NAME="ShowControls" VALUE="false">'; //  for Geko browsers
            str += '<PARAM NAME="ShowStatusBar" VALUE="false">';
            str += '<PARAM NAME="stretchToFit" VALUE="true">'; //  ToDo - check if this is actually wanted.
            str += '<PARAM NAME="enableContextMenu" VALUE="false">';
            str += '<PARAM NAME="volume" VALUE="60">';
            str += '<PARAM NAME="BufferingTime" VALUE="2">';
            str += '<PARAM NAME="PlayCount" VALUE="1">';
            str += '</OBJECT>';
            d = str;
            
            document.getElementById('vi_playerContainer').innerHTML = "";
            
            var oldElement = document.getElementById('vi_playerContainer');
            var newElement = document.createElement('div');
            newElement.id = 'vi_playerContainer';
             
            document.getElementById('vi_outerPlayerContainer').replaceChild(newElement, oldElement);
            document.getElementById('vi_playerContainer').innerHTML = d;
            
            vi_windowsMP = document.getElementById('vi_VIDEO');
                        
                if (window.ActiveXObject)
                {
                    vi_windowsMP.attachEvent("playStateChange",vi_genericWindowsMediaPlayerListeners);
                    vi_windowsMP.settings.volume = slider2.getValue();
                }
                
               // Hide actual scrub control and show static scrub control
                document.getElementById('staticScrubHolder').style.display = "none"; 
                document.getElementById('slider1').style.display = "block";
                document.getElementById('staticVolume').style.display = "none"; 
                document.getElementById('slider2').style.display = "block";
             
} // EOF
            
// Create a generic Image player.
function vi_createGenericImagePlayer(url)
{
    newImgDiv = document.createElement('img');
    var oldElement = document.getElementById("vi_playerContainer");
    
    oldElement.innerHTML = "";
    
    document.getElementById("vi_outerPlayerContainer").replaceChild(newImgDiv, oldElement);
    newImgDiv.id = "vi_playerContainer";
    newImgDiv.src = url;
}

// Create a generic Windows Media player.
function vi_createGenericWindowsMediaPlayer(url)
{
    
    var newWinPlayer = document.createElement('div');
    newWinPlayer.id = "vi_playerContainer";

// call silverlight checker
// re-write newWinPlayer.innerHTML depending on outcome.


    if (1 == 2) //  Temp so that silverlight doesn't load even if installed.
    //if (isSilverlightInstalled())
    {
        // Silverlight is installed.
        
        newWinPlayer.innerHTML = '<object id="vi_VIDEO" data="data:application/x-silverlight-2," type="application/x-silverlight-2" width="400" height="300"><param name="source" value="vi_genericWindowsMedia_1.xap"/><param name="background" value="black" /><param name="initParams" value="w=400,h=300,m=0,v=50,r=0,thestream=' + url + '" /><param name="minRuntimeVersion" value="2.0.31005.0" /><param name="autoUpgrade" value="true" /></object>';
        
        // Inject Silverlight Player.
        
        var oldElement = document.getElementById("vi_playerContainer");
        oldElement.innerHTML = "";
        document.getElementById("vi_outerPlayerContainer").replaceChild(newWinPlayer, oldElement);   
    }
    else
    {
        // Silverlight is not installed.
        // Create a windows media player object. 
      
        vi_wmpCreate("" + url + "", "400px", "300px");
    }
    
    // Enable controls for Windows Media Player.
    //vi_enableControls();

    
//newWinPlayer.innerHTML = '<object id="vi_VIDEO" data="data:application/x-silverlight-2" type="application/x-silverlight-2x" width="400" height="300"><param name="source" value="vi_genericWindowsMedia_1.xap"/><param name="background" value="black" /><param name="initParams" value="w=400,h=300,m=0,v=50,r=0,thestream=' + url + '" /><param name="minRuntimeVersion" value="2.0.31005.0" /><param name="autoUpgrade" value="true" /><div id="WMEPlay">&nbsp;</div><script type="text/javascript" defer="defer">vi_wmpCreate("' + url + '", "400px", "300px");' + '<' + '/script></object>';
             
} // EOF

// Create a generic Flash player.
function vi_createGenericFlashPlayer(vi_xmlPlayItem)
{
    var newFlashPlayer = document.createElement('div');
    newFlashPlayer.id = "vi_playerContainer";
    var playItemXML;
    // We pass the XML for the current PlayItem to the genericFlashPlayer.
    // IE and FF deal with this differently.  IE has a property call 'xml' which holds the xml from the current IXMLDOMElement.
    // FF doesn not have this (it is none-standard), so an XMLSerializer is used instead.
    // Either way, a variable called vi_playItemXML is created and populated with the XML required.
    if(-1 != navigator.userAgent.indexOf("MSIE"))
    {
        playItemXML = vi_xmlPlayItem.xml;
    }
    else
    {
        var serializer = new XMLSerializer();
        playItemXML = serializer.serializeToString(vi_xmlPlayItem);
    }
    
    
// this one.
    
// newFlashPlayer.innerHTML ='<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" width="400" height="300" id="FlashPlayer" name="FlashPlayer"><param name="movie" value="gmix-external-ctrl.swf" /><param name="quality" value="high" /><param name="allowfullscreen" value="true" /><param name="allowscriptaccess" value="always" /><param name="allownetworking" value="all" /><!--[if !IE]>--><object type="application/x-shockwave-flash" data="gmix-external-ctrl.swf" width="400" height="300"><param name="quality" value="high" /><param name="allowfullscreen" value="true" /><param name="allowscriptaccess" value="always" /><param name="allownetworking" value="all" /><!--<![endif]--><a href="http://www.adobe.com/go/getflashplayer"><img src="http://www.adobe.com/images/shared/download_buttons/get_flash_player.gif" alt="Get Adobe Flash player" /></a><!--[if !IE]>--></object><!--<![endif]--></object>'


 newFlashPlayer.innerHTML ='<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" width="400" height="300" id="FlashPlayer" name="FlashPlayer"><param name="movie" value="gmix-external-ctrl-xml.swf" /><param name="quality" value="high" /><param name="flashvars" value="thexml=' + playItemXML + '" /><param name="allowfullscreen" value="true" /><param name="allowscriptaccess" value="always" /><param name="allownetworking" value="all" /><!--[if !IE]>--><object type="application/x-shockwave-flash" data="gmix-external-ctrl-xml.swf" width="400" height="300"><param name="quality" value="high" /><param name="flashvars" value="thexml=' + playItemXML + '" /><param name="allowfullscreen" value="true" /><param name="allowscriptaccess" value="always" /><param name="allownetworking" value="all" /><!--<![endif]--><a href="http://www.adobe.com/go/getflashplayer"><img src="http://www.adobe.com/images/shared/download_buttons/get_flash_player.gif" alt="Get Adobe Flash player" /></a><!--[if !IE]>--></object><!--<![endif]--></object>'





 
    
    
    
    
    
//    newFlashPlayer.innerHTML = '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" width="400" height="300" id="FlashPlayer" name="FlashPlayer"><param name="movie" value="gmix-external-ctrl.swf" AllowScriptAccess="allows" /><param name="flashvars" value="thexml=' + playItemXML + '" /><!--[if !IE]>--><object type="application/x-shockwave-flash" data="gmix-external-ctrl.swf" width="400" height="300" id="FlashPlayer" AllowScriptAccess="allows"><param name="flashvars" value="thexml=' + playItemXML + '" /><!--<![endif]--><a href="http://www.adobe.com/go/getflashplayer"><img src="http://www.adobe.com/images/shared/download_buttons/get_flash_player.gif" alt="Get Adobe Flash player" /></a><!--[if !IE]>--></object><!--<![endif]--></object>'
    

//	newFlashPlayer.innerHTML = '<object id="FlashPlayer" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=4,0,2,0" width="400" height="300"><param name="allowFullScreen" value="true" /><param name="movie" value="gmix-external-ctrl.swf" /><param name="quality" value="high" /><param name="flashvars" value="thexml=' + playItemXML + '" /><embed src="gmix-external-ctrl.swf" quality="high" pluginspage="http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash" type="application/x-shockwave-flash" width="400" height="300" flashvars="thexml=' + playItemXML + '"</embed></object>';

    var oldElement = document.getElementById("vi_playerContainer");
    oldElement.innerHTML="";
    document.getElementById("vi_outerPlayerContainer").replaceChild(newFlashPlayer, oldElement);
    
 //   swfobject.registerObject("FlashPlayer", "9.0.0", "expressInstall.swf");
   idTmr = window.setInterval("vi_setSliderPosition()",100);
   
   
   // Hide actual scrub control and show static scrub control
   document.getElementById('staticScrubHolder').style.display = "block"; 
   document.getElementById('slider1').style.display = "none";
   document.getElementById('staticVolume').style.display = "block"; 
   document.getElementById('slider2').style.display = "none";
	
} // EOF



// destroy the current player ready for the new player to be created and displayed.
function vi_destroyPlayers()
{
    // Destroy Windows Media Player.
    if (vi_windowsMP.id == "vi_VIDEO") 
    {
        if (window.ActiveXObject)
        {
            vi_windowsMP.close();
            vi_windowsMP = null;
        }
        else
        {
            vi_windowsMP = null;
        }

        vi_windowsMP = new Object();
    }

    // Destroy player container.
    //var player = document.getElementById("vi_player1");
    var player = document.getElementById("vi_playerContainer");
    if (player != null)
    {
        player.innerHTML="";
        var newElement = document.createElement('div');
        newElement.id = "vi_playerContainer";       
        document.getElementById("vi_outerPlayerContainer").replaceChild(newElement, player);
        player = null;        
   }
   
    // Set Scrub to 0

    slider1.setValue(0);
    // Set time readout to 0 

    var vi_playBackTime = document.getElementById('time');
    vi_playBackTime.value = "00:00"
    
    // Set movie length readout to 0
    var vi_movieLength = document.getElementById('vi_movieLengthDisplay');
    vi_movieLength.value = "00:00"
   
} // EOF.


     // Gecko Silverlight Event Handler.
    function OnDSPlayStateChangeEvt(NewState) {
        
        if (NewState == 8)// Finished.
        {        
            StreamPlayerEvents("Video", "Has Ended", NewState);
            vi_disableControls();
        }
        else if (NewState == 3) // Playing.
        {
            vi_enableControls();
            //document.body.style.cursor="default";
        }
        
        // ToDo - enable this code. currently a little patchy.
        
//        else if (NewState == 9) // buffering
//        {
//            
//            // ToDo - alert is working, animation isn't being shown.
//        
//            //alert('waiting');
//            document.getElementById('animatedGif').style.visibility = 'visible';
//            
//            document.body.style.cursor="wait";
//            
//        }
            
            // For Gecko.
            // Set up timer. This is used to poll the vi_moveScrubControl function which moves the scrub control and can update a movie timer.   
            idTmr = window.setInterval("vi_setSliderPosition()",100);
            fireFoxPlayBackState = 3;

        
    }


    // Gecko events
     function StreamPlayerEvents(TheEVName, TheEVData, NewState) {
            
            if (TheEVData == "Ended" || NewState == 8)
            {
                           
                // This has to be changed slightly from the 'normal' event code for Media player and the Flash Player.
                vi_destroyPlayers();
                
                if (vi_xmlPlayItem.nextSibling == null)
                {
                    //vi_xmlPlayItem = null;
                    var newElement = document.createElement('div');
                    var oldElement = document.getElementById('vi_playerContainer');

                    newElement.id = "vi_playerContainer";
                    vi_playerContainerDiv = "background-image:url(" + vi_contentListImage + ");";
                    newElement.setAttribute('style',vi_playerContainerDiv);

                    newElement.innerHtml = "";
                    
                    var tmp = document.getElementById('vi_VIDEO');
                    oldElement.innerHTML="";
                    document.getElementById("vi_outerPlayerContainer").replaceChild(newElement, oldElement);
                }
                else
                {
                    vi_xmlPlayItem = vi_xmlPlayItem.nextSibling;
                    vi_playItem(vi_xmlPlayItem);
                }
            }
            
     }
         
    // called by the global mix flash player on movie end.
    function play_done()
    {
        
                //alert('hi, i am play done');
                vi_destroyPlayers();
	            vi_xmlPlayItem = vi_xmlPlayItem.nextSibling;
	            if (vi_xmlPlayItem != null)
	            {
	                vi_playItem(vi_xmlPlayItem);
                }
                else
                {
                    var newElement = document.createElement('div');
                    var oldElement = document.getElementById('vi_playerContainer');
                    
                    newElement.id = "vi_playerContainer";
                    vi_playerContainerDiv = "background-image:url(" + vi_contentListImage + ");";
                    newElement.setAttribute('style',vi_playerContainerDiv);
                    
                    document.getElementById("vi_outerPlayerContainer").replaceChild(newElement, oldElement);         
                }
        
    }


/*
Call destroyPlayers on page refresh to clear Windows Media Player
If this is not done then a frozen image of the last frame of the current movie being played by WMP gets left on the screen.
*/   
onbeforeunload = function()
{
    vi_destroyPlayers();
}   


   
// DO NOT DELETE YET - BUT MAY NOT NEED THIS.
// there is a function called 'OnDSPlayStateChangeEvt' which is handling both Media Player and Silverlight in Firefox.
// Have tested in IE7 and FF but will leave this for now.

//function OnDSStatusChangeEvt(newStatus) {
//         
//        if(vi_windowsMP.status == "Finished")
//        
//        {

//		        vi_destroyPlayers();
//	            vi_xmlPlayItem = vi_xmlPlayItem.nextSibling;
//	            if (vi_xmlPlayItem != null)
//	            {
//	                vi_playItem(vi_xmlPlayItem);
//                }
//                else
//                {
//                    var newElement = document.createElement('div');
//                    var oldElement = document.getElementById('vi_playerContainer');
//                    
//                    newElement.id = "vi_playerContainer";
//                    vi_playerContainerDiv = "background-image:url(" + vi_contentListImage + ");";
//                    newElement.setAttribute('style',vi_playerContainerDiv);
//                    
//                    document.getElementById("vi_outerPlayerContainer").replaceChild(newElement, oldElement);         
//                }
//        }
//           
//    } // EOF.


  
    
    
    

