var base_url = '/';
var isfavorited = false;

function processFavourites(videoId)
{
	var remove = isfavorited ? 1 : 0;

	$j.ajax({
		type: "POST",
		cache: false,
		data: { video_id: videoId, remove : remove },
		url: base_url + 'ajax/processFavourites.php',
		success: function(response){
			if (response != 'error')
			{
				if(remove)
				{
					isfavorited = false;
					document.getElementById('favouriteLink').className = "btn-03 main-sprite-img";
				}
				else
				{
					isfavorited = true;
					document.getElementById('favouriteLink').className = "btn-04 main-sprite-img";
				}
			}
			else
			{
				alert('Error occured. Please reload this page and try again.');
			}
		}
	});
}

$j(document).ready(function(){
	processCommentsPagination();
});

function processCommentsPagination()
{
	$j('.links-comments-pagination').live('click', function(){
		var num = $j(this).attr('page');
		if(document.getElementById('commentspage' + num))
		{
			$j('.comments-holder').hide();
			$j('#commentspage' + num).show();
			$j('ul.comments-pagination > li').removeClass('active-pag');
			$j('#commentspagination' + num).addClass('active-pag');

			var prev = num - 1;
			var next = ++num;

			$j('#commentspaginationprev').attr('page', prev);
			$j('#commentspaginationnext').attr('page', next);
		}
	});
}

function appendComment () {

	var commentText = $j('#comment_textarea').val();
	var videoId     = $j('#params_video_id').val();

	if (!commentText) {
		return;
	}

	// set the comment text to null
	$j('#comment_textarea').val('');
	$j('#comments').hide();
	$j('#loading').show();

	$j.ajax({
		type: "POST",
		dataType: 'json',
		cache: false,
		data: { comment: commentText, video_id : videoId, hash: hash },
		url: base_url + 'ajax/processComment.php',

		success: function(response){
			if (response.status != 'undefined' && response.status == 'ok' && response.html != 'undefined')
			{
				$j('#comments').html(response.html);
				processCommentsPagination();
			}
			else
			{
				alert('Error occured');
			}
			$j('#comments').show();
			$j('#loading').hide();
		},
		error: function()
		{
			alert('Server error occured');
			$j('#comments').show();
			$j('#loading').hide();
		}
	});
}

function reportComment (video_id) {
	$j.ajax({
		type: "POST",
		cache: false,
		data: { video_id : video_id  },
		url: base_url + 'ajax/reportComment.php',

		success: function(response){
			if (response != 'error') {
				$j('#report_' + video_id).html('Reported');
			}
		}
	});
}
