$(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)
		);
	}
	
	/* sprawdź czy w url jest jakiś znak */
	function checkForSomethingInUrl(u) {
		return location.href.indexOf(u)!==-1;
	}
	
	function checkForImageInUrl() {
		var fileHrefSplit =location.href.split('#'+slug);
		if (fileHrefSplit.length > 1 && fileHrefSplit[1] < aLight.length && fileHrefSplit[1]!=='' && !isNaN(fileHrefSplit[1])) {
			currentImage = parseFloat(fileHrefSplit[1]);
			showCurrentImage();
		}
	}
	
	/* przywiązanie funkcji do eventu click */
	function generateClickHandler(n) {
		return function(e) {
			e.preventDefault();
			currentImage = n;
			showCurrentImage();	
		};
	}
	
	function checkIfExists(newUrl) {
		var k;
		for (k=0; k<lightArray.length;k++) {
			if (lightArray[k].href===newUrl) {
				return true;
			}
		}
		return false;
	}
	
	/* stwórz tablicę obiektów do pokazania i przywiąż funkcję do kliknięcia */
	for (i = 0; i < aLight.length; i ++) {
		var href = aLight.eq(i).attr("href");
		var klasa = aLight.eq(i).attr("class");
		var alt = aLight.eq(i).attr("alt");
		var hrefSplit;
		if (href.indexOf('/prf/')!==-1){
			//jeśli jest...
			hrefSplit = href.split('prf/');
			href="/nfw/"+hrefSplit[1];
		} else if (href.indexOf('/prtf/')!==-1){
			//jeśli jest thumbnailem w notatce
			hrefSplit = href.split('prtf/');
			href="/nfw/"+hrefSplit[1];
		//jeśli jest thumbnailem w presskicie
		} else if (href.indexOf('/pktf/')!==-1){
			hrefSplit = href.split('pktf/');
			if (klasa.indexOf('video')!==-1) {
				//pokaż wideo
				//href=alt;
				href="/pktf/"+hrefSplit[1];
			} else {
				//wyświetl duży thumbnail
				//href="/pkwvf/"+hrefSplit[1].replace("QWERTYUIOPASDFGHJKLZXCVBNMĘÓĄŚŁŻŹĆŃęóąśłżźćń","qwertyuiopasdfghjklzxcvbnmeoaslzzcneoaslzzcn").replace(" ","-");
				href="/pkwvf/"+hrefSplit[1].replace(" ","-").toLowerCase();
			}
		} else {
			href = aLight.eq(i).attr("href");
		}
		
		if (!checkIfExists(href)) {
			lightArray.push({
				title: aLight.eq(i).attr("title"),
				href: href,
				className: aLight.eq(i).attr("class")
			});
		}
		
		aLight.eq(i).click(generateClickHandler(lightArray.length - 1));
	}
	
	/* pokaż odpowiedni obrazek */
	function showCurrentImage() {
		function appearLight() {
			$("#lightWrap").fadeTo(0,0,function(){
				/* złap z href nazwę pliku */
				var fileHrefSplit = lightArray[currentImage].href.split('/');
				
				/* sprawdź czy jest #!/ w url i ustaw odpowiedni*/
				if (checkForSomethingInUrl('#'+slug)) {
					var currentUrlSplit =  location.href.split('#'+slug);
					location.href = currentUrlSplit[0]+'#'+slug+currentImage;
				} else {
					location.href =  location.href+'#'+slug+currentImage;
				}
				
				$(".lightImage img, .lightImage #videoContainer").remove();
				
				$(".lightImage").append("<img src='"+lightArray[currentImage].href+"'/>");
				if(currentImage+1 < lightArray.length) {
					$(".lightImage").append("<img style='display: none;' src='"+lightArray[currentImage+1].href+"'/>");
				}
				
				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 (lightArray[currentImage].className.indexOf('video')!==-1) {
						img.wrap("<div id='videoContainer'></div>");
						var file;
						if (lightArray[currentImage].href.indexOf(".mp4")!==-1) {
							file = lightArray[currentImage].href;
						} else {
							file = lightArray[currentImage].href.replace(".jpg", ".mp4").replace("+","-").replace("/pktf/","/pkf/");
						}
						
						var videoUrl= lightArray[currentImage].href.replace(".jpg", ".mp4").replace("+","-").replace("&nbsp;","-").replace("/pktf/","/pkf/").replace("/pkwvf/","/pkf/");
						var videoUrlSplit = videoUrl.split("/");
						
						/* alert(videoUrlSplit.length); */
						
						if (videoUrlSplit.length===6) {
							videoUrl = videoUrlSplit[0]+"/"+videoUrlSplit[1]+"/"+videoUrlSplit[2]+"/"+videoUrlSplit[3]+"/"+videoUrlSplit[5];
						}
						
						var flashvars = {
							image: lightArray[currentImage].href,
							height: h+32,
							width: w,
							file: videoUrl,
							config: "http://inspiracjepr.netpr.pl/presskit?config=config.xml",
							autostart: false
						};
						var params = {
							allowfullscreen: true
						};
						var attributes;
						swfobject.switchOffAutoHideShow();
						swfobject.embedSWF("http://inspiracjepr.netpr.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(""+lightArray[currentImage].title).fadeIn();
					});
				});
			});
		}
		
		/* jeśli nie ma obrazka dodaj dwa */
		if($(".lightImage img").length===0){
			$(".lightImage").append("<img style='display: none;' src='"+lightArray[currentImage].href+"'/>");
			if(currentImage+1 < lightArray.length) {
				$(".lightImage").append("<img style='display: none;' src='"+lightArray[currentImage+1].href+"'/>");
			}
			return;
		}
		
		/* pierwsze odpalenie czy już jest? */
		if ($('#lightCloak').is(':visible')){
			$("#lightWrap").fadeOut('fast',appearLight);
		} else {
			$('#lightCloak').height(getDocHeight).fadeTo('fast',0.6,function(){
				$("#lightWrap").fadeOut(0,0,appearLight);
			});
		}
		
		/* pokazywanie nawigacji */
		if (currentImage < lightArray.length - 1) {
			$(".lightNext, .lightNavigationSpace").show();			
		} else {
			$(".lightNext, .lightNavigationSpace").hide();
		}
		
		if (currentImage === 0) {
			$(".lightPrev").hide();
		} else {
			$(".lightPrev").show();
		}
	}
	
	/* kliknięcie w guzik poprzednie */
	function previousClick() {
		if(currentImage === 0) {
			return;
		}
		currentImage--;
		showCurrentImage();
	}
	
	/* kliknięcie w guzik następne */
	function nextClick() {
		if(currentImage > lightArray.length) {
			return;
		}
		
		currentImage++;
		showCurrentImage();
	}
	
	/* 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='lightNavigation'>"+
						"<div class='lightPrev'>prev</div>"+
						"<div class='lightNavigationSpace'></div>"+ 
						"<div class='lightNext'>next</div>"+
					"</div>"+
					"<div class='clr'></div>"+
				"</div>"+
			"</div>"	);
		
		/* przywiązanie nawigacji i zamknięcia */
		$(".lightPrev").click(previousClick);
		$(".lightNext").click(nextClick);
		$(".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 (checkForSomethingInUrl('#'+slug)) {
				var currentUrlSplit = location.href.split('#'+slug);
				location.href = currentUrlSplit[0]+'#'+slug;
			}
		});
	}	
	
	if (aLight.length>0) {
	
		/* stwórz html */
		createLightWrapper();
		
		/* załaduj 1 obrazek po załadowaniu strony - preload */
		showCurrentImage();
	
	}
	
	/* sprawdź czy coś pokazać od razu */
	checkForImageInUrl();
	
});

