
var timer, interval, mover, tp=0, hash="", months=new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");

var current, count;

$(document).ready(function(){
	tp=$("#topPanel").length;
	if(tp) $("#mover").css("top","65px");
	var h=tp>0 ? $("#main").height()-65 : $("#main").height(); 
	$("#scrollingBox").css("height",h);
	
	maxh=(($("#scrollingBox").height()-$("#scrollingCont").height())<0) ? $("#scrollingBox").height()-$("#scrollingCont").height() : 0;
	$("#scrollingCont").attr("maxScroll", maxh);	
	initMoverScroll();						   
});

$(window).load(function(){
	$("#authForm").each(function(){
		this.reset();
	});
	$(".fieldBox span", "#topPanel").click(function(){
		$(this).hide();
		$(this).next().focus();
	});
	
	$("input:first", "#topPanel").keyup(function(){
		//alert($("input:eq(1)", "#topPanel").val());
		if($("input:eq(1)", "#topPanel").val()){
			$("span:eq(1)", "#topPanel").hide();
		}
	});
	
	$(".fieldBox input", "#topPanel").focus(function(){
		$(this).prev().hide();
	});
	
	$(".fieldBox input", "#topPanel").blur(function(){
		if(!$(this).val())
			$(this).prev().show();
	});
	
	
	tp=$("#topPanel").length;
	if(tp) $("#mover").css("top","65px");
	var h=tp>0 ? $("#main").height()-65 : $("#main").height(); 
	$("#scrollingBox").css("height",h);
	
	maxh=(($("#scrollingBox").height()-$("#scrollingCont").height())<0) ? $("#scrollingBox").height()-$("#scrollingCont").height() : 0;
	$("#scrollingCont").attr("maxScroll", maxh);	
	initMoverScroll();	
	if(document.location.hash)
		hash=document.location.hash.substring(1,document.location.hash.length);

	if(hash){
		var postY=tp>0 ? -$("#post"+hash).offset().top+65 : -$("#post"+hash).offset().top; 
		if(postY<parseInt($("#scrollingCont").attr("maxScroll"))){
			$("#scrollingCont").animate({"top": $("#scrollingCont").attr("maxScroll")+"px"}, 500, function(){
				y2= tp>0 ? ($("#main").height()-122)+65 : ($("#main").height()-57);
				$("#mover").animate({"top": y2+"px"}, 300);																								
			});	
		}else{															
			$("#scrollingCont").animate({"top": postY+"px"}, 500, function(){
				y= $("#scrollingCont").attr("maxScroll")!=0 ? parseInt($("#scrollingCont").css("top"))/$("#scrollingCont").attr("maxScroll") : 0;
				y2= tp>0 ? y*($("#main").height()-122)+65 : y*($("#main").height()-57);
				$("#mover").animate({"top": y2+"px"}, 300);																								
			});	
		}
	}
	
	$(".slide", "#illustrationBox").click(function(){
		if($(this).hasClass("active")){
			$("#scrollingCont").animate({"top":"0"}, 300);
			$("#illustrationBox").animate({"height": "210px"}, 1000, initScrolling);	
			$("#ill").animate({"height": "210px"}, 1000, initScrolling);	
			$("#top").slideDown(300);
			$("#postsList").slideDown(800, function(){
				$("#pages").css("display", "block");									   
			});
		}else{
			$("#scrollingCont").animate({"top":"0"}, 300);
			$("#top").slideUp(300);
			$("#postsList").slideUp(800, function(){
				$("#pages").css("display", "none");									   
			});
			$("#illustrationBox").animate({"height": "1211px"}, 1000, initScrolling);	
			$("#ill").animate({"height": "1211px"}, 1000, initScrolling);	
		}
		$(this).toggleClass("active");
		return false;
	});
	
	$("#tagsButton").click( function(){
		$(this).parent().css("display", "none");
		$("#tagsBox").css("display", "block");
		w=$("#tagsBox").width();
		tagsAnimation(1);
		$("#tagsList").attr("mS", -$("#tagsList").width());
		//$("#tagsList").attr("d", 5);
		
		//interval=setInterval("tagsMove()", 1);
		return false;
	});
	
	$("#tagsBox").mouseout(function(){
		timer=setTimeout("stopTags()", 1000);							 
	});
	
	$("#tagsBox").mouseover(function(){
		clearTimeout(timer);
	});
	$("a", "#tagsList").mouseover(function(){
		clearTimeout(timer);
		$("#tagsList").stop();
		tagsAnimation(14000);
		return false;
	});
	$("a", "#tagsList").mouseout(function(){
		$("#tagsList").stop();
		tagsAnimation(10000);
	});
	
	initPosts();
	initTextFields();
	
	$(".save").click(function(){
		$(this).hide();
		var form=$(this).parent().parent().parent().parent();					  
		var field1=$("input[name=author]",form).val();
		var field2=$("textarea[name=text]",form).val().replace("\n", "<br/>");
		var field3=$(".dateField",form).text();
		var field4=$(form).attr("id");
		if(field2)
			$.post("/comment",{
				author: field1,
				text: field2,
				date: field3,
				number: field4
			}, function(){
				$("a.comment.active", "#postsList").removeClass("active");
				$("span",$(form).prev()).text(field1);
				$(form).next().html(field2);
				$(".authorComment", $(form)).hide();
				$(".postComment", $(form)).hide();
				$(form).prev().show();
				$(form).next().show();
				initScrolling();
			});	
		return false;
	});
	
	$("#newPostButton").click(function(){
		$("#addBox").toggle();
		maxh=($("#scrollingBox").height()-$("#scrollingCont").height()>0) ? 0 : $("#scrollingBox").height()-$("#scrollingCont").height();
		$("#scrollingCont").attr("maxScroll", maxh);
		if($("#scrollingCont").position().top<parseInt($("#scrollingCont").attr("maxScroll"))){
			$("#scrollingCont").animate({"top": $("#scrollingCont").attr("maxScroll")+"px"}, 1000, function(){
				y= $("#scrollingCont").attr("maxScroll")!=0 ? parseInt($("#scrollingCont").css("top"))/$("#scrollingCont").attr("maxScroll") : 0;
				y2= tp>0 ? y*($("#main").height()-122)+65 : y*($("#main").height()-57);
				$("#mover").animate({"top": y2+"px"}, 300);																								
			});	
		}else{
			y= $("#scrollingCont").attr("maxScroll")!=0 ? parseInt($("#scrollingCont").css("top"))/$("#scrollingCont").attr("maxScroll") : 0;
			y2= tp>0 ? y*($("#main").height()-122)+65 : y*($("#main").height()-57);
			$("#mover").animate({"top": y2+"px"}, 300);																								
		}
		return false;							   
	});
	
	$("#addForm").submit(function(){
		$("input[name=date]", $(this)).val($("span", "#hour").text()+" "+$("span", "#day").text()+" "+$("span", "#month").attr("id")+" "+$("span", "#year").text());					   
		if(!$("input[name=subject]", $(this)).val() || !$("input[name=author]", $(this)).val())
			return false;
	});
	
	initEditForm();
	
	/*current=parseInt($("#current").text());
	count=parseInt($("#count").text());
	tag=parseInt($("#tag").text());
	*/
	$("#prevPage a").click(function(){
		$("#postsList li").slideUp("normal");
		href=$(this).attr("href");
		setTimeout("redirect('"+href+"')", 800);
		return false;
	});
	$("#nextPage a").click(function(){
		$("#postsList li").slideUp("normal");
		href=$(this).attr("href");
		setTimeout("redirect('"+href+"')", 800);
		return false;
	});
});

