Webadmin.Dialogue = function(id, options){
	this.id = options;
	this._open = false;
	this.options = options;
	this.create();		
	this.onshow = new Webadmin.Event.custom.publisher();
	this.onhide = new Webadmin.Event.custom.publisher();	
};

Webadmin.Dialogue.prototype = {
	create : function(){
		this.element = Webadmin.Dom.create('<div class="webadmin_dialogue"><div class="webadmin_dialogue_outer"><div class="webadmin_dialogue_inner"><div class="header"></div><div class="content"></div></div></div></div>');
		this.header = Webadmin.Dom.getElementsByClassName('header', 'div', this.element)[0];
		if(this.options.close){
			this.close = Webadmin.Dom.create('<a href="#" class="button close">Close</a>');
			this.header.appendChild(this.close);
		}
		this.content = Webadmin.Dom.getElementsByClassName('content', 'div', this.element)[0];
		if(this.options.content){
			this.content.innerHTML=this.options.content;
		}
		var s = this.element.style;
		s.zIndex=501;
		s.position='absolute';
		s.overflow='hidden';
		s.width='0';
		s.height='0';
		Webadmin.Event.onavailable(function(){
			document.body.appendChild(this.element);
			if(this.options.close){
				Webadmin.Event.listener.add(this.close, 'click', this.hide, this);
			}
		}, this);
	},
	show : function(){
		if(this._open===false){
			var s = this.element.style;
			s.display='block';
			s.height='auto';
			s.width='auto';
			var height = this.element.offsetHeight;
			var width = this.element.offsetWidth;
			s.display='none';
    			var offset = Webadmin.Window.scrollTop();
    			var top=(Webadmin.Window.viewportHeight()/2)-(height/2)+offset;
    			var left=(Webadmin.Window.viewportWidth()/2)-(width/2);
    			if(top<0){
    				top=10;
    			}
    			if(left<0){
    				left=10;
    			}
    			s.top = top+'px';
    			s.left = left+'px';					
    			s.display='block';
    			
    			//var end = "top:"+top+"px;left:"+left+"px;";
    			//var animation = new Webadmin.Animation(this.element, {end:end}, 250);
    			//animation.animate();
    			
		}
	},
	hide : function(e){
		if(e){
			e.stop();
		}
		var s = this.element.style;
		s.display='none';
		this.onhide.fire();
	}
};


/* Webadmin.Dialogue.Overlay
 * Creates a translucent overlay across the entire screen
 *
 * Requires Webadmin.Dom, Webadmin.Event
 * Webadmin.Animation is also required if you wish to use the 'fade' effect (instance.animate = true)
 *
 * Required CSS:
 *
 * .webadmin_dialogue_overlay {
 *	position:absolute;
 *	top:0;
 *	left:0;
 *	width:100%;
 *	background-color:#000;
 *	z-index:450;
 *	height:100px;
 *	filter:alpha(opacity=60);
 *	-moz-opacity: 0.6;
 *	opacity: 0.6;
 *}
 *
 * Feel free to change z-index to fit in with your application's requirements
 */

Webadmin.Dialogue.Overlay = function(){
	this.create();
	this.animate = false;
	this.onshow = new Webadmin.Event.custom.publisher();
	this.onhide = new Webadmin.Event.custom.publisher();
};

Webadmin.Dialogue.Overlay.prototype = {
	create : function(){
		this.element = Webadmin.Dom.create('<div class="webadmin_dialogue_overlay"></div>');
		Webadmin.Event.onavailable(function(){
			this.element.style.display='none';
			document.body.appendChild(this.element);
		}, this);		
	},
	show : function(){
		if(document.all){
	    selects = document.getElementsByTagName("select");
		    for (i = 0; i != selects.length; i++) {
		        selects[i].style.visibility = "hidden";
		    }    		
		}
		this.element.style.height=document.body.offsetHeight+'px';
		this.element.style.display='block';
		if(this.animate===true&&Webadmin.Animation){
			var animation = new Webadmin.Animation(this.element, {start:"opacity:0;",end:"opacity:0.6;"}, 100);
			var callback = function(){
				this.onshow.fire();
			};
			callback.subscribe(animation.oncomplete, this);
			animation.animate();
		}else{
			this.onshow.fire();
		}
	},
	hide : function(){
		var callback = function(){	
			if(document.all){
			    selects = document.getElementsByTagName("select");
			    for (i = 0; i != selects.length; i++) {
			        selects[i].style.visibility = "visible";
			    }   				
			}			
			this.element.style.display='none';
			this.onhide.fire();
		};	
		if(this.animate===true&&Webadmin.Animation){
			var animation = new Webadmin.Animation(this.element, {start:"opacity:0.6;",end:"opacity:0;"}, 100);
			callback.subscribe(animation.oncomplete, this);
			animation.animate();
		}else{			
			callback.call(this);
		}
	}
};
