/* LIGHTBOX
 * autor: Jakub Hruby - WEB&DTP (http://web-dtp.cz)
 * release: 1.1 opraven vypocet YOffsetu pro Google Chrome
 * 					1.0 startovaci verze
 *
 * do rootu webu je potreba umistit obrazky sipek: vpred.png a vzad.png
 * odkazy obrazku, ktere se maji otevirat v Lightboxu, musi mit nastaven atribut: rel="lightbox"
 */


/* ----------------------- LAYOUT - MENIT HODNOTY DLE POTREBY -------------------------- */

//barva overlaye
	var LTBBackground="#000000";

//pruhlendost overlaye v %
	var LTBOpacity=60;

//barva pozadi boxu
	var LTBBoxBackground="#ffffff";

//urcuje, zda je nebo neni zobrazeno zapati [true/false]
	var LTBZapati=true;

//barva pozadi zapati
	var LTBZapatiBackground="#ffffff";

//barva fontu titulku
	var LTBTitulekColor="#000000";

//font titulku [font,alternativni font,dalsi alternativni font, ...]
	var LTBTitulekFont="trebuchet ms,arial,sans-serif";

//velikost fontu titulku [px/em];
	var LTBTitulekSize="11px";

//urcuje, zda bude font titulku tucnym rezem [true/false]
	var LTBTitulekBold=true;

//urcuje, zda bude font titulku kurzivou [true/false]
	var LTBTitulekItalic=false;

//zarovnani textu titulku [left/justify/center/right]
	var LTBTitulekAlign="left";

//animovany gif pro loading [implicitne umisteny v rootu webu]
	var LTBLoadingGif="loadingGif.gif";

//rychlost fade in/out prechodu [0-100, 0 a 100 znamena vypnuto]
	var LTBRychlostFade=4;


/* ---------------------------- VYKONNY KOD - NEMENIT ----------------------------- */

var obrazky=new Array();
var aktualni;

//prida event iniciacni funkce do window.onload
if(window.addEventListener){window.addEventListener("load",initLightbox,false);}
else{window.attachEvent("onload",initLightbox);}

//iniciacni funkce - nastavi eventy odkazum s atributem rel="lightbox"
function initLightbox(){
	var odkazy=document.getElementsByTagName("a");
	var j=0;
	for(var i=0;i<odkazy.length;i++){
		if(odkazy[i].getAttribute("rel")=="lightbox"){
			obrazky[j]=new Obrazek(odkazy[i].href);
			obrazky[j].id=j;
			obrazky[j].titulek=odkazy[i].firstChild.getAttribute("alt");
			odkazy[i].onclick=otevriLTBBox;
			j++;
		}
	}
}

//otevre lightbox
function otevriLTBBox(id){
	//prochazeni vsech nactenych lightbox obrazku
	if(parseInt(id)!=""){aktualni=id;}
	for(var i=0;i<obrazky.length;i++){
		//vyber konkretniho obrazku
		if(obrazky[i].src==this.href){
			aktualni=i;
			break;
		}
	}

	//inicializace rozmeru v objektu
	if(!obrazky[aktualni].width){obrazky[aktualni].nactiObrazek();}
	else{obrazky[aktualni].publikuj();}

	return false;
}
//zavre lightbox
function zavriLTBBox(){
	document.body.removeChild(getLTBElement("LTBOverlay"));
	document.body.removeChild(getLTBElement("LTBLoadingGif"));
	document.body.removeChild(getLTBElement("LTBBox"));
}

//funkce listovani
function jdiVpred(){
	document.body.removeChild(getLTBElement("LTBBox"));
	otevriLTBBox(++aktualni);
}
function jdiZpet(){
	document.body.removeChild(getLTBElement("LTBBox"));
	otevriLTBBox(--aktualni);
}

//konstruktor objektu Obrazek
function Obrazek(src){
	this.src=src;
	this.id;
	this.titulek;
}

//metoda objektu Obrazek - nacte rozmery obrazku a publikuje box
Obrazek.prototype.nactiObrazek=function(){
	var obr=this;
	var img=new Image();
	img.onload=function(){
		obr.width=this.width;
		obr.height=this.height;
		obr.publikuj();
	}
	img.src=this.src;
}