function redirect(href){
	location.href=href;
}

$(window).resize(function(){
	var h=$("#topPanel").length>0 ? $("#main").height()-65 : $("#main").height(); 
	$("#scrollingBox").css("height",h);					  
	maxh=($("#scrollingBox").height()-$("#scrollingCont").height()>0) ? 0 : $("#scrollingBox").height()-$("#scrollingCont").height();
	$("#scrollingCont").attr("maxScroll", maxh);
});

function initScrolling(){
	maxh=($("#scrollingBox").height()-$("#scrollingCont").height()>0) ? 0 : $("#scrollingBox").height()-$("#scrollingCont").height();
	$("#scrollingCont").attr("maxScroll", maxh);
	if($("#scrollingCont").position().top<parseInt($("#scrollingCont").attr("maxScroll"))){
		$("#scrollingCont").animate({"top": $("#scrollingCont").attr("maxScroll")+"px"}, 1000, function(){
			y= $("#scrollingCont").attr("maxScroll")!=0 ? parseInt($("#scrollingCont").css("top"))/$("#scrollingCont").attr("maxScroll") : 0;
			y2= tp>0 ? y*($("#main").height()-122)+65 : y*($("#main").height()-57);
			$("#mover").animate({"top": y2+"px"}, 100);																								
		});	
	}else{
		y= $("#scrollingCont").attr("maxScroll")!=0 ? parseInt($("#scrollingCont").css("top"))/$("#scrollingCont").attr("maxScroll") : 0;
		y2= tp>0 ? y*($("#main").height()-122)+65 : y*($("#main").height()-57);
		$("#mover").animate({"top": y2+"px"}, 100);																								
	}
}

