

var oLBX = new Object;
function pLBX(){	
	
	if(oLBX.options.lb_slideshow==1){			
		oLBX.next();	
		setTimeout('pLBX()',parseInt(oLBX.options.lb_slideshowdur));		
	}
}


function oLBT(){

}


var viewportwidth;
var viewportheight;
 
function getVP(){
	if (typeof window.innerWidth != 'undefined'){
	viewportwidth = window.innerWidth,
	viewportheight = window.innerHeight
	} else if (typeof document.documentElement != 'undefined' && typeof document.documentElement.clientWidth !='undefined' && document.documentElement.clientWidth != 0){
    viewportwidth = document.documentElement.clientWidth,
    viewportheight = document.documentElement.clientHeight
	}else{
       viewportwidth = document.getElementsByTagName('body')[0].clientWidth,
       viewportheight = document.getElementsByTagName('body')[0].clientHeight
	 }
}
 
var LBX = {
	init: function(options){
		this.options = Object.extend({
			resizeDuration: 400,
			resizeTransition: Fx.Transitions.sineInOut,
			initialWidth: 250,
			initialHeight: 250,
			animateCaption: false,
			defaultIframeWidth : 500, 
			defaultIframeHeight: 300,
			lb_slideshowdur: 5000,
			lb_slideshow: 0,
			loop: 1,
			op:0
		}, options || {});
		
		
		try {
		document.execCommand("BackgroundImageCache", false, true);
		} catch(e) {}

		// IE 6 - XML prolog problem
		if(window.ie6 && document.compatMode=="BackCompat"){
			this.options.animateCaption = false;
		}

		this.anchors = [];
		$each(document.links, function(el){
			if (el.rel && el.rel.test(/^lbx/i)){
				el.onclick = this.click.pass(el, this);
				this.anchors.push(el);
			}
		}, this);
		this.eventKeyDown = this.keyboardListener.bindAsEventListener(this);
		this.eventPosition = this.position.bind(this);

		/*	Build float panel
			<div id="lbOverlay"></div>
			<div id="lbCenter">
				<div id="lbCanvas">
					<a id="lbPrevLink"></a>
					<a id="lbNextLink"></a>
					<!-- img or iframe element is inserted here -->
				</div>
			</div>
			<div id="lbBottomContainer">
				<div id="lbBottom">
					<a id="lbCloseLink"></a>
					<div id="lbCaption"></div>
					<div id="lbNumber"></div>
					<div style="clear:both;"></div>
				</div>
			</div>
		*/
		
		
		
		this.overlay = new Element('div').setProperty('id', 'lbOverlay').injectInside(document.body);
		//this.overlay = new Element('div').setProperty('id', 'lbOverlay').injectInside(this.GHolder);


		this.GHolder = new Element('div').setProperty('id', 'lbGHolder').injectInside(document.body);
		//this.center = new Element('div').setProperty('id', 'lbCenter').setStyles({width: this.options.initialWidth+'px', height: this.options.initialHeight+'px', marginLeft: '-'+(this.options.initialWidth/2)+'px', display: 'none'}).injectInside(document.body);
		this.center = new Element('div').setProperty('id', 'lbCenter').setStyles({width: this.options.initialWidth+'px', height: this.options.initialHeight+'px', marginLeft: '-'+(this.options.initialWidth/2)+'px', display: 'none'}).injectInside(this.GHolder);
		
		this.canvas = new Element('div').setProperty('id', 'lbCanvas').injectInside(this.center);
		this.prevLink = new Element('a').setProperties({id: 'lbPrevLink', href: '#'}).setStyle('display', 'none').injectInside(this.canvas);
		this.nextLink = this.prevLink.clone().setProperty('id', 'lbNextLink').injectInside(this.canvas);
		this.prevLink.onclick = this.previous.bind(this);
		this.nextLink.onclick = this.next.bind(this);
		
		//this.bottomContainer = new Element('div').setProperty('id', 'lbBottomContainer').setStyle('display', 'none').injectInside(document.body);
		this.bottomContainer = new Element('div').setProperty('id', 'lbBottomContainer').setStyle('display', 'none').injectInside(this.GHolder);
		this.bottom = new Element('div').setProperty('id', 'lbBottom').injectInside(this.bottomContainer);
		new Element('a').setProperties({id: 'lbCloseLink', href: '#'}).injectInside(this.bottom).onclick = this.overlay.onclick = this.close.bind(this);
		
		
		if(this.anchors.length>1){				
		this.playlink = new Element('div').setProperties({id: 'lbPlayLink', href: '#'}).setStyle('display', '').injectInside(this.bottom);
		this.playlink.onclick = this.togglep.bind(this);
		//onclick = this.overlay.onclick = this.togglep.bind(this);
		
		this.stoplink = new Element('div').setProperties({id: 'lbStopLink', href: '#'}).setStyle('display', 'none').injectInside(this.bottom);
		this.stoplink.onclick = this.togglep.bind(this);
		//onclick = this.overlay.onclick = this.togglep.bind(this);
		}
		
		
		
		this.caption = new Element('div').setProperty('id', 'lbCaption').injectInside(this.bottom);
		this.number = new Element('div').setProperty('id', 'lbNumber').injectInside(this.bottom);
		new Element('div').setStyle('clear', 'both').injectInside(this.bottom);

		/* Build effects */
		var nextEffect = this.nextEffect.bind(this);
		this.fx = {
			overlay: this.overlay.effect('opacity', {duration: 500}).hide(),
			resizeCenter: this.center.effects({duration: this.options.resizeDuration, transition: this.options.resizeTransition, onComplete: nextEffect}),
			image: this.canvas.effect('opacity', {duration: 1000, onComplete: nextEffect}),
			bottom: this.bottomContainer.effect('height', {duration: 400, onComplete: nextEffect})
		};

		this.preloadPrev = new Image();
		this.preloadNext = new Image();
	},

	click: function(link){
		
		if (link.rel.length == 8) return this.show(link.href, link.title, link.rev);

		var j, itemNumber, items = [];		
		this.anchors.each(function(el){
			if (el.rel == link.rel){
				for (j = 0; j < items.length; j++) if(items[j][0] == el.href && items[j][2] == el.rev) break;
				if (j == items.length){
					items.push([el.href, el.title, el.rev]);
					if (el.href == link.href && el.rev == link.rev) itemNumber = j;
				}
			}
		}, this);

		return this.open(items, itemNumber);
	},

	show: function(url, title, rev){		
		return this.open([[url, title, rev]], 0);
	},

	open: function(items, itemNumber){		
		oLBX = this;
		this.items = items;
		this.position();		
		this.setup(true);
		var wh = (window.getHeight() == 0) ? window.getScrollHeight() : window.getHeight();
		var st = document.body.scrollTop  || document.documentElement.scrollTop;
		this.top = st  + (wh / 20);				
		this.center.setStyles({top: this.top+'px', display: ''});		
		this.fx.overlay.start(0.8);
		this.options.op = 1;		

		return this.changeItem(itemNumber);
	},

	position: function(){
		//IE6 - XML prolog problem.				
		var ww = (window.getWidth() == 0) ? window.getScrollWidth()-22 : window.getWidth();
		var wh = (window.getHeight() == 0) ? window.getScrollHeight() : window.getHeight();
		var st = document.body.scrollTop  || document.documentElement.scrollTop;		
		this.overlay.setStyles({height:window.getScrollHeight()+'px', width:ww+'px',top:0+'px'});
		var pt = 20;
		var toppadd = parseInt(this.center.offsetHeight); // + parseInt(this.bottomContainer.offsetHeight);		
		//correct center
		if(isNaN(toppadd) || toppadd<1){  }else{ getVP(); pt = (viewportheight/2) - (toppadd / 2) - 20; } 
		this.center.setStyles({top: parseInt(st) + parseInt(pt) +'px'});		
		v = parseInt(this.center.offsetHeight) + parseInt(this.center.style.top);		
		this.bottomContainer.style.top = v+'px';				
	},

	checksls: function(){
			v = this.options.lb_slideshow;			
			if(v==1){						
				this.stoplink.setStyle('display', '');
				this.playlink.setStyle('display', 'none');											
			}else{					
				if(this.playlink){
				this.playlink.setStyle('display', '');
				this.stoplink.setStyle('display', 'none');				
				}
			}
	},
	
	setup: function(open){
		var elements = $A(document.getElementsByTagName('object'));
		
		if(getE('rssfeedhldr')){		
		getE('rssfeedhldr').style.visibility = open ? 'hidden' : '';
		}
		
				
		if (window.ie) elements.extend(document.getElementsByTagName('select'));
		elements.each(function(el){ el.style.visibility = open ? 'hidden' : ''; });
		
		var fn = open ? 'addEvent' : 'removeEvent';
		window[fn]('scroll', this.eventPosition)[fn]('resize', this.eventPosition);
		document[fn]('keydown', this.eventKeyDown);
		this.step = 0;		
	},

	keyboardListener: function(event){
		
		if(event.keyCode=='39'){
			if((parseInt(this.anchors.length) - 1) == this.activeItem){
			this.close();	
			}
		}
		
		a = parseInt(event.keyCode);
			if(a==27 || a==88 || a==67){
			options.lb_slideshow=0; 
			this.close(); 		
			}else if(a==37){
			this.previous(); this.options.lb_slideshow=0;
			}else if(a==39){
			this.next(); this.options.lb_slideshow=0;
			}
			
		
	},
	
	
	previous: function(){
		return this.changeItem(this.activeItem-1);
	},

	next: function(){			
	return this.changeItem(this.activeItem+1);
	},

	changeItem: function(itemNumber){	
		
		this.checksls();							

		if(this.step || (itemNumber < 0)) { return false; }
		else if(itemNumber >= this.items.length){
		this.close();
		return false;
		}
		
		this.step = 1;
		this.activeItem = parseInt(itemNumber);
						
		
		this.bottomContainer.style.display = this.prevLink.style.display = this.nextLink.style.display = 'none';
		this.fx.image.hide();
		this.center.className = 'lbLoading';

		// discard previous content by clicking
		this.removeCurrentItem();
		
		// check item type
		var url = this.items[this.activeItem][0];
		var rev = this.items[this.activeItem][2];
		
		var re_imageURL = /\.(jpe?g|png|gif|bmp)/i;
		if( url.match(re_imageURL) ) {
			
		
			this.preload = new Image();	// JavaScript native Object
			this.preload.datatype = 'image';
			this.preload.w = this.matchOrDefault(rev, new RegExp("width=(\\d+%?)", "i"), -1); //-1 if use original size.
			this.preload.h = this.matchOrDefault(rev, new RegExp("height=(\\d+%?)", "i"), -1);					
			this.preload.onload = this.nextEffect.bind(this);			
			this.preload.src = url;		
		}else{
			this.preload = new Object ();	// JavaScript native Object
			this.preload.datatype = 'iframe';
			neww =  this.matchOrDefault(rev, new RegExp("width=(\\d+)", "i"), this.options.defaultIframeWidth);
			newh =  this.matchOrDefault(rev, new RegExp("height=(\\d+)", "i"), this.options.defaultIframeHeight);
			this.preload.w = neww;		
			this.preload.h = newh; 
			this.preload.src = url;
			this.nextEffect(); //asynchronous loading
		}
		
		return false;
	},

	nextEffect: function(){
		switch (this.step++){
		case 1:
			this.center.className = '';

			// create HTML element
			if( this.preload.datatype == 'image' ) {
				
				var ws = (this.preload.w == -1) ? this.preload.width.toString() : this.preload.w.toString();
				var hs = (this.preload.h == -1) ? this.preload.height.toString() : this.preload.h.toString();
				
				ww = parseInt((window.getWidth() == 0) ? window.getScrollWidth()-22 : window.getWidth()) - 150;
				wh = parseInt((window.getHeight() == 0) ? window.getScrollHeight() : window.getHeight()) - 150 - this.bottom.offsetHeight;					
				
				ratio = parseInt(ws) / parseInt(hs);
				ws_ = parseInt(ws); hs_= parseInt(hs);

					if(ws_ > ww || hs_ > wh){
					//image height					
					if(hs_ > wh){							
							hs = wh.toString();
							ws = parseInt(hs) * ratio;
							ws = ws.toString();														
						}else if(ws_ > ww){
						  	ws = ww.toString();	
							hs = parseInt(ws) * ratio;
							hs = hs.toString();
						}
				}
				
				this.p_width = ( q = ws.match(/(\d+)%/) ) ? q[1] * this.preload.width * 0.01 : ws;
				this.p_height = ( q = hs.match(/(\d+)%/) ) ? q[1] * this.preload.height * 0.01 : hs;				
				new Element('img').setProperties({id: 'lbImage', src:this.preload.src, width:this.p_width, height:this.p_height}).injectInside(this.canvas);

				var st = document.body.scrollTop  || document.documentElement.scrollTop;
				var toppadd = parseInt(this.center.offsetHeight);
				if(isNaN(toppadd) || toppadd<1){  }else{ getVP(); pt = (viewportheight/2) - (toppadd / 2) - 20; } 
				this.center.setStyles({top: parseInt(st) + parseInt(pt) +'px'});
				
				this.nextLink.style.right = '';
				this.nextLink.blur();
				
				
				
					
				
					
			
			
			}else{
				this.p_width = this.preload.w;
				this.p_height = this.preload.h;
				// Safari would not update iframe content that has static id.
				this.iframeId = "lbFrame_"+new Date().getTime();
				new Element('iframe').setProperties({id: this.iframeId, width: this.p_width, height: this.p_height, frameBorder:0, scrolling:'yes', src:this.preload.src}).injectInside(this.canvas);
				this.nextLink.style.right = '25px';
			}
									
			this.canvas.style.width = this.bottom.style.width = ((parseInt(this.p_width)) )+'px';

			this.canvas.style.height = this.prevLink.style.height = this.nextLink.style.height = this.p_height+'px';					
			this.caption.setHTML(this.items[this.activeItem][1] || '');
			this.number.setHTML( (this.items.length == 1) ? '' : '' + (this.activeItem+1) + ' / ' + this.items.length );

			if (this.activeItem) this.preloadPrev.src = this.items[this.activeItem-1][0];
			if (this.activeItem != (this.items.length - 1)) this.preloadNext.src = this.items[this.activeItem+1][0];
			if (this.center.clientHeight != this.canvas.offsetHeight){
				var oh = (this.p_height == this.canvas.clientHeight) ? this.canvas.offsetHeight : eval(this.p_height)+18; // fix for ie
				this.fx.resizeCenter.start({height: oh});
				this.position();
				break;
			}
			this.position();
			this.step++;
		case 2:
			
			if (this.center.clientWidth != this.canvas.offsetWidth){
				var ow = (this.p_width == this.canvas.clientWidth) ? this.canvas.offsetWidth : eval(this.p_width)+18; // fix for ie
				this.fx.resizeCenter.start({width: ow, marginLeft: -ow/2}); 		
				this.position();
				break;
			}
			this.position();
			this.step++;
		case 3:		
			this.bottomContainer.setStyles({top: (this.top + this.center.clientHeight + 10)+'px', height:'0px', marginLeft: this.center.style.marginLeft, width:this.center.style.width, display: ''});
			this.fx.image.start(1);
			break;
		case 4:
			if (this.options.animateCaption){
				this.position();
				// This is not smooth animation in IE 6 with XML prolog.
				// If your site is XHTML strict with XML prolog, disable this option.				
				this.fx.bottom.start(0,this.bottom.offsetHeight+10);								
				break;
			}
			
			
			
		case 5:
			
			if (this.activeItem){
				this.prevLink.style.display = '';				
			}
			
			if (this.activeItem != (this.items.length - 1)){
				this.nextLink.style.display = '';				
			}
			
			
			this.step = 0;
		}
		
		
	
	},
	
	togglep: function(){
			v = this.options.lb_slideshow;			
			if(v==1){							
				this.options.lb_slideshow = 0;
			}else{
				this.options.lb_slideshow = 1;
				setTimeout('pLBX()',1000);
			}			
			this.checksls();
	},
	
	close: function(){
		this.options.op = 0;
		if (this.step < 0) return;
		this.step = -1;
		this.options.lb_slideshow = 0;
		this.removeCurrentItem();	// discard content
		for (var f in this.fx) this.fx[f].stop();
		this.center.style.display = this.bottomContainer.style.display = 'none';
		this.fx.overlay.chain(this.setup.pass(false, this)).start(0);
		return false;
	},

	removeCurrentItem: function(){
		if (this.preload){
			if( this.preload.datatype == 'image' ) {
				if($('lbImage')){ 
				$('lbImage').remove(); 
				}
				this.preload.onload = Class.empty;
			}else{
				$(this.iframeId).remove();
			}
			this.preload = null;
		}		
	},

	matchOrDefault: function(str, re, val){	
		if(str=="undefined"){ return -1; }
		var hasQuery = str.match(re);
		return hasQuery ? hasQuery[1] : val;
	}

};

window.addEvent('domready', LBX.init.bind(LBX));
