var Dialog = Class.create({
	initialize:function(caption,parameters){
		this.caption			= (caption||'');
		this.width				= 400;
		this.height				= 0;
		this.pos				= [];
		this.centerOnScreen  	= true;
		
		this.sunkenBorder		= false;
		this.element			= null;
		this.overlay			= null;
		this.html_container		= null;
		this.buttons 			= [];
		this.html				= '';
		this.icon				= '';
		this.draggable			= true;
		this.disabledControls	= false;
		this.autoFocus			= true;
		
		this.tabButtons			= [];
		this.activeTab			= undefined;
		this.activeTabIndex		= 0;
		this.tabs				= [];
		
		this.modal				= true;
		
		this.onClose			= function(){};
		this.onInit				= function(){};
		this.onTabChange		= function(){};
		
		Object.extend(this,parameters);
	},
	_onInit:function(){
		this.onInit.bind(this)();
	},
	_onClose:function(){
		this.onClose.bind(this)();
	},
	_initTabs:function(){
		this.tabs = $A(this.html_container.getElementsByClassName('tabstrip'));
		if(this.tabs.length){
			this.tabButtons = $A(this.tabs[0].getElementsByTagName('span')).each(function(e){Element.extend(e)});
			var self = this;
			this.tabButtons.each(function(e){
				Event.observe(e,'click',self._onTabClick.bind(self));
			});
			this.setActiveTab(this.activeTabIndex);
		}
	},
	setActiveTab:function(id,elem){
		if(this.activeTab){
			this.activeTab.removeClassName('active');
			$('tab_'+this.activeTab.id).hide();
		} else {
			this.tabButtons.each(function(e){
				$('tab_'+e.id).hide();
			});
		}
		
		this.activeTab = id!=undefined?this.tabButtons[id]:elem;
		this.activeTab.addClassName('active');
		
		$('tab_'+this.activeTab.id).show();
		
		this.onTabChange.bind(this)(this.activeTab);
	},
	_onTabClick:function(e){
		this.setActiveTab(null,Event.element(e));
	},
	getTabInputData:function(tab_index){
		return this.getInputData($('tab_'+this.tabButtons[tab_index].id));
	},
	_drawOverlay:function(){
		var h = document.viewport.getHeight()+document.viewport.getScrollOffsets().top;
		this.overlay = Builder.node('div',{id:'dialog_modal_overlay',className:'dialog_overlay',style:'height:'+h+'px'});
		Element.setOpacity(this.overlay,.5);
		document.body.appendChild(this.overlay);
	},
	_removeOverlay:function(){
		var existing_dialogs = $A(document.getElementsByClassName('dialog'));
		if(existing_dialogs.length>1){
			var o = $('dialog_modal_overlay');
			o.style.zIndex = existing_dialogs.length-1;
			
			var i=1;
			existing_dialogs.each(function(d){					 
				d.style.zIndex = i;
				i++;
			});
		} else {
			this.overlay.remove();
		}
	},
	_draw:function(){
		if(this.modal){
			var o = $('dialog_modal_overlay');
			if(o){
				var existing_dialogs = $A(document.getElementsByClassName('dialog'));
				o.style.zIndex = existing_dialogs.length+1;
				
				var i=1;
				existing_dialogs.each(function(d){					 
					d.style.zIndex = i;
					i++;
				});
			} else {
				this._drawOverlay();
			}
		}
		this.element = Element.extend(Builder.node('div',{className:'dialog',style:'width:'+this.width+'px'}));
		if(this.height>0){
			this.element.setStyle('height:'+this.height+'px');
		}
		var title_div = Builder.node('div',{className:'title'});
		var close_button = Builder.node('div',{className:'close_button'});
		var title = Element.extend(Builder.node('span',{},this.caption));
		if(this.icon){
			title.setStyle('background-image:url('+this.icon+');padding-left:20px');
		}
		
		var btn_div = Builder.node('div',{className:'buttons'});
		this.progressIcon = Builder.node('div',{className:'progress_icon',style:'display:none'},'\u00A0');

		btn_div.appendChild(this.progressIcon);
		var self = this;
		this.buttons.each(function(e){
			var btn_settings 		= {};
			btn_settings.type 		= 'button';
			btn_settings.className 	= 'control_button';
			btn_settings.value 		= e.label;
			if(e.id){
				btn_settings.id		= e.id;
			}
			
			var btn = Builder.node('input',btn_settings);
			btn_div.appendChild(btn);
			if(e.callback){
				Event.observe(btn,'click',e.callback.bind(self));
			}
		});
		
		title_div.appendChild(title);
		title_div.appendChild(close_button);
		this.element.appendChild(title_div);
		
		this.html_container = Element.extend(Builder.node('div',{id:'dialog_html_container'+(new Date()).getMilliseconds(),className:'html_container',style:'width:'+(this.width-20)+'px'}));
		if(this.height>0 && (this.height-77)>0){
			this.html_container.addClassName('vscroll');
			this.html_container.setStyle('max-height:'+(this.height-77)+'px');
		}
		if(this.sunkenBorder){
			this.html_container.addClassName('sunkenborder');
		}
		this.element.appendChild(this.html_container);
		this.element.appendChild(btn_div);
		
		document.body.appendChild(this.element);
		
		Element.setOpacity(close_button,.5);
		
		Event.observe(close_button,'mouseover',function(e){Element.setOpacity(close_button,1)});
		Event.observe(close_button,'mouseout',function(e){Element.setOpacity(close_button,.5)});
		Event.observe(close_button,'click',this._onSmallCloseClick.bind(this));
		
		this.setHtml(this.html);
		
		if(this.centerOnScreen){
			var screenDim 	= document.viewport.getDimensions();
			
			var top 		= ((screenDim.height/2)-this.element.getHeight()/2) + document.viewport.getScrollOffsets().top;
			var left		= ((screenDim.width/2)-(this.element.getWidth()/2));
			
			if(top<0) top = 0;
			if(left<0) left = 0;
			
			this.element.style.left = left+'px';
			this.element.style.top 	= top+'px';
		}
		
		this._initTabs();
		
		if(this.draggable){
			new Draggable(this.element,{handle:'title',starteffect:null,endeffect:null});
		}
		if(this.autoFocus){
			Try.these(
				function(){this.html_container.getElementsByTagName('input')[0].focus()}.bind(this),
				function(){this.html_container.getElementsByTagName('textarea')[0].focus()}.bind(this),
				function(){this.html_container.getElementsByTagName('select')[0].focus()}.bind(this)
			);
		}
	},
	showProgress:function(){
		this.progressIcon.show();
	},
	hideProgress:function(){
		this.progressIcon.hide();
	},
	getInputData:function(parentObj){
		if(!parentObj) parentObj = this.html_container;
		var data = {};
		var tags = ['select','input','textarea'];
		var self = this;
		tags.each(function(t){
			$A(parentObj.getElementsByTagName(t)).each(function(o){
				o = Element.extend(o);
				if(o.readAttribute('name')){
					var val = o.value;
					if(o.hasClassName('datepicker')){
						var t = val.split('-');
						val = t[2]+'-'+t[1]+'-'+t[0];
					} else if(o.hasClassName('editor')){
						val = tinyMCE.get(o.name).getContent();
					}
					data[o.name] = val;
				}
			});
		});
		return data;
	},
	_onSmallCloseClick:function(){
		if(!this.disabledControls) this.close();
	},
	addButton:function(btn){
		this.buttons.push(btn);
	},
	disableControls:function(){
		this.disabledControls = true;
		var btns = $A(this.element.getElementsByClassName('control_button'));
		btns.each(function(e){
			e.disable();
		});
	},
	enableControls:function(){
		this.disabledControls = false;
		var btns = $A(this.element.getElementsByClassName('control_button'));
		btns.each(function(e){
			e.enable();
		});
	},
	setHtml:function(content){
		this.html_container.innerHTML = content;
	},
	_remove:function(){
		if(this.modal) this._removeOverlay();
		Element.extend(this.element).remove();
	},
	show:function(html){
		if(html) this.html = html;
		
		this._draw();
		this._onInit();
	},
	close:function(){
		this._remove();
		this._onClose();
	}
});