//metoda objektu Obrazek - publikuje box (zobrazi box, obrazek, titulek, ovladace)
Obrazek.prototype.publikuj=function(){
	if(!getLTBElement("LTBOverlay")){
		//zobrazeni overlaye
		var overlay=document.createElement("div");
		overlay.style.position="absolute";
		overlay.style.zIndex=100;
		overlay.style.left="0px";
		overlay.style.top="0px";
		overlay.style.width=getLTBSirkaOkna()+"px";
		overlay.style.height=getLTBVyskaDokumentu()+"px";
		overlay.style.background=LTBBackground;
		overlay.style.opacity=LTBOpacity/100;
		overlay.style.filter="alpha(opacity="+LTBOpacity+")";
		overlay.id="LTBOverlay";
		overlay.onclick=zavriLTBBox;
		document.body.appendChild(overlay);

		//zobrazeni nacitaciho gifu
		var gif=document.createElement("img");
		gif.setAttribute("alt","načítám");
		gif.style.position="absolute";
		gif.style.zIndex=101;
		gif.style.width="126px";
		gif.style.height="22px";
		gif.style.left=(getLTBSirkaOkna()-126)/2+"px";
		gif.style.top=(getLTBVyskaOkna()-22)/2+getLTBYOffset()+"px";
		gif.src=LTBLoadingGif;
		gif.id="LTBLoadingGif";
		document.body.appendChild(gif);
	}

	//zobrazeni boxu
	var box=document.createElement("div");
	box.style.position="absolute";
	box.style.zIndex=102;
	box.style.width=this.width+10+"px";
	box.style.height=this.height+10+"px";
	box.style.left=(getLTBSirkaOkna()-parseInt(box.style.width))/2+"px";
	box.style.top=(getLTBVyskaOkna()-parseInt(box.style.height))/2+getLTBYOffset()+"px";
	box.style.background=LTBBoxBackground;
	box.id="LTBBox";
	document.body.appendChild(box);

	//zobrazeni obrazku
	var img=document.createElement("img");
	img.style.margin="5px 5px 0px 5px";
	img.id="LTBImg";
	img.src=this.src;
	img.setAttribute("alt","lightbox");
	box.appendChild(img);

	//zapati
	if(LTBZapati){
		//zapati obrazku
		var zapati=document.createElement("div");
		zapati.style.position="relative";
		zapati.style.margin="0px";
		zapati.style.background=LTBZapatiBackground;
		zapati.style.width=this.width+10+"px";
		zapati.id="LTBZapati";
		box.appendChild(zapati);

		//titulek
		var titulek=document.createElement("p");
		titulek.style.margin="0px";
		titulek.style.padding="5px";
		titulek.style.color=LTBTitulekColor;
		titulek.style.fontFamily=LTBTitulekFont;
		titulek.style.fontSize=LTBTitulekSize;
		titulek.style.textAlign=LTBTitulekAlign;
		if(LTBTitulekBold){titulek.style.fontWeight="bold";}
			else{titulek.style.fontWeight="normal";}
		if(LTBTitulekItalic){titulek.style.fontStyle="italic";}
			else{titulek.style.fontStyle="normal";}
		titulek.id="LTBTitulek";
		titulek.innerHTML=this.titulek;
		zapati.appendChild(titulek);

		//zavirac
		var x=document.createElement("a");
		x.style.fontSize="20px";
		x.style.textDecoration="none";
		x.style.width="15px";
		x.style.height="20px";
		x.style.position="absolute";
		x.style.top="0px";
		x.style.right="5px";
		x.style.color=LTBTitulekColor;
		x.style.background=LTBZapatiBackground;
		x.href="javascript:";
		x.onclick=zavriLTBBox;
		x.innerHTML="×";
		zapati.appendChild(x);
	}
	else{
		//zavirac
		var x=document.createElement("a");
		x.style.display="block";
		x.style.fontSize="15px";
		x.style.fontWeight="bold";
		x.style.textDecoration="none";
		x.style.textAlign="center";
		x.style.width="20px";
		x.style.height="20px";
		x.style.position="absolute";
		x.style.bottom="-20px";
		x.style.right="0px";
		x.style.color=LTBTitulekColor;
		x.style.background=LTBZapatiBackground;
		x.href="javascript:";
		x.onclick=zavriLTBBox;
		x.innerHTML="×";
		box.appendChild(x);
	}

	//ovladani
	if(this.id>0){
		var zpet=document.createElement("a");
		zpet.style.display="block";
		zpet.style.position="absolute";
		zpet.style.left="0px";
		zpet.style.top="0px";
		zpet.style.width=(this.width+10)/2+"px";
		zpet.style.height=this.height+10+"px";
		zpet.style.background="#ffffff";
		zpet.style.opacity=0;
		zpet.style.filter="alpha(opacity=0)";
		zpet.href="javascript:";
		zpet.onclick=jdiZpet;
		zpet.onmouseover=function(){zpet.style.background="url('zpet.png') left top no-repeat";zpet.style.opacity=1;zpet.style.filter="alpha(opacity=100)";}
		zpet.onmouseout=function(){zpet.style.background="#ffffff";zpet.style.opacity=0;zpet.style.filter="alpha(opacity=0)";}
		box.appendChild(zpet);
	}
	if(this.id<(obrazky.length-1)){
		var vpred=document.createElement("a");
		vpred.style.display="block";
		vpred.style.position="absolute";
		vpred.style.right="0px";
		vpred.style.top="0px";
		vpred.style.width=(this.width+10)/2+"px";
		vpred.style.height=this.height+10+"px";
		vpred.style.background="#ffffff";
		vpred.style.opacity=0;
		vpred.style.filter="alpha(opacity=0)";
		vpred.href="javascript:";
		vpred.onclick=jdiVpred;
		vpred.onmouseover=function(){vpred.style.background="url('vpred.png') right top no-repeat";vpred.style.opacity=1;vpred.style.filter="alpha(opacity=100)";}
		vpred.onmouseout=function(){vpred.style.background="#ffffff";vpred.style.opacity=0;vpred.style.filter="alpha(opacity=0)";}
		box.appendChild(vpred);
	}

	//animuje zobrazeni obrazku
	fadeIn(img.id);

}