function initMoverScroll(){
	$("#mover").mousedown(function(){
		mover=$(this);						   
		return false;
	});
	$("#main").mouseup(function(){
		if(mover){
			mover=null;						   
			return false;
		}
	});
	$(window).mouseup(function(){
		if(mover){
			mover=null;						   
			return false;
		}
	});
	$("#main").bind("mousemove",function(e){
		if(mover){
			y=e.pageY-28;
			if(y<0) y=0;
			if(tp && y<65) y=65;
			if(y>$("#main").height()-57) y=$("#main").height()-57;	
			mover.css("top", y+"px");
			y2=tp>0 ? $("#scrollingCont").attr("maxScroll")*(y-65)/($("#main").height()-122) : $("#scrollingCont").attr("maxScroll")*y/($("#main").height()-57);
			$("#scrollingCont").css("top", y2+"px");
			return false;
		}
	});
	$("#scrollingCont").bind("mousewheel",function(event, delta) {
		y=parseInt($(this).css("top"));
		//alert(delta);
		if(delta>0)
			if((y+delta)>0)
				$("#scrollingCont").css("top", 0);
			else
				$("#scrollingCont").css("top", (y+delta)+"px");
		else
			if((y+delta)<parseInt($("#scrollingCont").attr("maxScroll")))
				$("#scrollingCont").css("top", $("#scrollingCont").attr("maxScroll")+"px");
			else
				$("#scrollingCont").css("top", (y+delta)+"px");
		y= $("#scrollingCont").attr("maxScroll")!=0 ? parseInt($("#scrollingCont").css("top"))/$("#scrollingCont").attr("maxScroll") : 0;
		y2= tp>0 ? y*($("#main").height()-122)+65 : y*($("#main").height()-57);
		$("#mover").css("top", y2+"px");
		event.stopPropagation();
		event.preventDefault();
		return false;
	});
}

function addfile(file){
	var parentFile=file.parent().parent().parent();
	parentFile.clone(true).appendTo($("#imagesBox"));
	if(navigator.platform=="Win32"){
		var index=0;
		for(var i=file.val().length; i>0; i--) if (file.val()[i]=='\\'){ index=i; break;}
		$("a", parentFile).text(file.val().substring(index+1,file.val().length));
	}else
		$("a", parentFile).text(file.val());
	file.parent().css("display", "none");
	file.parent().next().css("display", "block");
	
	$(".number",".imgBox:last").text(parseInt($(".number",".imgBox:last").text())+1);
	$("input",".imgBox:last").attr("name", "imgs[" + (parseInt($(".number",".imgBox:last").text())+1) + "]");	
	$("input",".imgBox:last").val("");	
}

function tagsAnimation(time){
	$("#tagsList").animate({"left": $("#tagsList").attr("mS")+"px"}, time, "linear", function(){
		$("#tagsList").css("left", "100%");
		tagsAnimation(10000);
	});
}

function stopTags(){
	$("#titles").show();
	$("#tagsBox").hide();
	$("#tagsList").stop();
	$("#tagsList").css("left", "100%");
}

function initEditForm(){
	$("a", "#imagesBox").click(function(){
		$(this).parent().parent().remove();
		$(".number").each(function(){
			$(this).text(parseInt($(this).text())-1);
		});
		return false;									
	});
	$("input", "#imagesBox").change(function(){
		addfile($(this));
	});
	
	$(".pointD", "#hour").click(function(){
		if($(this).next().text()!=1)
			$(this).next().text($(this).next().text()-1);
		else
			$(this).next().text(24);
		return false;
	});
	$(".pointU", "#hour").click(function(){
		if($(this).prev().text()!=24)
			$(this).prev().text($(this).prev().text()-(-1));
		else
			$(this).next().text(1);
		return false;
	});
	
	$(".pointD", "#day").click(function(){
		if($(this).next().text()!=1)
			$(this).next().text($(this).next().text()-1);
		else
			$(this).next().text(31);
		return false;
	});
	$(".pointU", "#day").click(function(){
		if($(this).prev().text()!=31)
			$(this).prev().text($(this).prev().text()-(-1));
		else
			$(this).next().text(1);
		return false;
	});
	
	$(".pointD", "#month").click(function(){
		if($(this).next().attr("id")!=1)
			$(this).next().attr("id", $(this).next().attr("id")-1);
		else
			$(this).next().attr("id", 12);
		$(this).next().text(months[$(this).next().attr("id")-1]);
		return false;
	});
	$(".pointU", "#month").click(function(){
		if($(this).prev().attr("id")!=12)
			$(this).prev().attr("id",$(this).prev().attr("id")-(-1));
		else
			$(this).prev().attr("id",1);
		$(this).prev().text(months[$(this).prev().attr("id")-1]);
		return false;
	});
	
	$(".pointD", "#year").click(function(){
		$(this).next().text($(this).next().text()-1);
		return false;
	});
	$(".pointU", "#year").click(function(){
		$(this).prev().text($(this).prev().text()-(-1));
		return false;
	});
	$("#addDescription").click(function(){
		$("#editTextField").val($("#editTextField").val()+'<div class="descriptionBox"><div><span style="color:#393834; background:#ffae00; font-size:10px;"> text </span></div><img 1></div>');
		return false;								
	});
	$(".delButton").click(function(){
		$(this).parent().parent().attr("action", "/creation/del");						   
	});
	$("#editForm").submit(function(){
		if($(this).attr("action")!="/creation/del"){
			$("input[name=date]", $(this)).val($("span", "#hour").text()+" "+$("span", "#day").text()+" "+$("span", "#month").attr("id")+" "+$("span", "#year").text());					   
			if(!$("input[name=author]", $(this)).val())
				return false;
		}
	});
	$("#ill").css("background", "url(../images/ill2.jpg) no-repeat center 210px");
}

