$(function() {
	var menus = $("#shopSide div.productsList > ul:not(.dontFold) > li:has(ul)").not(".dontFold");
	var maxH = 0;
	menus.find("div.sub").each(function() {
		if ((h = $(this).height()) > maxH) maxH = h;
	})

	menus.each(function() {
		var $this = $(this);
		$this.find("div.sub").height(maxH);
		if ($this.is(".current"))
			$this.addClass("open");
		else
			$this.find("div.sub").hide()
		var to = 0;
		$this.find("> a.cat").mouseover(function() {
			var $this = $(this);
			to = setTimeout(function() {
				if ($this.parent().is(".open")) return;
				menus.filter(".open").removeClass("open").find("div.sub").slideUp("fast");
				$this.parent().find("div.sub").slideDown("fast");
				$this.parent().addClass("open");
				return false;
			}, 100);
		}).mouseout(function () { clearTimeout(to); });
	});
});



$(function() {
	$("#order_recipient_fields").hide();
	$("#order_recipient_same_address").change(function() {
		if ($(this).val() == 0) {
			$("#order_recipient_fields").hide();
		} else {
			$("#order_recipient_fields").show();
		}
	}).change();
	
	//kstudio
	$("#order_buyer_person, #order_buyer_firm").click(function() {
		if ($(this).val() == 0)
			$("#order_buyer_comp, #order_buyer_nip").attr('disabled','disabled').val('').addClass('inactive').prev().addClass('inactive');
		else
			$("#order_buyer_comp, #order_buyer_nip").removeAttr('disabled').parent().removeClass('inactive').prev().removeClass('inactive');
		return true;
		})
		.filter(':checked').click();
	});

function popupCenter(elem) {
	if (typeof elem == 'undefined') elem = $('#popup');
	var w = elem.width()
	var h = elem.height()

	var wh = (window.innerHeight ? window.innerHeight : document.documentElement.clientHeight)

	var p = wh - h;
	if (p < 0) p = 0;
	if (window.scrollY) s = window.scrollY;
	else s = document.documentElement.scrollTop;
	elem.css({ left: "50%", marginLeft: w/(-2), top: s + (p / 2)})
}

function showGallery(list, n) {
	if ($('#popup').length > 0) return false;


//	var animate = ! $.browser.msie;
	$('<div id="popup"><span class="close">X</span>' +
	'<div class="content"><div class="photo"></div><div class="nav"><a href="#" class="prev">Poprzedni widok</a> | <a href="#" class="next">Następny widok</a></div></div>' +
	'<div class="top"><div class="left"><div><!-- --></div></div><div class="center"><div><!-- --></div></div><div class="right"><div><!-- --></div></div></div>' +
	'<div class="middle"><div class="left"><div><!-- --></div></div><div class="center"><div><!-- --></div></div><div class="right"><div><!-- --></div></div></div>' +
	'<div class="bottom"><div class="left"><div><!-- --></div></div><div class="center"><div><!-- --></div></div><div class="right"><div><!-- --></div></div></div></div>').appendTo("body");

	$('#popup span.close').click(function() { $('#popup').remove(); return false; } );

	function setPhoto(i) {
		if (i < 0 || i > list.length) return;

		var loader = new Image();
		loader.onload = function() {
			$("#popup div.content div.photo").html('<img src="' + list[i] + '" />');
			n = i;
			if (i >= list.length - 1) {
				$("#popup div.nav a.next").addClass("disabled")
			} else {
				$("#popup div.nav a.next").removeClass("disabled")
			}
			if (n <= 0) {
				$("#popup div.nav a.prev").addClass("disabled")
			} else {
				$("#popup div.nav a.prev").removeClass("disabled")
			}
			if (!$.browser.msie) {
				$("#popup div.nav").css("width", "auto");
				$("#popup div.nav").width($("#popup div.content").width());
			}
			popupCenter();
			loader.onload = function () { }
		}
		loader.src = list[i];
	}

	$("#popup div.nav a.next").click(function() {
		if (n < list.length - 1)
			setPhoto(n + 1);
		return false;
	});

	$("#popup div.nav a.prev").click(function() {
		if (n > 0)
			setPhoto(n - 1);
		return false;
	});

	if (list.length == 1) {
		$("#popup div.nav").hide();
	}


	setPhoto(n);



	return false;
}

(function($) {
	$.fn.slideshow = function (opt) {
		return this.each(function () {
			var interval = null
			var $this = $(this);
			var elems = $this.find(opt.elems);
			var nums = $this.find(opt.nums);

			if (elems.length < 2) return;
			elems.hide().css({ position: "absolute", top: 0, left: 0 }).addClass("slide")
			     .eq(0).show().addClass("current");
			nums.find("li:first a").addClass("current");

			function switchTo(elem) {
				elems.filter(".current").fadeOut(opt.speed);
				elems.removeClass("current")
				elem.addClass("current").fadeIn(opt.speed);
				nums.find("li a")
					.removeClass("current")
					.filter(function() {
						return $(this).attr("href") == "#" + elem.attr("id")
					})
					.addClass("current")
			}
			function next() {
				var e = elems.filter(".current");
				if (e.next().is(".slide"))
					switchTo(e.next());
				else
					switchTo(elems.filter(":first"));
			}
			function prev() {
				var e = elems.filter(":visible");
				if (e.prev().is(".slide"))
					switchTo(e.prev());
				else
					switchTo(elems.filter(":last"));
			}
			nums.find("li a").click(function() {
				if (interval) clearInterval(interval)
				switchTo($($(this).attr("href")))
				return false
			});
			$this.find("> a.next").show().click(function () {
				if (interval) clearInterval(interval)
				next();
				return false
			});
			$this.find("> a.prev").show().click(function () {
				if (interval) clearInterval(interval)
				prev();
				return false
			});
			interval = setInterval(next, opt.interval)
		})
	}
})(jQuery);

$(function () {
	$("#slides").slideshow({
		elems: "> a",
		nums: "> ul",
		speed: 500,
		interval: 5000
	})
});

(function ($) {
	$.fn.hoverClass = function (className) {
		return this.each(function () {
			$(this).hover(
				function () { $(this).addClass(className) },
				function () { $(this).removeClass(className) }
			);
		})
	}
})(jQuery);

$(function () {
	if ($.browser.msie && parseInt($.browser.version) <= 6) {
		$("#mainMenu li.lvl1").hoverClass("lvl1-hover")
		$("#mainMenu li.lvl2:first-child").addClass("lvl2-first-child")
	}
});


$(function () {
	$("img.joker").mouseover(function () {
		$("#joker").remove()
		// console.log($(this).offset())
		$('<img src="'+$(this).attr("src").replace("-mini", "")+'" alt="" id="joker" />').
			css({ position: "absolute", left: $(this).offset().left, top: $(this).offset().top - 130 }).
			appendTo("body")
	}).mouseout(function () {
		$("#joker").remove()
	})
});

jQuery(function() {
		$('tr:has(.description)').mouseenter(
			function (event) {
				$(this).addClass('active');
				$(this).find('.description .wrapper').css('background-position', '0 -'+$(this).outerHeight()+'px');
				$(this).find('.description .wrapper').slideDown('200',function () {});
			});
		$('tr:has(.description)').mouseleave(
			function (event2) {
				$(this).find('.description .wrapper').slideUp('20',
					function () {
						$(this).parent().parent().parent().removeClass('active');
					});
			});
});



