$(function() {
	// featured-products

	$("#featured-products").each(function() {
		var opt = { speed: "medium", interval: 6000 }
		
		var interval = null
		var $this = $(this);
		var products = $this.find("div.product");
		if (products.length < 2) return;
		products
			.hide().css({ position: "absolute", top: 0, left: 0 })
			.eq(0).show().addClass("current");
		$this.find("> ul.nums li:first a").addClass("current");

		function switchTo(elem) {
			products.filter(".current").fadeOut(opt.speed);
			products.removeClass("current")
			elem.addClass("current").fadeIn(opt.speed);
			$this.find("> ul.nums li a")
				.removeClass("current")
				.filter(function() {
					return $(this).attr("href") == "#" + elem.attr("id")
				})
				.addClass("current")
		}
		function next() {
			var e = products.filter(".current");
			if (e.next().is("div.product"))
				switchTo(e.next());
			else
				switchTo(products.filter(":first"));
		}
		function prev() {
			var e = products.filter(":visible");
			if (e.prev().is("div.product"))
				switchTo(e.prev());
			else
				switchTo(products.filter(":last"));
		}
		$this.find("> ul.nums 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)
	});
});

$(function () {
	//$("#hot-news").fadingLayers({ timeout: 3000, speed: 700 })
	$("#hot-news").each(function () {
		var html = $("#hot-news").html()
		$("#hot-news").empty()
		var scroll = $('<div class="scroll"></div>').appendTo($("#hot-news")).html(html)
		var width = scroll.find("div.item").reduce(0, function (r) { return r + $(this).outerWidth(true) })
		var times = Math.ceil($("#hot-news").width() / width) + 3
		scroll.html((new Array(times+1)).join(html)).width(width*times)
		var pos = 0
		setInterval(function () {
			scroll.css({ left: -1 * pos })
			pos += 1
			if (pos > width) pos = 0 
		}, 10)
	})
})

$.popup = function (content, opts) {
	var opts = $.extend({ id: "popup", height: 900 }, opts)

	var overlay = $("<div id='overlay'><!-- --></div>").css({ opacity: 0 }).appendTo("body")
	var container = $('\
		<div id="' + opts.id + '">\
			<div class="top"><!-- --></div><div class="bottom"><!-- --></div>\
			<div class="content">' + content + '</div>\
		</div>\
	').css({ visibility: "hidden" }).appendTo("body");
	
	overlay.fadeTo("fast", 0.7, function () {
		var top = (function () {
			var h = opts.height;
			var wh = (window.innerHeight ?
				window.innerHeight : document.documentElement.clientHeight)
			var p = (wh - h < 0) ? 0 : wh - h;
			var s = (window.scrollY ? 
				window.scrollY : document.documentElement.scrollTop)
			return (s + (p / 2));
		})();
	
		container.css({ top: top, visibility: "visible" })
		container.bgiframe();
	})
	
	overlay.add(container.find("a.close")).click(function () {
		container.remove();
		overlay.fadeTo("fast", 0, function () { overlay.remove() })
		return false;
	})
	
	return container;
}

function showImage(anch, url) {
	var container = $(anch).parents(".product-gallery").eq(0)
	container.find("div.big img").attr("src", url);
	container.find("div.big a, div.zoom a").attr("href", $(anch).attr("href"));
	return false;
}

$(function() {
	// .product-gallery

	$(".product-gallery").each(function() {
		var container = $(this)
		var thumbs = container.find("ul.thumbs")
		
		thumbs.find("a:first").each(function () {
			container.find("div.big a, div.zoom a").attr("href", $(this).attr("href"))
		})
		
		container.find("div.big a, div.zoom a").click(function () {
		
			var popup = $.popup('<div class="nav"><a href="#prev" class="prev">poprzedni</a> | <a href="#close" class="close">zamknij</a> | <a href="#next" class="next">następny</a></div>')
			popup.find("div.content")
				.append(thumbs.clone())
				.append('<div class="photo-wrapper"><div class="photo"><span><span><span><img src="#" alt=""></span></span></span></div></div>')
			var popupThumbs = popup.find("div.content ul.thumbs")
			popup.find("div.content div.photo").hide()
			
			// wycentruj ul.thumbs
			popupThumbs.wrap('<div class="thumbs"></div>')
			var margin = .5 * (popupThumbs.parent().width() - popupThumbs.width())
			if (margin > 0 ) popupThumbs.css({ marginLeft: margin })
			
			function switchTo(anch) {
				popupThumbs.find("a").removeClass("current")
				$(anch).addClass("current")
				var loader = new Image();
				loader.onload = function () {
					popup.find("div.content div.photo img").attr("src", $(anch).attr("href"))
					
					// wycentruj foto
					var ph = popup.find("div.content div.photo");
					var m = ph.parent().width() - (20 + loader.width)
					if (m > 0) ph.css({ marginLeft: .5 * m })
					ph.show()
				}
				loader.src = $(anch).attr("href")
			}
			function switchToNext() {
				var next = popupThumbs.find("a.current").parent().next().find("a")
				if (next.length) {
					switchTo(next)
				} else {
					switchTo(popupThumbs.find('a').eq(0))
				}
				return false
			}
			function switchToPrev() {
				var prev = popupThumbs.find("a.current").parent().prev().find("a")
				if (prev.length) {
					switchTo(prev)
				} else {
					switchTo(popupThumbs.find('a').slice(-1))
				}
				return false
			}
			popupThumbs.find("a").click(function () { switchTo(this); return false })
			popup.find("div.photo img").click(switchToNext)
			popup.find("a.prev").click(switchToPrev)
			popup.find("a.next").click(switchToNext)
			
			var href = $(this).attr("href")
			switchTo(popupThumbs.find("a[href]").filter(function () {
				return $(this).attr("href").indexOf(href) > -1
			}))
			
			return false
		})
	})
})

function popupWindow(url, w, h) {
	if (screen.availHeight) {
		var left = (screen.availWidth - w) / 2;
		var top = (screen.availHeight - h) / 2;
	} else {
		var left = (screen.width - w) / 2;
		var top = (screen.height - h) / 2;
	}

	if (left < 0) left = 30;
	if (top < 0) top = 30;

	window.open(url,'', 'width='+ w +',height='+h+',top='+top+',left='+left
		+',resizable=yes,scrollbars=yes');
	return false;
}

$(function() {
	$("#top-menu a.recommend").click(function () {
		var html = '\
		<div class="nav"><a href="#close" class="close">zamknij</a></div>\
		<form action="' + $(this).attr("href") + '" method="post" id="recommend-form">\
			<div class="f">\
				<label>Podaj swój e-mail:</label>\
				<input type="text" name="" class="text" id="recommend-form-from" />\
			</div>\
			<div class="f">\
				<label>Podaj e-mail znajomego:</label>\
				<input type="text" name="" class="text" id="recommend-form-to" />\
			</div>\
			<div class="btn"><input type="submit" value="Powiadom" class="btn" /></div>\
		</form>'
		$.popup(html, { height: 230 });
		return false;
	})
})

function popupForm(elem) {
	var elem = $(elem);
	if (elem.length == 0) return true;
	if ($('#popup').length > 0) return false;
	var popup = $.popup(
		'<div class="nav"><a href="#close" class="close">zamknij</a></div>',
		{ height: 230 })
	popup.find('div.content').append(elem.clone().show())
	return false;
}

// fadingLayers plugin

(function ($) {
	$.fn.fadingLayers = function(options) {
		var defaults = { timeout: 6000, speed: 1000, selector: "> *" }
		var o = $.extend(defaults, options);
		return this.each(function() {
			var layers = $(this).find(o.selector);
			if (layers.length < 2) return;
			layers.css({ position: "absolute", top: 0, left: 0, zIndex: 0 });
			layers.slice(1).hide();
			setInterval(function() {
				var current = layers.filter(":visible").eq(0);
				layers.not(current).hide();
				var next = layers.eq(layers.index(current) + 1)
				if (next.length == 0) next = layers.eq(0);
				current.css({ zIndex: 1 });
				next.css({ zIndex: 0 });
				current.fadeOut(o.speed);
				next.fadeIn(o.speed);
			}, o.timeout);
		})
	}
})(jQuery);

jQuery.fn.reduce = function (c, fn) {
	this.each(function (i) { c = fn.apply(this, [c]) })
	return c
}
