$(document).ready(function() {
	$("#searchInput").attr("autocomplete", "off");
	$("#searchInputFull").attr("autocomplete", "off");
});

$("#flagContainer img").live("click", function() {
	var newLang = $(this).attr("alt");
	$.ajax({
		url: baseUrl + 'ajax/' + newLang + '/',
        type: "post",
        success: function (breadcrumb) {
			window.location = currentLocation.replace(/\/[a-z]{2}\//, '/' + newLang + '/')
        }
    });
	
});

/*
$("input#loginName").live("click", function() {
	$(this).val("");
});
*/

$('input#loginName').live("keyup", function(e){
	e.preventDefault();
	if (e.which==13 || e.keyCode==9) {
		e.preventDefault();
		if ($('input#loginPass').size() == 0) {
			$(this).after('<br/><input id="loginPass" type="password" value="" />');
		}
		$('input#loginPass').focus();
	}
});

$('input#loginPass').live("keypress", function(e){
	
	if (e.which==13) {
		$.ajax({
			url: baseUrl + 'ajax/login/true/',
			type: "post",
			data: "id=" + $('input#loginName').val() + "&pass=" + $('input#loginPass').val(),
			success: function (data) {
				if ((data.substr(0,2))=="Ok") {
					$("div#signContainer").html("<b>" + data.substr(2) + '</b><br/><span id="buttonLogout" class="textButton">Sign Out!</span>');
					$("ul#topMenuList").append('<li id="adminLink"><a href="'+baseUrl+'admin">Admin</a></li>');
				} else if (data=="Error") {
					$("div#signContainer input").addClass("error");
				}
			},
			error: function (data) { }
		});
	}
});

$("#buttonLogout").live("click", function() {
	$.ajax({
		url: baseUrl + 'ajax/logout/true/',
		type: "post",
		success: function (data) {
			$("div#signContainer").html('<input id="loginName" type="text" value="" />');
			$("ul#topMenuList li#adminLink").remove();
		}
	});
});

$("#searchInput, #searchFieldsContainer").live("click", function(el) {
	if (el.target.className != "searchInputButton") {
		$("#searchTermContainer").fadeIn();
		$("body").unbind("click").bind("click", function(e) {
			if(!(e.target.className == "searchCheckTerm" || e.target.className == "searchTermContainer" || e.target.className == "searchInputField" || e.target.className == "searchInputContainer" || e.target.className == "searchInputButton")) {
				$("#searchTermContainer").fadeOut();
				$("body").unbind("click");
			}
		});
	}
});

$("#searchInputButton").live("click", function(e){
	startSearch();
});

$("#searchInputButtonFull").live("click", function(e){
	startSearchFull();
});

$("#searchInput").live("keyup", function(e){
	if(e.keyCode == 13) {
		startSearch();
	} else {
		if ($(this).val()!="") {
			$("#searchInputButton").fadeIn();
		} else {
			$("#searchInputButton").fadeOut();
		}
	}
});

$("#searchInputFull").live("keyup", function(e){
	if(e.keyCode == 13) {
		//startSearchFull();
	} else {
		if ($(this).val()!="") {
			$("#searchInputButtonFull").fadeIn();
		} else {
			$("#searchInputButtonFull").fadeOut();
		}
	}
});

function startSearch() {
	if ($("#searchInput").val()!="") {
		$("#searchFormLeft").attr("action", baseUrl + currentLang + "/search/string/" + $("#searchInput").val() + "/").submit();
	}
}

function startSearchFull() {
	if ($("#searchInputFull").val()!="") {
		
		$("#searchStringHolder").html($("#searchInputFull").val());
		$("#searchResultHolder").fadeIn();
		$("#searchIndicator").fadeIn(500);
		$("#searchResultHolder div.entry").fadeTo(500, 0.5);
		
		$.ajax({
			url: baseUrl + currentLang + '/search/string/' + $("#searchInputFull").val() + '/',
			type: "post",
			data: $("#searchFormFull").serialize(),
			success: function(data) {
				$("#searchResultHolder div.entry").html(data);
				$("#searchResultHolder div.entry").fadeTo(100, 1);
				$("#searchIndicator").fadeOut(500);
			}
		});
	}
}

$("span.likeButtonDislike").live("click", function() {
	var wasThis = $(this);
	var commentId = $(wasThis).parents("div.commentButtons").attr("id").match(/\d+$/);
	$.ajax({
		url: baseUrl + 'ajax/like/' + commentId + '/',
		success: function(data) {
			$("#commentUserLikeCell" + commentId).fadeIn();
			var newLikeNumber = $("#commentUserLikeCell" + commentId + " span.userLikeNumber").html() * 1 + 1;
			$("#commentUserLikeCell" + commentId + " span.userLikeNumber").html(newLikeNumber);
			$(wasThis).children("img").attr("src", baseUrl + "gfx/icons/del.png");
			$(wasThis).children("span.buttonText").html(dislikeText);
			$(wasThis).removeClass("likeButtonDislike").addClass("likeButtonLike");
		}
	});
});

$("span.likeButtonLike").live("click", function() {
	var wasThis = $(this);
	var commentId = $(wasThis).parents("div.commentButtons").attr("id").match(/\d+$/);
	$.ajax({
		url: baseUrl + 'ajax/dislike/' + commentId + '/',
		success: function(data) {
		var newLikeNumber = $("#commentUserLikeCell" + commentId + " span.userLikeNumber").html() * 1 - 1;
		if (newLikeNumber < 1) {
			newLikeNumber = 0;
			$("#commentUserLikeCell" + commentId).fadeOut();	
		}
		$("#commentUserLikeCell" + commentId + " span.userLikeNumber").html(newLikeNumber);
		$(wasThis).children("img").attr("src", baseUrl + "gfx/icons/Favorite.png");
		$(wasThis).children("span.buttonText").html(likeText);
		$(wasThis).removeClass("likeButtonLike").addClass("likeButtonDislike");
		}
	});
});

$("span.replyButton").live("click", function() {
	var replyData = $(this).attr("rel").split("||");
	openCommentBox(replyData[0], replyData[1]);
});

$("span.newCommentButton").live("click", function() {
	openCommentBox($(this).attr("rel"), "0");
});

function openCommentBox(object_id, parent) {
		
		$(window).bind("resize",function(){
			$("#lightBox").css("width",$(document).width()).css("height",$(document).height());
		});
		
		$("body").append('<div id="lightBox" class="lightbox" style="width:'+$(document).width()+'px; height:'+$(document).height()+'px;"></div>');
		$("#lightBox").css({'opacity':'0'}).animate({'opacity':'0.6'}, 1000);
		$("#divCommentBox").load(baseUrl + "ajax/comment/" + object_id + "/parent/" + parent + "/", function() {
			$(this).css({top:(($(window).height() / 2) - ($(this).height() / 2) + $(window).scrollTop())+"px", left:(($(window).width() / 2) - ($(this).width() / 2))+"px"});
			$(this).fadeIn(1000);
		}).removeClass("none");
		
}

function closeCommentBox() {
    $("#divCommentBox").fadeOut(1000);
}

$("#commentSubmitButton").live("click", function() {
	if ($("#commentNick").val().length > 0) {
		var commentText = tinyMCE.get("commentText");
		var commentId = $("#parameterHolder #commentId").val();
		var parentId = $("#parameterHolder #commentParent").val();
				
		if ($("div#comment" + parentId).attr("rel")==null) {
			var parentLevel = -1;
			if ($("div.comment div.entry0, div.comment div.entry1, div.comment div.entry2").size()==0) {
				var lastId = 0;
			} else {
				var lastId = $("div.comment div.entry0, div.comment div.entry1, div.comment div.entry2").last().attr("id").match(/\d+$/);
			}
		} else {
			var relArray = $("div#comment" + parentId).attr("rel").split("||");
			var parentLevel = relArray[0];
			var lastId = relArray[1];
		}
		
		$.ajax({
			url: baseUrl + 'ajax/comment/' + commentId + '/parent/' + parentId + '/level/' + parentLevel + '/last/'+lastId+'/',
			type: "post",
			data: "nick=" + $("#commentNick").val() + "&" + "email=" + $("#commentEmail").val() + "&" + "comment=" + commentText.getContent(),
			success: function (data) {
				$("div#comment" + lastId).after(data);
				var parentHeight = $("div#comment" + lastId).height() + 20;
				$.scrollTo("div#comment" + lastId, 750, { offset:{ top: parentHeight - $(window).height()/3} } );
				closeLightBox();
			}
		});

	} else {
		$("#forgotName").removeClass("none");
	}
});

$("#lightBox, #commentCloseButton, div#divInfoBox img.closeInfoBox").live("click", function(){
	closeLightBox();
});

function closeLightBox() {
	$("#lightBox").animate({'opacity':'0'}, 1000, function(){$(this).remove();});
	closeCommentBox();
	closeInfoBox();
	$(window).unbind("resize");
}

$(".rssLink").live("click", function(){

	var rssData = $(this).attr("alt").split("||");
	var feed = "";
	if (typeof(rssData[1])!="undefined") {
		feed = rssData[1] + "/";
	}
	var identifier = "";
	if (typeof(rssData[2])!="undefined") {
		identifier = rssData[2] + "/";
	}
	document.location = baseUrl + currentLang + "/rss/" + feed + identifier;
	
});

function openInfoBox(infoPage, clb) {

		var callbackFnk = clb;
		
		$(window).bind("resize",function(){
			$("#lightBox").css("width",$(document).width()).css("height",$(document).height());
		});
		
		$("body").append('<div id="lightBox" class="lightbox" style="width:'+$(document).width()+'px; height:'+$(document).height()+'px;"></div>');
		$("#lightBox").css({'opacity':'0'}).animate({'opacity':'0.6'}, 1000);
		$("#divInfoBox").load(baseUrl + infoPage, function() {
			$(this).css({top:(($(window).height() / 2) - ($(this).height() / 2) + $(window).scrollTop())+"px", left:(($(window).width() / 2) - ($(this).width() / 2))+"px"});
			$(this).fadeIn(1000, function(){
				if	(typeof callbackFnk == 'function'){
					callbackFnk.call();
				}
			});
		}).removeClass("none");
		
}

function closeInfoBox() {
    $("#divInfoBox").fadeOut(1000);
}
