// **** var movieID = ...  // Anything that includes this page needs this value to be set***

var movieState = "atStart"; //movie state is a variable that keeps tabs for the one button interface.

function play() 
{
	document.movieplayer.Play();
}

function stop() 
{
	document.movieplayer.Stop();
}

function reset() 
{
	commentForm("hide");
	
	var cancelButton = getObject("cancelButton");
	cancelButton.style.display = "none"; //display the button
	
	movieState = "playing";
	oneButton();
	
	play();	
}

function comment() 
{
    	stop();
	movieState = "paused";
	oneButton();
	
	//display the form 
	commentForm("show");
	clearDIV("results");
		
	//set focus to textarea
	var textarea = getObject("commentArea");
	textarea.focus();
	textarea.focus(); //double for IE sometimes
	
	return false;
}

function oneButton() 
{
	//get the button
	var button = getObject("button");
	
	if (movieState == "atStart") 
	{
		button.value = "Play!";
		button.onclick = getStarted;	
	} 
	else if (movieState == "playing") 
	{
		//Controls making a comment. The playing movie will be stoped. 
		//And a comment form displayed. The 
		
		button.value = "Make Comment"; //change the button label
		button.onclick = comment; //attach button's onClick to another function, comment()	
	} 
	else if (movieState == "paused") 
	{
		button.value = "Save & Continue";
		button.onclick = save;	
	}

	button.blur();
}

function validateForm() 
{
	if (getObject("commentArea").value != "") 
	{
		return true;
	} 
	else 
	{
		return false;
	}

}

function getTheTime()
{
    var adate = new Date();
    return adate.getTime();
}

function save() 
{
	//verify the form is valid
	if (validateForm()) { //pass the commentForm into the validator function
		//send the contents of the form to a script to insert into the db.
		var frame = document.movieplayer.GetTime(); //grab the current frame of the movie
		var comment = encodeURIComponent(getObject("commentArea").value);
		var name = encodeURIComponent(getObject("uname").value);
		//create the URL for the http / php request.
		var commentURL = "stream/commentUtil.php?mode=new&video_id=" + movieID + "&timecode=" + formatTime(getCurTime()) + "&comments=" + comment + "&username=" + name + "&NOW=" + getTheTime();
		requestHTTP(commentURL,"results"); //make an HTTP request for the following document.
		
		movieState = "playing";
		oneButton();
		
		//start playing the movie again.
		play();
		
		//redisplay comments
		requestComments();
		
		//close the comment form
		commentForm("hide"); 
		return false;
	} 
	else 
	{
		alert("Please enter a comment");
		return false;
	}	
}

function getCurTime() 
{
	var curTime = Math.round(document.movieplayer.GetTime() / document.movieplayer.GetTimeScale());
	return curTime;
}

function formatTime(curTime) 
{
	//format hours
	if (curTime > 60*60)
	{
		hours = Math.floor(curTime/(60*60));
		curTime = curTime - hours*60*60;
		if (hours < 10) { hours = "0" + hours; }
	}
	else
	{
		hours = "00";
	}

	//format minutes
	if (curTime > 60) {
		minutes = Math.floor(curTime/60);
		curTime = curTime - minutes*60;
		if (minutes < 10) { minutes = "0" + minutes; }
	} else { 
		minutes = "00";
	}
	
	//format seconds
	seconds = Math.floor(curTime%60);
	if (seconds < 10) { 
		seconds = "0" + seconds; 
	}
	
	//prepare new time
	newTime = hours + ":" + minutes + ":" + seconds;
	return newTime;
}

function navigate(seconds) 
{
    if (seconds != "") 
    {
	stop();
        document.movieplayer.SetTime(seconds * document.movieplayer.GetTimeScale());        
	play();
    }            
}

function commentForm(mode) 
{
	var theForm = getObject("commentForm");
	
	if (mode == "show") 
	{
		theForm.style.display = "block"; //show the form, change it's display style	
		var cancelButton = getObject("cancelButton");
		cancelButton.style.display = "block"; //display the button
		cancelButton.onclick = reset; //set click to execute reset() function
	} 
	else if (mode == "hide") 
	{
		theForm.style.display = "none";  //hide the form
		var cancelButton = getObject("cancelButton");
		cancelButton.style.display = "none"; //display the button
		
		//clear the comment area
		var commentArea = getObject("commentArea");
		commentArea.value = "";
	}
}

function requestComments() 
{
	var tempURL = "stream/commentUtil.php?mode=view&video_id=" + movieID + "&movieRate=" + document.movieplayer.GetTimeScale() + "&NOW=" + getTheTime();
	requestHTTP(tempURL,"commentsSide");
}


var req;
var theID;
function requestHTTP(url, formElemID) {
	theID = formElemID;
    
    // Internet Explorer
    try 
    {  
        req = new ActiveXObject("Msxml2.XMLHTTP"); 
    }
    catch(e) 
    {
        try 
        {
            req = new ActiveXObject("Microsoft.XMLHTTP"); 
        }
        catch(oc) { req = null; }
   }

   // Mozailla/Safari
   if (!req && typeof XMLHttpRequest != "undefined") { req = new XMLHttpRequest(); }

   // Call the processChange() function when the page has loaded
   if (req != null) {
      req.onreadystatechange = processChange;
      req.open("GET", url, true);
      req.send(null);
   }
}

function processChange() 
{
   // The page has loaded and the HTTP status code is 200 OK
   if (req.readyState == 4 && req.status == 200) 
   {
      // Write the contents of this URL to the searchResult layer
      getObject(theID).innerHTML = req.responseText;
   }
}

function getObject(name) 
{
   var ns4 = (document.layers) ? true : false;
   var w3c = (document.getElementById) ? true : false;
   var ie4 = (document.all) ? true : false;

   if (ns4) return eval('document.' + name);
   if (w3c) return document.getElementById(name);
   if (ie4) return eval('document.all.' + name);
   return false;
}


function clearDIV(id) {
	var div = getObject(id);
	div.innerHTML = "";
}
