//VERSÃO 2.0
//mootools 1.3
var iWindow = new Class ({
	Implements :[Options, Events, Chain],
	options : {
		wShadowBackground: 'url(../html/images/iwindow/iwshadow_light.png)',
		wShadowDuration:500,
		modalSize   : {w:500,h:312},
		borderSize  : 6,
		clickToExit : false,
		escapeToExit: true,
		commandBar  : 32,
		titleBar    : 24,
		title		: "",
		commands:{
			type 		: 0, //0 = botão OK, 1 = botão ok+Cancel	
			okLabel 	: 'Ok',
			cancelLabel : 'Cancel'
		},
		autoClose : false //se true fecha sempre que se carrega no ok
		},
	
	initialize : function(callbackFn,options){
		this.setOptions(options);
		this.wShadow = this.createShadow();
		this.modal = this.createModal();
		this.titleBar = this.createTitleBar();
		this.setTitle(this.options.title);
		this.closeButton = this.createCloseButton();
		this.commandBar = this.createCommandBar();
		this.createButtons();
		this.callback = callbackFn;
		this.status = 0;//escondida
		window.addEvent('resize',function(){this.setSizes();}.bind(this));
		drag = new Drag.Move(this.modal.getParent(),{handle:this.titleBar});
	},
	getModal : function(){return this.modal;},
	getCommandBar : function(){return this.commandBar;},
	getTitle : function(){return this.title;},
	createButtons : function(){
		if (!this.commandBar) return false;
		//ok vai sempre
			var bOk = new Element('button',{'class':'iwBtn'});
			bOk.set('text',this.options.commands.okLabel);
			bOk.addEvent('click',function(){
				if (this.options.autoClose) { // é para fechar
						this.hideModal(true); 
				} else { //chama-se a callback para o que caller feche a iwindow
						this.callback(this,true);	
				}
			}.bind(this));
			bOk.inject(this.commandBar);
		
		switch (this.options.commands.type){
			case 1:
				var bCancel = new Element('button',{'class':'iwBtn'});
				bCancel.set('text',this.options.commands.cancelLabel);
				bCancel.addEvent('click',function(){
					this.hideModal(false);
				}.bind(this));
				bCancel.inject(this.commandBar);			
			break;
		}
	},
	createCloseButton : function(){
		if (!this.titleBar) return false;
		var cb = new Element('div',{'id':'iWCloseButton'});
		cb.setStyles({
					 'position':'absolute',
					 'height':16,
					 'width':16,
					 'top':5,
					 'right':5
		});
		cb.addEvent('click',function(){
			this.hideModal(false);
		}.bind(this));
		cb.inject(this.titleBar);
		
	},
	setTitle : function(title){
		this.titleBar.getElement('span').set('html',title);
		this.title = title;
	},
	createTitleBar : function(){
		if (!this.options.titleBar) return false;
		//barra de título
		var cb = new Element('div',{'class':'iWTitleBar'});
		cb.setStyles({
					 'height':this.options.titleBar,
					 'width':this.options.modalSize.w-(this.options.borderSize*2),
					 'top':this.options.borderSize,
					 'left':this.options.borderSize
					 });
		var sp = new Element('span');
		sp.set('text',this.options.title);
		sp.inject(cb);
		cb.inject(this.modal.getParent());
		return cb;
	},
	createCommandBar:function(){
		//barra de comandos?
		if (!this.options.commandBar) return false;
		var cb = new Element('div',{'class':'iWCommandBar'});
		cb.setStyles({
					 'height':this.options.commandBar,
					 'width':this.options.modalSize.w-(this.options.borderSize*2),
					 'bottom':this.options.borderSize,
					 'left':this.options.borderSize
					 });		
		this.commandBar = cb;			
		cb.inject(this.modal.getParent());
		return cb;
	},
	createShadow : function(){
		var wShadow = new Element('div');
		wShadow.setStyles({
				'position':'absolute',
				'width':'900',
				'height':'900',
				'opacity':0,
				'background':this.options.wShadowBackground+' repeat',
				'left':0,
				'z-index':999
						  });
		wShadow.injectTop(document.body);
		wShadow.set('morph',{'duration':this.options.wShadowDuration,link:'chain'}).set({'opacity':0});
		//click to exit
		if (this.options.clickToExit){
			wShadow.addEvent('click',function(e){
						new Event(e).stop();
						this.hideModal(false);//cancel
										  }.bind(this)
							);
		};		
		return wShadow;	
	},
	createModal : function(){
		var size = this.options.modalSize;
		var md = new Element('div',{'id':'iWindow'});
		md.setStyles({'width':this.options.modalSize.w,'height':this.options.modalSize.h,'position':'absolute'});
		var area = new Element('div',{'id':'iWContents'});
		area.setStyles({'position':'relative',
					   'left':this.options.borderSize,
					   'top':this.options.borderSize+this.options.titleBar+1,
					   'width':this.options.modalSize.w-(this.options.borderSize*2),
					   'height':this.options.modalSize.h-(this.options.borderSize*2)-this.options.titleBar-this.options.commandBar-2
					   });		
		area.inject(md);
		md.inject(this.wShadow);
				md.addEvent('click',function(e){
				e = new Event(e).stop();
					});
		return area;
	},
	showModal : function(fn){	
		this.status = 1;//visivel		
		this.setSizes();
		this.wShadow.morph({'opacity':1,'onComplete':function()
															  {
																if (fn) fn();
															  }
							}
							);

		//escapeToExit
		if (this.options.escapeToExit){
			window.addEvent('keypress',function(e){
						if (e.code == 27) {												
							new Event(e).stop();
							this.hideModal(false);//cancel
						}
										  }.bind(this)
							);
		};
		
		},
	hideModal : function(val){
		this.wShadow.morph({'opacity':0}).get('morph').chain(function(){
			this.callback(this,val);
			this.status = 0;//escondida
			window.removeEvent('keypress');
							}.bind(this));
	},
	kill :function(){
		this.wShadow.destroy();
	},
	setSizes : function(){
		if (this.status == 0) return;
		var s = window.getSize();
		ts = this.modal.getParent().getCoordinates();
		ps = document.getScroll();
		this.wShadow.setStyles({'width':'100%','height':'100%'});
		this.modal.getParent().setStyles({
				'top':(s.y/2)-(ts.height/2)+ps.y,
				'left':(s.x/2)-(ts.width/2)
				 });

	}

});//iWindow
