$(function () {
	$("#gallery").carousel()
	$("#gallery").carousel("auto")
	$("#gallery ul").photogallery()
	
	$("#page-content table.schedule tr:even").removeClass("odd").addClass("even")
	$("#page-content table.schedule tr:odd").removeClass("even").addClass("odd")
	
	$("#page-content div.product .photo").lightbox()
	
	if ($.browser.msie && parseInt($.browser.version) <= 6) {
		$("#menu li").hover(
			function () { $(this).addClass("hover") },
			function () { $(this).removeClass("hover") }
		)
	}
})

$.fn.reduce = function (c, fn) {
	this.each(function (i) { c = fn.apply(this, [c]) })
	return c
}

$.fn.carousel = function (arg) {
	return this.each(function () {
		var container = $(this)
		var items = container.find("li")
		var fnReduceWidth = function (r) { return r + $(this).outerWidth(true) }

		function go(i, noAnim) {
			var skip = container.data("carousel-skip")
			i = (typeof i == "number") ? i : items.index(i)
			if (i < 0 || i > items.length - skip) return
			var max = -items.reduce(0, fnReduceWidth) + container.width() 
			var pos = -items.slice(0, i).reduce(0, fnReduceWidth)
			container.find("ul")[noAnim ? "css" : "animate"]({ left: Math.max(pos, max) })
			items.removeClass("carousel-current").eq(i).addClass("carousel-current")
			container.find("a.prev")[ i > 0 ? "show" : "hide" ]()
			container.find("a.next")[ i < items.length - skip ? "show" : "hide" ]()
		}
		function next() { go(items.filter(".carousel-current").next()); return false }
		function prev() { go(items.filter(".carousel-current").prev()); return false }

		function auto() {
			setInterval(function () {
				var i = items.index(items.filter(".carousel-current"))
				if (i < 0 || i >= items.length - container.data("carousel-skip")) {
					go(0)
				} else {
					next()
				}
			}, 2000)
		}

		if (typeof arg == "string") {
			if (arg == "next") { next() }
			else if (arg == "prev") { prev() }
			else if (arg == "rewind") { go(0) }
			else if (arg == "auto") { auto() }
		} else {
			container.find("ul").width(items.reduce(0, fnReduceWidth))
			var skip = 0
			while (skip < items.length && items.slice(-skip-1).reduce(0, fnReduceWidth) < container.width()) skip++
			container.data("carousel-skip", skip)
			container.find("a.prev").click(prev)
			container.find("a.next").click(next)
			go(0, true)
		}
	})
}

$.fn.photogallery = function (opts) {
	var opts = $.extend({
		speed: "fast"
	}, opts)
	return this.each(function () {
		var container = $(this)
		var items = container.find("li")

		var html = '\
			<div id="popup">\
				<a href="#" class="close">zamknij</a>\
				<div class="popup-content"></div>\
				<div class="popup-nw"><!-- --></div>\
				<div class="popup-n"><!-- --></div>\
				<div class="popup-ne"><!-- --></div>\
				<div class="popup-e"><!-- --></div>\
				<div class="popup-w"><!-- --></div>\
				<div class="popup-sw"><!-- --></div>\
				<div class="popup-s"><!-- --></div>\
				<div class="popup-se"><!-- --></div>\
			</div>'

		function close() {
			$("#popup").remove()
			$("#my-overlay").fadeOut("fast", function () { $(this).remove() })
			return false
		}

		function show(li) {
			if ($("#my-overlay").length < 1) {
				$('<div id="my-overlay"></div>')
					.css({ opacity: 0 })
					.appendTo("body")
					.animate({ opacity: 0.5 }, "fast")
					.click(close)
			}
			if ($("#popup").length < 1) {
				$(html).appendTo("body")
				$("#popup a.close").click(close)
			}
			
			var w = parseInt(li.find("a").attr("class").replace(/size-(\d+)-(\d+)/, '$1'))
			var h = parseInt(li.find("a").attr("class").replace(/size-(\d+)-(\d+)/, '$2'))
			
			$("#popup div.popup-content").html('\
				<div class="photo"><img src="'+li.find("a").attr("href")+'" width="'+ w +'" height="'+ h +'" alt="" />\
				</div>\
				<div class="caption">'+li.find(".caption").html()+'</div>\
			')
			$("#popup div.popup-content div.photo img").click(next)
			items.removeClass("photogallery-current")
			li.addClass("photogallery-current")
			
			w = Math.max(w, 300)
			$("#popup div.popup-content").width(w)
			$("#popup").css({ marginLeft: -1 * w/2 })
			
			$("#popup").css({
				top: $(window).scrollTop() + 0.5 * Math.max($(window).height() - $("#popup").outerHeight(), 30)
			})
		}
		
		function next() {
			var n = items.filter(".photogallery-current").next()
			if (n.length > 0) {
				show(n)
			} else {
				close()
			}
			return false
		}

		items.find("a").click(function () { show($(this).parent()); return false })
	})
}
