$(window).load(function(){
		
	/* jakie obrazki pokazywać? */
	var aLight = $("a[rel='light']");
	var slug = "light-";
	var lightArray = [];
	var currentImage = 0;
	var i;
	
	/* jaka jest wyskokość strony? */
	function getDocHeight() {
		var D = document;
		return Math.max(
			Math.max(D.body.scrollHeight, D.documentElement.scrollHeight),
			Math.max(D.body.offsetHeight, D.documentElement.offsetHeight),
			Math.max(D.body.clientHeight, D.documentElement.clientHeight)
		);
	}
	
	
	
	aLight.click(function(e){
		e.preventDefault();
		var jqxhr = $.getJSON("http://biuroprasowe.netpr.pl/FileInfoGenerator.json?jsonp_callback=?", {
			"url": $(this).attr("href")
		}).success(function(data) {
			showCurrentImage(data[0]);
		});
	});

	/* pokaż odpowiedni obrazek */
	function showCurrentImage(obj) {
			$("#lightWrap").fadeTo(0,0,function(){
				$(".lightImage img, .lightImage #videoContainer").remove();
				
				if (obj.mime.indexOf('video')!==-1) {
					$(".lightImage").append("<img src='"+obj.webview+"'/>");
				} else {
					$(".lightImage").append("<img src='"+obj.webview+"'/>");
				}
				
				var img = $(".lightImage img").eq(0);
				
				img.load(function(){
					var w = img.width();
					var h = img.height();
					
					$(".lightMenu").css("width",""+w+"px");
				
					$(".lightImage img:eq(0)").fadeTo(0,1);
					$("#lightWrap").css("width",""+w+"px").center();
					
					/* sprawdź czy to wideo? */
					if (obj.mime.indexOf('video')!==-1) {
						img.wrap("<div id='videoContainer'></div>");
						
						var flashvars = {
							image: obj.webview,
							height: h+32,
							width: w,
							file:obj.url,
							config: "http://media.kbc.pl/presskit?config=config.xml",
							autostart: false
						};
						var params = {
							allowfullscreen: true
						};
						var attributes;
						swfobject.switchOffAutoHideShow();
						swfobject.embedSWF("http://media.warta.pl/m/video/jwplayer/4/player.swf", "videoContainer", w, h+32, "9.0.124", false, flashvars, params, attributes); 						
					}
					
					$(".lightTitle").fadeOut(0,0);
					
					$("#lightWrap").fadeTo('slow',1,function(){
						$(".lightTitle").html(""+obj.description).fadeIn();
					});
					
					$("#lightCloak").fadeTo('fast',0.7);		
				});
			});	
		
	}
	
	/* wygeneruj html z obrazkiem */
	function createLightWrapper() {
		$("body").append(
			"<div id='lightCloak' style='display: none;'></div>"+
			"<div id='lightWrap' style='display: none;'>"+
				"<div class='lightImage'><div class='lightExit'></div></div>"+
				"<div class='clr'></div>"+
				"<div class='lightMenu'>"+
					"<div class='lightTitle'></div>"+
					"<div class='clr'></div>"+
				"</div>"+
			"</div>"	);
		
		/* przywiązanie nawigacji i zamknięcia */
		$(".lightExit, #lightCloak").click(closeLight);
		
		/* zamknięcie klawiszem esc */
		$(document).keyup(function(e) {
			if (e.keyCode === 27) {closeLight();}
		});
	}
	
	/* funkcja do centrowania */
	jQuery.fn.center = function () {
		this.css("position","absolute");
		this.css("top", ( $(window).height() - this.height() ) / 2+$(window).scrollTop() + "px");
		this.css("left", ( $(window).width() - this.width() ) / 2+$(window).scrollLeft() + "px");
		return this;
	};
	
	/* mówi jak zamykać okienko */
	function closeLight() {
		$("#lightWrap").fadeOut('fast',function(){
			$("#lightCloak").fadeOut('fast');			
		});
	}	
	
	if (aLight.length>0) {
		/* stwórz html */
		createLightWrapper();
	}
	
	
});

