// lightbox_plus.js
// == written by Takuya Otani <takuya.otani@gmail.com> ===
// == Copyright (C) 2006 SimpleBoxes/SerendipityNZ Ltd. ==
/*	Copyright (C) 2006 Takuya Otani/SimpleBoxes - http://serennz.cool.ne.jp/sb/*/
/*	Original script : Lightbox JS : Fullsize Image Overlays*/

var popupHandle;
var popupHandleR;
var noborder;

function closePopup() {
if(popupHandle != null && !popupHandle.closed) popupHandle.close();
}

function displayPopup(url,width,height,bars,resize) {
closePopup();
if (bars=="") bars="0";
popupHandle = window.open(url,"","height="+height+",width="+(width)+",scrollbars="+bars+",resizable="+resize);
//alert(width+'--'+height);
//popupHandle = window.open(url,"",'width=555,height=444');
popupHandle.moveTo(500,300);
popupHandle.focus();
}


function closePopupR() {
if(popupHandleR != null && !popupHandleR.closed) popupHandleR.close();
}

function displayPopupR(url,width,height,bars,resize) {
closePopupR();
if (bars=="") bars="0";
popupHandleR = window.open(url,"","height="+height+",width="+width+",status=no,scrollbars="+bars+",resizable="+resize);
popupHandleR.focus();
}



