var windowManager = new Class({

	Implements: [Options,Events],
	
	options: {
		classInner: 'windowManagerInner',
		classOuter: 'windowManagerOuter',
		classHorizontal: 'windowManagerHorizontal',
		classVertical: 'windowManagerVertical',
		classOptions: 'windowManagerOptions',
		classContent: 'windowManagerContent',
		classYes:'windowManagerYes',
		classNo:'windowManagerNo',
		classCancel:'windowManagerCancel',
		classTitle:'windowManagerTitle',
		classTitleClose:'windowManagerTitleClose',
		autoLeft: '510',
		zindex: 1000,
		alerts: [],
		buttons: {
				ok: 'Ok',
				yes: 'Yes',
				no: 'No'
		}
	},
	
	initialize: function(options) {
		this.setOptions(options);
		
		this.options.alerts.each(function(wind) {
			$(wind.element).addEvent('click',this.createWindow.bind(this,wind));
		},this);
	},
	
	created: false,
	
	createWindow: function(wind) {
		
		if(this.created)
			return false;
	
		this.current = wind;
		
		this.created = true;
		
		this.outer = new Element('div',{'class':this.options.classOuter}).setStyle('z-index',this.options.zindex);
		this.top = new Element('div',{'class':this.options.classHorizontal});
		this.left = new Element('div',{'class':this.options.classVertical});
		this.inner = new Element('div',{'class':this.options.classInner});
		this.right = new Element('div',{'class':this.options.classVertical});
		this.bottom = new Element('div',{'class':this.options.classHorizontal});
		
		this.outer.adopt(this.top);
		this.outer.adopt(this.left);
		this.outer.adopt(this.inner);
		this.outer.adopt(this.right);
		this.outer.adopt(new Element('div').setStyle('clear','both'));
		this.outer.adopt(this.bottom);
												
		this.options.zindex++;
		
		if(wind.show && wind.show == 'content')
			this.inner.adopt(new Element('div',{'class':this.options.classContent,'html':$(wind.element).get('html')}));
		else if(typeof wind.content == 'object')
			this.inner.adopt(new Element('div',{'class':this.options.classContent}).adopt(wind.content));
		else
			this.inner.adopt(new Element('div',{'html':wind.content,'class':this.options.classContent}));
		
		if(wind.closeElement) {
			var ce = wind.closeElement;
			
			if(typeof ce == 'object')
				ce.addEvent('click',this.closeWindow.bind(this,true));
			else
				$(ce).addEvent('click',this.closeWindow.bind(this,true));
		}
		
		if(wind.title) {
			var title = new Element('div', {'class' : this.options.classTitle});
			title.set('text', wind.title);
			title.adopt(new Element('div', {
				'class' : this.options.classTitleClose,
				'events' : {
					'click' : this.closeWindow.bind(this,true)
				}
			}));
			title.inject(this.inner, 'top');
		}
		
		switch(wind.type) {
			case 'run':
				this.runFunction(wind);
				break;
			case 'confirm':
				this.callConfirm(wind);
				break;
			case 'redirect':
				this.callRedirect(wind);
				break;
			case 'error':
				this.callError(wind);
				break;
			default:
				break;
		}
		
		this.show();
	},
	
	runFunction: function(w) {
		if(w.fn)
			w.fn();
		this.closeWindow(true);
	},
	
	closeWindow: function(rv) {
		if(this.inner.getFirst('div') && this.inner.getFirst('div').getFirst())
			this.current.content = this.inner.getFirst('div').getFirst().dispose();
		
		if (this.current.beforeClose)
			this.current.beforeClose();
		
		this.outer.destroy();
		this.created = false;
		$('shade').destroy();
		return rv;
	},
	
	submitForm: function(w) {
		if(w.submit)
			$(w.submit).submit();
		if(w.fireClick)
			w.fireClick.fireEvent('click');
		this.closeWindow(true);
	},
	
	redirect: function(w) {
		if(w.redirect)
			window.location = w.redirect;
		this.closeWindow(true);
	},
	
	callError: function(w) {
		this.option = new Element('div',{'class':this.options.classOptions});
		okbutton = new Element('input',{'type':'button','accesskey':'c','class':this.options.classCancel,'value':this.options.buttons.ok});
		if (w.buttons && w.buttons.ok) okbutton.set('value', w.buttons.ok);
		okbutton.addEvent('click',this.closeWindow.bind(this,false));
		this.option.adopt(okbutton);
		this.inner.adopt(this.option);				
	},

	callConfirm: function(w) {
	
		this.option = new Element('div',{'class':this.options.classOptions});
		yesbutton = new Element('input',{'type':'button','class':this.options.classYes,'accesskey':'y','value':this.options.buttons.yes});
		if (w.buttons && w.buttons.yes) yesbutton.set('value', w.buttons.yes);
		nobutton = new Element('input',{'type':'button','class':this.options.classNo,'accesskey':'n','value':this.options.buttons.no});
		if (w.buttons && w.buttons.no) nobutton.set('value', w.buttons.no);
		yesbutton.addEvent('click',this.submitForm.bind(this,w));
		nobutton.addEvent('click',this.closeWindow.bind(this,false));
		this.option.adopt(yesbutton);
		this.option.adopt(nobutton);
		this.inner.adopt(this.option);		
	},
	
	callRedirect: function(w) {
	
		this.option = new Element('div',{'class':this.options.classOptions});
		yesbutton = new Element('input',{'type':'button','class':this.options.classYes,'accesskey':'y','value':this.options.buttons.yes});
		nobutton = new Element('input',{'type':'button','class':this.options.classNo,'accesskey':'n','value':this.options.buttons.no});
		yesbutton.addEvent('click',this.redirect.bind(this,w));
		nobutton.addEvent('click',this.closeWindow.bind(this,false));
		this.option.adopt(yesbutton);
		this.option.adopt(nobutton);
		this.inner.adopt(this.option);		
	},
	
	show: function() {
		$$('body').each(function(e){
			
			bgdiv = new Element('div', {
				'id' : 'shade',
				'styles' : {
					'position' : 'absolute',
					'height' : window.getScrollHeight().toInt()+'px',
					'width' : '100%',
					'background-color' : '#000000',
					'top' : 0,
					'opacity' : 0.6
				},
				'events' : {
					'click' : function(e) {
						e.stop();
						this.closeWindow();
					}.bind(this)
				}
				
			});
			
			e.adopt(bgdiv);
			e.adopt(this.outer);
			var h = this.inner.getHeight();
			var w = this.inner.getWidth();
			this.left.setStyle('height',h);
			this.right.setStyle('height',h);
			
						
			wh = $(document.window).getSize().y;
			ww = window.getScrollSize().x;
			
			if(this.options.autoLeft == false)
				wleft = Math.round((ww/2 - w/2));
			else
				wleft = Math.round((this.options.autoLeft - w/2));
			
			wtop = Math.round((wh/2 - h/2)) + $(document.window).getScroll().y;	
			if (wtop < 10) {
				wtop = 10;
			}
			
			this.outer.setStyle('left',wleft);
			this.outer.setStyle('top',wtop);
			
		},this);
	}
});
