﻿
var Lightbox=new Class({
	_element:null,
	options:{
		overlayClass:"lightboxOverlay",
		containerClass:"lightboxContainer",
		opacity:0.9
	},
	
	initialize:function (element,options) {
		this._element=element;
		this.setOptions(options);
	},

	show:function () {
		this.fireEvent("onBeforeShow");
		Lightbox._show(this._element,this.options);
		this.fireEvent("onAfterShow");
	},
	hide:function () {
		this.fireEvent("onBeforeHide");

		Lightbox._hide();

		this.fireEvent("onAfterHide");
	}
});
Lightbox.implement(new Options,new Events);

// static properties
$extend(Lightbox,{
	_initialized:false,
	_overlay:null,
	_container:null,

	_init:function (options) {
		this._overlay=new Element("div").inject(document.form).setStyles({
			width:"100%",
			height:Math.max(document.documentElement.clientHeight,Math.max(window.innerHeight || 0,document.documentElement.scrollHeight)),
			backgroundColor:"#000",
			position:"absolute",
			top:0,
			left:0,
			zIndex:1000
		})
		.addClass(options.overlayClass)
		.setOpacity(options.opacity)
		.hide();

		this._container=new Element("div").inject(document.form).setStyles({
			position:"absolute",
			top:0,
			left:0,
			zIndex:1001
		})
		.addClass(options.containerClass)
		.hide();
	},

	_show:function (element,options) {
		if (!this._initialized) {
			this._init(options);
			this._initialized=true;
		}

		this.fireEvent("onBeforeShow");
		
		this._overlay.show();
		this._container.show();
		element.show();

		this._container.removeChildren().adopt(element).setStyles({
			top:120,
			left:(this._overlay.offsetWidth-element.offsetWidth)/2
		});

		this.fireEvent("onAfterShow");
	},

	_hide:function () {
		this.fireEvent("onBeforeHide");

		this._overlay.hide();
		this._container.removeChildren();

		this.fireEvent("onAfterHide");
	},

	hide:function () {
		this._hide();
	}
});
$extend(Lightbox,new Events);

/*
-- js
var lb=new Lightbox(element);
lb.show();

lb.hide();
or Lightbox.hide();
*/

if (typeof(Sys)!=="undefined") Sys.Application.notifyScriptLoaded();
