$(function () {
	$("#top form.search input.submit").click(function () {
		$("#top form.search").submit()
		return false
	})

	if ($.browser.msie && parseInt($.browser.version) <= 6) {
		$("#menu li.lvl1").hoverClass("lvl1-hover")
		$("#menu li.lvl1-parent").hoverClass("lvl1-parent-hover")
		$("#content table").attr("cellspacing", "0")
		$("#content div.content-rt div.box ul.menu li:first-child").addClass("first-child")
		$("#content table th:first-child, #content table td:first-child").addClass("first-child")
	}
	$("#content table tr:last-child").addClass("last-child")
	$("#content table th:last-child, #content table td:last-child").addClass("last-child")

	jQuery(function() {
		$("#content a.zoomable").fancybox({
			zoomOpacity: true,
			overlayShow: false,
			zoomSpeedIn: 500,
			zoomSpeedOut: 500
		});
	})
});

$.fn.hoverClass = function (className) {
	return this.each(function () {
		$(this).hover(
			function () { $(this).addClass(className) },
			function () { $(this).removeClass(className) }
		);
	})
}

$.fn.photogallery = function (opts) {
	var opts = $.extend({
		speed: "fast",
		title: ""
	}, 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="top"><!-- --></div>\
				<div class="popup-content">\
					<div class="nav">\
						<a href="#prev" class="prev">&laquo; Poprzednie zdjęcie</a>\
						<a href="#next" class="next">Następne zdjęcie &raquo;</a>\
					</div>\
					<div class="title"></div>\
					<div class="photo">\
					</div>\
				</div>\
				<div class="btm">\
					<div class="lft"></div>\
					<div class="cnt"></div>\
					<div class="rgt"></div>\
				</div>\
			</div>'

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

		function show(li) {
			if ($("#overlay").length < 1) {
				$('<div id="overlay"></div>')
					.css({ opacity: 0 })
					.appendTo("body")
					.animate({ opacity: 0.75 }, "fast")
					.click(close)
			}
			if ($("#popup").length < 1) {
				$(html).appendTo("body")
				$("#popup a.close").click(close)
				$("#popup div.nav a.prev").click(prev)
				$("#popup div.nav a.next").click(next)
				$("#popup div.title").html(opts.title)
			}
			
			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.photo").html('\
				<img src="' + li.find("a").attr("href") + '" width="' + w + '" height="' + h + '" alt="" />\
			')
			$("#popup div.photo img").click(next)
			items.removeClass("photogallery-current")
			li.addClass("photogallery-current")
			
			// $("#popup div.nav a.prev")[li.prev().length > 0 ? "show" : "hide"]()
			// $("#popup div.nav a.next")[li.next().length > 0 ? "show" : "hide"]()
			$("#popup div.nav a.prev").css({ visibility: li.prev().length > 0 ? "visible" : "hidden" })
			$("#popup div.nav a.next").css({ visibility: li.next().length > 0 ? "visible" : "hidden" })
			
			w = Math.max($("#popup div.photo img").outerWidth(true), 300)
			$("#popup").width(w)
			$("#popup").css({ marginLeft: -1 * w/2 })
			
			$("#popup").css({
				top: $(window).scrollTop() + 0.5 * Math.max($(window).height() - $("#popup").outerHeight(), 30)
			})
		}
		
		function prev() {
			var n = items.filter(".photogallery-current").prev()
			if (n.length > 0) {
				show(n)
			}
			return false
		}

		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 })
	})
}