function addEvent(object, type, handler)
{
	if (object.addEventListener) {
		object.addEventListener(type, handler, false);
	} else if (object.attachEvent) {
		object.attachEvent(['on',type].join(''),handler);
	} else {
		object[['on',type].join('')] = handler;
	}
}
function WindowSize()
{ // window size object
	this.w = 0;
	this.h = 0;
	return this.update();
}
WindowSize.prototype.update = function()
{
	var d = document;
	this.w = 
	  (window.innerWidth) ? window.innerWidth
	: (d.documentElement && d.documentElement.clientWidth) ? d.documentElement.clientWidth
	: d.body.clientWidth;
	this.h = 
	  (window.innerHeight) ? window.innerHeight
	: (d.documentElement && d.documentElement.clientHeight) ? d.documentElement.clientHeight
	: d.body.clientHeight;
	return this;
};
function PageSize()
{ // page size object
	this.win = new WindowSize();
	this.w = 0;
	this.h = 0;
	return this.update();
}
PageSize.prototype.update = function()
{
	var d = document;
	this.w = 
	  (window.innerWidth && window.scrollMaxX) ? window.innerWidth + window.scrollMaxX
	: (d.body.scrollWidth > d.body.offsetWidth) ? d.body.scrollWidth
	: d.body.offsetWidt;
	this.h = 
	  (window.innerHeight && window.scrollMaxY) ? window.innerHeight + window.scrollMaxY
	: (d.body.scrollHeight > d.body.offsetHeight) ? d.body.scrollHeight
	: d.body.offsetHeight;
	this.win.update();
	if (this.w < this.win.w) this.w = this.win.w;
	if (this.h < this.win.h) this.h = this.win.h;
	return this;
};
function PagePos()
{ // page position object
	this.x = 0;
	this.y = 0;
	return this.update();
}
PagePos.prototype.update = function()
{
	var d = document;
	this.x =
	  (window.pageXOffset) ? window.pageXOffset
	: (d.documentElement && d.documentElement.scrollLeft) ? d.documentElement.scrollLeft
	: (d.body) ? d.body.scrollLeft
	: 0;
	this.y =
	  (window.pageYOffset) ? window.pageYOffset
	: (d.documentElement && d.documentElement.scrollTop) ? d.documentElement.scrollTop
	: (d.body) ? d.body.scrollTop
	: 0;
	return this;
};
function UserAgent()
{ // user agent information
	var ua = navigator.userAgent;
	this.isWinIE = this.isMacIE = false;
	this.isGecko  = ua.match(/Gecko\//);
	this.isSafari = ua.match(/AppleWebKit/);
	this.isOpera  = window.opera;
	if (document.all && !this.isGecko && !this.isSafari && !this.isOpera) {
		this.isWinIE = ua.match(/Win/);
		this.isMacIE = ua.match(/Mac/);
		this.isNewIE = (ua.match(/MSIE 5\.5/) || ua.match(/MSIE 6\.0/));
	}
	return this;
}
// === lightbox ===
function LightBox(option)
{  	 
	var self = this;
	self._imgs = new Array();
	self._wrap = null;
	self._box  = null;
	self._open = -1;
	self._page = new PageSize();
	self._pos  = new PagePos();
	self._ua   = new UserAgent();
	self._expandable = false;
	self._expanded = false;
	self._expand = option.expandimg;
	self._shrink = option.shrinkimg;
	self._next = option.next;
	self._prev = option.prev;
	return self._init(option);
}

//ii
var imgnow = 0;
var imgcount = 0;

LightBox.prototype = {
	_init : function(option)
	{
		var self = this;
		var d = document;
		if (!d.getElementsByTagName) return;
		var links = d.getElementsByTagName("a");
		for (var i=0;i<links.length;i++) {
			var anchor = links[i];
			var num = self._imgs.length;
			if (!anchor.getAttribute("href")
			  || anchor.getAttribute("rel") != "lightbox") continue;
			// initialize item
//			alert(anchor.getAttribute("href"));
			imgcount++; //ii
			self._imgs[num] = {src:anchor.getAttribute("href"),w:-1,h:-1,title:'',nid:'',cls:anchor.className,nid:anchor.getAttribute("nid"),vote:anchor.getAttribute("vote"),exif:anchor.getAttribute("exif"),name:anchor.getAttribute("name")};
			
			if (anchor.getAttribute("title")) self._imgs[num].title = anchor.getAttribute("title");
				  else  self._imgs[num].title = anchor.getAttribute("tip");
			if (anchor.getAttribute("node")) self._imgs[num].nid = anchor.getAttribute("nid");

      
			anchor.onclick = self._genOpener(num); // set closure to onclick event
		}
		var body = d.getElementsByTagName("body")[0];
		self._wrap = self._createWrapOn(body,option.loadingimg);
		self._box  = self._createBoxOn(body,option);
		return self;
	},
	_genOpener : function(num)
	{
		var self = this;
		return function() { self._show(num); return false; }
	},
	_createWrapOn : function(obj,imagePath)
	{
		var self = this;
		if (!obj) return null;
		// create wrapper object, translucent background
		var wrap = document.createElement('div');
		if (location.href.indexOf('/node/')>0)  wrap.id = 'overlay2';  else   wrap.id = 'overlay';		
		with (wrap.style) {
			display = 'none';
			position = 'fixed';
			top = '0px';
			left = '0px';
			zIndex = '50';
			width = '100%';
			height = '100%';			
		}
		if (self._ua.isWinIE) wrap.style.position = 'absolute';
		wrap.style.cursor = 'hand';
		addEvent(wrap,"click",function() { self._close(); });
		addEvent(wrap,"mouseover",function() { self._mover(); });
		addEvent(wrap,"mouseout",function() { self._mout(); });
		obj.appendChild(wrap);
		// create loading image, animated image
		var imag = new Image;
		imag.onload = function() {					
			var spin = document.createElement('img');
			spin.setAttribute('galleryimg','no');
			spin.id = 'loadingImage';
			spin.src = imag.src;
			spin.style.position = 'relative';
			spin.style.width = '40px'; //ii
			spin.style.height = '40px';
			self._set_cursor(spin);
			spin.style.cursor = 'hand';
			addEvent(spin,'click',function() { self._close(); });			
			wrap.appendChild(spin);
			imag.onload = function(){};
		};
		if (imagePath != '') imag.src = imagePath;
		return wrap;
	},

	_createBoxOn : function(obj,option)
	{
		var self = this;
		if (!obj) return null;
		// create lightbox object, frame rectangle
		var box = document.createElement('div');		 
    box.id = 'lightbox';
		with (box.style) {
			display = 'none';
			position = 'absolute';
			zIndex = '60';
		}
		addEvent(box,"mouseover",function() { self._mover(); });
		obj.appendChild(box);

		// create image object to display a target image
		var img = document.createElement('img');
		img.id = 'lightboxImage';
		self._set_cursor(img);
		img.style.cursor = 'hand';
		addEvent(img,'click',function(){ self._close(); });
		addEvent(img,'mouseover',function(){ self._show_action(); });
		addEvent(img,'mouseout',function(){ self._hide_action(); });
		addEvent(img,"mouseover",function() { self._mover(); });
		box.appendChild(img);
		var zoom = document.createElement('img');
		zoom.id = 'actionImage';
		with (zoom.style) {
			display = 'none';
			position = 'absolute';
			top = '15px';
			left = '15px';
			zIndex = '70';
		}
		self._set_cursor(zoom);
		zoom.src = self._expand;
		addEvent(zoom,'mouseover',function(){ self._show_action(); });
		addEvent(zoom,'click', function() { self._zoom(); });
		box.appendChild(zoom);
		addEvent(window,'resize',function(){ self._set_size(true); });
//ii
		var next = document.createElement('img');
		next.id = 'actionNext';
		with (next.style) {
			display = 'none';
			position = 'absolute';
			bottom = '25px';
			right = '25px';
			zIndex = '55';
		}
		next.title = 'Next';
		self._set_cursor(next);
		next.src = self._next;
		addEvent(next,'mouseover',function(){ self._show_action(); });
		addEvent(next,'click', function() { self._nextf(); });
		box.appendChild(next);

		var prev = document.createElement('img');
		prev.id = 'actionPrev';
		with (prev.style) {
			display = 'none';
			position = 'absolute';
			bottom = '25px';
			right = '65px';
			zIndex = '54';
		}
		prev.title = 'Previous';
		self._set_cursor(prev);
		prev.src = self._prev;
		addEvent(prev,'mouseover',function(){ self._show_action(); });
		addEvent(prev,'click', function() { self._prevf(); });
		box.appendChild(prev);

		// close button
		if (option.closeimg) {
			var btn = document.createElement('img');
			btn.id = 'closeButton';
			with (btn.style) {
				display = 'inline';
				position = 'absolute';
				right = '8px';
				top = '8px';
				zIndex = '80';
			}
			btn.src = option.closeimg;
			self._set_cursor(btn);
			addEvent(btn,'click',function(){ self._close(); }); 
			box.appendChild(btn);
		}
		// caption text
		var caption = document.createElement('span');
		caption.id = 'lightboxCaption';
		with (caption.style) {
			display = 'none';
			position = 'absolute';
			zIndex = '80';
		}
		box.appendChild(caption);

// exifspan text
		var exifspan = document.createElement('span');
		exifspan.id = 'lightboxExif';
		with (exifspan.style) {
			display = 'none';
			position = 'absolute';
			zIndex = '115';
		}
		box.appendChild(exifspan);

		// create effect image
		if (!option.effectpos) option.effectpos = {x:0,y:0};
		else {
			if (option.effectpos.x == '') option.effectpos.x = 0;
			if (option.effectpos.y == '') option.effectpos.y = 0;
		}
		var effect = new Image;
		effect.onload = function() {
			var effectImg = document.createElement('img');
			effectImg.id = 'effectImage';
			effectImg.src = effect.src;
			if (option.effectclass) effectImg.className = option.effectclass;
			with (effectImg.style) {
				position = 'absolute';
				display = 'none';
				left = [option.effectpos.x,'px'].join('');;
				top = [option.effectpos.y,'px'].join('');
				zIndex = '90';
			}
			self._set_cursor(effectImg);
			addEvent(effectImg,'click',function() { effectImg.style.display = 'none'; });
			box.appendChild(effectImg);
		};
		if (option.effectimg != '') effect.src = option.effectimg;
		return box;
	},
	_set_photo_size : function()
	{
		var self = this;
		if (self._open == -1) return;
		var imag = self._box.firstChild;
		var targ = { w:self._page.win.w - 30, h:self._page.win.h - 50 };
		var orig = { w:self._imgs[self._open].w, h:self._imgs[self._open].h };
		// shrink image with the same aspect
		var ratio = 1.0;
		if ((orig.w >= targ.w || orig.h >= targ.h) && orig.h && orig.w)
			ratio = ((targ.w / orig.w) < (targ.h / orig.h)) ? targ.w / orig.w : targ.h / orig.h;
		imag.width  = Math.floor(orig.w * ratio);
		imag.height = Math.floor(orig.h * ratio);
		self._expandable = (ratio < 1.0) ? true : false;
		if (self._ua.isWinIE) self._box.style.display = "block";
		self._box.style.top  = [self._pos.y + (self._page.win.h - imag.height - 40) / 2,'px'].join('');
		self._box.style.left = [((self._page.win.w - imag.width - 30) / 2),'px'].join('');
		self._show_caption(true);
		self._show_exif(true);
	},
	_set_size : function(onResize)
	{
		var self = this;
		if (self._open == -1) return;
		self._page.update();
		self._pos.update();
		var spin = self._wrap.firstChild; //loader
		if (spin) {
			var top = (self._page.win.h - spin.height) / 2;
			if (self._wrap.style.position == 'absolute') top += self._pos.y;
			spin.style.top  = [top,'px'].join('');
			spin.style.left = [(self._page.win.w - spin.width - 30) / 2,'px'].join('');
		}
		if (self._ua.isWinIE) {
			self._wrap.style.width  = [self._page.win.w,'px'].join('');
			self._wrap.style.height = [self._page.h,'px'].join('');
		}
		if (onResize) self._set_photo_size();
	},
	
	_show_action : function()
	{
		var self = this;
//ii
		var obj = document.getElementById('actionNext');
		if (obj  && (imgnow<imgcount-1)) {
			obj.src = self._next;
			obj.style.display = 'inline';
		}

		var obj = document.getElementById('actionPrev');
		if (obj  && (imgnow>0)) {
			obj.src = self._prev;
			obj.style.display = 'inline';
		}


		if (self._open == -1 || !self._expandable) return;
		var obj = document.getElementById('actionImage');
		if (obj) {
			obj.src = (self._expanded) ? self._shrink : self._expand;
			obj.style.display = 'inline';
		}
	},
	_hide_action : function()
	{
		var self = this;
		var obj = document.getElementById('actionImage');
		if (obj) obj.style.display = 'none';

		var obj = document.getElementById('actionNext');
		if (obj) obj.style.display = 'none';

		var obj = document.getElementById('actionPrev');
		if (obj) obj.style.display = 'none';

	},
	_zoom : function()
	{
		var self = this;
		if (self._expanded) {
			self._set_photo_size();
			self._expanded = false;
		} else if (self._open > -1) {
			var imag = self._box.firstChild;
			//self._box.style.top  = [self._pos.y,'px'].join('');
			//self._box.style.left = '0px';			
			imag.width  = self._imgs[self._open].w;
			imag.height = self._imgs[self._open].h;
			
			ww = (self._page.win.w-imag.width-30)/2;
			if (ww<0) ww=0;
			self._box.style.left = ww+'px';
			ww = (self._page.win.h-imag.height-30)/2;
			if (ww<0) ww=0;
			self._box.style.top = (self._pos.y+ww)+'px';
			
			
			self._show_caption(false);
			self._show_exif(false);
			self._expanded = true;
		}
		self._show_action();
	},

	
	_show_caption : function(enable)
	{
		var self = this;
		var caption = document.getElementById('lightboxCaption');
		if (!caption) return;
		if (caption.innerHTML.length == 0 || !enable) {
			caption.style.display = 'none';
		} else { // now display caption
			var imag = self._box.firstChild;
			with (caption.style) {
				top = [imag.height + 10,'px'].join(''); // 10 is top margin of lightbox
				left = '0px';
				width = [imag.width + 20,'px'].join(''); // 20 is total side margin of lightbox
				height = '1.2em';
				display = 'block';
			}
		}
	},

	_show_exif : function(enable)
	{
		var self = this;
		var exif = document.getElementById('lightboxExif');
		if (!exif) return;
		if (exif.innerHTML.length == 0 || !enable) {
			exif.style.display = 'none';
		} else { // now display exif
			var imag = self._box.firstChild;
			with (exif.style) {
				top = [imag.height + 10,'px'].join(''); // 10 is top margin of lightbox
				left = [imag.width - 150,'px'].join(''); // 20 is total side margin of lightbox
				width = [110,'px'].join(''); // 20 is total side margin of lightbox
				height = '1em';
				display = 'inline';
			}
		}
	},


	_show : function(num)
	{

		imgnow=num;
		var self = this;
		var st_links = ''; 
		var imag = new Image;
		if (num < 0 || num >= self._imgs.length) return;
		var loading = document.getElementById('loadingImage');
		var caption = document.getElementById('lightboxCaption');
		var exif = document.getElementById('lightboxExif');
		var effect = document.getElementById('effectImage');
		self._open = num; // set opened image number
		self._set_size(false); // calc and set wrapper size
		self._wrap.style.display = "block";
		
	
		
	if (self._imgs[self._open].src.indexOf('/files/cameras/')==-1 && self._imgs[self._open].src.indexOf('/files/lenses/') == -1) {
		if (self._imgs[self._open].exif == 'noborder') {		  
		   self._box.style.padding = "0px 0px";
		   self._box.style.border="0px";
		   self._box.style.marginTop="12px";
		   self._box.style.marginLeft="12px";
      noborder=true;    
    } else {      
		    self._box.style.padding = "10px 10px 15px 10px";
		    self._box.style.border="2px solid #666";
		    self._box.style.marginTop="0px";
		    self._box.style.marginLeft="0px";
      noborder=false;    
    }
  }
		
		if (caption && !noborder) caption.innerHTML = '<blink>Loading...</blink>';
		if (loading) loading.style.display = 'inline';
		imag.onload = function() {
			if (self._imgs[self._open].w == -1) {
				// store original image width and height
				self._imgs[self._open].w = imag.width;
				self._imgs[self._open].h = imag.height;				
				
			}
			if (effect) {
				effect.style.display = (!effect.className || self._imgs[self._open].cls == effect.className)
					? 'block' : 'none';
			}
			var nam;
			nam = self._imgs[self._open].name; 
      if (nam==null || nam=='') nam=''; else nam='<b>'+nam+'</b>: ';
			if (caption && !noborder && self._imgs[self._open].title) caption.innerHTML = nam+self._imgs[self._open].title; else caption.innerHTML = '';
			self._set_photo_size(); // calc and set lightbox size
			self._hide_action();
			self._box.style.display = "block";     			
			self._box.firstChild.src = imag.src;
			//self._box.firstChild.setAttribute('title',self._imgs[self._open].title); // - image title
			if (loading) loading.style.display = 'none';
			
  		// node count
  		if (self._imgs[self._open].nid) HTTPGet('/ajax_count_node/'+self._imgs[self._open].nid.substring(6));	
  		
		};
		self._expandable = false;
		self._expanded = false;
		imag.src = self._imgs[self._open].src;		
		if (exif && self._imgs[self._open].exif != 'noborder' && self._imgs[self._open].exif)
		{		// EXIF = nuoroda i foto puslapi (komentuoti) 
//		alert('aaaaaaaa');
			st_links = '<div class="l_links"><a href="'+self._imgs[self._open].nid+'" title="Nuotraukos puslapis [komentarai]">'+self._imgs[self._open].exif+'</a>';
      if (self._imgs[self._open].vote=='true') 
        st_links += '&nbsp;&nbsp;<b><a href="javascript:void();" onclick="displayPopup(\''+
        self._imgs[self._open].nid+
        '/komentaras\',370,210,\'yes\',\'yes\');"title="Rašyti komentara">Komentuoti</a></b></div>';
       else if (self._imgs[self._open].vote=='fotoistorija') st_links += '&nbsp;<b>Fotoistorija</b>'; 
			exif.innerHTML = st_links;			
			self._show_exif(true);
		} else exif.innerHTML = '';
	},
	_set_cursor : function(obj)
	{
		var self = this;
		if (self._ua.isWinIE && !self._ua.isNewIE) return;
		obj.style.cursor = 'pointer';
	},
	_close : function()
	{
		var self = this;
		closePopup();
		self._open = -1;
		self._hide_action();
		self._wrap.style.display = "none";
		self._box.style.display  = "none";
	},
	
	_mover : function()
	{
	var self = this;
			  self._box.style.padding = "10px 10px 15px 10px";
		    self._box.style.border="2px solid #666";
		    self._box.style.marginTop="0px";
		    self._box.style.marginLeft="0px";
		    //self._wrap.style.background="#000000";  
	},
	
	_mout : function()
	{
		var self = this;
			self._box.style.padding = "0px 0px";
		   self._box.style.border="0px";
		   self._box.style.marginTop="12px";
		   self._box.style.marginLeft="12px";
	},

	_nextf : function() //ii
	{
		var self = this;
		closePopup();
		self._show(imgnow+1);		
	},

	_prevf : function() //ii
	{
		var self = this;
		closePopup();
		self._show(imgnow-1);
	}


};
// === main ===
addEvent(window,"load",function() {	
	var mydir = 'client/img/';
	var lightbox = new LightBox({
		next:mydir+'right.gif', //ii
		prev:mydir+'left.gif', //ii
		loadingimg:mydir+'loading.gif',
		expandimg:mydir+'expand.gif',
		shrinkimg:mydir+'shrink.gif',
		effectimg:mydir+'zzoop.gif',
		effectpos:{x:-40,y:-20},
		effectclass:'effectable',
		closeimg:mydir+'close.gif'
	});
});

