var IpDialogWidgetTools = {

	viewPortSize: function()
	{
		if (typeof(window.innerWidth) == 'number')
		{
			// Non-IE
			return {
				width: window.innerWidth,
				height: window.innerHeight
			};

		}
		else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) )
		{
			//IE 6+ in 'standards compliant mode'
			return {
				width:  document.documentElement.clientWidth
				,height: document.documentElement.clientHeight
			};
		}
		else if( document.body && ( document.body.clientWidth || document.body.clientHeight))
		{
			//IE 4 compatible
			return {
				width: document.body.clientWidth,
				height: document.body.clientHeight
			};
		}

		return {width: 0, height: 0};
	}

	,scrollOffset: function()
	{
		if (! document.all)
		{
			return {
				left: window.pageXOffset
				,top: window.pageYOffset
			}
		}
		else if (document.documentElement)
		{
			return {
				left: document.documentElement.scrollLeft,
				top: document.documentElement.scrollTop
			};
		}
		else
		{
			return {
				left: document.body.scrollLeft,
				top: document.body.scrollTop
			};
		}
	}

	,documentSize: function()
	{
		var xScroll, yScroll;
		
		if (window.innerHeight && window.scrollMaxY) {	
			xScroll = window.innerWidth + window.scrollMaxX;
			yScroll = window.innerHeight + window.scrollMaxY;
		} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
			xScroll = document.body.scrollWidth;
			yScroll = document.body.scrollHeight;
		} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
			xScroll = document.body.offsetWidth;
			yScroll = document.body.offsetHeight;
		}
		
		var windowWidth, windowHeight;
		
		if (self.innerHeight) 
		{	// all except Explorer
			
			if(document.documentElement.clientWidth){
				windowWidth = document.documentElement.clientWidth; 
			} else {
				windowWidth = self.innerWidth;
			}
			
			windowHeight = self.innerHeight;
		}
		else if (document.documentElement && document.documentElement.clientHeight) 
		{ 
			// Explorer 6 Strict Mode
			windowWidth = document.documentElement.clientWidth;
			windowHeight = document.documentElement.clientHeight;
		} 
		else if (document.body) 
		{ 
			// other Explorers
			windowWidth = document.body.clientWidth;
			windowHeight = document.body.clientHeight;
		}	
		
		// for small pages with total height less then height of the viewport
		if (yScroll < windowHeight){
			pageHeight = windowHeight;
		} else { 
			pageHeight = yScroll;
		}
		
		// for small pages with total width less then width of the viewport
		if (xScroll < windowWidth) {	
			pageWidth = xScroll;		
		} else {
			pageWidth = windowWidth;
		}
		
		return { width: pageWidth, height: pageHeight };
	}
};

var IpDialogWidgetOverlay = Class.create({

	element: null

	,initialize: function()
	{
		this.element = new Element('div', { style: 'none', 'class': 'ip-dialog-widget-overlay' });
		this.element.onclick = function(e) { this.clicked(e); }.bindAsEventListener(this);
	}

	,show: function(callback)
	{
		var offset = IpDialogWidgetTools.scrollOffset();

		$$('body').first().appendChild(this.element);

		this.element.style.top = '0px';
		this.element.style.left = '0px';
		this.element.style.width = IpDialogWidgetTools.documentSize().width + 'px';
		this.element.style.height = IpDialogWidgetTools.documentSize().height + 'px';

		this.element.style.display = 'none';

		this.element.show();
		callback();
	}

	,hide: function()
	{
		var self = this;
		this.clicked = null;

		this.element.hide();
	}

	,clicked: function()
	{
		// callback
	}
});

var IpDialogWidget = Class.create({

	width: null
	,contents: null
	,modal: false
	,closeAction: null

	,initialize: function(contents, width, options)
	{
		this.contents = contents;
		this.width = width;
		this.options = options;
		
		
		this.options = (options)
			? options : $H({});

		this.createDialog();
	}

	,setModal: function(modal)
	{
		this.modal = modal;
	}

	,setCloseAction: function(action)
	{
		this.closeAction = action;
	}

	,createDialog: function()
	{
		var dialog = new Element('div', { style: 'width: ' + this.width + 'px;', 'class': 'ip-dialog-widget' });
		dialog.appendChild(this.contents);

		this.dialog = dialog;

		$$('body').first().appendChild(this.dialog);

		Event.observe(document, 'keyup', this.keyPressed.bindAsEventListener(this));
	}

	,closeHandler: function(event)
	{
		Event.stop(event);

		if (this.closeAction)
		{
			if (this.closeAction.bind(this)())
			this.close();
		}
		else
		{
			this.close();
		}
	}

	,keyPressed: function(e)
	{
		if (! this.isShowing)
			return;

		var code = (window.event) ? event.keyCode : e.keyCode;

		if (code == 27)
			this.closeHandler(e);
	}

	,show: function()
	{
		var overlay = this._getOverlay();

		if (! this.modal)
			overlay.clicked = this.closeHandler.bindAsEventListener(this);
		
		overlay.show(this._showDialog.bind(this));
	}

	,_getOverlay: function()
	{
		if (! IpDialogWidget.Overlay)
			IpDialogWidget.Overlay = new IpDialogWidgetOverlay();

		return IpDialogWidget.Overlay;
	}

	,_showDialog: function()
	{
		this.dialog.show();

		var offset = IpDialogWidgetTools.scrollOffset();

		// position the dialog
		var left = Math.max(
			10,
			IpDialogWidgetTools.viewPortSize().width / 2 - (this.dialog.offsetWidth / 2)
		) + offset.left;

		var top = Math.max(
			10
			,(IpDialogWidgetTools.viewPortSize().height / 2) - (this.dialog.offsetHeight / 2)
		) + offset.top;


		this.dialog.style.left = left + 'px';
		this.dialog.style.top = top + 'px';
		this.dialog.style.visibility = 'visible';

		this.isShowing = true;
	}

	,close: function()
	{
		if (this.options.onclose)
			this.options.onclose(this);

		this.dialog.style.visibility = 'hidden';
		this.isShowing = false;

		this._getOverlay().hide();
		this.dialog.hide();
	}

	,getInnerWidth: function()
	{
		return this.width;
	}
});
