var $mover=null,$mover2=null,$mover3=null, $list=null, rightHeight, commentsY, timer, gramma=0, maxT;
$(function(){
	$("form").each(function(){
		this.reset();							   
	});
	$("img","#menu").each(function(){
		$(this).width($(this).parent().width()+24);						   
	});
	if($("#langs").length)$("#langs").init_langs();
	$("#toFavor").live("click", function(){
		var url = window.document.location;
		var title = window.document.title;
		if($.browser.msie)
			window.external.AddFavorite(url,title)
		if($.browser.opera){
			$(this).attr("href",url); 
			$(this).attr("rel","sidebar");
			$(this).attr("title", url+','+title);	
			return true;
		}
		if($.browser.mozilla)
			window.sidebar.addPanel(title,url,"");
		if($.browser.safari)
			alert("Please press Command+D (Mac)\nPlease press Ctrl+D (Win)");
		return false;
	});
	$(".forClear").each(function(){$this=$(this);if(!$this.attr("val")) $this.attr("val",$this.val());});
	$(".forClear").live("focus",function(){$this=$(this);if(!$this.attr("val"))$this.attr("val",$this.val());if($this.val()==$this.attr("val"))$this.val('');$this.addClass("focused");});
	$(".forClear").live("blur",function(){$this=$(this);if(!$this.val()){$this.val($this.attr("val"));$this.removeClass("focused");}});
	$("#commentFields p").live("click", function(){
		$(this).next().focus();										 
	});
	$(".forClear2").each(function(){
		$(this).focus(function(){
			$(this).prev().fadeOut("fast");
		});
		$(this).blur(function(){
			if(!$(this).val())
				$(this).prev().fadeIn("fast");
		});
	});
	$(".forClearLink").each(function(){
		$(this).attr("val",$(this).val());
		$(this).blur(function(){
			if(!$(this).val())
				$(this).val($(this).attr("val"));
		});
	});
	$(".iconsMenu1 a").live("mouseover", function(){
		$(".iconsMenu1 a").removeClass("active");											  
		$(".iconsMenu2 a").removeClass("active");											  
	});
	$(".iconsMenu2 a").live("mouseover", function(){
		$(".iconsMenu1 a").removeClass("active");											  
		$(".iconsMenu2 a").removeClass("active");											  
	});
	$(".postsList .img").hover(
		function(){
			$("h1", $(this).parents("li:first")).addClass("active");
		},
		function(){
			$("h1", $(this).parents("li:first")).removeClass("active");
		}
	);
	$(".postsList .titleBox").hover(
		function(){
			$("h1",this).addClass("active");
		},
		function(){
			$("h1",this).removeClass("active");
		}
	);
	$("#addNew").live("click", function(){
		$("#submitForm").slideToggle();								
		return false;
	});
	
	$("#forSubmit").live("click", function(){
		if($("#submitForm").length)
			$("#submitForm").slideToggle();								
		else
			location.href="/#submit";
		return false;
	});
	if(location.hash=="#submit")
		$("#forSubmit").click();	
	
	$("#file").live("change", function(){
		$("#textUpload").val($(this).val());							   
	});
	$("#newsForm").validate();
	$("#newsForm").ajaxForm(function(data){
		location.reload();
	});
	$("#newsSubmit").live("click", function(){
		$(this).attr("disabled","disabled");									
	});
	
	$(".stars").mousemove(function(e){
		x=Math.ceil((e.pageX-$(this).offset().left)/13);							   
		$("div", this).width(x*13);
		return false;
	}).hover(
		function(){ $(this).attr("mark", $("div", this).width()/13); },
		function(){ $("div", this).width($(this).attr("mark")*13); }
	).click(function(e){
		var $this=$(this);
		x=Math.ceil((e.pageX-$this.offset().left)/13);							   
		$this.attr("mark", x);
		id=$this.attr("id");
		if(id)
			$.post("/ajax/voting/"+id+"/"+x+"/", function(data){
				if(data)$this.parent().find("em:first").text(data);
			});
	});
	
	$("#backInTop").live("click", function(){
		$(window).scrollTop(0);
		return false;
	});
	$("#mover").mousedown(function(e){
		$mover=$(this);
		$list=$(".list",$(this).parent().prev());
		var x=e.pageX;
		$mover.attr("x", $mover.parent().offset().left+x-$mover.offset().left);
		$mover.attr("cW", $list.parent().width()-$list.width());
		$mover.attr("maxX", $mover.parent().width()-$mover.width());
		return false;
	});
	$("#mover2").mousedown(function(e){
		$mover=$(this);
		$list=$(".list",$(this).parent().prev());
		var x=e.pageX;
		$mover.attr("x", $mover.parent().offset().left+x-$mover.offset().left);
		$mover.attr("cW", $list.parent().width()-$list.width());
		$mover.attr("maxX", $mover.parent().width()-$mover.width());
		return false;
	});
	$("#interviewsList").width($("#interviewsList li").length*240);
	$("#mover4").mousedown(function(e){
		$mover3=$(this);
		$list=$("#interviewsList");
		$mover3.data("count", $("li",$list).length-1);
		var x=e.pageX;
		$mover3.data("x", $mover3.parent().offset().left+x-$mover3.offset().left);
		$mover3.data("cW", $list.parent().width()-$list.width());
		$mover3.data("maxX", $mover3.parent().width()-$mover3.width());
		if(!$mover3.data("p"))
			$mover3.data("p",1)
		return false;
	});
	$("#bestList").width($("#bestList li").length*240);
	$("#mover3").mousedown(function(e){
		$mover2=$(this);
		$list=$("#bestList");
		$list.attr("count", $("li",$list).length-1);
		var x=e.pageX;
		$mover2.attr("x", $mover2.parent().offset().left+x-$mover2.offset().left);
		$mover2.attr("cW", $list.parent().width()-$list.width());
		$mover2.attr("maxX", $mover2.parent().width()-$mover2.width());
		if(!$mover2.attr("p"))
			$mover2.attr("p",1)
		return false;
	});
	$("#main").mousemove(function(e){
		if($mover){
			x=e.pageX;
			mX=x-$mover.attr("x");
			if(mX<0) mX=0;
			if(mX>$mover.attr("maxX")) mX=$mover.attr("maxX");
			$mover.css("left",mX+"px");
			$list.css("left", mX/$mover.attr("maxX")*$mover.attr("cW")+"px");
			return false;
		}
		if($mover2){
			x=e.pageX;
			mX=x-$mover2.attr("x");
			if(mX<0) mX=0;
			if(mX>$mover2.attr("maxX")) mX=$mover2.attr("maxX");
			$mover2.css("left",mX+"px");
			cell=$mover2.attr("maxX")/$list.attr("count");
			i=Math.floor(mX/cell);
			if($mover2.attr("p")!=(i+1)){
				$mover2.attr("p",i+1);
				$list.stop().animate({left:-i*240+'px'});
				$obj=$("img:eq("+i+")",$list);
				$("#bestMonth").text($obj.attr("id"));
				params=$obj.attr("class").split("|");
				$("#bestMonthBox").stop().animate({backgroundColor:params[1],color:params[0]});
			}
			return false;
		}
		if($mover3){
			x=e.pageX;
			mX=x-$mover3.data("x");
			if(mX<0) mX=0;
			if(mX>$mover3.data("maxX")) mX=$mover3.data("maxX");
			$mover3.css("left",mX+"px");
			cell=$mover3.data("maxX")/$mover3.data("count");
			i=Math.floor(mX/cell);
			if($mover3.data("p")!=(i+1)){
				$mover3.data("p",i+1);
				$list.stop().animate({left:-i*240+'px'});
				$obj=$("img:eq("+i+")",$list);
				params=$obj.attr("class").split("|");
				$("#name").html('<p>'+params[0]+'</p><div class="both"><span>'+params[1]+'</span></div>');
			}
			return false;
		}
	});
	$("#main").mouseup(function(){
		$mover=null;
		$mover2=null;
		$mover3=null;
	});
	$(window).mouseup(function(){
		$mover=null;
		$mover2=null;
		$mover3=null;
	});
	
	$("#friendsBox").hover(
		function(){ $(this).stop().animate({backgroundColor:"#fff000"},"fast"); },
		function(){ $(this).stop().animate({backgroundColor:"#ebebeb"},"fast"); }
	);
	
	$("a","#newsPages").live("click", function(){
		$this=$(this);
		if(!$this.hasClass("active")){
			url=$this.attr("href").substr(1);
			$.post(url, function(data){
				$("#dailyNews").html(data);
				$("a","#dailyNews").hover(
					function(){ $("span", this).css("background", "#fff000"); },
					function(){ $("span", this).css("background", "#ebebeb"); }
				);
			});
			$("a","#newsPages").removeClass("active");
			$this.addClass("active");
		}
		return false;									   
	})
	$("a","#dailyNews").hover(
		function(){ $("span", this).css("background", "#fff000"); },
		function(){ $("span", this).css("background", "#ebebeb"); }
	);
	
	$("a","#interviewsPages").live("click", function(){
		$this=$(this);
		if(!$this.hasClass("active")){
			url=$this.attr("href").substr(1);
			$.post(url, function(data){
				$("#dailyInterviews").html(data);
				$("a","#dailyInterviews").hover(
					function(){ $("p", this).css({"background":"#000", "color":"#fff"}); },
					function(){ $("p", this).css({"background":"#fff000", "color":"#000"}); }
				);
			});
			$("a","#interviewsPages").removeClass("active");
			$this.addClass("active");
		}
		return false;									   
	})
	$("a","#dailyInterviews").hover(
		function(){ $("p", this).css({"background":"#000", "color":"#fff"}); },
		function(){ $("p", this).css({"background":"#fff000", "color":"#000"}); }
	);
	
	$("#commentForm").init_form(function(data){
		if(data=="name"){
			$("#errorBox").text("This name already exists");
			$("#errorBox").fadeIn();
		}
		if(data==1){
			id=$("#commentForm").attr("class");
			$.post('/ajax/comments//'+id+'/', function(data){$.log(data);$("#comments").html(data);});
			$("#commentForm textarea").val('');
			$("#commentFields p").click();
		}
		if(data==2){
			$("#errorBox").text("Are you spamer? If not, please wait 5 minutes.");
			$("#errorBox").fadeIn(function(){ setTimeout(function(){ $("#errorBox").fadeOut("slow")},2000); });
		}
		if(data==3){
			$("#errorBox").text("Are you spamer? If not, please use more suitable words in your name and in comment.");
			$("#errorBox").fadeIn(function(){ setTimeout(function(){ $("#errorBox").fadeOut("slow")},2000); });
		}
		if(data==4){
			$("#errorBox").text("Are you spamer? Your URL in spamlist.");
			$("#errorBox").fadeIn(function(){ setTimeout(function(){ $("#errorBox").fadeOut("slow")},2000); });
		}
	});
	$("#commentSubmit").removeAttr("disabled");
	
	$("#name").live("focus", function(){
		$("#errorBox").fadeOut();								   
	});
	
	$("#contactForm").init_form(function(data){
		if(data==1){
			$(".buttonsBox").fadeOut("fast");
			$("#field1").animate({top:"-5px"}, 300, function(){ $(this).animate({top: "400px"}, "slow")});
			$("#field2").animate({top:"-8px"}, 400, function(){ $(this).animate({top: "350px"}, "slow")});
			$("#field3").animate({top:"-10px"}, 300, function(){ $(this).animate({top: "300px"}, "slow")});
			setTimeout(function(){ location.href="/"; },2000);
		}					  
	});
	$("#thx").show();
	
	$("#searchForm").validate();
	$("#subscribeForm").each(function(){
		$(this).validate();
		$(this).ajaxForm(function(data){
			if(data==1) $(".subscribeBox").hide(1000);					  
		});
	});
	
	$("#tags").mouseleave(function(){
		if(!timer){
			var $this=$(this);
			$.post('/ajax/tags/', function(data){
				$this.css("height", $this.height()+"px");
				$("a",$this).each(function(){
					$(this).fadeTo(200+Math.round(600*Math.random()), 0);					   
				});	
				timer=setTimeout(function(){
					$this.html(data);
					$("a",$this).hide();
					$("a",$this).each(function(){
						$(this).fadeIn(Math.round(800*Math.random()));					   
					});	
					$this.animate({height: $("div", $this).height()+"px"});
					timer=null;
				}, 1000);
			});	
		}
	});
	if($("#currentPage").length){
		page=$("#currentPage").val()-1;
		count=$("td",".postsPagesBox").length-6;
		if(page && count>0){
			page=page>count?count:page;
			$list=$(".list",".postsPagesBox");
			$list.css("left", (-page)*30+"px");
			$mover=$("#mover2",".postsPagesBox");
			w=$mover.parent().width()-$mover.width();
			l=w/count*page;
			$mover.css("left", l+"px");
			$list=null;
			$mover=null;
		}
	}
	
	$("area","#map").each(function(){
		$(this)
		.click(function(){ return false; })	
		.hover(
			function(){
				id="#"+$(this).attr("id")+"Box";
				$(id).css("opacity","1").stop().fadeIn();
			},
			function(){
				id="#"+$(this).attr("id")+"Box";
				$(id).stop().fadeOut();
			}
		);
	});
	$("#advForm").validate();
	$("#advForm").init_form(function(data){
		if(data==1)
			$(".formBox").fadeOut();					
	});
	if(!get_cookie('adv')) $.post("/ajax/counter/");
	
	$("#allTags").animate({"lineHeight":"30px"}, 600, function(){
		$(this).animate({"lineHeight":"24px"}, 300);														   
	});
	
	$("li","#interview").hover(
		function(){ $(this).addClass("hover"); },
		function(){ $(this).removeClass("hover"); }
	);
	$("li","#alike").each(function(){
		$(this).data("t",$("div:eq(0)",this).css("top"));
		$(this).data("b",$("div:eq(1)",this).css("bottom"));
		$(this).hover(
			function(){
				$("div:eq(0)",this).animate({"top":"-25px"},200);	
				$("div:eq(1)",this).animate({"bottom":"-25px"},200);	
			},
			function(){
				$("div:eq(0)",this).stop().animate({"top":$(this).data("t")},300);	
				$("div:eq(1)",this).stop().animate({"bottom":$(this).data("b")},300);	
			}
		);
	});
	$("#grammaForm").init_form(function(){
		$("#grammaForm").clear_form();
		$("#grammaPopup").fadeOut();
	});
	$("#addAuthor").click(function(){$("#closeTranslate").click();$o=$("#authorPopup");if($o.is(":visible"))$o.fadeOut(); else $o.fadeIn(); return false;})
	$("#authorForm").validate();
	$("#authorForm").init_form(function(data){
		$("#closeAuthor").click();
	});
	$("#closeAuthor").click(function(){
		$("#authorForm").clear_form();
		$("#authorPopup").fadeOut();
		return false;
	});
	$("#addTranslation").click(function(){
		if(!$("#translatePopup").is(":visible")){
			$("#closeAuthor").click();
			$("#translatePopup").fadeIn();
			$("img","#postBox").hide("fast",function(){$(window).scrollTop($("#postBox").position().top);$("#subPost").addClass("pad");});
		}else
			$("#closeTranslate").click();		
		return false;
	});
	$("#translateForm").validate();
	$("#translateForm").init_form(function(data){
		$("#closeTranslate").click();
	});
	$("#closeTranslate").click(function(){
		$("img","#postBox").show("fast");								
		$("#subPost").removeClass("pad");
		$("#translateForm").clear_form();
		$("#translatePopup").fadeOut();
		return false;
	});
	$("#close,#close2").live("click", function(){
		$("#grammaForm").each(function(){ $(this).clearFields(); });
		$("#grammaPopup").fadeOut();
		return false;							   
	});
	$("#grammaButton").live("click", function(){
		text=window.getSelection?window.getSelection().toString():document.selection.createRange().text;
		text=($("p:contains('"+text+"')","#comments").length)?'':text;
		if(text){
			$("#mistakeField").val(text);
			$("#mistake").text(text);
			$("#withMistake").show();
			$("#noMistake").hide();
		}else{
			$("#withMistake").hide();
			$("#noMistake").show();
		}
		$("#grammaPopup").fadeIn();
		return false;									  
	}).hover(
		function(){ $(this).next().fadeIn(); },
		function(){ $(this).next().fadeOut(); }	
	);
	$("#grammaUrl").val(location.href);
	gramma=$("#grammaBanner").length?1:0; 
	$("#grammaThx").live("click", function(){
		$("#grammaBg").fadeOut(function(){
			$("#grammaInfo").fadeOut();
			set_cookie('gramma',1,30);
			$("#grammaCursor1").hide("slow");
			$("#grammaCursor2").hide("slow", function(){
				$("#grammaBanner").hide();
				gramma=0;
				$("#emptyBanner").fadeIn();											
			});
		});								   
		return false;
	});
	if(get_cookie('oldVersion')){
		$("#interviews1").hide();
		$("#interviews2").show();
		$("#oldInterviews").hide();
		$("#newInterviews").show();
	}	
	$("#oldInterviews").click(function(){
		$("#interviews1").slideUp();
		$("#interviews2").slideDown();
		$("#oldInterviews").hide();
		$("#newInterviews").show();
		set_cookie('oldVersion',1,30);
		return false;							   
	});
	$("#newInterviews").click(function(){
		$("#interviews2").slideUp();
		$("#interviews1").slideDown();
		$("#newInterviews").hide();
		$("#oldInterviews").show();
		set_cookie('oldVersion','');
		return false;							   
	});
	function init_sticker(){
		var sticker=$(".sticker1");
		if(sticker.length){
			sticker.data("w",sticker.css("width")).width(0);
			$(".bannerBox").hover(function(){sticker.animate({"width":sticker.data("w")});});	
		}
	}
	init_sticker();
	$("#shareButton").click(function(){return false;});
	$("#share").hover(
		function(){clearTimeout($(this).data('t'));$("#shareButton").stop().animate({"top":"-30px"},100,function(){$("a","#shares").each(function(i){var $this=$(this);setTimeout(function(){$this.surface()},i*50);});});},
		function(){$(this).data('t',setTimeout(function(){$("a","#shares").stop().animate({"top":"25px"},300,function(){$("#shareButton").stop().animate({"top":"3px"})});},1000));}
	);
	$("#translate").live("click",function(){
		$("#translatePopup").slideDown();
		return false;								  
	});
	
	$("#submitEmail1").click(function(){
		if(!$(this).data('l')){
			var $email=$("#postEmail1"),val=$email.val();
			if(val!=$email.attr('val')&&$.check_email(val)){
				$.post('/ajax/emails/',{'email':val},function(r){
					$.log(r);
					if(r==1){
						$(".emailBox").fadeOut(function(){
							$("#res1").fadeIn();
							$("#res2").fadeIn();
						});
						$("#submitEmail1").data('l',1);
						$("#submitEmail2").data('l',1);
					}
					if(r==0)
						$(".postError").show();
				});
			}else{
				$(".postError").show();
			}
		}
		return false;
	});
	$("#submitEmail2").click(function(){
		if(!$(this).data('l')){
			var $email=$("#postEmail2"),val=$email.val();
			if(val!=$email.attr('val')&&$.check_email(val)){
				$.post('/ajax/emails/',{'email':val},function(r){
					$.log(r);
					if(r==1){
						$(".emailBox").fadeOut(function(){
							$("#res1").fadeIn();
							$("#res2").fadeIn();
						});
						$("#submitEmail1").data('l',1);
						$("#submitEmail2").data('l',1);
					}
					if(r==0)
						$(".postError").show();
				});
			}else{
				$(".postError").show();
			}
		}
		return false;
	});
	$("#postEmail1").focus(function(){$(".postError").hide();});
	$("#postEmail2").focus(function(){$(".postError").hide();});
	
	$.dragDrop();
}).keypress(function(e){if((e.keyCode==13&&e.ctrlKey)||(e.keyCode==10&&e.ctrlKey))$("#grammaButton").click();});

