/**
  author: daniel bulli
  wgbh interactive
  **/
  
  //lets do video
  var cookieval = false;
  var dovideo   = true;
  


//while video playing
var ctrl_changesetting = '<table id="change-controls" border="0" cellspacing="0" cellpadding="0"><tr><td align="right"><a href="#" onclick="write_ctrl_default(); return false;"><img src="../images/video/button_ch_settings_off.gif" class="padright" onmouseover="vidroll(this)" alt="Change Settings" width="103" height="15" border="0"></a></td></tr></table>';

//not playing but cookie is set
var ctrl_cookieset = '<table id="play-controls" border="0" cellspacing="0" cellpadding="0"><tr><td align="right"><a href="#" onclick="write_ctrl_default(); return false;"><img src="../images/video/button_ch_settings_off.gif" class="padme" onmouseover="vidroll(this)" alt="Change Settings" width="103" height="15" border="0" /></a>&nbsp;<a href="#" onclick="playvideo(\'cookieval\'); return false;"><img src="../images/video/button_play_video_off.gif" onmouseover="vidroll(this)"alt="Play video" width="104" height="22" border="0" /></a></td></tr></table>';
                

//open panel
var ctrl_all       = '<table id="vid-controls" border="0" cellspacing="0" cellpadding="0"><tr><td><img src="../images/video/wmp_off.gif" alt="Windows Media Player" width="153" height="21" border="0" /><a href="#" onclick="playvideo(\'wm_high\'); return false;"><img src="../images/video/button_high_off.gif" onmouseover="vidroll(this)" alt="high" width="81" height="21" border="0" /></a><img src="../images/video/vertical.gif" width="13" height="21" border="0" alt="" /><a href="#" onclick="playvideo(\'wm_low\'); return false;"><img src="../images/video/button_dial-up_off.gif" onmouseover="vidroll(this)" alt="dial-up" width="61" height="21" border="0" /></a></td></tr><tr><td><img src="../images/video/qt_off.gif" alt="QuickTime" width="153" height="21x" border="0" /><a href="#" onclick="playvideo(\'qt_high\'); return false;"><img src="../images/video/button_high_off.gif" onmouseover="vidroll(this)" alt="high" width="81" height="21" border="0" /></a><img src="../images/video/vertical.gif" width="13" height="21" border="0" alt="" /><a href="#" onclick="playvideo(\'qt_low\'); return false;"><img src="../images/video/button_dial-up_on.gif" onmouseover="vidroll(this)" alt="dial-up" width="61" height="21" border="0" /></a></td></tr></table>'; 

/**
 * initializes video
 **/                
function initVideo()
{
    if(dovideo)
    {
        //get cookie value
        cookieval = getCookie("vidpref");
        
        //write out appropiate things
        write_controls();
        write_curtain();
    }
}

  
  
/**
 * writes out curtain
 **/
function write_curtain()
{
    if (!document.getElementById && !document.getElementById("video-holder"))
    {
        alert("You need to a newer browser version to view content.");
        return;
    }
    var videoholder = document.getElementById("video-holder");

    
    if(!cookieval)
    {
    	if (vid_img == null || vid_img == '') {
    		vid_img = '../images/video/poster_dr_instruct.jpg';
    	}

        videoholder.innerHTML = '<img src="' + vid_img + '" title="Choose setting to play video" alt="Choose setting to play video" width="384" height="256" border="0" />';    
    }
    else
    {
    	if (vid_img == null || vid_img == '../images/video/poster_dr_instruct.jpg' || vid_img == '') {
    		vid_img = '../images/video/poster_dr.jpg';
    	}
        videoholder.innerHTML = '<img src="' + vid_set + '" title="Hit play below." alt="Hit play below." width="384" height="256" border="0" />';    
    }
}


/**
 * writes out controls
 **/
function write_controls()
{
    if(!cookieval)
    {
        /** if no cookie then show all controls **/
        write_ctrl_default();
    }
    else
    {
        write_ctrl_cookieset();
    }    
    
}



