$.fn.fadingLayers = function(options) {
	var defaults = { timeout: 6000, speed: 1000 }
	var o = $.extend(defaults, options);
	return this.each(function() {
		var layers = $(this).find("> *");
		if (layers.length < 2) return;
		layers.css({ position: "absolute", top: 0, left: 0, zIndex: 0 });
		layers.slice(1).hide();
		setInterval(function() {
			var visible = layers.filter(":visible").eq(0);
			layers.not(visible).hide();
			var next = (visible.next().length > 0 ? visible.next() : layers.eq(0));
			visible.css({ zIndex: 1 });
			next.css({ zIndex: 0 });
			visible.fadeOut(o.speed);
			next.fadeIn(o.speed);
		}, o.timeout);
	})
}

$(function() {
	$("#clients-logos-layers").fadingLayers();
	
	$("#gallery-thumbs").each(function() {
		//$(this).find("a").not(":first").hide()
		$(this).find("a").click(function() {
			if ($("#popup").length > 0) return ;
			var current = $(this).parents("li").eq(0);

			function switchTo(item) {
				current = item;
				$("#popup div.photo")
					.empty()
					.html('<img src="'+current.find("a").attr("href")+'" alt="'+current.find("a").attr("title")+'" />')
				$("#popup div.desc").html(current.find("a").attr("title"))
				$("#popup div.photo img")
					.click(function() {
						if (current.next().length > 0) {
							switchTo(current.next())
						} else {
							$("#popup").remove();
						}
						return false;
					});
				if (current.next().length == 0) {
					$("#popup a.next").addClass("inactive");
				} else {
					$("#popup a.next").removeClass("inactive");
				}
				if (current.prev().length == 0) {
					$("#popup a.prev").addClass("inactive");
				} else {
					$("#popup a.prev").removeClass("inactive");
				}
			}
			$('\
				<div id="popup">\
					<div class="top"><!-- --></div><div class="bottom"><!-- --></div>\
					<div class="content">\
						<div class="desc">' + current.find("a").attr("title") + '</div>\
						<div class="nav">\
							<a href="#prev" class="prev">&lt; poprzedni</a> | \
							<a href="#close" class="close">zamknij</a> | \
							<a href="#next" class="next">następny &gt;</a>\
						</div>\
						<div class="photo-wrapper"><div class="photo"></div></div>\
					</div>\
				</div>\
			').appendTo("body");
			
			var top = (function () {
				var h = 600;
				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));
			})();
			
			$("#popup").css({ top: top }).show();
			
			$("#popup a.close").click(function() {
				$("#popup").remove();
				return false;
			});
			$("#popup a.next").click(function() {
				if ($(this).is(".inactive")) return false;
				switchTo(current.next())
				return false;
			});
			$("#popup a.prev").click(function() {
				if ($(this).is(".inactive")) return false;
				switchTo(current.prev())
				return false;
			});
			switchTo(current);
			return false;
		})
	})
})

$(function() {
	$("ul.videos-list").each(function() {
		$(this).find("a").click(function () {
			if ($("#popup").length > 0) return ;

			$('\				<div id="popup">\
					<div class="top"><!-- --></div><div class="bottom"><!-- --></div>\
					<div class="content"><div class="loader"></div></div>\
				</div>\
			').appendTo("body");
			
			var top = (function () {
				var h = 400;
				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));
			})();
			
			$("#popup").css({ top: top }).show();
			
			function loadUrl(url) {
				$("#popup div.content")
					.html('<div class="loader"></div>')
					.load(url, { }, function () {
						$("#popup a.close").click(function() {
							$("#popup").remove();
							return false;
						});
						$("#popup a.prev").click(function() {
							loadUrl($(this).attr("href"));
							return false;
						});
						$("#popup a.next").click(function() {
							loadUrl($(this).attr("href"));
							return false;
						});
					});
			}
			
			loadUrl($(this).attr("href"));
		
			return false;
		})
	})
})

