/*
	Slimbox v1.41 - The ultimate lightweight Lightbox clone
	by Christophe Beyls (http://www.digitalia.be) - MIT-style license.
	Inspired by the original Lightbox v2 by Lokesh Dhakar.
*/

var Lightbox = function(){};

Lightbox.anchors=[];
Lightbox.lastIndex=1000;
Lightbox.hooks= function(options){
		$each(document.links, function(el){
			if (el.rel && el.rel.test(/^lightbox/i)){
				el.onclick = Lightbox.click.pass(el, Lightbox);
				Lightbox.anchors.push(el);
			}
		}, Lightbox);
	};
Lightbox.windows=[];
Lightbox.click= function(link){
		if (link.rel.length == 8) return this.show(link.href, link.title);

		var j, imageNum, images = [];
		var myrel = link.rel.split('[');
		for(i=0; i<3; i++) myrel.pop();
		myrel = myrel.join('[');

		this.anchors.each(function(el){
			if (el.rel.indexOf(myrel)!=-1){
				for (j = 0; j < images.length; j++) if(images[j][0] == el.href) break;
				if (j == images.length){
					images.push([el.href, el.title, el.rel]);
					if (el.href == link.href) imageNum = j;
				}
			}
		}, this);
		return this.open(images, imageNum, myrel);
	};

Lightbox.show=function(url, title, rel){
		return this.open([[url, title, rel]], 0, rel);
	};

Lightbox.open= function(images, imageNum, name){
		if (!this.windows[name]) {
			var lb = new Lightbox();
			lb.init();
			lb.images = images;
			lb.position();
			lb.setup(true);
			lb.top = window.getScrollTop() + (window.getHeight() / 15);
			lb.left = window.getScrollLeft() + ((window.getWidth()+lb.options.initialWidth) / 2);
			lb.container.setStyles({left: lb.left,top: lb.top, display: ''});
			lb.name = name;
			//this.fx.overlay.start(0.8);
			this.windows[name] = lb;
			return this.windows[name].changeImage(imageNum);
		} else {
			this.windows[name].onTop();
			return false;
		}
	};
Lightbox.ids=0;