/** write play/changesettings **/
function write_ctrl_cookieset()
{
    var linkholder = document.getElementById("links-holder");
    linkholder.innerHTML = ctrl_cookieset;
}


/** write changesettings **/
function write_ctrl_changesettings()
{
    var linkholder = document.getElementById("links-holder");
    linkholder.innerHTML = ctrl_changesetting;
}

/** write all options **/
function write_ctrl_default()
{
    var linkholder = document.getElementById("links-holder");
    
    //get cookie value
    cookieval = getCookie("vidpref");
    
    var wm_stat = "off";
    var wm_lo_vers = "off";
    var wm_hi_vers = "off";
    var qt_stat = "off";
    var qt_lo_vers = "off";
    var qt_hi_vers = "off";  
    
    if(cookieval)
    {
        if(cookieval.match("wm"))
        { 
            wm_stat = "on";
            if(cookieval.match("low"))  { wm_lo_vers = "on"; }
            if(cookieval.match("high")) { wm_hi_vers = "on"; }
        }
        
        if(cookieval.match("qt"))
        { 
            qt_stat = "on";
            if(cookieval.match("low"))  { qt_lo_vers = "on"; }
            if(cookieval.match("high")) { qt_hi_vers = "on"; }
        }
        
    }


	var ctrl_default       = '<table id="vid-controls" border="0" cellspacing="0" cellpadding="0"><tr><td><img src="../images/video/wmp_' + wm_stat +'.gif" alt="Windows Media Player" width="153" height="21" border="0" /><a href="#" onclick="playvideo(\'wm_high\'); return false;"><img src="../images/video/button_high_' + wm_hi_vers +'.gif" onmouseover="vidroll(this)" alt="high" width="81" height="21" border="0" /></a><img src="../images/video/vertical.gif" width="13" height="21" border="0" alt="" /><a href="#" onclick="playvideo(\'wm_low\'); return false;"><img src="../images/video/button_dial-up_' + wm_lo_vers +'.gif" onmouseover="vidroll(this)" alt="dial-up" width="61" height="21" border="0" /></a></td></tr><tr><td><img src="../images/video/qt_' + qt_stat +'.gif" alt="QuickTime" width="153" height="21" border="0" /><a href="#" onclick="playvideo(\'qt_high\'); return false;"><img src="../images/video/button_high_' + qt_hi_vers +'.gif" onmouseover="vidroll(this)" alt="high" width="81" height="21" border="0" /></a><img src="../images/video/vertical.gif" width="13" height="21" border="0" alt="" /><a href="#" onclick="playvideo(\'qt_low\'); return false;"><img src="../images/video/button_dial-up_' + qt_lo_vers +'.gif" onmouseover="vidroll(this)" alt="dial-up" width="61" height="21" border="0" /></a></td></tr></table>';  
	linkholder.innerHTML = ctrl_default;
		
    

 

    
    
    
}

/**
 * opens up panel not really a play button
 */
function fakeplay(somevar)
{
	var videoholder = document.getElementById("video-holder");
	videoholder.innerHTML = '<img src="../images/video/poster_dr_instruct.jpg" title="Choose setting to play video" alt="Choose setting to play video" width="384" height="256" border="0" />';    

	var linkholder = document.getElementById("links-holder");
	linkholder.innerHTML = ctrl_all;
	
	return;	
}


/**
 * plays video
 */
