/*

	Script		: Lightbox Class
	Version		: 1.0
	Author		: Scott Mackenzie
	Desc			: Displays screenshot in a lightbox
	Licence		: Open Source MIT Licence

*/

var Lightbox = new Class({
	
	getOptions : function(){
		return {
			useOverlay			: true,
			container				: document.body,
			topOffset				: -60,
			defaultHeight 	: 573,
			zIndex: 2
		};
	},
	
	initialize : function (el, options){
		
		this.el = el;
		this.target = el.href;
		this.caption_text = el.title;
		
		this.setOptions(this.getOptions());
		
		// overlay div
		if(this.options.useOverlay){
			this.overlay = new Overlay({ 
				opacity : 0.55,
				container : this.options.container,
				duration: 200,
				onClick : this.hide.bind(this),
				onCompleteIn : this.setup.bind(this)
			});
		}
		
		this.zIndex = this.options.useOverlay ? this.options.zIndex+1 : this.options.zIndex;
		
		el.addEvent('click', function(e){
			new Event(e).stop();
			if(this.options.useOverlay){
				this.overlay.show();
			} else {
				this.setup();
			}
		}.bind(this));
		
		window.addEvent('resize', this.resize.bind(this));
	},
	
	setup : function () {
	
		this.outer			= 	new Element('div').setProperty('id', 'lightbox').setStyle('z-index', this.zIndex).injectInside(this.options.container);
		this.wrap				= 	new Element('div').addClass('lbox-wrap').injectInside(this.outer);
		this.box				= 	new Element('div').addClass('lbox-product clearfix').injectInside(this.wrap);
		
		
		
		this.image_wrap = 	new Element('div').addClass('lbox-image-wrap').injectInside(this.box);
		
		this.image_wrap.addEvent('click', function(e){
			new Event(e).stop();
		});
		// preloader
		this.preload		=		new Asset.image('/images/gallery/loading.gif', { id : 'lbox-preload'}).injectInside(this.image_wrap);
		this.preload.setStyle('visibility', 'hidden');
		
		//desc_clearfix		= 	new Element('div').addClass('clearfix').injectInside(this.box);
		//this.desc				= 	new Element('div').addClass('lbox-desc').injectInside(desc_clearfix);
		
		this.close 			= 	new Element('a').addClass('lbox-close').setProperty('href', '#close').injectInside(this.box);
		this.close_img	=		new Asset.image('/images/gallery/close.gif').injectInside(this.close);
		
		this.close .addEvent('click', function(e){
			new Event(e).stop();
			this.hide();
		}.bind(this));
		
		this.outer.addEvent('click', this.hide.bind(this));
		
		i = this.el.id.split('-')[1];
		$('info-'+i).clone().injectInside(this.box);
		
		this.preload.setStyle('visibility', 'visible');
		// load image
		this.image = new Asset.image(this.target, { onload: this.show.bind(this), id:'lbox-image' });
	},
	
	show : function () {
		this.preload.setStyle('display', 'none');
		this.image.injectInside(this.image_wrap);
		this.image.setStyle('visibility', 'hidden');
		this.image.setStyle('visibility', 'visible');
		this.resize(false);
		
		this.close.setStyle('visibility', 'visible');
		this.outer.setStyle('display', 'block');
	},
	
	resize : function (animate) {
		
		if (this.image) {
			h = this.image.getStyle('height').toInt();
			if (!h || h == 0) {
				h = this.image.height;
			}
			//this.box.setStyle('width', this.image.width);
		} else if (this.box) {
			h = this.box.getStyle('height').toInt();
		}
		
		if (this.outer) {
			var top = (((window.getHeight()/2) - (h/2)) + window.getScrollTop()) + this.options.topOffset;
			if (animate) {
				var topChange = new Fx.Style(this.box, 'top', {duration: 500});
				topChange.start(top);
			} else {
				this.box.setStyle('top', top);
			}
		}
	},
	
	hide : function () {
		if(this.options.useOverlay){
			this.overlay.hide();
		}
		this.outer.remove();
	}
	
});
Lightbox.implement(new Options);