
var timer, interval, tp=0, hash="";


$(document).ready(function(){
	tp=$("#topPanel").length;
	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(){
	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);
			});	
		}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);
			});	
		}
	}
	
	$(".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);	
		}else{
			$("#scrollingCont").animate({"top":"0"}, 300);
			$("#top").slideUp(300);
			$("#illustrationBox").animate({"height": "1211px"}, 1000, initScrolling);	
			$("#ill").animate({"height": "1211px"}, 1000, initScrolling);	
		}
		$(this).toggleClass("active");
		return false;
	});
	
	
	
	initPosts();
	initTextFields();
	

	$("#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);
			});	
		}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);
		}
		return false;							   
	});
	
 initEditForm();
	
});



$(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);
		});	
	}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);
	}
}

function initMoverScroll(){
	$("#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;	
			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;
	});
	}

function initEditForm(){
	$("#ill").css("background", "url(images/ill2.jpg) no-repeat center 210px");
}

function initPosts(){
	$("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"));
		});	
	});
	
}
 