$(document).ready(function() {

	$('form.sform input[type=text]').each(function(index, value){
		if($(this).val()=='') $(this).val($(this).attr('rel'));
	})
	$('form.sform input[type=text]').bind('click',function(){
		var rel=$(this).attr('rel');
		var val=$(this).val();
		if(rel==val)$(this).val('');
	})
	$('form.sform input[type=text]').bind('blur',function(){
		var rel=$(this).attr('rel');
		var val=$(this).val();
		if(val=='') $(this).val(rel);
	})
	$('form.sform').bind('submit',function(){
		$('form.sform input[type=text]').each(function(index, value){
			var rel=$(this).attr('rel');
			var val=$(this).val();
			if(val==rel) $(this).val('');
		})
	})
	
	$('form.newsletter input[type=text]').each(function(index, value){
		if($(this).val()=='') $(this).val($(this).attr('rel'));
	})
	$('form.newsletter input[type=text]').bind('click',function(){
		var rel=$(this).attr('rel');
		var val=$(this).val();
		if(rel==val)$(this).val('');
	})
	$('form.newsletter input[type=text]').bind('blur',function(){
		var rel=$(this).attr('rel');
		var val=$(this).val();
		if(val=='') $(this).val(rel);
	})
	$('form.newsletter').bind('submit',function(){
		$('form.newsletter input[type=text]').each(function(index, value){
			var rel=$(this).attr('rel');
			var val=$(this).val();
			if(val==rel) $(this).val('');
		})
	})
	
	
	
	jQuery.validator.messages.required = "wymagane";

	jQuery.validator.setDefaults(
	{
		errorPlacement: function(error, element) {
			error.appendTo( element.parent().parent().parent().parent() );
		}
		
	});	

	$('input.buy').closest('form').bind('submit',function(){
	    var form=$(this);
	    if(form.valid()){  
	    	$('#box_header').hidein('slow');
	    	$.ajax({
		        url: "/basket?ajax=1",
		        type: "POST",
		        cache: false,
		        data: form.serialize(),
		        success: function(data){
		            $('#box_header').html(data);
		            $('html, body').animate({scrollTop:0}, 'slow');
		            addEvent();
		        }
		    });
		}
	    return false;
	})
	


	
	
	
	$('#box_header a.close').bind('click',function(){
		var href=$(this).attr('href');
		if(!href!='')
       	 $("#box_header div.boxTop").hidein("slow",function(){
       		 $("#box_header div.boxTop").remove(); 
       	 });
    })
    

	
	$('a.my-account').bind('click',function(){
		//if($('#box_header .boxTop').length) 
		$('#box_header').hidein('slow');
		$.ajax({
	        url: "/index/account?ajax=1",
	        cache: false,
	        success: function(data){
	            $('#box_header').html(data);
	            addEvent();
	        }
	    });
		return false;
	})
	
	
	$('a.basket').bind('click',function(){
		//if($('#box_header .boxTop').length) 
		$('#box_header').hidein('slow');
		$.ajax({
			url: "/basket?ajax=1",
	        cache: false,
	        success: function(data){
	            $('#box_header').html(data);
	            addEvent();
	        }
	    });
		return false;
	})

	function addEvent()
	{
		if($('#box_header .boxTop').length==0) $('#box_header').hidein();
		if($('#box_header .boxTop').length>0) $('#box_header').showin('slow');
		//$('html, body').animate({scrollTop:0}, 'slow');
		
		$('#box_header a.close').bind('click',function(){
	       	 $("#box_header div.boxTop").hidein("slow",function(){
	       		 $("#box_header div.boxTop").remove(); 
	       	 });
	    })
	    
	    $('#box_header a').bind('click',function(){
			var href=$(this).attr('href');
			if(href!=undefined){
				if($(this).is('.ajax')){
					$.ajax({
				        url: href+"?ajax=1",
				        cache: false,
				        success: function(data){
				            $('#box_header').html(data);
				            addEvent();
				        }
				    });
					
				}else
				 $("#box_header div.boxTop").hidein("slow",function(){
		       		 $("#box_header div.boxTop").remove(); 
		       		 document.location=href;
		       	 });
	    	}
			return false;
		})

		$('#box_header form').bind('submit',function(){
			var action=$(this).attr('action');
			var data=$(this).serialize();
			var method= (data==null)? "GET":"POST";
		    $.ajax({
		        url: action+"?ajax=1",
		        type: method,
		        cache: false,
		        data: data,
		        success: function(data){
		            $('#box_header').html(data);
		            addEvent();
		        }
		    });
			return false;
		})
		
	}
	
	
	
	
	$('#faq li a').bind('click',function()
	{
		var u=$(this).attr('href').replace('#','');
		$('ul.faq li:visible').hidein('slow',function(){
			$('ul.faq li#'+u).showin('slow');	
		});
		if($('ul.faq li:visible').length==0)$('ul.faq li#'+u).showin('slow');
		
	})
	
	$('ul.faq li').css('display','none');
	
	$('form.newsletter').bind('submit',function(){
		if($('#box_header .newsletter').length==0) $('#box_header').hidein();
		var data=$(this).serialize();
		var url=$(this).attr('action');
		$(this).find('#email').val('');
		$.ajax({
	        url: url+"?ajax=1",
	        type: 'POST',
	        cache: false,
	        data: data,
	        success: function(data){
	            $('#box_header').html(data);
	            $('html, body').animate({scrollTop:0}, 'slow');
	            if($('#box_header .newsletter').length) $('#box_header').showin('slow');
	            $('#box_header a.close').bind('click',function(){
	   	       	 	$("#box_header div.boxTop").hidein("slow",function(){
	   	       	 		$("#box_header div.boxTop").remove(); 
	   	       	 	});
	            })
	        }
	    });
		return false;
	})
});


jQuery.fn.center = function () {
    this.css("position", "absolute");
    this.css("top", ( $(window).height() - this.height() ) / 2+$(window).scrollTop() + "px");
    this.css("left", ( $(window).width() - this.width() ) / 2+$(window).scrollLeft() + "px");
    return this;
}


jQuery.fn.extend({
	showin: function(speed,callback){
		return speed ?
			this.animate({
				height: "show", opacity: "show"
			}, speed, callback) :

			this.filter(":hidden").each(function(){
				this.style.display = this.oldblock || "";
				if ( jQuery.css(this,"display") == "none" ) {
					var elem = jQuery("<" + this.tagName + " />").appendTo("body");
					this.style.display = elem.css("display");
					// handle an edge condition where css is - div { display:none; } or similar
					if (this.style.display == "none")
						this.style.display = "block";
					elem.remove();
				}
			}).end();
	},

	hidein: function(speed,callback){
		return speed ?
			this.animate({
				height: "hide", opacity: "hide"
			}, speed, callback) :

			this.filter(":visible").each(function(){
				this.oldblock = this.oldblock || jQuery.css(this,"display");
				this.style.display = "none";
			}).end();
	}
})