function playvideo(somevar)
{
    if (!document.getElementById && !document.getElementById("video-holder"))
    {
        alert("You need to a newer browser version to view content.");
        return;
    }
    

    
    
    //grab video holder
    var videoholder = document.getElementById("video-holder");
    
    //if we are playing cookie val then get it
    if ((somevar == "cookieval"))   {  somevar = getCookie("vidpref"); } 
    
    //reset cookie
    setCookie("vidpref",somevar);
    cookieval = somevar;

	var vidval = eval(somevar);
	var vidval_noext  = vidval.substring(0,vidval.length-4);
	
	var wm_ext = '.asx';

    if(cookieval.match("wm"))
    {
        //local file from pc fix
        var defloc = "../video/";    
        var loc = window.location.href;
        //second two takes out ie mac and firefox
        //if((loc.lastIndexOf('file://') != -1) && !(loc.lastIndexOf('file:///') != -1) && !(loc.lastIndexOf('file://localhost') != -1))
        var agt=navigator.userAgent.toLowerCase();
        var is_win   = ( (agt.indexOf("win")!=-1) || (agt.indexOf("16bit")!=-1) );
        if(is_win && (loc.lastIndexOf('file:///') != -1) && !(loc.lastIndexOf('file://localhost') != -1))
        {
            var deflocv2 = loc.substring(8,loc.lastIndexOf('/'));
            deflocv2 = deflocv2.substring(0,deflocv2.lastIndexOf('/'));
            deflocv2 = deflocv2.substring(0,deflocv2.lastIndexOf('/')+1);
            var deflocv = unescape(deflocv2 + "video/" + vidval_noext + wm_ext);    
            var samiFile = unescape(deflocv2 + "video/" + vidval_noext + '.sami');
        }
        else
        {
            var deflocv = defloc + vidval_noext + wm_ext;
            var samiFile= defloc + vidval_noext + '.sami';//;'vid_1_1_creating_220.sami';
        } 

		var captionDiv = ((navigator.platform.indexOf('Win') > -1) ? "<div id=\"captions-ie\">&nbsp;<\/div>" : "");

        videoholder.innerHTML = '<object id="videoie" classid="CLSID:22d6f312-b0f6-11d0-94ab-0080c74c7e95" width="384" height="256"><param name="FileName" value="' + deflocv + '" /><param name="AutoStart" value="1" /><param name="ShowControls" value="1" /><param name="ShowStatusBar" value="0" /><param name="ShowDisplay" value="0" /><param name="DisplaySize" value="0" /><param name="AutoSize" value="false" /><param name="captioningID" value="captions-ie" /><embed bgcolor="#fff" type="application/x-mplayer2" pluginspage="http://www.microsoft.com/Windows/Downloads/Contents/Products/MediaPlayer/" 		src="' + deflocv + '" showcaptioning="1" displaysize="1" autosize="false" stretchtofit="1" name="MediaPlayer" width="384" height="256"></embed></object>' + captionDiv;


        //videoholder.innerHTML = '<embed type="application/x-mplayer2" pluginspage="http://www.microsoft.com/Windows/Downloads/Contents/Products/MediaPlayer/" 		src="' + loc + '" name="MediaPlayer" width="320" height="226"></embed>';
    
    }
    

	
    if(cookieval.match("qt"))
    { 
        //write out embed
        videoholder.innerHTML = '<embed width="384" height="270" src="../video/' + eval(somevar) + '" bgcolor="#fff" pluginspage="http://www.apple.com/quicktime/download/" type="video/quicktime" controller="true" autoplay="true">';
    }
    


    write_ctrl_changesettings();
}



	
/** 
 * roll over for high low
 **/
function vidroll(this_img)
{
    //if image reference not passed then quit out
	if (!this_img) return true;
	
	//if rollovers not set then let's set them up
	if(!this_img.is_setup)
	{
	    //let's store old value
	    this_img.setAttribute('osrc', this_img.getAttribute('src'));
	    
	    var sTempSrc = '';
	    
	    if(this_img.getAttribute('src').match('off')) 
	    { 
	        sTempSrc = this_img.getAttribute('src').replace('_off.gif', '_over.gif');
	    }
	    else if(this_img.getAttribute('src').match('on')) 
	    { 
	        sTempSrc = this_img.getAttribute('src').replace('_on.gif', '_over.gif');
	    }
	    
	    //let's store hover state
	    this_img.setAttribute('hsrc', sTempSrc);
	    
        this_img.onmouseout = function() 
        {
            this.setAttribute('src', this.getAttribute('osrc'));
        }
        
        //let's set to true
        this_img.is_setup = true;
	}
	
	this_img.setAttribute('src', this_img.getAttribute('hsrc'));
	
	return false;
}



