// JavaScript Document
$(document).ready(function(){
	$("form").each(function(){
		this.reset();							   
	});
	$(".forClear").each(function(){
		if(!$(this).attr("val"))$(this).attr("val",$(this).val());
		$(this).focus(function(){
			if($(this).val()==$(this).attr("val"))
				$(this).animate({"letterSpacing": "-6px"}, 100, function(){ $(this).val(''); $(this).css("letterSpacing",0); });
		});
		$(this).blur(function(){
			if(!$(this).val()){
				$(this).val($(this).attr("val"));
				$(this).css("letterSpacing","-6px");
				$(this).animate({"letterSpacing":"0"}, 100);
			}
		});
	});
	$("#addFavorite").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;
	});
	$("div", "#leftMenu").hover(
		function(){ 
			$obj=$(this).parent();
			$obj.addClass("active"); 
			if($obj.prev().prev().length)
				$obj.prev().prev().addClass("prev");
			if($obj.next().next().length)
				$obj.next().next().addClass("next");
		},
		function(){ 
			$obj=$(this).parent();
			$obj.removeClass("active"); 
			$obj.prev().prev().removeClass("prev");
			$obj.next().next().removeClass("next");
			if($obj.hasClass("active2"))
				$("ul", $obj).slideUp(function(){ $(this).parents("li:first").removeClass("active2"); });
		}
	);
	$("a", "#leftMenu").live("click",function(){ 
		if($(this).next().length){
			$o=$(this).parents("li:first");
			if($o.hasClass("active2"))
				$(this).next().slideUp(function(){ $o.removeClass("active2"); });
			else{
				$o.addClass("active2");		
				$(this).next().slideDown();
			}
			return false;	
		}
	});
	$("span", "#authForm").click(function(){
		$(this).fadeOut(100);
		$(this).next().focus();
	});
	$("input", "#authForm div").focus(function(){
		$(this).prev().fadeOut(100);
	});
	$("input", "#authForm div").blur(function(){
		if(!$(this).val())
			$(this).prev().fadeIn(100);
	});
	$("#authForm").mousemove(function(){
		$("div input", this).each(function(){
		if($(this).val())
			$(this).prev().fadeOut(100);
		});							  
	});
	$("#authForm").ajaxForm(function(data){
		if(data==1)
			location.reload();
		else
			$(".error","#authForm").slideDown();
	});
	$("a", "#popular").hover(
		function(){ $(this).next().stop().css("opacity",1).fadeIn(); },
		function(){ $(this).next().stop().fadeOut(); }
	);
	$("#inTop").live("click", function(){
		$(document).scrollTop(0);
		return false;
	});
	
	if($("#feedbackForm").length) 
		$("#feedbackForm").validate();
		$("#feedbackForm").ajaxForm(function(){
			$("#feedbackForm").hide();
			$("#thxBox").slideDown();
		});
	
	if($("#regForm").length){ 
		$("#regForm").validate();
		$("#regForm").ajaxForm(function(data){
			switch(data){
				case 'login': $("#login").val("Логин уже занят другим пользователем"); break;	
				case 'email': $("#email").val("Пользователь с таким e-mail уже зарегестрирован"); break;	
				case '': $("#forReg").fadeOut(function(){$("#finish").fadeIn();setTimeout(function(){location.href='/';},3000);}); break;
			}							
		});
	}
	
	$("a","#faq").live("click",function(){
		$("div","#faq").slideUp();
		$(this).parent().next().slideDown();
		return false;								
	});
	$(".toBasket").live("click", function(){
		$this=$(this);
		url=$this.attr("href");
		if(url)$.post(url,function(){$.post('/ajax/basket/list/',function(data){$("#basket").html(data);});});
		return false;								  
	});
	$(".basketCount").keyup(function(e){
		$this=$(this);
		val=$this.val();
		var filter=/^([0-9])+$/;
		val=val&&parseInt(val)!=0&&filter.test(val)?val:1;
		switch(e.keyCode){case 38:val++;break;case 40:if(val>1)val--;break;}
		$this.val(val);
		val2=$this.attr("id");
		$("strong",$this.parent().next()).html(val*val2+' <span>Р</span>');
		total=0;
		$(".sum").each(function(){total+=parseInt($(this).text());});
		$("#total").text(total);
	});
	$("#basketForm").ajaxForm(function(){
		$("#basketFormBox").fadeOut(function(){$("#forReg").fadeIn();});							   
	});
	$(".del","#basketFormBox").live("click",function(){
		if(confirm("Вы уверены что хотите убрать из корзины этот товар?")){
			obj=$(this);url=obj.attr("href");
			parent=obj.parents("tr:first");
			sum=parseInt($(".sum",parent).text())*parseInt($(".basketCount",parent).val());
			if(url)$.post(url, function(data){
				if(data==1)$o1=parent.remove();
				if(!$("tr","#basketFormBox").length)location.reload();
				else $("#total").text($("#total").text()-sum);
			});
		}	
		return false;
	});
});
