var Dialogs={Lang:{close:"&nbsp;&times;&nbsp;",prev:"&laquo; Previous",next:"Next &raquo;",loading:"Loading...",ok:"OK",yes:"Yes",no:"No"},Default:{handle:null,autoOpen:false,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,afterIframeLoad: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(j){if(!!Dialogs._exec){return;}Dialogs._exec=true;var h=Dialogs._elements;for(var b in h){var i=h[b],c={style:"display:none"};if(i[1]){c["id"]=i[1];}if(i[2]){c["className"]=i[2];
}switch(i[0]){case"a":c["href"]="javascript:;";break;}var f=new Element(i[0],c);if(Dialogs.Lang[b]){f.update(Dialogs.Lang[b]);}Dialogs._elements[b]=f;}var g=function(){var a=Dialogs._elements;$(document.body).insert(a["overlay"]).insert(a["container"].insert(a["top"].insert(a["title"]).insert(a["close"])).insert(a["content"]).insert(a["bottom"].insert(a["prev"]).insert(a["curr"]).insert(a["next"])));
if(Dialogs.Browser.IE6){a["top"].insert(new Element("div",{style:"clear:both"}));}};if(!!j){document.observe("dom:loaded",g);}else{g.call();}},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("left:-99999px;top:-99999px");if(Dialogs.fix.select){$$("select.dialog-hideselect").invoke("show").invoke("removeClassName","dialog-hideselect");}Dialogs._open=false;},alert:function(c){var d=new Element("input",{value:Dialogs.Lang.ok,type:"button"}),b=new Dialog({className:"alert",close:{link:false,esc:true},padding:20,content:function(){d.observe("click",Dialogs.close);
return[c,"<br /><br />",d];},afterOpen:function(){d.focus();}});b.open();},confirm:function(d,b,a){var g=new Element("input",{value:Dialogs.Lang.yes,type:"button"}),f=new Element("input",{value:Dialogs.Lang.no,type:"button"}),e=new Dialog({className:"confirm",close:{link:false},padding:20,content:function(){g.observe("click",function(){if(Object.isFunction(b)){b();
}else{Dialogs.close();}});f.observe("click",function(){if(Object.isFunction(a)){a();}else{Dialogs.close();}});return[d,"<br /><br />",g,f];},afterOpen:function(){g.focus();}});e.open();}});var Dialog=Class.create();Dialog.prototype={initialize:function(f){this.opt=Object.extend(Object.clone(Dialogs.Default),f||{});
var g=this.opt.content;if(Object.isFunction(g)){Object.extend(this.opt,{content:g()});}g=this.opt.content;if(Object.isString(this.opt.target.id)||Object.isElement(this.opt.target.id)){var d=$(this.opt.target.id);Object.extend(this.opt,{content:d.innerHTML});if(this.opt.target.auto){var e=/#(.+)$/.exec(window.location);
if(Object.isArray(e)&&Object.isString(e[1])){e=e[1].split(",").last();if(e==d.identify()){this.open.bind(this).delay(1);}}}}else{if(Object.isHash(g)){this.steps={i:0,k:g.keys(),v:g.values(),m:g.size()};}}this.attachEvents();if(this.opt.autoOpen){this.open();}},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("show").invoke("observe","click",function(b){b.stop();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 d=b.which||b.keyCode;if(this.exec((d==Event.KEY_LEFT)||(d==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 k=this.auto,g=Dialogs.view(),n=Dialogs.elm("content"),i=Dialogs.elm("container"),b={m:((parseInt(this.opt.margin)||0)*2),p:((parseInt(this.opt.padding)||0)*2),t:k.top.height,b:k.bottom.height},e={width:(g.width-b.m),height:(g.height-b.m-b.t-b.b)},f=this.opt.height,l=this.opt.width,j=y=false;
if(Object.isNumber(l)){l+=b.p;}if(l=="max"){l=e.width;}if(!Object.isNumber(l)){l=k.max;}if(l<(this.opt.minWidth||0)){l=this.opt.minWidth||0;}if(l>e.width){l=e.width;j=true;}n.setStyle("width:"+(l-b.p)+"px;height:auto");if(Object.isNumber(f)){f+=b.p;}if(f=="max"){f=e.height;}if(!Object.isNumber(f)){f=n.getHeight()+b.p;
}if(f<(this.opt.minHeight||0)){l=this.opt.minHeight||0;}if(f>e.height){f=e.height;y=true;}n.setStyle("height:"+(f-b.p)+"px;padding:"+(b.p/2)+"px");if(this.opt.innerScroll&&(j||y)){n.setStyle("overflow:scroll");}var p={w:l,h:(f+b.t+b.b)};i.setStyle("width:"+p.w+"px;height:"+p.h+"px;top:50%;left:50%;margin:-"+parseInt(p.h/2)+"px 0 0 -"+parseInt(p.w/2)+"px");
if(Dialogs.fix.scroll){Dialogs.elm("overlay").setStyle("width:"+g.width+"px;height:"+g.height+"px");this.setScroll();}},setScroll:function(){if(!this.exec(true)){return;}var b=Dialogs.view(),g=Dialogs.elm("container"),f=g.getDimensions(),e=b.top+parseInt((b.height-f.height)/2),a=b.left+parseInt((b.width-f.width)/2);
g.setStyle("margin:0;top:"+e+"px;left:"+a+"px");Dialogs.elm("overlay").setStyle("margin:"+b.top+"px 0 0 "+b.left+"px");},setLoad:function(){var c=Dialogs.elm("loading").show(),d=Dialogs.elm("content"),a=d.down("#"+c.identify());if(!Object.isElement(a)){d.insert(c);}},setAjax:function(){this.setLoad();
var d=this.opt.ajax.options||{},e=(d.onComplete&&Object.isFunction(d.onComplete)?d.onComplete:null),b=function(c){var a=this.opt.ajax.jsonTemplate;if(c.responseJSON&&Object.isString(a)){Dialogs.elm("content").update(a.interpolate(c.responseJSON));}else{Dialogs.elm("content").update(c.responseText||"");
}this.setImages();this.setDimensions();if(Object.isFunction(e)){e(c);}}.bind(this);Object.extend(d,{onComplete:b});new Ajax.Request(this.opt.ajax.url,d);},setIframe:function(){this.setLoad();var a=new Element("iframe",{src:this.opt.iframe,frameborder:0,id:"dialog-iframe"});Dialogs.elm("content").insert(a);
a.observe("load",function(){Dialogs.elm("loading").hide();a.setStyle("width:100%;height:100%");this.setDimensions();if(Object.isFunction(this.opt.afterIframeLoad)){this.opt.afterIframeLoad();}}.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 e=this.opt.content,d=Dialogs.elm("content");d.update("");if(Object.isString(e)||Object.isElement(e)){d.insert(e);}else{if(Object.isArray(e)){e.each(function(c){d.insert(c);});}else{if(Object.isHash(e)){var a=Dialogs.elm("bottom");
d.update("").insert(this.steps.v[this.steps.i]);Dialogs.elm("curr").update(this.steps.k[this.steps.i]);if(!a.visible()){a.show().childElements().invoke("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.setImages();this.setDimensions.bind(this).defer();},setImages:function(){Dialogs.elm("content").select("img").each(function(a){a.onload=function(){this.setDimensions();}.bind(this);}.bind(this));},open:function(){if(Dialogs.fix.select){$$("select").select(function(c){return c.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();}else{Dialogs.elm("close").hide();}Dialogs.elm("top").show();
}else{Dialogs.elm("top").hide();}var b=Dialogs.elm("overlay"),d=Dialogs.elm("container"),a=Dialogs.elm("content");[b,d,a].invoke("show");b.setOpacity(this.opt.opacity||1).setStyle({background:this.opt.background[0]||"#000"});d.writeAttribute("style","left:-99999px;top:-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();}}};