Proto.UI.DialogsMinimized=0;
Proto.UI.Dialogs=[];
Proto.UI.Dialog=Class.create({
	    initialize: function() 
	    {
	    this.zIndex = 9999;
	    this.brd = 20;
	    this.minsize = 155;
	    this.maximize = false;
	    this.options = Object.extend({
	    
				classname:'Proto_dialog',
				url:false,
				iframe:false,
				html:"",
				title:"",
				status:"",
				onClose:function(){},
				onOpen:function(){},
				resize:false,
				dragable:true,
				closable:true,
				maxi:true,
				mini:true,
				height:300,
				width:400,
				destroyOnClose:true,
				modal:true,
				element:false,
				parent:document.body,
				sendingButton:false,
				macStyle:((navigator.userAgent.indexOf('Mac')!=-1)?true:false),
				buttons:[
							{text:"OK",'classname':'_ok','callback':this.close.bind(this)},
							{text:"Cancel",'classname':'_cancel','callback':this.close.bind(this)}
						]
				
	            	}, arguments[0] || { });
	            if(Prototype.Browser.IE)
	            {
	            	            if( typeof document.body.onload == "function")
					            {
					            	this.oldboonl = document.body.onload;
					            }
					            else
					            {
					            	this.oldboonl=function(){};
					            }
					            document.body.onload=function() {
					            this.render();
					            this.oldboonl();
					            }.bind(this);
	            }
	            else
	            {
	            this.render();
	            }

	            
    			Event.observe(window, "resize", this.moveMinimized.bind(this));
    			return(this);
	     },
	     open:function()
	     {
	     	Proto.UI.Dialogs.push(this);
	     	
	     if(this.minimize){this.mini();return;}
	     	if(this.options.modal)
			{
		     	new Effect.Appear(this.overlay);
		     }
		     
		     //this.container.show();

			//this.content.style.width = this.container.getWidth() -  this.title.getWidth() - -  this.buttons.getWidth() -  this.status.getWidth()+"px";
			//this.content.style.height= this.container.getHeight() - this.content.getAllDimensions().height  -this.title.getAllDimensions().height - this.buttons.getAllDimensions().height - this.status.getAllDimensions().height+this.content.getHeight()+"px"
			if(this.options.sendingButton)
			{
				this.container.show();
				this.buttons.hide();
				this.content.hide();
				Position.clone(this.options.sendingButton,this.container);
				new Effect.Morph(this.container, { style: 'top:'+this.tp+'; left:'+this.lft+'; width:'+((this.options.width)?this.options.width+"px":"auto;")+'; '+((this.options.height)?"height:"+this.options.height+"px":"height:auto;"), duration:0.15,afterFinish: function(){
				if(this.options.height&&this.options.width){
				
				this.content.style.height= this.container.getHeight() - this.title.getAllDimensions().height - this.buttons.getAllDimensions().height - this.status.getAllDimensions().height+"px";
				}
				else
				{
				
				if(!this.options.height){this.container.style.height="auto";}
				if(!this.options.width){this.container.style.width="auto";}
				}
				new Effect.Appear(this.content,{duration:1});
				new Effect.Appear(this.buttons,{duration:1});
				this.content.style.overflow="auto";
			//    this.container.style.position="fixed";
    //curvyCorners(settings, this.content);
    
				//Proto.UI.scrollbarsCollectionControl.rebuild();
				
				}.bind(this)});
			}
			else
			{
				new Effect.Appear(this.container,{afterFinish: function(){
				
				this.content.style.overflow="auto";
								new Effect.Appear(this.content,{duration:1});
				new Effect.Appear(this.buttons,{duration:1});
				}.bind(this)});
			}
			this.focus();
	     	this.options.onOpen();
	     },
	     focusOther:function()
	     {
	     	     	Proto.UI.Dialogs.each(function(d){
	     	     				if(!d.minimize&&d!=this)
	     	     				{
	     	     					d.focus();return;
	     	     				}
	     	     			});
	     },
	     close:function()
	     {
	     if(this.minimize){this.minimize=false;Proto.UI.DialogsMinimized--;}
	     this.moveMinimized();
	     //Proto.UI.scrollbarsCollectionControl.hide();
	     				this.buttons.hide();
				this.content.hide();
        Proto.UI.Dialogs=Proto.UI.Dialogs.without(this);	     	
        this.focusOther();
	     	if(this.options.modal)
			{
		     	new Effect.Fade(this.overlay,{duration:0.3});
		     }
		     if(this.options.sendingButton)
			{
				//new Effect.Fade(this.container);
					//			Position.clone(this.options.sendingButton,this.container);
					var dm = this.options.sendingButton.cumulativeOffset();
					//var d = this.options.sendingButton.getDimensions()
					var pagescroll={top:((document.documentElement.scrollTop) ? document.documentElement.scrollTop : document.body.scrollTop),left:((document.documentElement.scrollLeft) ? document.documentElement.scrollLeft : document.body.scrollLeft)};
					
					//this.container.style.overflow="hidden";
					//this.content.style.overflow="hidden";
					new Effect.Fade(this.container,{duration:0.3});
				new Effect.Morph(this.container, { style: 'top:'+Number(dm.top-pagescroll.top)+'px; left:'+dm.left+'px; width:50px; height:50px', duration:0.3});
			}
			else
			{
				new Effect.Fade(this.container);
			}
	     	this.options.onClose();
	     },
	     maxi:function()
	     {
	     	this.focus();
	     	if(this.minimize){this.minimize=false;Proto.UI.DialogsMinimized--;}
	     	if(!this.maximize){
	     	if(this.options.modal&&!this.overlay.visible())
	     	{
	     	new Effect.Appear(this.overlay,{duration:0.3});
				
	     	}
				this.content.hide();this.buttons.hide();
	     	var vpp = document.viewport.getDimensions();
	     	new Effect.Morph(this.container, 
	     	{ 
	     		style: 'top:'+ this.brd+'px; left:'+this.brd+'px; width:'+(vpp.width-this.brd*2)+'px; height:'+(vpp.height-this.brd*2)+'px',
	     	 	duration:0.3,
	     	 	afterFinish: function(){
				this.content.style.height= this.container.getHeight() - this.title.getAllDimensions().height - this.buttons.getAllDimensions().height - this.status.getAllDimensions().height+"px";
				
				new Effect.Appear(this.content,{duration:1});
				new Effect.Appear(this.buttons,{duration:1});
				this.content.style.overflow="auto";
				//Proto.UI.scrollbarsCollectionControl.rebuild();
				
				}.bind(this)});
	     	this.maximize = true;
	     	}
	     	else
	     	{
	     	new Effect.Morph(this.container, { style: 'top:'+this.tp+'; left:'+this.lft+'; width:'+this.options.width+'px; height:'+this.options.height+'px', duration:0.3,afterFinish: function(){
				//new Effect.Appear(this.content,{duration:1});
				//new Effect.Appear(this.buttons,{duration:1});
				this.content.style.height= this.container.getHeight() - this.title.getAllDimensions().height - this.buttons.getAllDimensions().height - this.status.getAllDimensions().height+"px"
				this.content.style.overflow="auto";
				//Proto.UI.scrollbarsCollectionControl.rebuild();
				
				}.bind(this)});
				
	     	this.maximize = false;
	     	}
	     },
	     mini:function()
	     {
	     this.focus();
	     this.maximize = false;
	     if(!this.minimize){
	     	
	     	Proto.UI.DialogsMinimized++;
				this.content.hide();
				this.buttons.hide();
	     	var vpp = document.viewport.getDimensions();
	     	new Effect.Morph(this.container, 
	     	{ 
	     		style: 'top:'+(vpp.height-22)+'px; left:'+(vpp.width-((Proto.UI.DialogsMinimized>0)?this.minsize*Proto.UI.DialogsMinimized:this.minsize))+'px; width:150px; height:40px',
	     	 	duration:0.3});
	     	 	if(this.options.modal)
	     	{
	     	new Effect.Fade(this.overlay,{duration:0.3});
				
	     	}
	     	 	
	     	this.minimize = true;
	     	this.focusOther();
	     	}
	     	else
	     	{
	     	
	     	Proto.UI.DialogsMinimized--;
	     	this.minimize = false;
	     	this.moveMinimized();
	     	new Effect.Morph(this.container, { style: 'top:'+this.tp+'; left:'+this.lft+'; width:'+this.options.width+'px; height:'+this.options.height+'px', duration:0.3,afterFinish: function()
	     	{
				new Effect.Appear(this.content,{duration:1});
				new Effect.Appear(this.buttons,{duration:1});
				this.content.style.height= this.container.getHeight() - this.title.getAllDimensions().height - this.buttons.getAllDimensions().height - this.status.getAllDimensions().height+"px"
				this.content.style.overflow="auto";
				
				if(this.options.modal)
	     	{
	     	new Effect.Appear(this.overlay,{duration:0.3});
				
	     	}
				}.bind(this)});
				
	     	
	     	}
	     },
	     moveMinimized:function()
	     {
	     	var arr = [];
	     	Proto.UI.Dialogs.compact().each(function(d){
	     	if(d.minimize )
	     			{
	     				arr[Number(d.container.style.left.replace("px",""))] = d;
	     			}
	     	});
	     	
	     	var cntr = 1;
	     	var vpp = document.viewport.getDimensions();
	     	arr.compact().reverse().each(function(d){
	     			new Effect.Move(d.container, { x: vpp.width-(this.minsize*cntr), y: vpp.height-22, mode: 'absolute',duration:0.3 });
	     				cntr++;
	     			
	     		}.bind(this));
	     },
	     focus:function()
	     {
	     	Proto.UI.Dialogs.compact().reverse().each(function(d){
	     	d.container.style.zIndex = this.zIndex;
	     	if(d!=this&&!d.minimize)
	     	{
	     		d.container.addClassName("ProtoDialogBlired");
	     	}
	     	}.bind(this));
	     	
	     	
	     	this.container.style.zIndex=this.zIndex+1;
	     	this.container.removeClassName("ProtoDialogBlired");
	     },
	     resizeProcess:false,
	     startResize:function()
	     {
	     	this.resizeProcess = true;	
	     	this.draggCrnr = new Draggable(this.resizeCorner, { revert: false, constraint :false,
	     	change :this.resize.bind(this),
	     	 onEnd:function(){this.stopResize();}.bind(this) }); 
	     },
	     stopResize:function()
	     {
	     	this.resizeProcess = false;
	     	if(this.draggCrnr){this.draggCrnr.destroy();}
	     	this.resizeCorner.style.left='';
	     	this.resizeCorner.style.top='';
	     	this.resizeCorner.style.bottom='-1px';
	     	this.resizeCorner.style.right='-1px';
	     	

	     },
	     resize:function(event)
	     {
	     	if(!this.resizeProcess){return;}
	     	var dim = this.resizeCorner.getAllDimensions();
	     	var of = this.container.cumulativeOffset();
	     	var drgo = this.resizeCorner.cumulativeOffset();
	     	this.pointer = [drgo.left,drgo.top];
	     	var pagescroll={top:((document.documentElement.scrollTop) ? document.documentElement.scrollTop : document.body.scrollTop),left:((document.documentElement.scrollLeft) ? document.documentElement.scrollLeft : document.body.scrollLeft)};
            
	     	this.options.width = this.pointer[0] - pagescroll.left - of.left + dim.width ;
	     	this.options.height = this.pointer[1]  - of.top + dim.height ;
	     	this.container.style.width = this.options.width +"px";
	     	this.container.style.height = this.options.height +"px";
	     	this.content.style.height= this.container.getHeight() - this.title.getAllDimensions().height - this.buttons.getAllDimensions().height - this.status.getAllDimensions().height+"px";
				
	     },
	     render:function()
	     {
	     	this.content = new Element('div',{'class':this.options.classname+"_content"});
	     	if(this.options.element)
	     	{
	     		this.options.element.wrap(this.content);
	     	}
	     	else
	     	{
	     		Element.insert(this.options.parent,this.content);
	     	}
	     	this.content.hide();
	     	this.container = new Element('div',{
	     			'class':this.options.classname+"_container"+((this.options.modal)?"":" proto_shadow"), 
	     			style:"width:"+this.options.width+"px; "+((this.options.height)?"height:"+this.options.height+"px":"")
	     		});
	     	this.content.wrap(this.container);
	     	this.container.hide();
	     	this.container.observe("mousedown",this.focus.bind(this));
	     	
	     	this.wrapie = new Element('div',{'class':this.options.classname+"_wrapie"});
	     	this.content.wrap(this.wrapie);
	     	this.content.show();
	     	this.title = new Element('div',{'class':this.options.classname+"_title"});
	     	this.container.insertBefore(this.title,this.wrapie);
	     	this.title.observe('click',function(){if(this.minimize){this.mini();}}.bind(this));
	     	this.title.observe("mousedown",this.focus.bind(this));
	     		     	
	     	if(this.options.closable){
	     	this.closeButton = new Element('div',{'class':this.options.classname+"_closeButton"+((this.options.macStyle)?"_mac":"")});
	     	this.title.insert(this.closeButton);
	     	this.closeButton.observe("click",this.close.bind(this))
	     	}
	     	if(this.options.maxi){
	     	this.maxiButton = new Element('div',{'class':this.options.classname+"_maxiButton"+((this.options.macStyle)?"_mac":"")});
	     	this.title.insert(this.maxiButton);
	     	this.maxiButton.observe("mouseup",this.maxi.bind(this))
	     	this.maxiButton.observe("mousedown",function(e){Event.stop(e);})
	     	this.maxiButton.observe("click",function(e){Event.stop(e);})
	     	}
	     	if(this.options.mini){
	     	this.miniButton = new Element('div',{'class':this.options.classname+"_miniButton"+((this.options.macStyle)?"_mac":"")});
	     	this.title.insert(this.miniButton);
	     	this.miniButton.observe("mouseup",this.mini.bind(this))
	     	this.miniButton.observe("mousedown",function(e){Event.stop(e);})
	     	this.miniButton.observe("click",function(e){Event.stop(e);})
	     	}
	     	this.titleText = new Element('div',{'class':this.options.classname+"_titleText"});
	     	this.title.insert(this.titleText);
			this.titleRightCorner = new Element('div',{'class':this.options.classname+"_titleRightCorner"});
	     	this.title.insert(this.titleRightCorner);
			this.titleText.update(this.options.title);
	     	this.buttons = new Element('div',{'class':this.options.classname+"_buttons"});
	     	this.container.insert(this.buttons);
			this.wrapiebuttons = new Element('div',{'class':this.options.classname+"_wrapie"});
	     	this.buttons.wrap(this.wrapiebuttons);
	     	
			
			if(this.options.modal)
			{
		     	
		     	this.overlay = new Element('div',{'class':this.options.classname+"_overlay",'style':'display:none'});
		     	
		     	document.body.insert(this.overlay);
	     	}
	     	//if(!Prototype.Browser.IE){
	     	this.options.buttons.each(function(b){
	     	
	     	if(typeof b == "object"){
	     	
	     			     	var but = new Element('button',{
	     			     	'class':this.options.classname+"_button"+b.classname
	     			     	});
	     			     	
	     					this.buttons.insert(but);
	     					but.observe("click",function(){b.callback();}.bind(this));
	     					but.update(b.text);	
	     					}
	     		}.bind(this));
	     		     //}	
	     	if(!this.options.element)
	     	{
		     	if(this.options.url)
		     	{
		     		new Ajax.Updater(this.content, this.options.url);
		     	}
		     	else
		     	{
			     	if(this.options.iframe)
			     	{
			     	this.content.update("<iframe src='"+this.options.iframe+"'></iframe>");
			     	}
			     	else
			     	{
			     	this.content.update(((typeof this.options.html == "object")?_(this.options.html).innerHTML:this.options.html));
			     	}
		     	}
	     	}
	     	
	     	this.status = new Element('div',{'class':this.options.classname+"_status"});
	     	this.container.insert(this.status);
	     	this.statusText = new Element('div',{'class':this.options.classname+"_statusText"});
	     	this.status.insert(this.statusText);
	     	this.statusText.update(this.options.status);
		    this.statusRightCorner = new Element('div',{'class':this.options.classname+"_statusRightCorner"});
	     	this.status.insert(this.statusRightCorner);
	     	if(this.options.resize)
	     	{
	     		this._resizeCorner = new Element('div',{'class':this.options.classname+"__resizeCorner"});
	     		this.statusRightCorner.insert(this._resizeCorner);
	     		this.resizeCorner = new Element('div',{'class':this.options.classname+"_resizeCorner"});
	     		this._resizeCorner.insert(this.resizeCorner);
	     		this.resizeCorner.observe("mousedown",this.startResize.bind(this));
	     			     	
	     	}
			var dim = document.viewport.getDimensions();
		    this.tp = this.container.style.top=(dim.height/2-this.container.getHeight()/2)+"px"; 
		    this.lft = this.container.style.left=(dim.width/2-this.container.getWidth()/2)+"px"; 

			if(this.options.dragable)
			{
				new Draggable(this.container, { 
						constraint: false,
						zindex:this.zIndex+5,
						handle: this.title,
						onStart:function(){this.title.style.cursor="move";}.bind(this),
						onEnd:function(){
						this.title.style.cursor="default";
							this.tp = this.container.style.top; 
		    				this.lft = this.container.style.left; 
		    					}.bind(this) 
		    		}); 
			}
	     	
	     }
            
      })