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

var oneButtonState = "normal";

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
	
	oneButtonState = "normal";
	oneButton();
	
	if (!isLive)
	{
		play();	
	}
}

function comment() 
{
	if (!isLive)
	{
		stop();
	}
	oneButtonState = "commenting";
	oneButton();
	
	//display the form 
	commentForm("show");
		
	//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 (oneButtonState == "normal") 
	{
		//Controls making a comment.
		//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 (oneButtonState == "commenting") 
	{
		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 comment = encodeURIComponent(getObject("commentArea").value);
		var name = encodeURIComponent(getObject("uname").value);

		//create the URL for the http / php request.
		var commentURL = "";
		
		if (isLive)
		{
			commentURL = "stream/commentUtil.php?mode=new&video_id=" + movieID + "&timecode=" + formatTime(getCurTime()) + "&comments=" + comment + "&username=" + name + "&islive=" + "1" + "&NOW=" + getTheTime();		
		}
		else
		{
			commentURL = "stream/commentUtil.php?mode=new&video_id=" + movieID + "&timecode=" + formatTime(getCurTime()) + "&comments=" + comment + "&username=" + name + "&islive=" + "0" + "&NOW=" + getTheTime();		
		}

		createAjaxRequest(commentURL,returnComments,"GET");
		
		oneButtonState = "normal";
		oneButton();
		
		//start playing the movie again.
		if (!isLive)
		{
			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.round(curTime/(60*60));
		curTime = curTime - hours*60*60;
	}
	else
	{
		hours = "00";
	}

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


function navigate(seconds) 
{
	if (!isLive)
	{
		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 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 = "";
}


/* Basic AJAX Call */
/* THANK YOU DAVID NOLEN */

function initAjaxObject() 
{
	var aobject;
	var msxmlhttp = new Array('Msxml2.XMLHTTP.5.0','Msxml2.XMLHTTP.4.0','Msxml2.XMLHTTP.3.0','Msxml2.XMLHTTP','Microsoft.XMLHTTP');
	for (var i = 0; i < msxmlhttp.length; i++) 
	{
		try 
		{
			log(msxmlhttp[i]);
			aobject = new ActiveXObject(msxmlhttp[i]);
			break;
		} 
		catch (err) 
		{
			log(err);
			aobject = null;
		}
	}
	
	if (!aobject && (typeof(XMLHttpRequest) != "undefined"))
	{
		aobject = new XMLHttpRequest();
	}

	return aobject;
}

/* Create the request, default method is "POST" */
/* This stuff needs more work */
function createAjaxRequest(url,responseFunction,method) 
{   	
	var xmlHTTPReq = false;
	
	xmlHTTPReq = initAjaxObject();

	if (xmlHTTPReq)
	{
		log(url);
		
		//xmlHTTPReq.overrideMimeType('text/html');
		xmlHTTPReq.onreadystatechange = function() 
		{
			if (xmlHTTPReq.readyState == 1)
			{
						
			}
			else if (xmlHTTPReq.readyState == 4)
			{
				// Do something with the data		
				log("initial response:" + xmlHTTPReq.responseText);
				
				responseFunction(xmlHTTPReq.responseText);
			}
		}
		
		xmlHTTPReq.open('GET', url, true); 
		xmlHTTPReq.send(null);
	}
	else
	{
		log("Ajax not working");
	}
}

/* Logging utility */
function log(message) 
{
/*    if (!log.window_ || log.window_.closed) {
        var win = window.open("", null, "width=400,height=200," +
                              "scrollbars=yes,resizable=yes,status=no," +
                              "location=no,menubar=no,toolbar=no");
        if (!win) return;
        var doc = win.document;
        doc.write("<html><head><title>Debug Log</title></head>" +
                  "<body></body></html>");
        doc.close();
        log.window_ = win;
    }
    var logLine = log.window_.document.createElement("div");
    logLine.appendChild(log.window_.document.createTextNode(message));
    log.window_.document.body.appendChild(logLine);
*/
}
