//singleton version V1.0 13.12.08 based on lytebox V3.0 http://www.dolem.com/lytebox//
var LBX = {	
	//global variables	
	tranzicija:0, //koristenje tranzicije
	borderSize: 12,
	
	dok: null,
	isFrame: false,
	baserel: "lytebox",
	currentimg :0,
	imageArray :[],
	winX:0,
	winY:0,
	enumlinks: function (baserel) {	//public funkcija
		if (! document.getElementById || !document.childNodes || !document.createElement ) {return;} //no DOM support
		if (!baserel) {baserel="lytebox";}
		this.baserel=baserel;
		var anchors = (this.isFrame) ? window.parent.frames[window.name].document.getElementsByTagName('a') : document.getElementsByTagName('a');
		for (var i = 0; i < anchors.length; i++) {
			var anchor = anchors[i];
			if (anchor.getAttribute('href')) {
				var relAttribute = String(anchor.getAttribute('rel'));
				if (relAttribute.toLowerCase().match(baserel)) {anchor.onclick = function () { LBX.F_gallery(this); return false; }}
			}
		}
	},
	lbx : function (caller,url) {		//public funkcija
		if (! document.getElementById || !document.childNodes || !document.createElement ) {return;} //no DOM support
		var tmp=caller;
		this.currentimg=0;
		this.imageArray = [];
		while (tmp) {	//ako je slika u linku onemoguci lightbox
			tmp=tmp.parentNode;
			if (tmp.nodeName =='A') {
				return;
			}else if (tmp.nodeName =='BODY') {
				break;
			}
		}
		this.imageArray[0]=[url, caller.getAttribute('title')];
		LBX.F_lightbox();
	},
	
	F_gallery: function(caller) {
		var rel=caller.getAttribute('rel');
		var j =0;
		this.currentimg=0;
		this.imageArray = [];
		if (rel == this.baserel) { 
			 this.imageArray[0]= [caller.getAttribute('href'), caller.getAttribute('title')];
			LBX.F_lightbox();
		}else if (rel.toLowerCase().match(this.baserel)){
			var anchors = (this.isFrame) ? window.parent.frames[window.name].document.getElementsByTagName('a') : document.getElementsByTagName('a');
			for (var i = 0; i < anchors.length; i++) {
				var anchor = anchors[i];
				if (anchor.getAttribute('href') && (anchor.getAttribute('rel') == rel)) {
					if (anchor.getAttribute('href')==caller.getAttribute('href')) {this.currentimg=j;}
					this.imageArray[j]=[anchor.getAttribute('href'), anchor.getAttribute('title')];
					j++;
				}
			}
			if (j) {LBX.F_lightbox();}
		}
	},

	F_end :  function (){
		this.currentimg=-1;
		 document.onkeydown = '';
		this.dok.getElementById('LBMain').style.display = 'none';
		LBX.F_fadeout( (this.tranzicija) ? 80 :1 );
		LBX.F_hideshow(1);
		return false;
	},

	F_hideshow : function(state) {
		state=(state)? 'visible':'hidden';
		var tags= ["object","embed","select"];
		var objects;
		for (t in tags){
			objects = this.dok.getElementsByTagName(tags[t]);	
			for (var i = 0; i < objects.length; i++) {objects[i].style.visibility = state;}
		}	
		if (!this.isFrame) {return;}
		for (var j = 0; j < parent.frames.length; j++) {
			for (t in tags){
				try {
					objects = parent.frames[j].window.document.getElementsByTagName(tags[t]);	
					for (var i = 0; i < objects.length; i++) {objects[i].style.visibility = state;}
				} catch(e) { /* ignore */ }
			}		
		}
	},	

	F_fadeout : function (opacity) {
		var object = this.dok.getElementById('LBOverlay').style;
		if (opacity <= 1) {
			try {object.display = 'none';} catch(e) {}
			return;
		}
		object.opacity = (opacity / 100);
		object.MozOpacity = (opacity / 100);
		object.KhtmlOpacity = (opacity / 100);
		object.filter = "alpha(opacity=" + opacity + ")";
		setTimeout("LBX.F_fadeout(" + (opacity-20) + ")", 1);
	},
	
	F_fadein : function (id,opacity) {
		if (this.currentimg==-1) {return;}
		var object =this.dok.getElementById(id).style;
		object.opacity = (opacity / 100);
		object.MozOpacity = (opacity / 100);
		object.KhtmlOpacity = (opacity / 100);
		object.filter = "alpha(opacity=" + (opacity) + ")";
		if (opacity >= 99 && (id == 'LBImage')) {
			try { object.removeAttribute("filter"); } catch(e) {} /* Fix added for IE Alpha Opacity Filter bug. */
			 LBX.F_updateInfo();
		} else if (opacity >= 80 && id == 'LBOverlay') {
			 LBX.F_showlbx();
		} else if (opacity >= 100 && id == 'LBInfoCnt') {
			try { object.removeAttribute("filter"); } catch(e) {}	/* Fix added for IE Alpha Opacity Filter bug. */
		}else {
			setTimeout("LBX.F_fadein('" + id + "', " + (opacity+20)+ ")", 5);
		}
	},			
	
	F_init: function() {
		if (this.dok) {return;}
		this.isFrame = false;
		this.dok = document;
		if (window.parent.frames[window.name] && (parent.document.getElementsByTagName('frameset').length <= 0)) {
			this.isFrame = true;
			this.dok = parent.document;
		} 
		var a = this.dok.getElementsByTagName("body").item(0);
		if (this.dok.getElementById('LBOverlay')) {
			a.removeChild(this.dok.getElementById("LBOverlay"));
			a.removeChild(this.dok.getElementById("LBMain"));
		}
		
		var b = this.dok.createElement("div");
		b.setAttribute('id','LBOverlay');
		b.style.position = 'absolute';
		b.style.display = 'none';
		a.appendChild(b);
	
		var oLBox = this.dok.createElement("div");
		oLBox.setAttribute('id','LBMain');
		oLBox.style.display = 'none';
		a.appendChild(oLBox);
		
		var Cnt = this.dok.createElement("div");
		Cnt.setAttribute('id','LBImgCnt');
		oLBox.appendChild(Cnt);

		a = this.dok.createElement("img");
		a.setAttribute('id','LBImage');
		Cnt.appendChild(a);
		
		a = this.dok.createElement("div");
		a.setAttribute('id','LBLoading');
		Cnt.appendChild(a);
		
		a = this.dok.createElement("div");
		a.setAttribute('id','LBHoverNav');
		Cnt.appendChild(a);
	
		b = this.dok.createElement("a");
		b.setAttribute('id','LBPrev');
		b.setAttribute('href','#');
		a.appendChild(b);
	
		b = this.dok.createElement("a");
		b.setAttribute('id','LBNext');
		b.setAttribute('href','#');
		a.appendChild(b);
	
		Cnt = this.dok.createElement("div");
		Cnt.setAttribute('id','LBInfoCnt');
		oLBox.appendChild(Cnt);
		a = this.dok.createElement("div");
		Cnt.appendChild(a);
		
		b = this.dok.createElement("div");
		a.appendChild(b);
		
		b = this.dok.createElement("span");
		b.setAttribute('id','LBCaption');
		a.appendChild(b);
		
		b = this.dok.createElement("span");
		b.setAttribute('id','LBNumberDisplay');
		a.appendChild(b);
	},		

	F_lightbox: function (){
		if (!this.dok) { LBX.F_init();}
		var xScroll, yScroll,winX,winY;
		var tmp;
		if (self.innerHeight) {// all except Explorer
			winX = (this.isFrame) ? parent.innerWidth : self.innerWidth;
			winY = (this.isFrame) ? parent.innerHeight : self.innerHeight;
		} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
			winX = this.dok.documentElement.clientWidth;
			winY = this.dok.documentElement.clientHeight;
		} else if (document.body) { // other Explorers
			winX =this.dok.body.clientWidth;
			winY = this.dok.body.clientHeight;
		}
		if (winX<100 ||winY<100) {return;}
		this.winX= winX;
		this.winY= winY;
		if (window.innerHeight && window.scrollMaxY) {
			if (this.isFrame) {
				xScroll = parent.innerWidth + parent.scrollMaxX ;
				yScroll = parent.innerHeight  + parent.scrollMaxY ;
			}else {
				xScroll =  self.innerWidth +  self.scrollMaxX;
				yScroll = self.innerHeight +  self.scrollMaxY;
			}
		
		} else if (LBX.dok.body.scrollHeight > LBX.dok.body.offsetHeight){ // all but Explorer Mac
			xScroll = this.dok.body.scrollWidth;
			yScroll = this.dok.body.scrollHeight;
		} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
			tmp=this.dok.getElementsByTagName("html").item(0);
			xScroll = tmp.offsetWidth;
			yScroll = tmp.offsetHeight;
			// Strict mode fixes
			if (xScroll < this.dok.body.offsetWidth) {xScroll=this.dok.body.offsetWidth;}
			if (yScroll < this.dok.body.offsetHeight) {yScroll=this.dok.body.offsetHeight;}
		}
		tmp=this.dok.getElementById('LBImgCnt').style;
		tmp.height='100px';
		tmp.width='100px';
		tmp=this.dok.getElementById('LBOverlay');
		tmp.style.height=((yScroll < winY) ? winY : yScroll)+ "px";
		tmp.style.width=((xScroll < winX) ? winX : xScroll)+ "px";
		tmp.onclick=function() { LBX.F_end(); return false; }
		tmp.style.display = 'block';
		this.dok.getElementById('LBInfoCnt').onclick = function() { LBX.F_end(); return false; }	
		this.dok.getElementById('LBMain').onclick = function(e) {
			if (!e) {
				if (window.parent.frames[window.name] && (parent.document.getElementsByTagName('frameset').length <= 0)) {
					e = window.parent.window.event;
				} else {
					e = window.event;
				}
			}
			if ( (e.target ? e.target.id : e.srcElement.id)== 'LBMain'){ LBX.F_end();}
			return false;
		}

		yScroll=0;
		xScroll=0;
		 if (self.pageYOffset) {
		 	xScroll=  this.isFrame ? parent.pageXOffset : self.pageXOffset;
			yScroll=  this.isFrame ? parent.pageYOffset : self.pageYOffset;
		} else if (document.documentElement && this.dok.documentElement.scrollTop){
			xScroll =this.dok.documentElement.scrollLeft;
			yScroll =this.dok.documentElement.scrollTop;
	 	} else if (document.body) {
	 		xScroll = this.dok.body.scrollLeft;
			yScroll = this.dok.body.scrollTop;
		}
		LBX.dok.getElementById('LBMain').style.top =yScroll+(winY/15) + "px";
		LBX.dok.getElementById('LBMain').style.left =xScroll+ "px";
		LBX.F_hideshow(0); 
		LBX.F_fadein('LBOverlay',(this.tranzicija) ? 0 :80);
	},
	
	F_showlbx : function() {
		this.dok.getElementById('LBInfoCnt').style.display = 'none';
		this.dok.getElementById('LBImgCnt').style.borderBottom = '';
		this.dok.getElementById('LBLoading').style.display = '';
		this.dok.getElementById('LBImage').style.display = 'none';
		this.dok.getElementById('LBPrev').style.display = 'none';
		this.dok.getElementById('LBNext').style.display = 'none';
		this.dok.getElementById('LBInfoCnt').style.display = 'none';
		this.dok.getElementById('LBNumberDisplay').style.display = 'none';
		this.dok.getElementById('LBMain').style.display='';
		var imgPreloader = new Image();
		imgPreloader.onload = function() {
			var imgW = imgPreloader.width;
			var imgH = imgPreloader.height;
			var x =LBX.winX - 150;
			var y =LBX.winY - 150;
			if (imgW > x) {
				imgH = Math.round(imgH * (x / imgW));
				imgW = x; 
				if (imgH > y) { 
					imgW = Math.round(imgW * (y / imgH));
					imgH = y; 
				}
			} else if (imgH > y) { 
				imgW = Math.round(imgW * (y / imgH));
				imgH = y; 
				if (imgW > x) {
					imgH = Math.round(imgH * (x / imgW));
					imgW = x;
				}
			}
			var LBImage = LBX.dok.getElementById('LBImage')
			LBImage.src = LBX.imageArray[LBX.currentimg][0];
			LBImage.width = imgW;
			LBImage.height = imgH;
			LBX.dok.getElementById('LBPrev').style.height = imgH + "px";
			LBX.dok.getElementById('LBNext').style.height = imgH + "px";
			
			
			LBX.F_resize (imgW, imgH);
			imgPreloader.onload = function() {};
		}
		imgPreloader.src = this.imageArray[LBX.currentimg][0];
	},
	
	F_resize: function (imgWidth, imgHeight) {
		var step=10;
		var style = this.dok.getElementById('LBImgCnt').style;
		var tmp=parseInt(style.height);
		var max= imgHeight + this.borderSize*2;
		if (tmp < max) {
			tmp +=step;
			if (tmp>max) {tmp=max;}
			style.height=tmp +'px';
		}else if (tmp > max){
			tmp -=step;
			if (tmp<max) {tmp=max;}
			style.height=tmp +'px';
		}else {
			tmp=parseInt(style.width);
			max= imgWidth + this.borderSize*2;
			if (tmp < max) {
				tmp +=step;
				if (tmp>max) {tmp=max;}
				style.width=tmp +'px';
			}else if (tmp > max){
				tmp -=step;
				if (tmp<max) {tmp=max;}
				style.width=tmp +'px';
			}else {
				this.dok.getElementById('LBInfoCnt').style.width = style.width; 
				this.dok.getElementById('LBImgCnt').style.borderBottom = 'none';
				this.dok.getElementById('LBLoading').style.display = 'none';
				this.dok.getElementById('LBImage').style.display = '';
				LBX.F_fadein('LBImage', 0);
				if ((this.imageArray.length - 1) > this.currentimg) {
					var preload1 = new Image();
					preload1.src = this.imageArray[this.currentimg + 1][0];
					this.dok.getElementById('LBHoverNav').style.display = '';
				}
				if(this.currentimg > 0) {
					var preload2 = new Image();
					preload2.src = this.imageArray[this.currentimg - 1][0];
					this.dok.getElementById('LBHoverNav').style.display = '';
				}
				return;
			}
		}
		setTimeout("LBX.F_resize(" + imgWidth + "," + imgHeight+")", 10)
	},
	
	F_updateInfo :  function(){	
		var object = this.dok.getElementById('LBCaption');
		var sTitle = this.imageArray[this.currentimg][1];
		object.style.display = '';
		object.innerHTML = (sTitle == null ? '' : sTitle);
		if(this.currentimg != 0) {
			var object =this.dok.getElementById('LBPrev');
			object.style.display = '';
			object.onclick = function() {  LBX.currentimg --;  LBX.F_showlbx(); return false;}
		} 
		if(this.currentimg != (this.imageArray.length - 1)) {
			var object = this.dok.getElementById('LBNext');
			object.style.display = '';
			object.onclick = function() { LBX.currentimg++;  LBX.F_showlbx(); return false;}
		} 
		this.dok.getElementById('LBInfoCnt').style.display = '';
		object = this.dok.getElementById('LBNumberDisplay');
		if (this.imageArray.length > 1) {
			object.style.display = '';
			object.innerHTML = "Slika " + eval(this.currentimg + 1) + " of " + this.imageArray.length;
		} else {
			object.style.display = 'none';
		}
		LBX.F_fadein('LBInfoCnt', 0);
		document.onkeydown = this.keyboardAction;
	},	
	keyboardAction :function(e) {
		var keycode = key = escape = null;
		keycode	= (e == null) ? event.keyCode : e.which;
		key	= String.fromCharCode(keycode).toLowerCase();
		escape  = (e == null) ? 27 : e.DOM_VK_ESCAPE;
		if ((key == 'x') || (keycode == escape)) {
			LBX.F_end();
		} else if ((key == 'p'  || keycode == 109)  && LBX.currentimg>0 )   {
			document.onkeydown ="";
			LBX.currentimg--;
			LBX.F_showlbx();
		} else if ((key == 'n'  || keycode == 107)  &&  LBX.currentimg != (LBX.imageArray.length - 1)) {
			document.onkeydown ="";
			LBX.currentimg++;
			LBX.F_showlbx();
		}
	}
}
