/**
 * @author - deepakb
 * @created on - 19th Nov 2009 at 2:30 PM
 * @Purpose - For handling video detail page functioanlity
 */

/**
 * Function for refreshing the comment list
 * @param baseUrl - Pass the URL
 * @param videoId - Pass the video id
 * @return - Refresh the comment list in video detail page
 */
function refreshCommentList(baseUrl, videoId){
	$('#comment').attr('value', '');
	$('#loading').hide();
	$.post(baseUrl+'/ajax/refreshcommentlist',
    {
    	type:'total',
    	videoId:videoId
    },function(result){
    	var res = result.split("|");
    	if(res[0] == 1 &&  res[1] != 0){
    		$('#comment_list').show();
    		$('#no_comment').hide();
	    	var pageArr = new Array();
	    	for(var i = 0;i < res[1];i++){
	    		pageArr[i] = baseUrl + '/ajax/refreshcommentlist?pg=' + (i + 1) + '&vid=' + res[2];
	    	}
	    	var searchData = {
	    			pages: pageArr,
	    			selectedpage: 0 //set page shown by default (0=1st page)
    			}

	    	var mybookinstance=new ajaxpageclass.createBook(searchData, "comment_list", ["paginate-top", "paginate-bottom"]);
    	}else{
    		$('#comment_list').hide();
    		$('#paginate-top').hide();
    		$('#paginate-bottom').hide();
    		$('#no_comment').show();
    		$('#no_comment').html('No comments available.');
    	}
    });
}

/**
 * Function for open the Post Comment form
 * @return - Only open the form
 */
function viewAddComment(){
	$('#add_comment').show();
}

/**
 * Function for close the Post Comment form
 * @return - Only close the form
 */
function closeAddComment(){
	$('#sMsg').hide();
	$('#eMsg').hide();
	$('#comment').attr('value', '');
	$('#add_comment').hide();
	$('#comment').css('border','1px solid #83C6EE');
}

/**
 * Function for validate the Add Comment form 
 * @param baseUrl - Pass the URL
 * @return - false or True
 * If True - call postComment() function
 */
function validatePostComment(baseUrl, videoId){
	var comment = $('#comment').val();
	if(comment == ''){
		$('#comment').css('border','2px solid red');
	}else if(comment != '' && comment.length > 400){
		$('#comment').css('border','2px solid red');
	}else{
		postComment(baseUrl, comment, videoId);
	}
}

/**
 * Function for post comment for video
 * @param baseUrl - Pass the URL
 * @param comment - Pass the comment
 * @param videoId - Pass the video id
 * @return True or False
 */
function postComment(baseUrl, comment, videoId){
	$('#loading').show();
	$.post(baseUrl+'/ajax/postvideocomment',
    {
    	commentPost:comment,
    	videoId:videoId
    },function(result){
    	var res = result.split("|");
    	if(res[0] == 1){
    		$('#sMsg').show();
    		$('#sMsg').html('Comment posted successfully');
    		$('#eMsg').hide();
    		$('#comment').attr('value', '');
    		$('#add_comment').hide();
    		$('#comment').css('border','1px solid #83C6EE');
    		refreshCommentList(baseUrl, res[1]);
    	}else{
    		$('#eMsg').show();
    		$('#eMsg').html('Not able to post comment');
    		$('#sMsg').hide();
    		$('#comment').attr('value', '');
    		$('#add_comment').hide();
    		$('#comment').css('border','1px solid #83C6EE');
    		$('#comment').attr('value', '');
    	}
    });
}

/**
 * Function for delete the comment
 * @param baseUrl - Pass the URL
 * @param commentId - Pass the comment id
 * @param videoId - Pass the video id
 * @return - 1|0
 */
function deleteComment(baseUrl, commentId, videoId){
	$('#loading').show();
	
	$.post(baseUrl+'/ajax/deletevideocomment',
    {
    	commentId:commentId,
    	videoId:videoId
    },function(result){
    	var res = result.split("|");
    	if(res[0] == 1){
    		$('#sMsg').show();
    		$('#sMsg').html('Comment deleted successfully');
    		refreshCommentList(baseUrl, res[1]);
    	}else{
    		$('#loading').hide();
    		$('#eMsg').show();
    		$('#eMsg').html('Not able to delete the comment');
    	}
    });
}

/**
 * Function to show reply comment form
 * @param commentId - Pass the comment id
 * @return - Show the reply comment form
 */
function replyComment(commentId){
	$('#reply_comment_'+commentId).show();
}

/**
 * Function to hide reply comment form
 * @param commentId - Pass the comment id
 * @return - Hide the reply comment form
 */
