var WK = WK || {};
WK.Lightbox = function(content, spec)
{
	this._content = content;
	this._spec = spec = spec || {};
	this._spec.close = spec.close || 'close'
	this._spec.destroy = spec.destroy || false;
}
WK.Lightbox.prototype = {
	_content: null,
	_contentElt: null,
	_overlay: null,

	// customization
	_onHide: null,
	_onShow: null,
	_width: null,
	_height: null,
	_absolute: false,
	_modal: false,
	_customClass: null,

	onHide: function(funcName)
	{
		this._onHide = funcName;
		return this;
	},
	
	onShow: function(funcName)
	{
		this._onShow = funcName;
		return this;
	},
	
	setDimensions: function(width, height)
	{
		this._width = width;
		this._height = height;
		return this;
	},
	
	setContent: function(content)
	{
		this._content = content;
		if (this._contentElt) {
			this._contentElt.html(content);
		}
		this.center();
	},

	center: function()
	{
		// get dimensions
		var pagesize = getDocumentSize();
		var windowWidth = pagesize[0];
		var windowHeight = pagesize[1];
		var width = (this._width > 0) ? this._width : this._popupElt.width();
		var height = (this._height > 0) ? this._height : this._popupElt.height();

		// centering
		var left = (windowWidth/2 - width/2);
		var top = (windowHeight/2 - height/2);

		// fix ie6 positioning
		if ($.browser.msie && parseInt($.browser.version) == 6)
		{
			this._overlay.height($('body').height());
			this._popupElt.css('top', $('body, html').scrollTop() + 'px');
		}
		else {
			this._popupElt.css("top", (top > 0) ? top + 'px' : '5%');
		}
		this._popupElt.css("left", (left > 0) ? left + 'px' : 0).height(height).width(width);
	},
	
	show: function()
	{
		if (this._overlay && this._popupElt)
		{
			this._overlay.show();
			this._popupElt.show();
		}
		else {
			var self = this;
			var overlay = $(document.createElement('div'))
			overlay.addClass('wk_lightbox_overlay');
			
			if (this._modal == false) {
				overlay.click(function()
				{
					self.hide();
				});
			}
	
			$('body').append(overlay);
	
			this._overlay = overlay;
	
			// create inside div
			this._createContentElt();
			
			// bind 'hide' to esc
			if (this._modal == false) {
				$(document).keypress(function(e)
				{
					if(e.keyCode == 27){
						return self.hide();
					}
				});
			}
			
			// call onShow function
			if (typeof(this._onShow) == 'function')
			{
				this._onShow();
			}
		}

		return false;
	},
	
	hide: function(destroy)
	{
		if (this._overlay && this._popupElt) {
			// remove elements
			if (destroy || this._spec.destroy)
			{
				this._overlay.remove();
				this._popupElt.remove();			
			}
			else
			{
				this._overlay.hide();
				this._popupElt.hide();
			}
			
			// unbind keypress
			$(document).unbind('keypress');
			
			// call onHide function
			if (typeof(this._onHide) == 'function')
			{
				this._onHide();
			}
		}

		return false;
	},
	
	destroy: function()
	{
		this.hide(true);
	},

	_createContentElt: function()
	{
		var popup = $(document.createElement('div'));
		popup.addClass('wk_lightbox_popup');
		if (this._customClass != null) {
			popup.addClass(this._customClass);
		}
		
		this._contentElt = contentElt = $(document.createElement('div'));
		contentElt.html(this._content);
		contentElt.addClass('wk_lightbox_content');
		contentElt.appendTo(popup);

		// render outside the visible pane to get dimensions
		popup.css({'top': '-2000px'});
		popup.appendTo('body');
		
		if (this._absolute == true) {
			popup.css({'position': 'absolute'});	
		}

		this._popupElt = popup;
		
		this.center();
		if (this._content && this._modal == false)
		{
			this._createCloseButton();
		}
	},
	
	_createCloseButton: function()
	{
		var self = this;
		var button = $(document.createElement('a'));
		button.html(this._spec.close);
		button.attr('alt', 'close');
		button.attr('href', '#');
		button.addClass('wk_lightbox_close');
		button.click(function()
		{
			self.hide();
			return false;
		});
		
		button.appendTo(this._popupElt);
	}
};