//var xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
//load xml file
// code for IE
if (window.ActiveXObject)
  {
  	xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
  }
// code for Mozilla, Firefox, Opera, etc.
else if (document.implementation && document.implementation.createDocument)
  {
  	xmlDoc=document.implementation.createDocument("","",null);
  }
else
  {
  	alert('Your browser cannot handle this script');
  }

var xmlFile = "/focusnyc2/content/xml/Media.xml";
var default_category = "3";
var list_str = "";
var first_video_path = "";
var first_summary = "";
var first_title = "";
var now_playing = "";

function loadXML(xmlFile,default_category) 
{ 
	xmlDoc.async=false;
	xmlDoc.onreadystatechange=verify; 				
	xmlDoc.load(xmlFile); 				
	xmlObj=xmlDoc.documentElement; 				
	getContent(xmlObj,default_category);
}

function getContent(tree,category_val)
{	
      var intNode,i;
	  var cellcount = 0;
      var intCode,strValue;
	  list_str = '<table width="100%" border="0" cellspacing="0" cellpadding="2" id="listTable">';
      if(tree.hasChildNodes())
      {
            var nodes=tree.childNodes.length; 		      
            for(i=0;i<nodes;i++)
            {			
				try {
					title_str = xmlDoc.getElementsByTagName("resource_title")[i].childNodes[0].nodeValue;
	                summary_str = xmlDoc.getElementsByTagName("resource_teaser")[i].childNodes[0].nodeValue;
	                thumb_str = xmlDoc.getElementsByTagName("thumbpath")[i].childNodes[0].nodeValue;
	                video_path_str = xmlDoc.getElementsByTagName("resource_link")[i].childNodes[0].nodeValue;
					cat_val = xmlDoc.getElementsByTagName("category")[i].childNodes[0].nodeValue;

					cellcount = cellcount +1 ;
					if (cat_val.toUpperCase() == category_val.toUpperCase())
					{
						if(first_video_path == "")
						{
							first_video_path = video_path_str;
							first_summary = summary_str;
							first_title = title_str;
						}			
						list_str = list_str + '<tr><td id=row' + cellcount + ' onClick=makeObject(\'' + video_path_str + '\',' + cellcount + ',\'' + escape(title_str) + '\',\'' + escape(summary_str) + '\');><div class=titleText><a href="#" onClick=makeObject(\'' + video_path_str + '\',' + cellcount + ',\'' + escape(title_str) + '\',\'' + escape(summary_str) + '\'); class=titlelink>' + title_str + '</a></div><div>' + summary_str +  '</div></td></tr>';
						list_str = list_str + '<tr><td colspan=2 class=summary-row><img src="images/spacer.gif" width=50 height=2 border=0 vspace=0></td></tr>'
					}
				} catch (e) {
					//--- To see any errors that occur in this loop uncomment the alert below;
					//alert(e)
				}
            }
       }
	   
	   list_str = list_str + '</table>';
	   videolist.innerHTML = list_str;
	   //alert(videolist);
}

function verify() 
{ 
     // 0 Object is not initialized 
     // 1 Loading object is loading data 
     // 2 Loaded object has loaded data 
     // 3 Data from object can be worked with 
     // 4 Object completely initialized 
     if (xmlDoc.readyState != 4) 
     { 
              return false; 
     } 
}

//----------------------------------------------------------------------
function makeObject(objecturl,rowid,objtitle,objectsummary)
{
	//alert(objecturl);
	var object_width = 500;
	var object_height = 350;
	//var object_str = '<object width="' + object_width +'" height="' + object_height +'" id="mediaPlayer" classid="CLSID:22d6f312-b0f6-11d0-94ab-0080c74c7e95" codebase="http://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab#Version=5,1,52,701" standby="Loading Microsoft Windows Media Player components..." type="application/x-oleobject"><param name="fileName" value="' + objecturl + '"/><param name="animationatStart" value="true"/><param name="transparentatStart" value="true"/><param name="autoStart" value="true"/><parm name="showControls" value="true"/><param name="loop" value="false"/><embed width="' + object_width +'" height="' + object_height +'" type="application/x-mplayer2" pluginspage="http://microsoft.com/windows/mediaplayer/en/download/" id="mediaPlayer" name="mediaPlayer" displaysize="4" autosize="-1" bgcolor="darkblue" showcontrols="true" showtracker="-1" showdisplay="0" showstatusbar="-1" videoborder3d="-1" src="' + objecturl + '" autostart="True" designtimesp="5311" loop="False"></embed></object>';
	//videoDiv.innerHTML = object_str; 
  	
	var s1 = new SWFObject("flvplayer.swf","single",object_width,object_height,"7");
	s1.addParam('allowscriptaccess','always');
	s1.addParam("allowfullscreen","true");
	s1.addVariable("file",objecturl);
	if(objecturl.indexOf(".mp3") > 0){
		s1.addVariable("image","preview-audio.jpg");
	}else{
		s1.addVariable("image","preview.jpg");	
	}
	s1.addVariable("width",object_width);
	s1.addVariable("height",object_height); 
	s1.addVariable("autostart","false");
	s1.addVariable("enablejs","true");
	s1.addVariable("javascriptid","single"); 	
	s1.write("videoDiv");	
	//videoSummaryDiv.innerHTML = "<span class=titleText>" + unescape(objtitle) + "</span><br><span>" + unescape(objectsummary) + "</span>";
	//alert(title);
	var rowidval = "row" + rowid;
	now_playing = rowidval;
	if(rowid != ""){
		updateCellColor(rowidval);
	}
}

var currentState;

function getUpdate(typ,pr1,pr2,pid) {
	if(pid != "null") {
		if((typ == "state")&&(pr1 != undefined)) { 
			currentState = pr1; 
			if(currentState=="3") {
			 //alert('Praise the Lord.'); 
			 }
		}
	}
};

var lastdiv;
function updateCellColor(thisval) 
{
	//--- called by clicking on days in the calendar
	//alert(document.getElementById(thisval).style.background)
	if((lastdiv != "")&&(lastdiv != null))
	{
		try{
				document.getElementById(lastdiv).className = "";
			} catch (e) {
				//--- To see any errors that occur in this loop uncomment the alert below;
				//alert(e)
			}
	}
	document.getElementById(thisval).className = "selectedVideo";
	lastdiv = thisval;
}
var lastcat;
function updateCellCategory(thisval) 
{
	//--- called by clicking on days in the calendar
	//alert(document.getElementById(thisval).style.background)
	if((lastcat != "")&&(lastcat != null))
	{
		document.getElementById(lastcat).className = "catcell";
	}
	document.getElementById(thisval).className = "catcell_sel";
	lastcat = thisval;
}
function getCat(cat_val)
{
	if(cat_val != ""){
		getContent(xmlObj,cat_val);
		updateCellCategory(cat_val);
		try{
			updateCellColor(now_playing) 
		}catch (e){ }
	}
}

function doLoad() {
	loadXML(xmlFile,default_category);
	updateCellCategory(default_category)
	makeObject(first_video_path,0,first_title,first_summary);
}

//----------------------------------------------------------------------