/*ANIMACNI FUNKCE*/

//fade in
var fadeInTimer;
var fadeInOpacity=0;
function fadeIn(id){
	if(LTBRychlostFade>0 && LTBRychlostFade<100){
		clearTimeout(fadeInTimer);
		fadeInOpacity=fadeInOpacity+LTBRychlostFade;
		if(fadeInOpacity>=100){fadeInOpacity=100;}

		getLTBElement(id).style.opacity=fadeInOpacity/100;
		getLTBElement(id).style.filter="alpha(opacity="+fadeInOpacity+")";

		if(fadeInOpacity<100){
			var prikaz=function(){fadeIn(id);}
			fadeInTimer=setTimeout(prikaz,"10");
		}
		else{fadeInOpacity=0;}
	}
}

//fade out
var fadeOutTimer;
var fadeOutOpacity=100;
function fadeOut(id){
	if(LTBRychlostFade>0 && LTBRychlostFade<100){
		clearTimeout(fadeOutTimer);
		fadeOutOpacity=fadeOutOpacity-LTBRychlostFade;
		if(fadeOutOpacity<=0){fadeOutOpacity=0;}

		getLTBElement(id).style.opacity=fadeOutOpacity/100;
		getLTBElement(id).style.filter="alpha(opacity="+fadeOutOpacity+")";

		if(fadeOutOpacity>0){
			var prikaz=function(){fadeOut(id);}
			fadeOutTimer=setTimeout(prikaz,"10");
		}
		else{
			fadeOutOpacity=100;
			document.body.removeChild(getLTBElement(id));
		}
	}
}


/*GETTERY*/

function getLTBElement(id){return document.getElementById(id);}

function getLTBSirkaOkna(){
	return (document.documentElement.clientWidth)?document.documentElement.clientWidth:document.body.clientWidth;
}

function getLTBVyskaOkna(){
	return (document.documentElement.clientHeight)?document.documentElement.clientHeight:document.body.clientHeight;
}

function getLTBVyskaElementu(el){
	return (window.getComputedStyle)?parseInt(window.getComputedStyle(el,null).getPropertyValue("height")):parseInt(el.offsetHeight);
}

function getLTBYOffset(){
	var max1=0,max2=0,max3=0;
	if(document.documentElement.scrollTop!="undefined"){max1=document.documentElement.scrollTop;}
	if(document.body.scrollTop!="undefined"){max2=document.body.scrollTop;}
	if(window.pageYOffset){max3=window.pageYOffset;}
	return Math.max(max1,max2,max3);
}

function getLTBVyskaDokumentu(){
	var vyska=Math.max(document.body.scrollHeight,document.documentElement.scrollHeight);
	vyska+=document.body.scrollTop;
	if(vyska>getLTBVyskaOkna()){return vyska;}
	else{return getLTBVyskaOkna();}
}
