/**
 * Overlay helper widget
 */ 
kff.widgets.Overlay = function(options)
{
	this.options = $.extend({
		zIndex: 500000,
		opacity: 0.1,
		click: null
	}, options);

	this.$overlayElement = null;
	this.$iframeElement = null;
	this.isShown = false;
	this.zIndex = this.options.zIndex;
};

kff.widgets.Overlay.prototype.init = function(){};

kff.widgets.Overlay.prototype.destroy = function()
{
	this.$overlayElement.remove();
	this.$iframeElement.remove();
	this.isShown = false;
};

kff.widgets.Overlay.prototype.render = function()
{
	if(!this.isShown)
	{
		this.$iframeElement = $('<iframe/>');
		this.$overlayElement = $('<div/>');
		
		this.$iframeElement.css({ zIndex: this.zIndex - 1, position: 'fixed', top: 0, left: 0, width: '100%', height: '100%', backgroundColor: '#fff', border: '0', opacity: 0 });
		this.$overlayElement.css({ position: 'fixed', zIndex: this.zIndex, width: '100%', height: '100%', top: '0', left: '0', minHeight: '100%', backgroundColor: '#000', opacity: this.options.opacity });
		
		$("body").append(this.$iframeElement).append(this.$overlayElement);
		
		if($.browser.msie && parseInt($.browser.version) == 6)
		{
			this.$iframeElement.css({ position: 'absolute', height: $('body').height() });
			this.$overlayElement.css({ position: 'absolute', height: $('body').height() });
		}
		
		this.$overlayElement.bind('click', $.proxy(this.click, this));
		this.isShown = true;
	}
};

kff.widgets.Overlay.prototype.resizeToDocument = function()
{
  	var documentHeight = $(document).height();
  	var windowHeight = $(window).height();
  	var maxHeight = documentHeight > windowHeight ? documentHeight : windowHeight;
	this.$iframeElement.css("height", maxHeight);
	this.$overlayElement.css("height", maxHeight);
}

kff.widgets.Overlay.prototype.click = function()
{
	if(typeof this.options.click === 'function') this.options.click();
}

/**
 * Base constructor for dialog boxes
 */
kff.dialogs = kff.dialogs || {};

kff.dialogs.Dialog = function(options)
{
	this.options = $.extend({
		width: 540,
		height: 600,
		topOffset: 40,
		zIndex: 600000,
		className: 'popup',
		htmlTemplate: "<div/>",
		title: 'Dialog',
		useOverlay: true,
		overlayOpacity: 0.5
	}, options);
	
	this.$popupElement = null;
};

kff.dialogs.Dialog.prototype.open = function()
{
	this.render();
};

kff.dialogs.Dialog.prototype.render = function()
{
	if(!this.isOpen)
	{
		this.$popupElement = $('<div/>').append(this.options.htmlTemplate).addClass(this.options.className);
		if(this.options.useOverlay) this.showOverlay();
		$("body").append(this.$popupElement);
		this.positionBox();
		this.isOpen = true;
	}
};

kff.dialogs.Dialog.prototype.close = function()
{
	this.$popupElement.remove();
	if(this.overlay) this.overlay.destroy();
	return this.isOpen = false;
};

kff.dialogs.Dialog.prototype.positionBox = function()
{
	var windowWidth = $(window).width();
	var windowHeight = $(window).height();
	var popupWidth = this.options.width;

	this.$popupElement.css({
		left: (windowWidth - this.options.width - 10)  / 2,
		width: this.options.width,
		height: this.options.height,
		top: (windowHeight - this.options.height - 10)  / 2,
		zIndex: 600000,
		position: 'fixed'
	});
	
	if($.browser.msie && parseInt($.browser.version) == 6)
	{
		this.$popupElement.css({ position: 'absolute' });
	}

};

kff.dialogs.Dialog.prototype.showOverlay = function()
{
	this.overlay = new kff.widgets.Overlay({ opacity: this.options.overlayOpacity });
	this.overlay.render();
};


