var xmlHttp
var ContentID
var strFilename
var strTitle
var strKeyword = ""
var strSearchType = ""
var blnRTMP = false
var blnHasAttachment = false
var strAttachmentFilename = ""
var strThumbnailFilename = ""
var currentState
var player = null


function playFile(fileName) {
	var archiveUrl = "http://www.carrollhistory.org/streaming/archive/clips/"
	var thumbnailUrl = "../streaming/thumbnails/"
	var rtmpUrl = "rtmp://www.cmcmd.tv/vod/"
	
	var so = new SWFObject("player.swf", "jwplayer", "400", "330", "8");
	   	
   	if (blnRTMP == true)
   	{   so.addVariable("streamer", rtmpUrl);
   	    so.addVariable("file", fileName);
   	}
   	else
   	{   so.addVariable("file", archiveUrl+fileName);   	}
   	
   	if (strThumbnailFilename != "")
   	{   so.addVariable("image", thumbnailUrl+strThumbnailFilename);  }
   	
   	so.addVariable("autostart","true");
	so.addVariable("width","400");
	so.addVariable("height","330");
	so.addVariable("skin","stijl.swf");
	so.addVariable("bufferlength",0);
	so.addVariable("volume",100);
	so.addParam("allowfullscreen","true");
	so.addParam("allowscriptaccess","always");
	so.addParam("wmode","transparent");
	so.write("player-window");
	
    currentState = "STOPPED";

//    document.getElementById("player-window").innerHTML = "<embed src='player.swf' id='jwplayer' width='400px' height='330px' wmode='transparent' flashvars='file="+archiveUrl+fileName+"&skin=stijl.swf&autostart=true&allowfullscreen=true' />"
}

function playerReady(thePlayer) 
{
	player = window.document[thePlayer.id];
	addListeners();
}

function addListeners() 
{
	if (player) { player.addModelListener("STATE", "stateListener"); } 
	else { setTimeout("addListeners()",100); }
}

function stateListener(obj)
{
    currentState = obj.newstate;
}

function togglePlayback()
{
    var player = document.getElementById("jwplayer");
    player.sendEvent("PLAY");
}


function playCue(timeSecs) {

    var player = document.getElementById("jwplayer");
     
    if (currentState != "PLAYING" && currentState != "PAUSED")
    {   player.sendEvent("PLAY"); 
        setTimeout("playCue("+timeSecs+")",1000);
    }
    else
    {   player.sendEvent("SEEK",timeSecs); }
    
}

function initVOD() 
{
    ContentID = null;
    strKeyword = "";
}


function testEnterKey(e)
{

    if (e.keyCode == 13)
    {

        document.getElementById("btnFind").click();
    }
}

function GetXmlHttpObject() 
{
    var xmlHttp = null;
    try
      {
      // Firefox, Opera 8.0+, Safari
      xmlHttp=new XMLHttpRequest();
      }
    catch (e)
      {
      // Internet Explorer
      try
        {
        xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
        }
      catch (e)
        {
        xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
        }
      }
    return xmlHttp;
}

function showCategories() 
{ 
    xmlHttp=GetXmlHttpObject();
    
    if (xmlHttp==null) {
      alert ("Your browser does not support AJAX!");
      return;
    } 
    
    var url="get_content_categories.asp";    
    url=url+"?sid="+Math.random();
    xmlHttp.onreadystatechange=CategoryRequestStateChanged;
    xmlHttp.open("GET",url,true);
    xmlHttp.send(null);

}

function CategoryRequestStateChanged()
{
    if (xmlHttp.readyState==4)
    {
        document.getElementById("category-list").innerHTML = xmlHttp.responseText;
        clearFilter();
    }
}


function showLibrary() 
{ 
    xmlHttp=GetXmlHttpObject();
    
    if (xmlHttp==null) {
      alert ("Your browser does not support AJAX!");
      return;
    } 
   
    var url="get_library.asp";    
    url=url+"?keyword="+strKeyword;
    url=url+"&type="+strSearchType;
//    url=url+"&sid="+Math.random();
    xmlHttp.onreadystatechange=LibraryRequestStateChanged;
    xmlHttp.open("GET",url,true);
    xmlHttp.send(null);

}

function LibraryRequestStateChanged()
{
    if (xmlHttp.readyState==4)
    {
        document.getElementById("content-list").innerHTML = xmlHttp.responseText;
    }
}

