jQuery(function() {
	$("#slides").slideshow({
		elems: "> .slide",
		nums: "> ul.n",
		speed: 1000,
		interval: 5000
	})
	
	$(window).load(function () {
		$("body.home").each(function () {
			$(this).css({
				backgroundPosition:
					"0 " + ($("#site-intro").outerHeight() + $("#site-intro").offset().top - 700) + "px"
			})
		})
	})

	if ($.browser.msie) {
		$("tr:first-child, td:first-child, th:first-child").addClass("first-child")
		$("tr:last-child, td:last-child, th:last-child").addClass("last-child")
		$("#content div.event div.dates span.date:last-child").addClass("date-last-child")
		if (parseInt($.browser.version) <= 6) {
			$("#menu img").pngFixImage()
		}
		$("#content div.event div.dates ul li:last-child").addClass("last-child")
	}

	$("#sidebar form.search input.submit").click(function () {
		$(this).parents("form").submit()
		return false
	})
	
	$("#sidebar div.poll form").submit(function () {
		$(this).find(".loader").show()
		$("#sidebar div.poll div.wrapper").load($(this).attr("action"), $(this).serialize())
		return false
	})
	
	$("#sidebar div.calendar div.month a").live("click", function () {
		$("#sidebar div.calendar").load($(this).attr("href"))
		return false
	})
	
	$("#content div.testimonial a.thumb").fancybox({
		zoomOpacity: true,
		overlayShow: false,
		zoomSpeedIn: 500,
		zoomSpeedOut: 500
	});


	$("#content div.event div.dates ul").each(function () {
		if ($(this).find("li").length > 4 && $(this).find("li.more").length < 1) {
			var items = $(this).find("li:not(.expand)").slice(4).hide()
			$(this)
				.append('<li class="expand"><a href="#">rozwiń</a></li>')
				.find("li.expand a").click(function () {
					items.show()
					$(this).parent().remove()
					return false
				})
		}
	});
});

jQuery(function ($) {
	function wrapTooltip(str) {
		return '\
			<div id="tooltip">\
				<div class="tooltip-top"><!-- --></div>\
				<div class="tooltip-mid">\
					<div class="tooltip-content">'+str+'</div>\
				</div>\
				<div class="tooltip-btm"><!-- --></div>\
			</div>\
		'
	}

	$("#sidebar div.calendar td:has(ul.events)").live("mouseenter mouseleave", function (event) {
		if (event.type == "mouseenter" || event.type == "mouseover") {
			$("#tooltip").remove()
			var html = wrapTooltip($(this).find("ul.events").outerHTML())
			$(html).appendTo("body").css({
				top: $(this).offset().top - $("#tooltip").height(),
				left: $(this).offset().left + $(this).width()
			})
		} else if (event.type == 'mouseleave' || event.type == 'mouseout') {
			$("#tooltip").addClass("remove")
			setTimeout(function () {
				$("#tooltip.remove").remove()
			}, 100)
		}
	})
});

(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
			});
			interval = setInterval(next, opt.interval)
		})
	}
})(jQuery);

/*! Copyright (c) 2006 Brandon Aaron (brandon.aaron@gmail.com || http://brandonaaron.net)
* Dual licensed under the MIT (http://www.opensource.org/licenses/mit-license.php)
* and GPL (http://www.opensource.org/licenses/gpl-license.php) licenses.
*/
(function($) {
	// Returns whether or not a result set has results in it
	$.fn.outerHTML = function() {
		return $('<div>').append( this.eq(0).clone() ).html();
	};
})(jQuery);