Lightbox.prototype = {

	init: function(options){
		this.options = $extend({
			resizeDuration: 1,
			resizeTransition: false,	// default transition
			initialWidth: 250,
			initialHeight: 250,
			animateCaption: true,
			showCounter: true
		}, options || {});
		this.id = Lightbox.ids++;
		this.eventKeyDown = this.keyboardListener.bindAsEventListener(this);
		this.eventPosition = this.position.bind(this);

		//this.overlay = new Element('div', {'id': 'lbOverlay'}).injectInside(document.body);
		this.container = new Element('div', {'id': 'lbContainer', 'styles': {'width': this.options.initialWidth, 'height': this.options.initialHeight,  'marginLeft': -(this.options.initialWidth/2), 'display': 'none'}}).injectInside(document.body);
		this.center = new Element('div', {'id': 'lbCenter', 'styles': {'width': this.options.initialWidth, 'height': this.options.initialHeight}}).injectInside(this.container);
		this.image = new Element('div', {'id': 'lbImage'}).injectInside(this.center);
		this.video = new Element('div', {'id': 'lbVideo', 'style':'display:none'}).injectInside(this.center);

		//this.prevLink = new Element('a', {'id': 'lbPrevLink', 'href': '#', 'styles': {'display': 'none'}}).injectInside(this.image);
		//this.nextLink = this.prevLink.clone().setProperty('id', 'lbNextLink').injectInside(this.image);
		//this.prevLink.onclick = this.previous.bind(this);
		//this.nextLink.onclick = this.next.bind(this);

		this.bottomContainer = new Element('div', {'id': 'lbBottomContainer'
				//, 'styles': {'display': 'none'}
				}).injectInside(this.container);
		this.bottom = new Element('div', {'id': 'lbBottom'}).injectInside(this.bottomContainer);
		//new Element('a', {'id': 'lbCloseLink', 'href': '#'}).injectInside(this.bottom).onclick = this.overlay.onclick = this.close.bind(this);
		new Element('a', {'id': 'lbCloseLink', 'href': '#'}).injectInside(this.bottom).onclick = this.close.bind(this);
		this.caption = new Element('div', {'id': 'lbCaption'}).injectInside(this.bottom);
		this.pgnx = new Element('div', {'id': 'pgnx'}).injectInside(this.bottom);
		this.pages = new Element('div', {'id': 'pages'}).injectInside(this.pgnx);
		this.nxprv = new Element('div', {'id': 'nxprv'}).injectInside(this.pgnx);
		this.number = new Element('div', {'id': 'lbNumber'}).injectInside(this.bottom);
		new Element('div', {'styles': {'clear': 'both'}}).injectInside(this.bottom);
		new Drag.Move(this.container, {
				/*onStart: function(){
					this.initialx=this.container.getStyle('left');
					this.initialy=this.container.getStyle('top');
				}.bind(this),
				onComplete: function(){
					this.dragCompleted=(this.initialx!=this.container.getStyle('left') || this.initialy!=this.container.getStyle('top'));
				}.bind(this)*/
				});
		this.onTop();
		this.container.onclick=function(){
			this.onTop();
		}.bind(this);
		var nextEffect = this.nextEffect.bind(this);
		this.fx = {
			//overlay: this.overlay.effect('opacity', {duration: 500}).hide(),
			resize: this.center.effects($extend({duration: this.options.resizeDuration, onComplete: nextEffect}, this.options.resizeTransition ? {transition: this.options.resizeTransition} : {})),
			image: this.image.effect('opacity', {duration: 500, onComplete: nextEffect}),
			bottom: this.bottom.effect('margin-top', {duration: 1, onComplete: nextEffect})
		};

		this.preloadPrev = new Image();
		this.preloadNext = new Image();
	},
	onTop: function (){
		this.container.setStyle('z-index',Lightbox.lastIndex++);
	},
	position: function(){
		//this.overlay.setStyles({'top': window.getScrollTop(), 'height': window.getHeight()});
	},

	setup: function(open){
		var elements = $A(document.getElementsByTagName('object'));
		elements.extend(document.getElementsByTagName(window.ie ? 'select' : 'embed'));
		elements.each(function(el){
			if (open) el.lbBackupStyle = el.style.visibility;
			el.style.visibility = open ? 'hidden' : el.lbBackupStyle;
		});
		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){
		switch (event.keyCode){
			case 27: case 88: case 67: this.close(); break;
			case 37: case 80: this.previous(); break;
			case 39: case 78: this.next();
		}
	},

	previous: function(){
		return this.changeImage(this.activeImage-1);
	},

	next: function(){
		return this.changeImage(this.activeImage+1);
	},

	jumppage: function(tpagenum){
		return this.changeImage(tpagenum - 1);
	},


	changeImage: function(imageNum){
		if (this.step || (imageNum < 0) || (imageNum >= this.images.length)) return false;
		/*if (this.dragCompleted) {
			this.dragCompleted = false;
			return false;
		}*/
		this.step = 1;
		this.activeImage = imageNum;

		var imgsrc = this.images[imageNum][0];
		this.isVideo = (imgsrc.substr(imgsrc.lastIndexOf('.'))=='.mov');

		this.bottomContainer.style.display
		  //= this.prevLink.style.display = this.nextLink.style.display
		  = 'none';
		this.fx.image.hide();
		this.center.className = 'lbLoading';
		if (this.isVideo){
			setTimeout(this.nextEffect.bind(this), 1);
		} else {
			this.preload = new Image();
			this.preload.onload = this.nextEffect.bind(this);
			this.preload.src = this.images[imageNum][0];
		}
		return false;
	},

	nextEffect: function(){
		switch (this.step++){
		case 1:
			var otherinformation=this.images[this.activeImage][2].replace(/\]/g, '').split("[");
			this.center.className = '';
			if (this.isVideo){
				this.video.innerHTML='<object id="movie'+this.id+'" CLASSID="clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B" width="'+otherinformation[5]+'" height="'+otherinformation[6]+'" CODEBASE="http://www.apple.com/qtactivex/qtplugin.cab"><param name="src" value="'+this.images[this.activeImage][0]+'"><param name="autoplay" value="true"><EMBED '+
				' TYPE="video/quicktime"'+
                '       name="movie'+this.id+'"'+
                '		  src="'+this.images[this.activeImage][0]+'"'+
                '       autoplay="true" loop="false" controller="true" pluginspage="http://www.apple.com/quicktime/"'+
                '		width="'+otherinformation[5]+'" height="'+otherinformation[6]+'"'+
                '       enablejavascript="true">'+
                '</EMBED></object>';
				/*try{
					this.video.firstChild.src=this.images[this.activeImage][0];
				}catch(e){
					document.movie1.SetSRC(this.images[this.activeImage][0]);
				}*/
				this.video.style.display = "";
		 		//this.video.firstChild.style.width =
		 			this.video.style.width = this.bottom.style.width = otherinformation[5]+'px';
				//this.video.firstChild.style.height =
					this.video.style.height = otherinformation[6]+'px';
				this.image.style.display = 'none';
			} else {
				this.image.style.backgroundImage = 'url('+this.images[this.activeImage][0]+')';
				this.image.style.width = this.bottom.style.width = this.preload.width+'px';
				this.image.style.display = "";
				this.video.style.display = 'none';
				this.image.style.height =
					//this.prevLink.style.height = this.nextLink.style.height =
					this.preload.height+'px';
			}


			var entirecaption = this.images[this.activeImage][1] || '';

			if ((otherinformation[2] != null) && (otherinformation[2] != "")) {
				var entirecaption = entirecaption + '<br /><a onclick="Lightbox.show(\'view_video.php?id=' + otherinformation[2] + '&.mov\', \'\', \''+this.images[this.activeImage][2]+'\')">View Video</a>';
			}
			if ((otherinformation[3] != null) && (otherinformation[3] != "")) {
				var entirecaption = entirecaption + '<br /><a href="' + otherinformation[3] + '" target="_blank">' +  otherinformation[4] + '</a>';
			}
			this.caption.setHTML(entirecaption);

			var mylinks = "";

			for (var xg = 1; xg <= this.images.length; xg++)
			   {
				   if (this.activeImage != xg - 1){
				   mylinks = mylinks + '<a href="#" class="page_images" id="'+this.name+'pageimage'+ xg +'">'+ xg +'</a> ';
				   } else {
					  mylinks = mylinks + xg + " "
				   }
			   }


				this.pages.setHTML(mylinks);

				this.nxprv.setHTML('<a href="#" id="'+this.name+'prevIm"><img src="images/prevsmall.gif" alt="Previous" class="navi" border="0"></a> <a href="#" id="'+this.name+'nextIm"><img src="images/nextsmall.gif" alt="Next" class="navi" border="0"></a>');


				for (var xg = 1; xg <= this.images.length; xg++)
				   {
					 if (this.activeImage != xg - 1){
						$(this.name+'pageimage'+xg).onclick = this.jumppage.bind(this, xg);
					 }

				   }




					$(this.name+'prevIm').onclick = this.previous.bind(this);
					$(this.name+'nextIm').onclick = this.next.bind(this);



			if (this.activeImage) this.preloadPrev.src = this.images[this.activeImage-1][0];
			if (this.activeImage != (this.images.length - 1)) this.preloadNext.src = this.images[this.activeImage+1][0];
			if (this.center.clientHeight != (this.image.offsetHeight + this.video.offsetHeight)){
				this.fx.resize.start({height: (this.image.offsetHeight+this.video.offsetHeight)});
				break;
			}
			this.step++;
		case 2:
			if (this.center.clientWidth != (this.image.offsetWidth + this.video.offsetWidth)){
				this.fx.resize.start({width: this.image.offsetWidth +this.video.offsetWidth , marginLeft: -(this.image.offsetWidth + this.video.offsetWidth)/2});
				break;
			}
			this.step++;
		case 3:
			this.bottomContainer.setStyles({
				top: this.center.clientHeight,
				height: 0,
				marginLeft: this.center.style.marginLeft,
				width: this.image.offsetWidth + this.video.offsetWidth,
				display: ''});
			this.fx.image.start(1);
			break;
		case 4:
			if (this.options.animateCaption){
				this.fx.bottom.set(-this.bottom.offsetHeight);
				this.bottomContainer.style.height = '';
				this.fx.bottom.start(0);
				break;
			}
			this.bottomContainer.style.height = '';
		case 5:
			//if (this.activeImage) this.prevLink.style.display = '';
			//if (this.activeImage != (this.images.length - 1)) this.nextLink.style.display = '';
			this.onTop();
			this.step = 0;
		}
	},

	close: function(){
		if (this.step < 0) return;
		this.step = -1;
		if (this.preload){
			this.preload.onload = Class.empty;
			this.preload = null;
		}
		for (var f in this.fx) this.fx[f].stop();
		this.center.style.display = this.bottomContainer.style.display = 'none';
		this.container.parentNode.removeChild(this.container);
		delete (Lightbox.windows[this.name]);
		//this.fx.overlay.chain(this.setup.pass(false, this)).start(0);
		return false;

	}
};

window.addEvent('domready', Lightbox.hooks.bind(Lightbox));