var commentsNumPages = 0;
var commentsShowAll = 0;

function fpv_BuildCommentPaging(totalPages, currentPage) {
	if (totalPages == 0) {
		$('fpv_commPagingContainerDiv').innerHTML = "";
		return;
	}
	
	var pagesBefore = 5;
	var pagesAfter = 5;
	var showPrevious = 1;
	var showNext = 1;
	var previousPage = currentPage - 1;
	var nextPage = currentPage + 1;
	var start = 0;
	var end = 0;
	
	if (currentPage <= 5) {
		start = 1;
		if (totalPages < 10) {
			end = totalPages;
		}
		else {
			end = 10;
		}
	}
	else if (currentPage <= pagesBefore + 1) {
		start = 1;
		if ((currentPage + pagesAfter) < totalPages) {
			end = currentPage + pagesAfter;
		}
		else {
			end = totalPages;
		}
	}
	else if (currentPage >= totalPages - pagesAfter) {
		start = currentPage - pagesBefore;
		end = totalPages;
	}
	else {
		start = currentPage - pagesBefore;
		end = currentPage + pagesAfter;
	}
	
	if (currentPage == 1) {
		showPrevious = 0;
	}
	if (currentPage == totalPages) {
		showNext = 0;
	}
	
	htmlOutput = '';
	
	if (showPrevious == 1) {
		htmlOutput += '&lt;&lt; <a href="javascript:fpv_LoadComments(' + previousPage + ');">Previous</a>';
	}
	
	if (start > 1) {
		htmlOutput += '<a href="javascript:fpv_LoadComments(1);">1...</a>';
	}
	
	for (i=start ; i<=end ; i++) {
		linkStyle = "";
		if (i == currentPage) {
			linkStyle = " style=\"font-weight:bold\" ";
		}
		htmlOutput += '<a href="javascript:fpv_LoadComments(' + i + ');"' + linkStyle + '>' + i + '</a>';
	}
	
	if (end < (totalPages - 1)) {
		htmlOutput += '<a href="javascript:fpv_LoadComments(' + totalPages + ');">...' + totalPages + '</a>';
	}
	
	if (showNext == 1) {
		htmlOutput += '<a href="javascript:fpv_LoadComments(' + nextPage + ');">Next</a> &gt;&gt;';
	}
			
	document.getElementById('fpv_commPagingContainerDiv').innerHTML = htmlOutput;
	//$('fpv_commPagingContainerDiv').innerHTML = htmlOutput;
}

// ===================================================================================
// get comments
var http = fpv_createRequestObject();

function fpv_createRequestObject() {
	var req;
	if (window.XMLHttpRequest){
		// Firefox, Safari, Opera...
		req = new XMLHttpRequest();
	}
	else if (window.ActiveXObject) {
		// Internet Explorer 5+
		req = new ActiveXObject("Microsoft.XMLHTTP");
	}
	else {
		alert('Problem creating the XMLHttpRequest object');
	}
	return req;
}

function fpv_comments(page) {
	commentsCurrentPage = page ? page : 1;
	document.getElementById('fpv_uiComments').style.display = "block";
	
	var myDate = new Date();
	 
	myurl = SERVICES_BASE+'commenting/out/get_comments.php?siteID=' + siteID + '&typeID=1&objectID=' + videoID + '&page=' + commentsCurrentPage + '&refresh=' + String(myDate.getTime());
	http.open('get', myurl);
	http.onreadystatechange = fpv_LoadComments_callback;
	http.send(null);
}

function fpv_ShowAllCollapseComments() {
	if (!isCommentPaneExpanded) {
		$('fpv_uiComments').style.height = "auto";
		isCommentPaneExpanded = true;
	}
	else {
		$('fpv_uiComments').style.height = String(commentHeightMax) + "px";
		isCommentPaneExpanded = false;
	}
}

function fpv_CheckCommentsHeight() {
	$('fpv_uiComments').style.height = "auto";
	
	if (commentScrolling == 1) {		
		if (Element.getHeight('fpv_uiComments') > commentHeightMax) {
			if ($('fpv_commentsPanelTitleRight')) {
				$('fpv_commentsPanelTitleRight').style.display = "inline";
			}

			if (!isCommentPaneExpanded) {
				$('fpv_uiComments').style.height = String(commentHeightMax) + "px";
				// $('fpv_uiComments').style.overflow = "auto"; // Kerem commented out
			}
		}
		else {
			if ($('fpv_commentsPanelTitleRight')) {
				$('fpv_commentsPanelTitleRight').style.display = "none";
			}
		}
	}
	else {
		if ($('fpv_commentsPanelTitleRight')) {
			$('fpv_commentsPanelTitleRight').style.display = "none";
		}
	}
}

function fpv_comments_callback() {
	if (http.readyState == 4 && http.status == 200) {
		var response = http.responseText;
		if (response) {
			document.getElementById('fpv_uiComments').innerHTML = response;
		}
		else {
			document.getElementById('fpv_uiComments').innerHTML = "No Comments";
		}
		
		// get number of pages out of HTML comment <!--cNM(5)-->
		var regexpCP = /<!--cNM\((\d+)\)-->/;
		var aryMatches = regexpCP.exec(response);
		if (!aryMatches) {
			// not found...
		}
		else {
			if (!isNaN(aryMatches[1])) {
				commentsNumPages = parseInt(aryMatches[1]);
			}
		}
		
		fpv_BuildCommentPaging(commentsNumPages, commentsCurrentPage);
	}
}