$(window).load(function(){
	maxT=$(".adminPanel").length?715:655;
	yW=$(this).scrollTop()+$(this).height()/2;
	$("#grammaPopup").css("top",yW+"px");					
	x=($(this).width()-985)/2-110;
	y=yW<maxT?maxT:yW;
	$("#grammaBox").css({"top":y+"px","right":x+"px"});
	rightHeight=$(".rightBlock").height();
	if($("#comments").length)
		commentsY=$("#comments").offset().top;
	y=yW-rightHeight-280;
	y=y>0?y:0;
	$("#backInTop").css("top",y+"px");		
}).resize(function(){
	maxT=$(".adminPanel").length?715:655;
	yW=$(this).scrollTop()+$(this).height()/2;
	$("#grammaPopup").css("top",yW+"px");					
	x=($(this).width()-985)/2-110;
	y=yW<maxT?maxT:yW;
	$("#grammaBox").css({"top":y+"px","right":x+"px"});
	rightHeight=$(".rightBlock").height();
	if($("#comments").length)
		commentsY=$("#comments").offset().top;
	y=yW-rightHeight-280;
	y=y>0?y:0;
	$("#backInTop").css("top",y+"px");		
}).scroll(function(){
	yW=$(this).scrollTop()+$(this).height()/2;
	$("#grammaPopup").stop().animate({"top":yW+"px"}, "fast");
	$("#authorPopup").stop().animate({"top":yW+"px"}, "fast");
	x=($(this).width()-985)/2-110;
	$("#grammaBox").css("right",x+"px");
	y=yW<maxT?maxT:yW;
	if(gramma && y==maxT)
		$("#grammaCursor2").show();
	else
		$("#grammaCursor2").hide();
	$("#grammaBox").stop().animate({"top":y+"px"}, "fast");
	y=yW-rightHeight-280;
	y=y>0?y:0;
	$("#backInTop").stop().animate({"top":y+"px"}, "fast");		
	if($("#comments").length){
		y=yW-commentsY;
		y=y>0?y:0;
		$("#addComment").stop().animate({"top":y+"px"}, "fast");		
	}
}).keypress(function(e){if((e.keyCode==13&&e.ctrlKey)||(e.keyCode==10&&e.ctrlKey))$("#grammaButton").click();});

