/**
 * @author Lyov
 * @site 	 http://naghashyan.com
 * @email  levon@naghashyan.com	
 * @year   2009-2010
 **/
var comment = "";
var videoId;
var alias;
var commentBut;
var fbStatusUpdateCheck = 0;
function saveComment(){
  comment = document.getElementById("t_comment").value;
  
  if (!comment) {
  	
    document.getElementById("comment_err").style.display = "block";
    return false;
  }
  
  if(loginMethod == "fb"){
	
		if(!fbStatusUpdateCheck){
					  fbStatusCheck();
						return false;
		}
	}

	fbStatusUpdateCheck = 0;
  
  document.getElementById("comment_err").style.display = "none";
  //commentBut.style.display = "none";
  document.getElementById("comentLoader").style.display = "block";
  evalProgress = addComment;
  videoId = document.getElementById("f_videoId").value;
  alias = document.getElementById("f_alias").value;
  var params = "videoId=" + videoId + "&alias=" + alias + "&comment=" + comment;
  makeRequest('showvideo/do_savecomment', evalProgress, params);
}

function addComment(httpRequest){
	
  if (httpRequest.readyState == 4) {
		if (httpRequest.status == 200) {
			eval('var data = ' + httpRequest.responseText);
		
			if (data.save) {
				document.getElementById("div_chars_left").innerHTML = "110";
				
				var commentsBlock = document.getElementById("f_commentOl");
				var userPage = static_path + '/go/user/' + data.screen_name;
				
				var userComment = '<li class="comment">	<span class="user_icon"><a href="' + userPage + '">';
				userComment += '<img src="' + data.avatar + '" alt="user icon" /></a></span>';
				userComment += '<a href="' + userPage + '"><p class="username">' + data.screen_name + '</p></a><p class="user_comments">' + data.comment + '</p>	</li>';
				commentsBlock.innerHTML = userComment+commentsBlock.innerHTML;
				document.getElementById("t_comment").value = "";
				var commentCountBox = document.getElementById("commentCount");
				var commentCount = parseInt(document.getElementById("f_commentCount").value);
				commentCount = commentCount+1;
				commentCountBox.innerHTML = "("+commentCount+")";
				document.getElementById("f_commentCount").value = commentCount;
				
				document.getElementById("comentLoader").style.display = "none";
				//commentBut.style.display = "block";
				
			}
		}	
	}
}



function validate(obj) {
	var mlength = obj.getAttribute ? parseInt(obj.getAttribute("maxlength")) : "";

	if (obj.getAttribute && obj.value.length > mlength) {
		obj.value = obj.value.substring(0, mlength);
	}

	if (obj.value.length > 0) {
		document.getElementById('commentBut').removeAttribute('disabled');
	} else {
		document.getElementById('commentBut').setAttribute('disabled',	'disabled');
	}

	document.getElementById('div_chars_left').innerHTML = mlength	- obj.value.length;
}

function fbStatusCheck(){

		 params = 0;
		 makeRequest('go/fblogin/do_check_status/', evalFbStatusCheck, params);

}
function evalFbStatusCheck(httpRequest) {
	if (httpRequest.readyState == 4) {
		if (httpRequest.status == 200) {
			eval('var data = ' + httpRequest.responseText)
		
				if (data.status_check == 1) {
	        	fbStatusUpdateCheck = 1;

						saveComment();
	        }
	        else {
	        	fbc_show_status_update_permission_dialog_comment();
	        	//location.reload(true);
	        }
				
				
		
	        
		}
	}
}