function fpv_SendComments(objButton) {
	// validate fields
	if (fpv_ValidateComments()) {
		document.body.style.cursor = "wait";
		objButton.disabled = true;
		
		var sendingColor = fpv_rgb2hex(fpv_getStyleBySelector('.fpv_commMessagePanelSendingColor').color);
		fpv_ShowInlineMessage("fpv_commentMessage", "fpv_strCommentMessageContents", comMsgSending, sendingColor, true);
		
		var url = SERVICES_BASE + "commenting/in/commentingwsdl.php";
		var pl = new SOAPClientParameters();
		
		if (document.frmComments.hdnPostID) {
			pl.add("entry_id", parseInt(document.frmComments.hdnPostID.value));
		}
		else if (document.frmComments.hdnDynamicPostID) {
			pl.add("entry_id", parseInt(document.frmComments.hdnDynamicPostID.value));
		}
		else {
			pl.add("entry_id",0);
		}
		
		pl.add("blogID", blogID);
		pl.add("objectID", videoID);
		pl.add("mtcategoryID", mtCategoryID);
		pl.add("senderName", document.frmComments.txtName.value);
		pl.add("senderEmail", document.frmComments.txtEmail.value);
		pl.add("authorurl", '');
		pl.add("commenttext", document.frmComments.txtComments.value);
		
		var r = SOAPClient.invoke(url, "addComment", pl, true, fpv_SendComments_callback); //url of service, method, params, async (true), callback function
		
		if (document.frmComments.radRemember) {
			if (document.frmComments.radRemember[0].checked) {
				var strCookieValue = document.frmComments.txtName.value + "|" + document.frmComments.txtEmail.value;
				fpv_createCookie(comCookieName,strCookieValue,365*3);
			}
			else if (document.frmComments.radRemember[1].checked) {
				var strCookieValue = "";
				fpv_createCookie(comCookieName,strCookieValue,-1);
			}
		}
	}
	
	if (commRoundMsgCorners == "1") {
		if (!msgRound_com) {
			var roundColor = fpv_rgb2hex(fpv_getStyleBySelector('.fpv_commMessagePanel').backgroundColor);
			var roundColorBG = fpv_rgb2hex(fpv_getStyleBySelector('.fpv_commMessagePanelBG').backgroundColor);
			eval("new Rico.Effect.Round(\"div\", \"fpv_commMessagePanel\", {color:'" + roundColor + "', bgColor:'" + roundColorBG + "'})");
			msgRound_com = true;
		}
	}
}



function fpv_SendComments_callback(r) {
	var msgContents = "";
	var msgColor = "";
	
	if (!r) {
		msgContents = comMsgFailure;
		msgColor = fpv_rgb2hex(fpv_getStyleBySelector('.fpv_commMessagePanelFailureColor').color);
	}
	else if (!isNaN(r.response)) {
		document.frmComments.hdnDynamicPostID.value = r.response;
		msgContents = comMsgSuccess;
		msgColor = fpv_rgb2hex(fpv_getStyleBySelector('.fpv_commMessagePanelSuccessColor').color);
		document.frmComments.txtComments.value = "";
		fpv_LoadComments();
	}
	else {
		msgContents = r.response;
		msgColor = fpv_rgb2hex(fpv_getStyleBySelector('.fpv_commMessagePanelFailureColor').color);
	}
	
	fpv_ShowInlineMessage("fpv_commentMessage", "fpv_strCommentMessageContents", msgContents, msgColor, false);
	
	document.frmComments.btnAddComment.disabled = false;
	
	if (commRoundMsgCorners == "1") {
		if (!msgRound_com) {
			var roundColor = fpv_rgb2hex(fpv_getStyleBySelector('.fpv_commMessagePanel').backgroundColor);
			var roundColorBG = fpv_rgb2hex(fpv_getStyleBySelector('.fpv_commMessagePanelBG').backgroundColor);
			eval("new Rico.Effect.Round(\"div\", \"fpv_commMessagePanel\", {color:'" + roundColor + "', bgColor:'" + roundColorBG + "'})");
			msgRound_com = true;
		}
	}
	document.body.style.cursor = "auto";
}

function fpv_ValidateComments() {
	var isValid = true;
	var strErrorMessage = "";
	
	if (CommentingFieldYourNameRequired)
	{
		if (fpv_Trim(document.frmComments.txtName.value) == "") {
			strErrorMessage += CommentingFieldYourNameMsg + "<br>";
			isValid = false;
		}
	}
	
	if (CommentingFieldYourEmailRequired)
	{
		if (fpv_Trim(document.frmComments.txtEmail.value) == "") {
			strErrorMessage += CommentingFieldYourEmailMsg + "<br>";
			isValid = false;
		}
	}
	
	if (CommentingFieldCommentsRequired)
	{
		if (fpv_Trim(document.frmComments.txtComments.value) == "") {
			strErrorMessage += CommentingFieldCommentsMsg + "<br>";
			isValid = false;
		}
	}
	
	msgColor = fpv_rgb2hex(fpv_getStyleBySelector('.fpv_commMessagePanelFailureColor').color);
	fpv_ShowInlineMessage("fpv_commentMessage", "fpv_strCommentMessageContents", strErrorMessage, msgColor, false);

	document.frmComments.txtComments.value = fpv_QuotesFix(document.frmComments.txtComments.value); // TODO: temporary fix
	
	return isValid;
}