function initPosts(){
	$("a.slide", "#postsList").live("click",function(){
		if($(this).next().hasClass("a2")){
			$(this).next().toggleClass("a2");
			$(".postCont", $(this).parent().parent()).toggle();
			$(".postComment", $(this).parent().parent()).toggle();
			$(".author", $(this).parent().parent()).toggle();
			$(".authorComment", $(this).parent().parent()).toggle();
		}
		$(this).toggleClass("a1");
		$(".postCont", $(this).parent().parent()).slideToggle("normal", function(){
			if($("a.slide", $(this).parent().parent()).attr("title")=="HIDE"){
				$("a.slide", $(this).parent().parent()).attr("title", "TURN");	
				$("a.slide img", $(this).parent().parent()).attr("alt", "TURN");	
			}else{
				$("a.slide", $(this).parent().parent()).attr("title", "HIDE");	
				$("a.slide img", $(this).parent().parent()).attr("alt", "HIDE");	
			}
			initScrolling();
		});
		return false;
	});
	$("a.comment", "#postsList").live("click",function(){
		if($(this).prev().hasClass("a1")){
			$(this).prev().toggleClass("a1");
			$(".postCont", $(this).parent().parent()).toggle();
		}
		if(!$(this).hasClass("a2")){
			$("a.comment.a2", "#postsList").each(function(){
				$(this).toggleClass("active");
				$(".postCont", $(this).parent().parent()).toggle();
				$(".postComment", $(this).parent().parent()).toggle();
				$(".author", $(this).parent().parent()).toggle();
				$(".authorComment", $(this).parent().parent()).toggle();												  
			});		
		}
		$(this).toggleClass("a2");
		$(".postCont", $(this).parent().parent()).toggle();
		$(".postComment", $(this).parent().parent()).toggle();
		$(".author", $(this).parent().parent()).toggle();
		$(".authorComment", $(this).parent().parent()).toggle();
		initScrolling();
		return false;
	});
	$("a.edit", "#postsList").live("click",function(){
		var post=$(this).parent().parent();
		var postCont=post.html();
		var postId=$(this).attr("id");
		$.post("/form", {id: postId}, function(data){
			post.html(data);
			$("#addBox").remove();
			initEditForm();
			initScrolling();
		});
		return false;
	});
	
	$("textarea", ".postComment").live("keypress",function(){
		if($(this).val().length>3)
			$(this).next().css("display","block");
		else
			$(this).next().css("display","none");
	});
	$(".descriptionBox").mouseover(function(){
		$("div", this).show();									 
	});
	$(".descriptionBox").mouseout(function(){
		$("div", this).hide();									 
	});
}
function initTextFields(){
	$(".textField").each(function(){
		$(this).attr("text", $(this).val());
		$(this).bind("focus",function(){
			if(!$(this).attr("text")) $(this).attr("text", $(this).val());							  
			if($(this).val()==$(this).attr("text"))
				$(this).val("");
		});
		$(this).bind("blur",function(){
			if(!$(this).val())
				$(this).val($(this).attr("text"));
		});	
	});
	$("span", ".editBox").each(function(){
		$(this).click(function(){			
			$(this).attr("text", $(this).text());
			$(this).text("fuck you");
			$(this).addClass("fuck");
			$(this).fadeOut(1000, function(){
				$(this).removeClass("fuck");
				$(this).text($(this).attr("text"));
				$(this).fadeIn(100);
			});
		});
	});
}
 