
$(function() {
	$(".producenci form").jqTransform({imgPath:'./jqtransformplugin/img/'});
	$("#bottomAdds a").hover(function() {
		$(this).children("img").css("margin-top","-98px");
	}, function() {
		$(this).children("img").css("margin-top","0px");	
	});
	$(".top_logos").hover(function() {
		$(this).children("img").css("margin-left","-144px");
	}, function() {
		$(this).children("img").css("margin-left","0px");	
	});
	$(".search_txt").focus(function() {
		($(this).val()=="" || $(this).val()=="szukaj...") ? $(this).val("") : $(this).val($(this).val()); 
		$(this).addClass("focused_txt");
	}).blur(function() {
		if($(this).val()=="") {
			$(this).removeClass("focused_txt");
		}
		($(this).val()=="") ? $(this).val("szukaj...") : $(this).val($(this).val());
	});
	$(".productBox .product_thumb").tooltip({ 
		effect: 'slide',
		position: 'center left',
		offset: [0, 40],
		predelay: 500,
		relative: true
	});
	$(".quantity_plus").click(function(event) {	
		var gty;
		if(qty = $(this).parent().next().children().children(".quantity_input")){
			if( !isNaN( qty.val() )) {
				qty.val( parseInt( qty.val(), 10 ) + 1 );
			}
		}
		if (qty = $(this).prev("#iIloscInput")){
			if( !isNaN( qty.val() )) {
				qty.val( parseInt( qty.val(), 10 ) + 1 );
			}
		}
	});
	$(".quantity_minus").click(function() {
		var gty;
		if(qty = $(this).parent().next().children().children(".quantity_input")) {
			if( !isNaN( qty.val() ) && qty.val() > 1 ) {
				qty.val( parseInt( qty.val(), 10 ) - 1 );
			}
		}
		if (qty = $(this).prev().prev("#iIloscInput")){
			if( !isNaN( qty.val() ) && qty.val() > 1 ) {
				qty.val( parseInt( qty.val(), 10 ) - 1 );
			}
		}
	});
	$(".quantity_input, #box_quantity input, .basket_quantity input").blur(function() {
		if($(this).val()=="" || isNaN($(this).val()) || parseInt($(this).val()) < 1 ) {			
			$(this).val(1);
		}
	})
	$("#accordion").tabs("#accordion div.pane", {tabs: 'div.menuBar', effect: 'slide', initialIndex: null});
	$(".menu_group li.selected").parent().parent().addClass("open");
	var click = false;
	$("#show_button").click(  function(){
		if($(this).hasClass("closed")) {
			click=true;
			var panel_height = $("div#panel_contents_wrap").height();
			$("div#panel_bottom_bar").css({
					opacity: "1"  
				});
			$("div#panel_contents").animate({ 
				height: panel_height+50+"px" 
			}, 300).animate({  
				height: panel_height+"px"
			}, 300); 		 
			$("div#panel").animate({ 
				top:"0px"
			}, 10); 	
			$(this).text("ZWIŃ MENU");
		} else {
			click=false;
			var panel_height = $("div#panel_contents_wrap").height();
			$("div#panel_contents").animate({ 
				height: panel_height+50+"px" 
			}, 300).animate({  
				height: "0px"
			}, 300); 
			$("div#panel").delay(600).animate({ 
				top:"-18px"
			}, 10);
			$("div#panel_bottom_bar").delay(610).animate({
					opacity:"0"
				});
			$(this).text("SZYBKIE MENU");
		}
		$(this).toggleClass("closed");
	});  
	$("#top_panel").mouseleave(function() {
		if(click){
			var czas =0;
			if($("div#panel_contents").height() == $("div#panel_contents_wrap").height()){
				czas = 500;
			} else {
				czas = 1110;
			}
				var panel_height = $("div#panel_contents_wrap").height();
				if (this.timer) clearTimeout(this.timer);
				this.timer = setTimeout(function () {
					click=false;
					$("div#panel_contents").animate({ 
						height: panel_height+50+"px" 
					}, 300).animate({  
						height: "0px"
					}, 300); 
					$("div#panel").delay(600).animate({ 
						top:"-18px"
					}, 10);
					$("div#panel_bottom_bar").delay(610).animate({
						opacity:"0"
					});
					$("#show_button").toggleClass("closed");
					$("#show_button").text("SZYBKIE MENU");
				}, czas);
		}
	}).mouseenter(function(){clearTimeout(this.timer)});
	if($(this).scrollTop() > 19) {
		if($.browser.msie && $.browser.version=="6.0") {
			$("#top_panel").css("top","0px");
		}else{
			$("#top_panel").css("top","0px");
		}
	} else {
		if($.browser.msie && $.browser.version=="6.0") {
			$("#top_panel").css("top",$(window).scrollTop() + "px");
		}else{
			$("#top_panel").css("top",20-$(window).scrollTop() + "px");
		}
	}	
	$(window).scroll(function () {
		if($.browser.msie && $.browser.version=="6.0") {
			if($(this).scrollTop() > 0) {
				$("#top_panel").css("top",$(window).scrollTop()-20 + "px");
			} else {			
				$("#top_panel").css("top","0px");
			}	
		} else {
			if($(this).scrollTop() > 0) {
				$("#top_panel").css("top","0px");
			} else {		
				$("#top_panel").css("top","20px");
			}
		}
	});

	$(".info #zapytaj").click(function(){
		$("#question_text:visible").slideUp(300);
		$("#question_email:visible").slideUp(300);
		$(".ajax_loader").show();
        $.post("zadaj_pytanie.php",{ sEmail:$('#sEmail').val(), sTresc:$('#sTresc').val(), op:$("#op").attr("VALUE"), sTytul:$("#sTytul").attr("VALUE"), check:$("#check").attr("VALUE") } ,function(data)
                {
                  if(data=='brak') {
                	  $(".ajax_loader").hide();
                	  $(".ok_message").slideDown(300);
                	  $(".ok_message").delay(1000).slideUp(300);
                  } else if(data=='email'){
                	  $(".ajax_loader").hide();
                	  $("#question_email").slideDown(300);
                  } else if(data=='tresc'){
                	  $(".ajax_loader").hide();
                	  $("#question_text").slideDown(300);
                  } else if(data=='emailtresc'){
                	  $(".ajax_loader").hide();
                	  $("#question_text").slideDown(300);
                	  $("#question_email").slideDown(300);
                  }
               });
        return false;
	});
	// skalowanie obrazka w ie6
	if($.browser.msie && $.browser.version=="6.0") {
		if($("#gallery_box #main_img a img").height() > $("#gallery_box #main_img a img").width()){
			$("#gallery_box #main_img a img").css("height",$("#gallery_box #main_img").css("height"));
		} else {		
			$("#gallery_box #main_img a img").css("width",$("#gallery_box #main_img").css("width"));
		}
		$(".product_thumb img").load(function(event) {
			var $target = $(event.target);
			if($target.height() > $target.width()){
				if($target.height() > 100) {
					$target.css("height","100px")
				}else{
					$target.css("height","auto")
				}
			} else {
				if($target.width() > 212) {
					$target.css("width","212px")
				}else{
					$target.css("width","auto")
				}
			}
		});
	}
	$("#description_panel .info img[src^='http:']").each(function(i) {
		$(this).wrap("<a href='"+$(this).attr("src")+"' title='Podgląd zdjęcia' onclick='return false' rel='desc_images'/>");
	});
	$("#description_panel .info a[rel='desc_images'], #gallery_box a").fancybox({
		'type' : 'image',
		autoScale: true,
		centerOnScroll: true,
		transitionIn:'elastic',
		transitionOut:'elastic',
		titlePosition:'over',
		easingIn:'swing',
		easingOut:'swing'
	});
	// dynamiczne uaktualnianie koszyka
	$("#basket_edit .b_quantity").keyup(function() {
		var path = $(this); 
		var p_name = new Array();
		p_name = $(this).attr("name").split("_");
		var p_id = parseInt(p_name[1]);
		var p_qty = parseInt(path.val());
		$.post("koszyk.php?akcja=uaktualnij_ilosc",{ iProdId: p_id, iIlosc: p_qty } ,function(data) {
       		if(data) {
       				var wynik = eval(data);
       				path.parent().parent().parent().parent().next().next().next().text(wynik[0]);
       				$("#basket_edit tfoot td .sum_r").text("Razem: "+wynik[1]+" PLN");
       				$("#quick_basket_content .basket_link span:last").text(wynik[1]+" PLN");
       				$(".basket_update_msg:not(:animated)").fadeIn().delay(500).fadeOut();
       		}
		}, "json");       
	});
	$("#basket_edit .b_comments").keyup(function() {
		var p_name = new Array();
		p_name = $(this).attr("name").split("_");
		var p_id = parseInt(p_name[1]);
		var p_tresc = $(this).val();
		var t = this; 
		if (this.value != this.lastValue) {
			if (this.timer) clearTimeout(this.timer);
			this.timer = setTimeout(function () {
				$.post("koszyk.php?akcja=uaktualnij_tresc",{ iProdId: p_id, iTresc: p_tresc }, function(data) {
					$(".basket_update_msg:not(:animated)").fadeIn().delay(500).fadeOut();
				}, "json");  
			}, 500);
			this.lastValue = this.value;
		}
	});
	$("#filtr_producent, #filtr_kategoria, #filtr_cena").prev().children("li").children("a").click(function() {
		var location="index.php?akcja=produkty&iKatId=";
		location+=$("#filtr_kategoria").attr("value");
		if($("#filtr_producent").attr("value")) {
			location+="&iProducent1=";
			location+=$("#filtr_producent").attr("value");
		}
		if($("#filtr_cena").attr("value")!="x") {
			location+="&iCena=";
			location+=$("#filtr_cena").attr("value");
		}
		if($("#filtr_kategoria").attr("value")!="x") {
			window.location = location;
		}
	});
});
