var Lightbox=new Class({
	_element:null,
	options:{
		overlayClass:"lightboxOverlay",
		containerClass:"lightboxContainer",
		opacity:0.4
	},

	initialize:function (element,options) {
		this._element=element;
		this._elementOldParent=element.getParent();
		this._hiddenAtFirst=!element.visible();
		this.setOptions(options);
	},

	show:function () {
		if (Lightbox._current) Lightbox._hide();

		this.fireEvent("onBeforeShow");
		Lightbox._show(this._element,this.options);
		this.fireEvent("onAfterShow");

		Lightbox._current=this;
	},
	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:options.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");

		element.setVisibility(false);
		element.show();

		this._overlay.show();
		this._container.show();

		var top = 0;
		var scrollTop = document.documentElement.scrollTop;
		
		if (document.addEventListener) {
			top = document.documentElement.clientHeight;
		} else {
			top = document.documentElement.offsetHeight;
		}
		
		this._container.adopt(element).setStyles({
			top:((top-element.offsetHeight)/2)+scrollTop,
			left:(element.id == "addFriend") ? ((document.documentElement.offsetWidth-975)/2)+380 : (document.documentElement.offsetWidth-element.offsetWidth)/2
			//top:(this._overlay.offsetHeight-element.offsetHeight)/2,
			//left:(this._overlay.offsetWidth-element.offsetWidth)/2
		});
		
		element.setVisibility(true);

		this.fireEvent("onAfterShow");
	},

	_hide:function () {
		try {
			this.fireEvent("onBeforeHide");

			this._overlay.hide();
			this._container.hide();

			this.fireEvent("onAfterHide");

			if (Lightbox._current._hiddenAtFirst) Lightbox._current._element.hide();
			Lightbox._current._elementOldParent.adopt(Lightbox._current._element);

			Lightbox._current=null;
		} catch (e) {
		}
	},

	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();