var Dialog = {

	init: function()
	{
		this.overlay = new Element('div').setProperty('id', 'dlgOverlay').setStyles({display: 'none'}).injectInside(document.body);
		this.container = new Element('div').setProperty('id', 'dlgContainer').setStyles({width: '400px',  height: '100px', margin: '80px auto', display: 'none'}).injectInside(document.body);
		this.content = new Element('div').setProperty('id', 'dlgContent').injectInside(this.container);
		this.closelabel = new Element('a').setProperty('id', 'dlgClose').injectInside(this.container);
		this.overlay.setStyle('height',  window.getHeight());
		this.container.setStyles({'margin-left': (window.getWidth() - 400) / 2 + 'px'})

		window.addEvent('resize', function(){
			this.overlay.setStyles({top: window.getScrollTop()+'px', height: window.getHeight()+'px'});
			this.container.setStyles({'margin-left': (window.getWidth() - 400) / 2 + 'px'})
		}, this);

		window.addEvent('scroll', function(){
			this.overlay.setStyles({top: window.getScrollTop()+'px', height: window.getHeight()+'px'});
		}, this);

		this.closelabel.set('text','Close');

		this.closelabel.addEvent('click', function(){ Dialog.hide();});

	},

	show: function(fragmentURL)
	{
		$('dlgOverlay').setStyles({'display':'block', 'opacity':'0'});
		$('dlgOverlay').fade(0.7);

		$('dlgContainer').setStyles({'display':'block', 'opacity':'0'});


		var req = new Request({url:fragmentURL,
			onSuccess: function(html, xml) {
			
				
				//Clear the text currently inside the results div.
				$('dlgContent').set('text', '');
				//Inject the new DOM elements into the results div.
				$('dlgContent').innerHTML = html;
				window.scrollTo(0,0);
				$('dlgContainer').setStyle('height', $('dlgContent').getHeight());
				$('dlgContainer').fade(1);
			},
			//Our request will most likely succeed, but just in case, we'll add an
			//onFailure method which will let the user know what happened.
			onFailure: function() {
				$('dlgContent').set('html', '<h2>Error</h2><p>Unable to load the dialog fragment.</p>');
				window.scrollTo(0,0);
				$('dlgContainer').setStyle('height', $('dlgContent').getHeight());
				$('dlgContainer').fade(1);
			},
			evalScripts: true


		}).send();


		$('dlgContainer').setStyles({'display':'block'});
	},

	hide: function()
	{
		$('dlgContainer').setStyles({'display':'none'});
				$('dlgOverlay').fade('out');
				$('dlgContent').set('text', '');

	}
};

window.addEvent('domready', Dialog.init);
