var currentOrder = "ma";
var fadeSpeed = "slow";

$j(window).keydown(function(event){
	if (event.keyCode == 27){
		toggleLayer();
	}
});

function showDim() {
	if($j("#dim_layer").is(':visible')){
		$j("#dim_layer").fadeOut(fadeSpeed);
		$j("#dim_anchor").attr('class', '');
	}
	else {
		$j("#dim_layer").fadeIn(fadeSpeed);
		$j("#dim_anchor").attr('class', 'active');
	}
}

function toggleLayer( whichLayer ) {
	$j(".pop-up-box-wrapper").each(function (id, onep) {
		$j("#"+onep.id).fadeOut(fadeSpeed);
		$j("#"+onep.id+"_img").fadeOut(fadeSpeed);
		$j("#cover_layer").hide();
	});
	if(whichLayer && !$j("#"+whichLayer).is(':visible')) {
		$j("#"+whichLayer).fadeIn(fadeSpeed);
		$j("#"+whichLayer+"_img").fadeIn(fadeSpeed);
		$j("#cover_layer").show();
	}
}

function toggleLayerEmbed() {
	toggleLayer('embed');
}

var relatedsPages = {};
var relatedsUrl = "";
var relatedsVideoId = 0;

function getRelatedsPage(page){
	scrollToElement("div.container", 5);
	
	if(relatedsPages[currentOrder+page]){
		$j('#relateds_pane').html(relatedsPages[currentOrder+page]);
		markSelectedPage(page);
		return;
	}

	$j('#relateds_pane').html('<li><div class="img-loading" title="Loading..." style="margin:auto;"></div></li>');
	
	$j.get(relatedsUrl, { 'id': relatedsVideoId, 'page': page, 'order': currentOrder },
		function(response){
			relatedsPages[currentOrder+page] = response;
			$j('#relateds_pane').html(response);
			markSelectedPage(page);
	 	});
}

function changeOrder(tab, order) {
	currentOrder = order;
	markSelectedTab(tab);
	switchCommentsAndRelateds("none", "block");
	getRelatedsPage(1);
}

function markSelectedTab(tab) {
	$j("a.tabs").removeClass("active");
	$j("a."+tab).addClass("active");
}

function markSelectedPage(page) {
	$j("a.relateds-pages").removeClass("active");
	$j("#page_"+page).addClass("active");
}

function scrollToElement(anchor, speed){
	var fromScroll = jQuery(document).scrollTop();	
	var toScroll   = jQuery(anchor).offset().top;
	for(var i=fromScroll; i>toScroll; i-=speed){
		jQuery(document).scrollTop(i);
	}
}

var commentCurrentPage = 0;
var commentCurrentOrder = 0;

function markComment(commentId) {
	var answer = confirm('Are you sure you want to flag this comment?');
	if (answer)	{
		$j.post("http://"+document.domain+"/ajax/mark_comment", { "object_id": commentId }, function(result) {			
			if( result ) {
				$j("#comment_"+commentId).attr('class','flagged');
				$j("#comment_"+commentId).html("");
			} else {
				alert("Can't mark as spam");
			}
		},"json");
	}
}

function addComment(objectId) {
	if($j("input#comment_content").val().length < 3 ) {
		alert("Please enter text");		
	} else {
		$j.post("http://"+document.domain+"/ajax/add_comment", { "object_id": objectId, "content": $j("input#comment_content").val() }, function(result) {			
			if( result ) {
				$j("input#comment_content").val("");
				getComments(objectId, commentCurrentPage, commentCurrentOrder);				
			} else {
				alert("Can't add comment");
			}
		},"json");
	}
}

function getComments(objectId, page, order) {
	var currentDate = new Date();
	$j.get("http://"+document.domain+"/ajax/get_comments", { "object_id": objectId, "page": page, "order": order, "time": currentDate.getTime() }, function(result) {
		$j("#comments_pane").html(result);
	},"json");
}

function showCommentsPage(objectId, inc) {
	getComments(objectId, commentCurrentPage+inc, commentCurrentOrder);
}

function showCommentsOrder(objectId, order) {
	getComments(objectId, commentCurrentPage, order);
}

function showComments(objectId) {	
	if($j("#dim_layer").is(':visible')){
		showDim();
	}
	markSelectedTab("video-comments");
	switchCommentsAndRelateds("block", "none");
	getComments(objectId, commentCurrentPage, commentCurrentOrder);
}

function switchCommentsAndRelateds(comments, relateds) {
	$j("div.comment-wrapper").css("display", comments);
	$j("#relateds_container").css("display", relateds);
	$j("div.pags-bottom").css("display", relateds);	
}

function onEnterAddComment( evt, objectId ) {
	var keyCode = null;

	if( evt.which ) {
		keyCode = evt.which;
	} else if( evt.keyCode ) {
		keyCode = evt.keyCode;
	}
	if( 13 == keyCode ) {
		addComment(objectId);
		return false;
	}
	return true;
}