function closeReplyComment(commentId){
	$('#sMsg').hide();
	$('#eMsg').hide();
	$('#reply_'+commentId).attr('value', '');
	$('#reply_comment_'+commentId).hide();
	$('#reply_'+commentId).css('border','1px solid #83C6EE');
}

/**
 * Function for validate the Add Comment form 
 * @param baseUrl - Pass the URL
 * @param videoId - Pass the video id
 * @param commentId - Pass the comment id
 * @return - false or True
 * If True - call ReplyComment() function
 */
function validateReplyComment(baseUrl, videoId, commentId, commentUserId){
	var comment = $('#reply_'+commentId).val();
	if(comment == ''){
		$('#reply_'+commentId).css('border','2px solid red');
	}else if(comment != '' && comment.length > 400){
		$('#reply_'+commentId).css('border','2px solid red');
	}else{
		closeReplyComment();
		sendComment(baseUrl, comment, videoId, commentUserId, commentId);
	}
}

/**
 * Function for sending reply to Fan's comment
 * @param baseUrl - Pass the URL
 * @param comment - Pass the reply notes
 * @param videoId - Pass the video id
 * @param commentUserId - Pass the user id of fan
 * @param commentId - Pass the comment id
 * @return - Success or False message
 */
function sendComment(baseUrl, comment, videoId, commentUserId, commentId){
	$('#loading_reply_'+commentId).show();
	
	$.post(baseUrl+'/ajax/replyvideocomment',
    {
    	commentPost:comment,
    	commentUserId:commentUserId,
    	commentId:commentId,
    	videoId:videoId
    },function(result){
    	var res = result.split("|");
    	if(res[0] == 1){
    		$('#reply_'+commentId).attr('value', '');
    		$('#reply_comment_'+commentId).hide();
    		$('#reply_'+commentId).css('border','1px solid #83C6EE');
    		$('#loading_reply_'+commentId).hide();
    		$('#sMsg').show();
    		$('#sMsg').html('Comment reply sent successfully');
    		viewAllComment(baseUrl, res[1]);
    	}else{
    		$('#reply_'+commentId).attr('value', '');
    		$('#reply_'+commentId).attr('value', '');
    		$('#reply_comment_'+commentId).hide();
    		$('#reply_'+commentId).css('border','1px solid #83C6EE');
    		$('#eMsg').show();
    		$('#eMsg').html('Not able to send comment reply');
    	}
    });
}

function addRateVideo(baseUrl, videoId){
	var tFactor = $('#tfHid').val();
	var eFactor = $('#efHid').val();
	if(tFactor == '' || eFactor == ''){
		$('#dragnote').css('color','red');
		$('#dragnote').css('font-weight','bold');
	}else{
		$('#dragnote').css('color','black');
		$('#dragnote').css('font-weight','normal');
		$('#rate_loader').show();
		
		$.post(baseUrl+'/ajax/addvideorating',
	    {
	    	tFactor:tFactor,
	    	eFactor:eFactor,
	    	videoId:videoId
	    },function(result){
	    	if(result == 1){
	    		$('#rate_loader').hide();
	    		$('#rStatus').show();
	    		$('#rStatus').html('Rating added successfully');
	    		$('#rStatus').css('color','green');
	    		$('#rStatus').css('font-weight','bold');
	    		$('#dragnote').hide();
	    		$('#sbtBtn').hide();
	    	}else if(result == 2){
	    		$('#rate_loader').hide();
	    		$('#rStatus').show();
	    		$('#rStatus').html('Already rated this video.');
	    		$('#rStatus').css('color','red');
	    		$('#rStatus').css('font-weight','bold');
	    	}else{
	    		$('#rate_loader').hide();
	    		$('#rStatus').show();
	    		$('#rStatus').html('Unable to rate this video.');
	    		$('#rStatus').css('color','red');
	    		$('#rStatus').css('font-weight','bold');
	    	}
	    });
	}
}

function viewAllComment(baseUrl, id){
	$('#all_comment_loading_'+id).hide();
	$('#all_comment_'+id).html('');
	$('#all_comment_loading_'+id).show();
	
	$.post(baseUrl+'/ajax/refreshreplycommentlist',
    {
    	commentId:id
    },function(result){
    	$('#all_comment_'+id).html(result);
    	$('#all_comment_loading_'+id).hide();
    	$('#all_comment_'+id).show();
    	$('#hide_all_link_'+id).show();
    	$('#view_all_link_'+id).hide();	
    });
}

function hideComment(id){
	$('#all_comment_loading_'+id).hide();
	$('#all_comment_'+id).hide();
	$('#hide_all_link_'+id).hide();
	$('#view_all_link_'+id).show();	
}