jQuery.fn.surface=function(){
	var $this=$(this);
	$this.stop().animate({"top":0},200,function(){$this.animate({"top":"3px"},100);});
}

$.fn.init_langs=function(){
	var $this=$(this), $ul=$("ul",$this),$sl=$("#selectLang"),h=location.hash.substr(1),$o;
	$this.hover(function(){$ul.slideDown(100);},function(){$ul.slideUp(100);})
	$("a",$this).live("click", function(){
		var $t=$(this),id=$t.attr("href").substr(1);
		$("li.active",$this).removeClass("active");
		$t.parent().addClass("active");
		$sl.text($t.text());
		$ul.slideUp(200);
		set_lang(id);
		//return false;
	});
	$(".lang",".postBox").hide();
	function set_lang(id){
		$(".lang",".postBox").hide();
		$("."+id,".postBox").show();
		set_cookie('lang',id,30);
	}
	$o=$("a.l"+h,$this);	
	if(!h||!$o.length)
		$o=$("a.l"+get_cookie('lang'),$this);
	if($o.length)
		$o.click();
	else
		$("a:first",$this).click();
};

$.extend({
	dragDrop:function(){
		var $m=null;
		$(document).mousedown(function(e){
			if(!$(e.target).is("textarea")&&!$(e.target).is("input")){
				$o=$(e.target).parents(".draged");
				if($o.length){
					$m=$o;
					$m.data({"x":$m.position().left-e.pageX,"y":$m.position().top-e.pageY});
				}
			}
		}).mousemove(function(e){
			if($m){
				$m.css({"left":e.pageX+$m.data("x"),"top":e.pageY+$m.data("y")});
				return false;
			}
		}).mouseup(function(){
			$m=null;
		});
	},
	log:function(data){if(window.console&&window.console.log)console.log(data);}
});

