  var gMediaID=""
  function GetRecordingInfo(mediaId)
  {
    gMediaID = mediaId;
	var url = "api.aspx?cmd=GetRecInfo&arg1=" + mediaId
    if (document.getElementById('ddFolder'))
    {
        if (document.getElementById('ddFolder').value == "Trash")
            url += "&arg2=Trash" 
     } 
    UpdateAndGetInfo(url)
  }   
  
  function GetTimeFromMediaId(mediaId)
  {
    var Y, M, D, h, m, s
    Y = mediaId.substr(0,4)
    M = mediaId.substr(4,2)
    D = mediaId.substr(6,2)
    h = mediaId.substr(8,2)
    m = mediaId.substr(10,2)
    s = mediaId.substr(12,2)
    return Y+"/"+M+"/"+D+" "+h+":"+m+":"+s
    
  }
  
  function RecInfoCB(strResp)
  {
	if (req.readyState == 4) {
		// only if "OK"
		if (req.status == 200) {
			//document.getElementById('status').innerHTML = req.responseText
			//alert(req.responseText)
			if (req.responseText != "")
			{
				var pathElmArr = req.responseText.split("^");
				var strInfo = "<table width=100% border=1><tr valign=top><td>";
				var playURL=""
				var isPublished="No"
				var isPublic = false
				var ytid= ""
				var contentOwner = pathElmArr[1];
				var dur="0";
				strInfo += "Recording Time: <b>"+ GetTimeFromMediaId(pathElmArr[5])+ "</b><br>";
				for (var j=0; j< pathElmArr.length-1; j++)
				{
					switch(j)
					{
						case 0:
							strInfo += "Description: <b>"+ pathElmArr[j]+ "</b><br>";
							break;
						case 1:
							strInfo += "Presenter's Email: <b>" +pathElmArr[j] + "</b><br>";
							break;
						case 2:
							strInfo += "Copyright: <b>" + pathElmArr[j] + "</b><br>";
							break;
						case 3:
							strInfo += "Password: <b>" + pathElmArr[j] + "</b><br>";
							isPublic = (pathElmArr[j] =="")? true:false
							break;
						case 4:
						    playURL = pathElmArr[j].replace(" ","%20")
							strInfo += "URL to play:"
 			                if (document.getElementById('ddFolder'))
 			                {
 			                   if (document.getElementById('ddFolder').value == "Library") 
					               strInfo += "<input type=button class=button value='Play' onclick='window.open(\"" + playURL + "\")'> &nbsp;&nbsp;"
			                }
							strInfo += "<br><b>" + playURL + "<br></b>"
							break;
						case 5:
							gMediaID = pathElmArr[j]
							break;
						case 6:
						    if (isPublic)
						    {
						        isPublished = (pathElmArr[j]=="1" ? "Yes" : "No")
							    strInfo += "Published status: <b>"  + isPublished+ "</b><br></b>"
							}
							break;
						case 7:
						    ytid = pathElmArr[j]
						    if (ytid != "")
						        strInfo += "YouTube url: http://www.youtube.com/watch/v/"  + ytid+ "<br></b>"
							break;
						case 8:
						    dur = pathElmArr[j]
						    if (dur != "")
						        strInfo += "Duration: <b>"  + formattime(dur)+ "<b><br></b>"
							break;
						default:
							break;
					}
				}
				
				strInfo += "</td><td>"
				
				var strBtnHtml = ""
				if (document.getElementById('lblAdmin'))
				{
				    if (document.getElementById('lblAdmin').innerHTML == "1" ||
				        document.getElementById('lblChnlAdmin').innerHTML == "1" ||
				        contentOwner == document.getElementById('txtUserName').value)
				    {
                        document.getElementById('divDDFolder').display= (document.getElementById('lblAdmin').innerHTML == "1")?'block':'none'
 			            if (document.getElementById('ddFolder').value == "Library")
				        {
    				        if(document.getElementById('lblAdmin').innerHTML == "1")
    				        {
    				            strBtnHtml += "<input type=button class=button style='width:120px' value='"
				                strBtnHtml += "Move To Trash' onclick='TrashMedia( )'> <br>"
				            }
				            if (isPublic)
				            {
				                if (isPublished == "Yes")
				                    strBtnHtml += "<input type=button class=button style='width:120px' value='UnPublish' onclick='Publish(false)'> <br>"
				                else
				                    strBtnHtml += "<input type=button class=button style='width:120px' value='Publish' onclick='Publish(true)'> <br>"
			                    strInfo += "<input type=button class=button style='width:120px' value='Make Private' onclick='Public(false)'> <br>"				            
				            }
				            else
				            {
				                 strBtnHtml += "<input type=button class=button value='Make Public with no key' onclick='Public(true)'> <br>"				            
				            }
				            strBtnHtml += "<br>"
                           strBtnHtml += "<input type=button id='btnRunOCR' class=button style='width:120px' value='Run OCR' onclick='RunOCR()'> <br>" 
                           if (ytid == "" && parseInt(dur,10)<600)
                                strBtnHtml += "<input type=button id='btnYT' class=button style='width:120px' value='Upload to YouTube' onclick='Up2YT()'> <br>" 
				        }
			           else
			           {
                            strBtnHtml += "<input type=button class=button style='width:120px' value='"
			   	            strBtnHtml += "<= Move Back To Library' onclick='TrashMedia(true)'> <br>"
					       // strInfo += "<input type=button class=button value='Delete permanently' onclick='DeleteMedia( )''> &nbsp;&nbsp;"
                        }				   	    
                       
                    }
                 }
                 strInfo += strBtnHtml + "</td></tr></table>"
                 
                 
                //alert(document.getElementById('ddFolder').value )
			   
				if (document.getElementById('divInfo'))
				{
				 	   if (document.getElementById('ddFolder'))
				 	   {
				 	        if (document.getElementById('ddFolder').value == "Trash")
                        	    document.getElementById('divInfo').innerHTML = "<font color=red>" +strInfo
                            else
                        	    document.getElementById('divInfo').innerHTML = strInfo
                       }
                       else
                        	document.getElementById('divInfo').innerHTML = strInfo
                }                           
			}
			
			GetSegments();
		}
	}
}