function showCategory(x)
{
    if (x == "All")
    {
        strKeyword = "";
        strSearchType = "OnDemand";
    }
    else if (x == "Recent")
    {
        strKeyword = "Most Recent 25";
        strSearchType = "Recent";
    }
    else
    {
        strKeyword = x;
        strSearchType = "Category";
    }
       
    showLibrary();

}

function searchLibrary()
{
    if (document.getElementById("keyword-search").value != "")
    {
        var temp = document.getElementById("keyword-search").value
    
        temp =  temp.replace(/[^a-zA-Z \' 0-9]+/g,"");
        document.getElementById("keyword-search").value = temp;
        strKeyword = temp;
        strSearchType = "Search";              
    }
    else
    {
        strKeyword = "";
        strSearchType = "OnDemand";
    }
    
    showLibrary();
    
    if (ContentID !=null)
    {
        setTimeout("showCueList(ContentID)",500);
    }

}

function clearFilter()
{
    document.getElementById("keyword-search").value = "";
    document.getElementById("keyword-search").focus();
    strKeyword = "";
    strSearchType = "OnDemand"
    showLibrary();
    
    if (ContentID !=null)
    {
        setTimeout("showCueList(ContentID)",500);
    }
}

function toggleCueFilter()
{
    if (ContentID != null)
    {   showCueList(ContentID); }
}

function playContent(intID) 
{ 
    window.open("player.asp?id="+intID, "viewvideo", "width=800,height=500,top=200,left=500,toolbars=0,status=0,resizable=0,scrollbars=0");
}

function ContentDetailsRequestStateChanged()
{
    if (xmlHttp.readyState==4)
    {
        var strResponse = xmlHttp.responseText;
        var field = strResponse.split("<field-break>");
        strContentID = field[0];
        strTitle = field[1];
        strCreationDate = field[2];
        strDuration = field[3];
        strAvailableWMV = field[4];
        strFilename = field[5];
        
        if (field[6] == -1)
        {   blnRTMP = true; }
        else
        {   blnRTMP = false; }
        
        if (field[7] == -1)
        {   blnHasAttachment = true; 
            strAttachmentFilename = field[8]; }
        else
        {   blnHasAttachment = false; }
        
        if (field[9] == -1)
        {   strThumbnailFilename = field[10];   }
        else
        {   strThumbnailFilename = "";  }
        
        
        var strContentDetails;
        strContentDetails = "<div class='largetext'>"+strTitle+"<div><br /><p class='smalltext'>Date: "+strCreationDate+"&nbsp;&nbsp;Duration: "+strDuration+"</p>";

        if (blnHasAttachment == true)
        {   strContentDetails = strContentDetails+"&nbsp;&nbsp;&nbsp;<a title='Open attachment ("+strAttachmentFilename+") in a new window' href='javascript:openAttachment(\""+strAttachmentFilename+"\")'><img border='1' src='images/pdf-logo.gif' alt='View Attachment' /></a>" }
        
        
        document.getElementById("content-details").innerHTML = strContentDetails;
        playFile(strFilename);
        showCueList(strContentID);
    }
}

function openAttachment(n)
{
    window.open("../streaming/attachments/" + n, "viewattachment", "width=640,height=480,toolbars=no,status=no,resizable=no,scrollbars=yes");
}

function showCueList(intID) 
{ 
    xmlHttp=GetXmlHttpObject();
    
    if (xmlHttp==null) {
      alert ("Your browser does not support AJAX!");
      return;
    } 
    
    ContentID = intID;
    var url="get_content_cues.asp";
    url=url+"?q="+ContentID;
    
    if (document.getElementById("transcript-filter").checked && strSearchType != 'Category' && strSearchType != 'Recent') 
    {
        url=url+"&filter=true";
    }
    else
    {
        url=url+"&filter=false";
    }
    
    url=url+"&keyword="+strKeyword;
    url=url+"&sid="+Math.random();
    xmlHttp.onreadystatechange=CueListRequestStateChanged;
    xmlHttp.open("GET",url,true);
    xmlHttp.send(null);

}

function CueListRequestStateChanged()
{
    if (xmlHttp.readyState==4)
    {
        document.getElementById("content-cues").innerHTML = xmlHttp.responseText;
    }
}

function playContentCue(ID, Cue)
{
    if (ContentID != ID)
    {    playContent(ID);   
        setTimeout("playCue("+Cue+")",2000);
    }
    else
    { playCue(Cue); }	
}
