var Overlay=new Class({Implements:[Options,Events],options:{id:"overlay",color:"#000",duration:500,opacity:0.5,zIndex:5000},initialize:function(a,b){this.setOptions(b);this.container=document.id(a);this.bound={window:{resize:this.resize.bind(this),scroll:this.scroll.bind(this)},overlayClick:this.overlayClick.bind(this),tweenStart:this.tweenStart.bind(this),tweenComplete:this.tweenComplete.bind(this)};this.build().attach()},build:function(){this.overlay=new Element("div",{id:this.options.id,opacity:0,styles:{position:(Browser.ie6)?"absolute":"fixed",background:this.options.color,left:0,top:0,"z-index":this.options.zIndex}}).inject(this.container);this.tween=new Fx.Tween(this.overlay,{duration:this.options.duration,link:"cancel",property:"opacity"});return this}.protect(),attach:function(){window.addEvents(this.bound.window);this.overlay.addEvent("click",this.bound.overlayClick);this.tween.addEvents({onStart:this.bound.tweenStart,onComplete:this.bound.tweenComplete});return this},detach:function(){var a=Array.prototype.slice.call(arguments);a.each(function(b){if(b=="window"){window.removeEvents(this.bound.window)}if(b=="overlay"){this.overlay.removeEvent("click",this.bound.overlayClick)}},this);return this},overlayClick:function(){this.fireEvent("click");return this},tweenStart:function(){this.overlay.setStyles({width:"100%",height:this.container.getScrollSize().y});return this},tweenComplete:function(){this.fireEvent(this.overlay.get("opacity")==this.options.opacity?"show":"hide");return this},open:function(){this.fireEvent("open");this.tween.start(this.options.opacity);return this},close:function(){this.fireEvent("close");this.tween.start(0);return this},resize:function(){this.fireEvent("resize");this.overlay.setStyle("height",this.container.getScrollSize().y);return this},scroll:function(){this.fireEvent("scroll");if(Browser.ie6){this.overlay.setStyle("left",window.getScroll().x)}return this}});var MooDialog=new Class({Implements:[Options,Events],options:{"class":"MooDialog",title:null,scroll:true,forceScroll:false,useEscKey:true,destroyOnHide:true,autoOpen:true,closeButton:true,onInitialize:function(){this.wrapper.setStyle("display","none")},onBeforeOpen:function(){this.wrapper.setStyle("display","block");this.fireEvent("show")},onBeforeClose:function(){this.wrapper.setStyle("display","none");this.fireEvent("hide")}},initialize:function(b){this.setOptions(b);this.options.inject=this.options.inject||document.body;b=this.options;var c=this.wrapper=new Element("div."+b["class"].replace(" ",".")).inject(b.inject);this.content=new Element("div.content").inject(c);if(b.title){this.title=new Element("div.title").set("text",b.title).inject(c);c.addClass("MooDialogTitle")}if(b.closeButton){this.closeButton=new Element("a.close",{events:{click:this.close.bind(this)}}).inject(c)}if((b.scroll&&Browser.ie6)||b.forceScroll){c.setStyle("position","absolute");var a=c.getPosition(b.inject);window.addEvent("scroll",function(){var d=document.getScroll();c.setPosition({x:a.x+d.x,y:a.y+d.y})})}if(b.useEscKey){document.addEvent("keydown",function(d){if(d.key=="esc"){this.close()}}.bind(this))}this.addEvent("hide",function(){if(b.destroyOnHide){this.destroy()}}.bind(this));this.fireEvent("initialize",c)},setContent:function(){var b=Array.from(arguments);if(b.length==1){b=b[0]}this.content.empty();var a=typeOf(b);if(["string","number"].contains(a)){this.content.set("text",b)}else{this.content.adopt(b)}return this},open:function(){this.fireEvent("beforeOpen",this.wrapper).fireEvent("open");this.opened=true;return this},close:function(){this.fireEvent("beforeClose",this.wrapper).fireEvent("close");this.opened=false;return this},destroy:function(){this.wrapper.destroy()},toElement:function(){return this.wrapper}});Element.implement({MooDialog:function(a){this.store("MooDialog",new MooDialog(a).setContent(this).open());return this}});MooDialog.implement("options",{duration:400,closeOnOverlayClick:true,onInitialize:function(a){this.fx=new Fx.Tween(a,{property:"opacity",duration:this.options.duration}).set(0);this.overlay=new Overlay(this.options.inject,{duration:this.options.duration});if(this.options.closeOnOverlayClick){this.overlay.addEvent("click",this.close.bind(this))}},onBeforeOpen:function(a){this.overlay.open();this.fx.start(1).chain(function(){this.fireEvent("show")}.bind(this))},onBeforeClose:function(a){this.overlay.close();this.fx.start(0).chain(function(){this.fireEvent("hide")}.bind(this))}});MooDialog.Alert=new Class({Extends:MooDialog,options:{okText:"Ok",focus:true,textPClass:"MooDialogAlert"},initialize:function(b,a){this.parent(a);var c=new Element("input[type=button]",{events:{click:this.close.bind(this)},value:this.options.okText});this.setContent(new Element("p."+this.options.textPClass,{text:b}),new Element("div.buttons").adopt(c));if(this.options.autoOpen){this.open()}if(this.options.focus){this.addEvent("show",function(){c.focus()})}}});