function TrashMedia(reverse)
{
    if (!confirm("Are you sure?"))
        return

	var url = "api.aspx?cmd=Trash&arg1=" + gMediaID
	if (reverse != null || reverse != 'undefined')
	{
	    if (reverse == true)
	         url += "&arg2=Reverse"
	}
	//alert(url)
	// branch for native XMLHttpRequest object
	if (window.XMLHttpRequest) {
		req = new XMLHttpRequest();
		req.onreadystatechange = TrashCB;
		req.open("GET", url, true);
		req.send(null);
	// branch for IE/Windows ActiveX version
	} else if (window.ActiveXObject) {
		req = new ActiveXObject("Microsoft.XMLHTTP");
		if (req) {
			req.onreadystatechange = TrashCB;
		// document.getElementById('status').innerHTML = url
			req.open("GET", url, true);
			req.send();
		}
	}
  }   
  function TrashCB(strResp)
  {
	if (req.readyState == 4) {
		// only if "OK"
		if (req.status == 200) {
			//document.getElementById('status').innerHTML = req.responseText
			//alert(req.responseText)
			if (req.responseText != "")
			{
                if (req.responseText != "Done")
                    alert(req.responseText)
                else
                    GetMediaTree()
            }
        } 	
     } 
}
function RunOCR()
{
	var url = "api.aspx?cmd=RunOCR&arg1=" + gMediaID 
	document.getElementById('btnRunOCR').disabled = true
    UpdateAndGetInfo(url)
}
function Up2YT()
{
	var url = "api.aspx?cmd=Up2yt&arg1=" + gMediaID 
	document.getElementById('btnYT').disabled = true
    UpdateAndGetInfo(url)
}
function SetRecPW()
{
	var url = "api.aspx?cmd=SetRecPW&arg1=" + gMediaID + "&arg2=" + document.all.item('txtNewPW').value
    UpdateAndGetInfo(url)
}
function Publish(yesno)
{
    if (!confirm("Are you sure?"))
        return
	var url = "api.aspx?cmd=Publish&arg1=" + gMediaID
	if (yesno != null || yesno != 'undefined')
	{
	    if (yesno == true)
	         url += "&arg2=" + (yesno?"1":"0")
	}
    UpdateAndGetInfo(url)

}

function Public(yesno)
{
    if (!confirm("Are you sure?"))
        return
	var url = "api.aspx?cmd=Public&arg1=" + gMediaID
	if (yesno != null || yesno != 'undefined')
	{
	    if (yesno == true)
	         url += "&arg2=" + (yesno?"1":"0")
	}
    UpdateAndGetInfo(url)
}	

function UpdateAndGetInfo(url)
{
 if (window.XMLHttpRequest) {
		req = new XMLHttpRequest();
		req.onreadystatechange = RecInfoCB;
		req.open("GET", url, true);
		req.send(null);
	// branch for IE/Windows ActiveX version
	} else if (window.ActiveXObject) {
		req = new ActiveXObject("Microsoft.XMLHTTP");
		if (req) {
			req.onreadystatechange = RecInfoCB;
		// document.getElementById('status').innerHTML = url
			req.open("GET", url, true);
			req.send();
		}
	}	
}

