
function Dialog()
{
	var self = this;
	
	self.init = function(dialog, callback, options)
	{
		if (typeof options != "undefined")
		{
			self.opt = options;
			
			if (typeof self.opt['buttons'] != 'undefined')
			{
				for (var i=0; i < self.opt['buttons'].length; i++)
				{
					$('#' + self.opt['buttons'][i]).bind("click", function(){
						callback(self, $(this).attr('id'));
					});
				}
			}
		}
		else
		{ // setup default
			self.opt = {'scroll': false};
		}
	
		self.errorText = '';
		self.dialog = dialog;
		$("#" + dialog + " a[class='btn']").each(function(i){
			
			$(this).bind("click", function(){
				callback(self, $(this).attr('id'));
			});
		});
		
		$(window).resize(function(){
			self.setOverlaySize();
		});
	}
	
	self.setError = function(errorText)
	{
		self.errorText = errorText;
		$('#' + self.dialog + '-error').html(self.errorText);
	}
	
	self.showDialog = function(options)
	{
		if (typeof options == "undefined")
			options = []
			
		for (var i in options)
		{
			$('#' + i).html(options[i]);			
		}

		self.setOverlaySize();
		$('#overlay').show();
		
		this.setError('');
		
		if (self.opt['scroll'] == true)
			$.scrollTo($("#overlay"), 200);
		else
			$('#' + self.dialog).css('top', ($(document).scrollTop() + 60) + 'px');
		
		$('#' + self.dialog).show();
		return false;
	}
	
	self.hideDialog = function()
	{
		$('#overlay').hide();
		$('#' + self.dialog).hide();
	}
	
	self.setOverlaySize = function()
	{
		var size = this.getSize();
		var overlay = $('#overlay');
		overlay.css('width', size[0]);
		overlay.css('height', size[1]);
	}
	
	self.getSize = function()
	{
		var xScroll, yScroll;
		var windowWidth, windowHeight;

		if (window.innerHeight && window.scrollMaxY)
		{
			xScroll = window.innerWidth + window.scrollMaxX;
			yScroll = window.innerHeight + window.scrollMaxY;
		}
		else if (document.body.scrollHeight > document.body.offsetHeight)
		{
			xScroll = document.body.scrollWidth;
			yScroll = document.body.scrollHeight;
		}
		else
		{
			xScroll = document.body.offsetWidth;
			yScroll = document.body.offsetHeight;
		}

		if (self.innerHeight)
		{
			if (document.documentElement.clientWidth)
				windowWidth = document.documentElement.clientWidth;
			else
				windowWidth = self.innerWidth;
				
			windowHeight = self.innerHeight;
		}
		else if (document.documentElement && document.documentElement.clientHeight)
		{
			windowWidth = document.documentElement.clientWidth;
			windowHeight = document.documentElement.clientHeight;
		}
		else if (document.body)
		{
			windowWidth = document.body.clientWidth;
			windowHeight = document.body.clientHeight;
		}

		if (yScroll < windowHeight)
			pageHeight = windowHeight;
		else
			pageHeight = yScroll;
		if (xScroll < windowWidth)
			pageWidth = xScroll;
		else
			pageWidth = windowWidth;

		var ret = new Array(pageWidth, pageHeight);
		return ret;
	}
}
