var Dialogs={Lang:{close:"&nbsp;&times;&nbsp;",prev:"&laquo; Previous",next:"Next &raquo;",loading:"Loading...",ok:"OK",yes:"Yes",no:"No"},Default:{handle:null,background:["#000","#fff"],width:"auto",height:"auto",minWidth:null,minHeight:null,innerScroll:true,opacity:0.75,margin:10,padding:10,title:null,className:null,content:null,iframe:null,target:{id:null,auto:true},ajax:{url:null,jsonTemplate:null,options:{}},close:{link:true,esc:true,overlay:true},afterOpen:Prototype.emptyFunction,afterClose:Prototype.emptyFunction,afterClick:Prototype.emptyFunction},Browser:{IE6:(Prototype.Browser.IE&&parseInt(navigator.appVersion)==4&&navigator.userAgent.toLowerCase().indexOf("msie 6.")!=-1)}};Object.extend(Dialogs,{_exec:false,_open:false,_elements:{overlay:["div","dialog-overlay","fixed"],container:["div","dialog-container","fixed"],content:["div","dialog-content"],loading:["div","dialog-loading"],top:["div","dialog-top"],bottom:["div","dialog-bottom"],title:["span","dialog-title"],close:["a","dialog-close"],next:["a",null,"next"],prev:["a",null,"prev"],curr:["span",null,"curr"]},fix:{scroll:Dialogs.Browser.IE6,select:Dialogs.Browser.IE6},view:function(){var B=document.viewport,D=B.getDimensions(),C={width:D.width,height:D.height};if(Dialogs.fix.scroll){var A=B.getScrollOffsets();C.top=A.top;C.left=A.left}return C},elm:function(A){return Dialogs._elements[A]},load:function(){if(!!Dialogs._exec){return }Dialogs._exec=true;var D=Dialogs._elements;for(var A in D){var E=D[A],B={style:"display:none"};if(E[1]){B["id"]=E[1]}if(E[2]){B["className"]=E[2]}switch(E[0]){case"a":B["href"]="javascript:;";break}var C=new Element(E[0],B);if(Dialogs.Lang[A]){C.update(Dialogs.Lang[A])}Dialogs._elements[A]=C}document.observe("dom:loaded",function(){var F=Dialogs._elements;$(document.body).insert(F["overlay"]).insert(F["container"].insert(F["top"].insert(F["title"]).insert(F["close"])).insert(F["content"]).insert(F["bottom"].insert(F["prev"]).insert(F["curr"]).insert(F["next"])));if(Dialogs.Browser.IE6){F["top"].insert(new Element("div",{style:"clear:both"}))}})},close:function(){[Dialogs.elm("title"),Dialogs.elm("content"),Dialogs.elm("curr")].invoke("update","");for(var A in Dialogs._elements){Dialogs._elements[A].writeAttribute("style","display:none")}Dialogs.elm("container").setStyle("top:-99999px;left:-99999px");if(Dialogs.fix.select){$$("select.dialog-hideselect").invoke("show").invoke("removeClassName","dialog-hideselect")}Dialogs._open=false},alert:function(B){var C=new Element("input",{value:Dialogs.Lang.ok,type:"button"}),A=new Dialog({className:"alert",close:{link:false,esc:true},padding:20,content:function(){C.observe("click",Dialogs.close);return[B,"<br /><br />",C]},afterOpen:function(){C.focus()}});A.open()},confirm:function(C,B,A){var F=new Element("input",{value:Dialogs.Lang.yes,type:"button"}),E=new Element("input",{value:Dialogs.Lang.no,type:"button"}),D=new Dialog({className:"confirm",close:{link:false},padding:20,content:function(){F.observe("click",function(){if(Object.isFunction(B)){B()}Dialogs.close()});E.observe("click",function(){if(Object.isFunction(A)){A()}Dialogs.close()});return[C,"<br /><br />",F,E]},afterOpen:function(){F.focus()}});D.open()}});Dialogs.load(true);var Dialog=Class.create();Dialog.prototype={initialize:function(C){this.opt=Object.extend(Object.clone(Dialogs.Default),C||{});var D=this.opt.content;if(Object.isFunction(D)){Object.extend(this.opt,{content:D()})}D=this.opt.content;if(Object.isString(this.opt.target.id)||Object.isElement(this.opt.target.id)){var A=$(this.opt.target.id);Object.extend(this.opt,{content:A.innerHTML});if(this.opt.target.auto){var B=/#(.+)$/.exec(window.location);if(Object.isArray(B)&&Object.isString(B[1])){B=B[1].split(",").last();if(B==A.identify()){this.open.bind(this).delay(1)}}}}else{if(Object.isHash(D)){this.steps={i:0,k:D.keys(),v:D.values(),m:D.size()}}}this.attachEvents()},exec:function(A){return Dialogs._open==this._open&&Dialogs.elm("overlay").visible()&&A},attachEvents:function(){Event.observe(window,"resize",this.setDimensions.bindAsEventListener(this));if(Dialogs.fix.scroll){Event.observe(window,"scroll",this.setScroll.bindAsEventListener(this))}var A=[];if(Object.isElement(this.opt.handle)){A.push($(this.opt.handle))}else{if(Object.isArray(this.opt.handle)){this.opt.handle.each(function(B){A.push($(B))})}else{if(Object.isString(this.opt.handle)){A=$$(this.opt.handle)}}}A.invoke("observe","click",function(B){B.stop();if(!Dialogs.elm("overlay").visible()){if(Object.isFunction(this.opt.afterClick)){this.opt.afterClick(B)}this.open()}}.bindAsEventListener(this));Dialogs.elm("close").observe("click",function(){if(this.exec(this.opt.close.link)){this.close()}}.bindAsEventListener(this));Dialogs.elm("overlay").observe("click",function(){if(this.exec(this.opt.close.overlay)){this.close()}}.bindAsEventListener(this));document.observe("keyup",function(B){if(this.exec(this.opt.close.esc&&(B.which||B.keyCode)==Event.KEY_ESC)){this.close()}}.bindAsEventListener(this));if(this.steps){[Dialogs.elm("prev"),Dialogs.elm("next")].invoke("observe","click",this.setSteps.bindAsEventListener(this));document.observe("keydown",function(B){var C=B.which||B.keyCode;if(this.exec((C==Event.KEY_LEFT)||(C==Event.KEY_RIGHT))){this.setSteps(B)}}.bindAsEventListener(this))}},setAuto:function(){this.auto={max:0};var A=Dialogs.elm("title"),B=Dialogs.elm("close");[A,B].invoke("setStyle","float:none");$w("top content bottom").each(function(C){var D=Dialogs.elm(C);if(!D.visible()){this.auto[C]={width:0,height:0}}else{D.writeAttribute("style","display:inline;float:left;overflow:visible;white-space:nowrap");this.auto[C]=D.getDimensions();D.writeAttribute("style","overflow:hidden");if(C=="content"){this.auto[C].width+=(parseInt(this.opt.padding)||0)*2}if(this.auto[C].width>this.auto.max){this.auto.max=this.auto[C].width}}}.bind(this));A.setStyle("float:left");B.setStyle("float:right")},setDimensions:function(){if(!this.exec(true)){return }this.setAuto();var G=this.auto,D=Dialogs.view(),J=Dialogs.elm("content"),E=Dialogs.elm("container"),A={m:((parseInt(this.opt.margin)||0)*2),p:((parseInt(this.opt.padding)||0)*2),t:G.top.height,b:G.bottom.height},B={width:(D.width-A.m),height:(D.height-A.m-A.t-A.b)},C=this.opt.height,I=this.opt.width,F=y=false;if(Object.isNumber(I)){I+=A.p}if(I=="max"){I=B.width}if(!Object.isNumber(I)){I=G.max}if(I<(this.opt.minWidth||0)){I=this.opt.minWidth||0}if(I>B.width){I=B.width;F=true}J.setStyle("width:"+(I-A.p)+"px;height:auto");if(Object.isNumber(C)){C+=A.p}if(C=="max"){C=B.height}if(!Object.isNumber(C)){C=J.getHeight()+A.p}if(C<(this.opt.minHeight||0)){I=this.opt.minHeight||0}if(C>B.height){C=B.height;y=true}J.setStyle("height:"+(C-A.p)+"px;padding:"+(A.p/2)+"px");if(this.opt.innerScroll&&(F||y)){J.setStyle("overflow:scroll")}var K={w:I,h:(C+A.t+A.b)};E.setStyle("width:"+K.w+"px;height:"+K.h+"px;margin:-"+parseInt(K.h/2)+"px 0 0 -"+parseInt(K.w/2)+"px");if(Dialogs.fix.scroll){Dialogs.elm("overlay").setStyle("width:"+D.width+"px;height:"+D.height+"px");this.setScroll()}var H=function(){E.setStyle("top:50%;left:50%")};H.defer()},setScroll:function(){if(!this.exec(true)){return }var B=Dialogs.view(),E=Dialogs.elm("container"),D=E.getDimensions(),C=B.top+parseInt((B.height-D.height)/2),A=B.left+parseInt((B.width-D.width)/2);E.setStyle("margin:0;top:"+C+"px;left:"+A+"px");Dialogs.elm("overlay").setStyle("margin:"+B.top+"px 0 0 "+B.left+"px")},setLoad:function(){var B=Dialogs.elm("loading").show(),C=Dialogs.elm("content"),A=C.down("#"+B.identify());if(!Object.isElement(A)){C.insert(B)}},setAjax:function(){this.setLoad();var B=this.opt.ajax.options||{},C=(B.onComplete&&Object.isFunction(B.onComplete)?B.onComplete:null),A=function(E){var D=this.opt.ajax.jsonTemplate;if(E.responseJSON&&Object.isString(D)){Dialogs.elm("content").update(D.interpolate(E.responseJSON))}else{Dialogs.elm("content").update(E.responseText||"")}this.setDimensions();if(Object.isFunction(C)){C(E)}}.bind(this);Object.extend(B,{onComplete:A});new Ajax.Request(this.opt.ajax.url,B)},setIframe:function(){this.setLoad();var A=new Element("iframe",{src:this.opt.iframe,frameborder:0});Dialogs.elm("content").insert(A);A.observe("load",function(){Dialogs.elm("loading").hide();A.setStyle("width:100%;height:100%");this.setDimensions()}.bindAsEventListener(this))},setSteps:function(C){if(!this.exec(true)){return }var A=this.steps.m,B=false,E=Dialogs.elm("next"),D=Dialogs.elm("prev");if((C.which||C.keyCode)==Event.KEY_RIGHT||C.element().hasClassName("next")){if(this.steps.i<(A-1)){B=true}if(B){++this.steps.i}if(((this.steps.i+1)>=A)&&E.visible()){E.hide()}if(((this.steps.i-1)>=0)&&!D.visible()){D.show()}}else{if(this.steps.i>0){B=true}if(B){--this.steps.i}if(((this.steps.i-1)<0)&&D.visible()){D.hide()}if(((this.steps.i+1)<=A)&&!E.visible()){E.show()}}if(B){this.setContent()}},setContent:function(){var C=this.opt.content,B=Dialogs.elm("content");if(Object.isString(C)||Object.isElement(C)){B.insert(C)}else{if(Object.isArray(C)){C.each(function(D){B.insert(D)})}else{if(Object.isHash(C)){var A=Dialogs.elm("bottom");B.update("").insert(this.steps.v[this.steps.i]);Dialogs.elm("curr").update(this.steps.k[this.steps.i]);if(!A.visible()){A.childElements().invoke("show");A.show()}if(this.steps.i<=0){Dialogs.elm("prev").hide()}if(this.steps.i>=(this.steps.m-1)){Dialogs.elm("next").hide()}}else{if(Object.isString(this.opt.ajax.url)){this.setAjax()}else{if(Object.isString(this.opt.iframe)){this.setIframe()}}}}}this.setDimensions.bind(this).defer()},open:function(){if(Dialogs.fix.select){$$("select").select(function(D){return D.visible()}).invoke("hide").invoke("addClassName","dialog-hideselect")}if(Object.isString(this.opt.title)||this.opt.close.link){if(Object.isString(this.opt.title)){Dialogs.elm("title").show().update(this.opt.title)}if(this.opt.close.link){Dialogs.elm("close").show()}Dialogs.elm("top").show()}var B=Dialogs.elm("overlay"),C=Dialogs.elm("container"),A=Dialogs.elm("content");[B,C,A].invoke("show");B.setOpacity(this.opt.opacity||1).setStyle({background:this.opt.background[0]||"#000"});C.writeAttribute("style","top:-99999px;left:-99999px;background:"+(this.opt.background[1]||"#fff"));A.writeAttribute("class",this.opt.className||"");Dialogs._open=new Date().getTime();this._open=Dialogs._open;this.setContent();if(Object.isFunction(this.opt.afterOpen)){this.opt.afterOpen()}},close:function(){Dialogs.close();if(Object.isFunction(this.opt.afterClose)){this.opt.afterClose()}}}