
$(function() {
	// fix dla menu w IE6
	if ($.browser.msie && $.browser.version == "6.0") {
		$("#menu ul.top").hover(
			function () { $(this).addClass("top-hover") },
			function () { $(this).removeClass("top-hover") }
		)
		$("#menu ul.top li").hover(
			function () { $(this).addClass("hover") },
			function () { $(this).removeClass("hover") }
		)
	}

	// tlo na glownej
	$("#hp-top-bg").fadingLayers({ timeout: 3000, speed: 700 })
	
	// newsy na glownej
	$("#hp-news").each(function () {
		var o = { speed: "slow", timeout: 3000 }
		var anchs = $(this).find("#hp-news-list > li > a")
		var items = $(this).find("#hp-news-items .hp-news-item")
		items.css({ position: "absolute", top: 0, left: 0, zIndex: 0 })
		items.slice(1).hide();
		anchs.eq(0).addClass("current");
		anchs.click(function() {
			if (interval) clearInterval(interval);
			switchTo($(this));
			return false;
		});
		var interval = setInterval(function() {
			var next = anchs.eq(anchs.index(anchs.filter(".current")) + 1)
			if (next.length < 1) next = anchs.eq(0);
			switchTo(next);
		}, o.timeout)
		
		function switchTo(anch) {
			var target = $(anch.attr("href"));
			if (target.is(":visible")) return;
			items.fadeOut(o.speed)
			target.fadeIn(o.speed)
			anchs.removeClass("current")
			anch.addClass("current")
		}
	});
	
	// kontakt
	$("ul.contact-people-tabs").tabs()
});

// popup

(function ($) {
	$.popup = function (html) {
		if ($("#popup, #overlay").length > 0) return
		$('<div id="overlay"></div>')
			.css({ opacity: 0 })
			.appendTo("body")
			.fadeTo("fast", 0.7, function () {
				var top = (function centerVert(h) {
					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));
				})(586);
				
				$('<div id="popup"><div class="wrapper">\
						<div class="close"><a href="#" class="close">zamknij okno</a></div>\
						<div class="content"></div>\
				   </div></div>\
				').css({top: top}).appendTo("body").find("div.content").html(html)
				$("#popup a.close, #overlay").click(function () {
					$("#popup").remove()
					$("#overlay").fadeOut("fast", function() {
						$(this).remove();
					})
					return false
				})
			})
	}
})(jQuery);

// 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);

function openWindow(url, name, w, h, opts) {
	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;
	
	if (typeof opts == "undefined") opts = ""

	window.open(url, name, 'width='+w+',height='+h+',top='+top+',left='+left+','+opts)
}



