// JavaScript Document

function makeComment(id){
	openModalBox();
	//alert("make comment");
	/*embeds = $$('embed');
	alert(typeof(embeds));
	alert(embeds.length);
for(i = 0; i < embeds.length; i++) {
	alert("hiding" + i);
	embeds[i].setStyle('display', 'none');
    //embeds[i].style.visibility = 'hidden';
}*/

	modalStatus('<table border\="0" cellspacing\="1" cellpadding\="2"><tr><td align\="right" valign\="top">Name:<\/td><td align\="left" valign\="top"><input name\="alias" type\="text" id\="alias" size\="30" \/><\/td><\/tr><tr><td align\="right" valign\="top">Email: <\/td><td align\="left" valign\="top"><input name\="emailadd" type\="text" id\="emailadd" size\="30" \/><\/td><\/tr><tr><td align\="right" valign\="top">Subject: <\/td><td align\="left" valign\="top"><input name\="cmtsubj" type\="text" id\="cmtsubj" size\="30" \/><\/td><\/tr><tr><td align\="right" valign\="top">Comment:<\/td><td align\="left" valign\="top"><textarea name\="cmttext" id\="cmttext" cols\="30" rows\="4" \/><\/textarea><\/td><\/tr><tr><td>&nbsp;<\/td><td align\="left"><input type\="button" onclick\="callCommentPost(\'' + id + '\');" value\="Send Comment" \/><input type\="button" name\="button" id\="button" value\="Cancel" onclick\="closeModalBox();" \/><\/td><\/tr><\/table>');
	
}

function makeCommentInline(id){
	$("commentFormWrapper").style.display = "inline";
}

function callCommentPostInline(id) {
	//alert("callCommentPostInline " + id);
	var sAlias = $('alias').value;
	var sSubject = $('cmtsubj').value;
	var sText = $('cmttext').value;
	var sEmail = $('emailadd').value;
	if (pageConfig.commentUid){		
		var sType = pageConfig.commentUid; 		
	} else {		
		var sType = pageConfig.zone;		
	}
	
	var myRequest = new Request({method: 'get', url: '/comments/comment_proxy.jhtml'}).send('act=post&uid=' + id + '&utype='+sType+'&alias=' + sAlias + '&emailadd=' + sEmail + '&cmtsubj=' + sSubject + '&cmttxt=' + sText);
	myRequest.onSuccess = function(responseText, responseXML) {
		
		$("commentFormWrapper").style.display = "none";
		refreshComments(id,sType,pageConfig.hideUnmodComments);
	}
	
}

function callCommentPost(id){

	var sAlias = $('alias').value;
	var sSubject = $('cmtsubj').value;
	var sText = $('cmttext').value;
	var sEmail = $('emailadd').value;
	if (pageConfig.commentUid){		
		var sType = pageConfig.commentUid; 		
	} else {		
		var sType = pageConfig.zone;		
	}
	
	var myRequest = new Request({method: 'get', url: '/comments/comment_proxy.jhtml'}).send('act=post&uid=' + id + '&utype='+sType+'&alias=' + sAlias + '&emailadd=' + sEmail + '&cmtsubj=' + sSubject + '&cmttxt=' + sText);
	myRequest.onSuccess = function(responseText, responseXML) {
		modalStatus('Submitting your comment...');
		closeModalBox();
		refreshComments(id,sType,pageConfig.hideUnmodComments);
	}

}

function hideCommentForm(){
	$("commentFormWrapper").style.display = "none";
	
}

function toggleComments(){
	$("relatedVideo").style.display = "none";
	$("comments").style.display = "inline";	
}

function toggleRelatedVideo(){	
	$("comments").style.display = "none";
	$("relatedVideo").style.display = "inline";
}
function commentRandomNumber(limit){
  return Math.floor(Math.random()*limit);
}
function refreshComments(id,utype,hideUnmod){

/* can be called automatically if pageConfig.autoRefComments = true; in calling document. Also a timeout should be set
in the body tag of the caller. See ema2k8/video_live.jhtml for an example. 

Also, use pageConfig.hideUnmodComments = true; to hide unmoderated comments on auto refresh.

*/


//alert("refreshcomments");
if (hideUnmod == true){
		modstatus = "1";
	} else {
		modstatus = "01";
	}
var myRequest = new Request({method: 'get', url: '/comments/retrieve_proxy.jhtml'}).send('id=' + id + '&utype='+ utype + '&modstatus='+ modstatus + '&rand=' + commentRandomNumber(10000));

myRequest.onSuccess = function(responseText, responseXML) {
		//alert(responseText);
		$('commentWrapper').setHTML(responseText);
	}
	if (pageConfig.autoRefComments == true){
		setTimeout('refreshComments("' + id + '","' + utype +'",' + hideUnmod + ')',8000); 
	}
}
var idArray = [];

function commentCounter(zone,idString){
	idArray = idString.split(",");
	//alert(zone);
	//alert(idArray[3]);
	var myQuery = "";
	for (i =0; i < idArray.length; i++){
		myQuery += zone + "|" + idArray[i]; 
		if(i != idArray.length -1){myQuery += ","};
	}
	ajaxCountComments(myQuery);
	//$(debug).innerHTML = "doCmt.php?act=getnums&ids=" + myQuery;

};

function ajaxCountComments(id) {
	var myRequest = new Request({method: 'get', url: '/comments/count_proxy.jhtml'}).send('id=' + id + '&rand=' + commentRandomNumber(10000));

myRequest.onSuccess = function(responseText, responseXML) {
		//alert(responseText);
		countArray = responseText.split(",");
		for (i=0; i < countArray.length - 1; i++){
			//alert("commentCnt" + idArray[i]);
			$("commentCnt" + idArray[i]).setHTML(countArray[i]);
				
		}
	}
}