﻿function ThickBox(boxid, options, events)
{
	this.element = jQuery('#' + boxid)[0];
	this.element.control = this;
	if (options.useIframe) {
		this.voile = this.element.getElementsByTagName('iframe')[0];
		this.child = this.element.getElementsByTagName('div')[0];
	}
	else {
		this.voile = this.element.getElementsByTagName('div')[0];
		this.child = this.element.getElementsByTagName('div')[1];
	}
	var el = jQuery(this.element);
	el.remove();
	el.appendTo(document.forms[0]);
	if (events) {
		if(events.onShown) el.bind('show', events.onShown);
		if (events.onHidden) el.bind('hide', events.onHidden);
	}
	var voile = jQuery(this.voile);
	var child = jQuery(this.child);
	var thiz = this;
	if(!options.modal)
		voile.bind('click', function() { thiz.hide(); })
	child.css('z-index', '1');
	if (options.autoLaunch) this.show();
}

ThickBox.prototype = {
	show: function(args)
	{
		var jel = jQuery(this.element)
		jel.show();
		jQuery(this.element).css('z-index', '1');
		jQuery(this.voile).fadeTo(0, 0); //TODO: refine this
		this.voile.style.display = '';
		var child = this.child;
		var voile = jQuery(this.voile);
		var newHeight = Math.max(jQuery(window).height(), jQuery('body').height());
		voile.css('height', newHeight);
		var thiz = this;
		voile.fadeTo("slow", 0.75, function()
		{
			var height = jQuery(child).height();
			var wHeight = Math.min(jQuery(window).height(), jQuery('body').height());
			var top = (wHeight / 2) - (height / 2) + jQuery('html').scrollTop();
			jQuery(child).css('top', top + 'px');
			child.style.display = '';
			jel.trigger('show', args);
		});
	},
	hide: function(args)
	{
		var jel = jQuery(this.element)
		this.child.style.display = 'none';
		var voile = jQuery(this.voile);
		var thiz = this;
		voile.fadeOut("normal", function()
		{
			jel.css('z-index', '-1');
			jel.hide();
			jel.trigger('hide', args);
		});
	},
	set_modal: function(isModal)
	{
		jQuery(this.voile).unbind('click');
		if (isModal)
		{
			var thiz = this;
			jQuery(this.voile).bind('click', function() { thiz.hide(); })
		}
	}
}
