// JavaScript Document
if ( ! window.Prototype )
	throw new Exception('Can\'t work without prototype');
/*	
if ( ! window.Scriptaculous )
	throw new Exception('Can\'t work without scriptaculous');
*/	
if ( ! window.BrowserDetect )
	throw new Exception('Can\'t work without BrowserDetect');
	
if ( ! window.hensam )
	var hensam = { Projects : { mehrdafon : {} } };
else if ( ! (hensam.Projects ) )
	hensam.Projects = { mehrdafon : {} } ;
else if ( ! hensam.Projects.mehrdafon  )
	hensam.Projects.mehrdafon = {};
	
hensam.Projects.mehrdafon.AjaxWrapper = Class.create({
    wrapper : 'ajaxWrapper',
	content : 'ajaxContent',
	helpIE6 : false,
	cancel : null,
	
	bgDarkener : null,
	bgDarkenerForIE : null,
	
	hide : function() {
		if ( this.cancel )
		{
			this.cancel.stopObserving();
		}
		this.wrapper.hide();
		if (this.bgDarkenerForIE)
		{
			this.bgDarkenerForIE.hide();
			['main','header','footer'].each(function(element) { element = $(element); element.setStyle('position: relative;') });
		}
		this.bgDarkener.hide();
		document.fire('ajaxwrapper:hidden');
	},
	
	clear : function() {
		return this.content.update('');
	},
	
	show : function() {
	
		this.cancel = this.content.down('.cancel');
		if ( this.cancel )
		{
			this.cancel.observe('click', this.cancelEventListener.bindAsEventListener(this));
		}
		
		if ( ! this.helpIE6 ) {	
			this.bgDarkener.show();
			if (this.bgDarkenerForIE)
			{
				this.bgDarkenerForIE.show();
				['main','header','footer'].each(function(element) { element = $(element); element.setStyle('position: static;') });
			}
		}
		this.content.show();
		this.wrapper.show();
		document.fire('ajaxwrapper:shown');
	},
	
	alertMessage : function(message) {
		this.content.update('<span class="headline_box">'+message+'</span>');
		this.show();
	},
	
	update : function(newContent) {
		this.content.update(newContent);
	},
	
	cancelEventListener : function(event) {
		if ( this.content.visible() && 
			 (
			 	event.keyCode == 27 ||
			 	(
				 this.cancel && (event.findElement() == this.cancel)
				)
			 )
		   )
		{
			this.hide();
		}
	},
	
	initialize : function(forceIE6Compability) {
		this.wrapper = $(this.wrapper);
		this.content = $(this.content);
		
		if ( forceIE6Compability && BrowserDetect.version < 7 && BrowserDetect.browser == 'Explorer')
		{
			this.helpIE6 = true;
		}
		
		this.bgDarkener = $('darkener') || new Element('div', {	'id' : 'darkener' });
		this.bgDarkener.setStyle({ 
			'display': 'none',
			'opacity': 0.5,
			'height' : '100%'
		}).hide();
		if ( BrowserDetect.browser == 'Explorer' )
		{
			if ( BrowserDetect.version < 7 && ! this.helpIE6 )
			{
				// disable darkener
				this.show = function() { return; };
				this.hide = function() { return; };
				return;
			}
			else if ( BrowserDetect.version < 8 )
			{
				this.bgDarkenerForIE = $('darkenerIE') || new Element('div', {	id: 'darkenerIE' });
				this.bgDarkenerForIE.setStyle({ 
					'display': 'none',
					'opacity': 0.5
				}).hide();				
				this.content.up().up().appendChild(this.bgDarkenerForIE);
			}
		}
		document.body.appendChild(this.bgDarkener);
		document.observe('keypress', this.cancelEventListener.bindAsEventListener(this));
	}
});