function GetMediaList()
{
	if (document.getElementById('divSeg') == null)
	    document.getElementById('divInfo').innerHTML  += "<div id=divSeg></div>"
	var url = "api.aspx?cmd=GETMEDIATREE&arg3=time"
	DoAjax(url, GetMediaListCB)
}
function GetMediaListCB()
{
    if (!IsAjaxResponseValid()) return
	var strHTML = ""
	var dataArr = req.responseText.split("<br>");
	if (dataArr.length == 0) return
	
	var playUrl = "", thumbUrl = "", title="", topic="", presenter=""
	strHTML += "<table style:='border-collapse:collapse' cellpadding=0 cellspacing=0>"
	for (var i=0; i<dataArr.length-1; i++)
	{
	    var itemArr = dataArr[i].split("^")
	    if (itemArr.length == 0) continue
	    
	    presenter = itemArr[1];
	    topic = itemArr[2];
	    title= itemArr[3];
	    thumbUrl = itemArr[4];
	    playUrl = itemArr[5];
        strHTML += '<tr onmouseover="OverLink(this)" onmouseout="OutLink(this)" onclick="ClickedLink(this);GetRecordingInfo(\'' + itemArr[0] + '\')">'
        strHTML += "<td><a href='javascript:GetRecordingInfo(\"" + itemArr[0] + "\")'><img border=0 onerror='loadDefImg(this)' id='imgThumb" + i.toString() + "' width=64 height=48 title='" + title + "' src='" + thumbUrl + "'</a></td>"
        strHTML += "<td>Title: " + title + "<br>Topic: " + topic + "<br>Presenter: " + presenter +"</td>"
        strHTML += '<tr bgcolor="#cccccc"><td colspan=2><img border=0 height=1 width="100%" src="img/spacer.gif"></td></tr>'
   }
	document.getElementById('divRecordings').innerHTML = strHTML + "</table>"

}

function loadDefImg(imgObj)
{
    imgObj.src = "img/spacer.gif"
}

  function GetMediaTree()
  {
  
	if (document.getElementById('divInfo'))
		document.getElementById('divInfo').innerHTML = ""


	if (document.getElementById('divRecordings'))
		document.getElementById('divRecordings').innerHTML = "<br /><br /><center><img src='img/loading.gif' /></center>"

      if (document.getElementById('ddView').value != "Tree")
      {
        GetMediaList()
        return;
      }

	var url = "api.aspx?cmd=GetMediaTree"
   if (document.getElementById('ddFolder').value == "Trash")
        url += "&arg1=Trash" 
//alert(url)
	// branch for native XMLHttpRequest object
	if (window.XMLHttpRequest) {
		req = new XMLHttpRequest();
		req.onreadystatechange = MediaTreeCB;
		req.open("GET", url, true);
		req.send(null);
	// branch for IE/Windows ActiveX version
	} else if (window.ActiveXObject) {
		req = new ActiveXObject("Microsoft.XMLHTTP");
		if (req) {
			req.onreadystatechange = MediaTreeCB;
		// document.getElementById('status').innerHTML = url
			req.open("GET", url, true);
			req.send();
		}
	}
  }
  var prevCourse="", prevTopic=""
  var courseChanged = false
  function MediaTreeCB()
  {
	if (req.readyState == 4) {
		// only if "OK"
		if (req.status == 200) {
	        gTreeHTML = ""
          gNodeID = parseInt("1");
            prevCourse=""
            prevTopic=""
            courseChanged = false
    		var strRet = req.responseText
			var titlesArr = strRet.split("\r\n")

		//	alert(strRet)
			// find the loading item (s)
			var loadingCourse, loadingTopic;
			for (var i=0; i< titlesArr.length-1; i++)
			{
				if (titlesArr[i].indexOf(".wmv") == -1)
				{
					var elmArr = titlesArr[i].split("^");
					loadingCourse = elmArr[0]
					loadingTopic = elmArr[1]
				}
			}
			
			for (var i=0; i< titlesArr.length; i++)
			{
				var pathElmArr = titlesArr[i].split("^");
				for (var j=0; j< pathElmArr.length-1; j++)
				{
					//_curItemCount = 1
					switch(j)
					{
						case 0:
							if (prevCourse != pathElmArr[j])
							{
								prevTopic = ""
								if (i!=0) 
									EndNode()
								EndNode()
								
								// if a row of data does not have .wmv = means it is still loading
								if (pathElmArr[j] == loadingCourse)
									AddNode(pathElmArr[j],'img/course.gif',0,true)
								else
									AddNode(pathElmArr[j],'img/course.gif')
								prevCourse = pathElmArr[j]
								courseChanged = true
							}
							else
								courseChanged = false
							break;
						case 1:
							if (prevTopic != pathElmArr[j])
							{
								if (courseChanged == false) 
									EndNode()

								if (pathElmArr[j] == loadingTopic)
									AddNode(pathElmArr[j],'img/topic.gif',1,true)	
								else
									AddNode(pathElmArr[j],'img/topic.gif',1)	
								prevTopic = pathElmArr[j]
							}
							break;
						case 2:
							AddNodeLink(pathElmArr[j],pathElmArr[3],pathElmArr[5],2,pathElmArr[4],true) //false)
							break;
						default:
							break;
					}
				}
			}
			EndNode()
			EndNode()
			//alert(req.responseText)
			if (gTreeHTML.substr(0,6) == "</div>")
			    gTreeHTML = gTreeHTML.substr(6,gTreeHTML.length-6)
			document.getElementById('divRecordings').innerHTML = gTreeHTML
		}
	}

  }
  
  function ShowMediaTab()
  {
  
  }
  
  function ShowUserTab()
  {
  
  }
  
  function ShowOptionsTab()
  {
  
  
  }