
var SimpleModal=new Class({Implements:[Options],request:null,buttons:[],options:{onAppend:Function,offsetTop:40,overlayOpacity:.3,overlayColor:"#000000",width:400,draggable:true,keyEsc:true,overlayClick:true,closeButton:true,hideHeader:false,hideFooter:false,lightboxExcessWidth:40,lightboxExcessHeight:120,btn_ok:"OK",btn_cancel:"Cancel",template:"<div class=\"simple-modal-header\"> \
            <h1>{_TITLE_}</h1> \
          </div> \
          <div class=\"simple-modal-body\"> \
            <div class=\"contents\">{_CONTENTS_}</div> \
          </div> \
          <div class=\"simple-modal-footer\"></div>"},initialize:function(options){this.setOptions(options);},show:function(options){if(!options)options={};this._overlay("show");switch(options.model){case"confirm":this.addButton(this.options.btn_ok,"btn primary btn-margin",function(){try{options.callback()}catch(err){};this.hide();})
this.addButton(this.options.btn_cancel,"btn secondary");var node=this._drawWindow(options);this._addEscBehaviour();break;case"modal":var node=this._drawWindow(options);this._addEscBehaviour();break;case"modal-ajax":var node=this._drawWindow(options);this._loadContents({"url":options.param.url||"","onRequestComplete":options.param.onRequestComplete||Function})
break;default:this.addButton(this.options.btn_ok,"btn primary");var node=this._drawWindow(options);this._addEscBehaviour();break;}
node.setStyles({width:this.options.width});if(this.options.hideHeader)node.addClass("hide-header");if(this.options.hideFooter)node.addClass("hide-footer");if(this.options.closeButton)this._addCloseButton();if(this.options.draggable){var headDrag=node.getElement(".simple-modal-header");new Drag(node,{handle:headDrag});headDrag.setStyle("cursor","move")
node.addClass("draggable");}
this._display();},hide:function(){try{if(typeof(this.request)=="object")this.request.cancel();}catch(err){}
this._overlay('hide');return;},_drawWindow:function(options){var node=new Element("div#simple-modal",{"class":"simple-modal"});node.inject($$("body")[0]);var html=this._template(this.options.template,{"_TITLE_":options.title||"Untitled","_CONTENTS_":options.contents||""});node.set("html",html);this._injectAllButtons();this.options.onAppend();return node;},addButton:function(label,classe,clickEvent){var bt=new Element('a',{"title":label,"text":label,"class":classe,"events":{click:(clickEvent||this.hide).bind(this)}});this.buttons.push(bt);return bt;},_injectAllButtons:function(){this.buttons.each(function(e,i){e.inject($("simple-modal").getElement(".simple-modal-footer"));});return;},_addCloseButton:function(){var b=new Element("a",{"class":"close","href":"#","html":"x"});b.inject($("simple-modal"),"top");b.addEvent("click",function(e){if(e)e.stop();this.hide();}.bind(this))
return b;},_overlay:function(status){switch(status){case'show':this._overlay('hide');var overlay=new Element("div",{"id":"simple-modal-overlay"});overlay.inject($$("body")[0]);overlay.setStyle("background-color",this.options.overlayColor);overlay.fade("hide").fade(this.options.overlayOpacity);if(this.options.overlayClick){overlay.addEvent("click",function(e){if(e)e.stop();this.hide();}.bind(this))}
this.__resize=this._display.bind(this);window.addEvent("resize",this.__resize);break;case'hide':window.removeEvent("resize",this._display);if(this.options.keyEsc){var fixEV=Browser.name!='ie'?"keydown":"onkeydown";window.removeEvent(fixEV,this._removeSM);}
try{$('simple-modal-overlay').destroy();}
catch(err){}
try{$('simple-modal').destroy();}
catch(err){}
break;}
return;},_loadContents:function(param){$('simple-modal').addClass("loading");var re=new RegExp(/([^\/\\]+)\.(jpg|png|gif)$/i);if(param.url.match(re)){$('simple-modal').addClass("hide-footer");$("simple-modal-overlay").removeEvents();var images=[param.url];new Asset.images(images,{onProgress:function(i){immagine=this;},onComplete:function(){try{$('simple-modal').removeClass("loading");var content=$('simple-modal').getElement(".contents");var padding=content.getStyle("padding").split(" ");var width=(immagine.get("width").toInt())+(padding[1].toInt()+padding[3].toInt())
var height=immagine.get("height").toInt();var ns=this._scaleImage(width,height);width=ns.width
height=ns.height
var myFx1=new Fx.Tween($("simple-modal"),{duration:'normal',transition:'sine:out',link:'cancel',property:'width'}).start($("simple-modal").getCoordinates().width,width);var myFx2=new Fx.Tween(content,{duration:'normal',transition:'sine:out',link:'cancel',property:'height'}).start(content.getCoordinates().height,height).chain(function(){immagine.inject($('simple-modal').getElement(".contents").empty()).fade("hide").setStyles({"width":width,"height":height}).fade("in");this._display();this._addEscBehaviour();}.bind(this));var myFx3=new Fx.Tween($("simple-modal"),{duration:'normal',transition:'sine:out',link:'cancel',property:'left'}).start($("simple-modal").getCoordinates().left,(window.getCoordinates().width-width)/2);}catch(err){}}.bind(this)});}else{this.request=new Request.HTML({evalScripts:false,url:param.url,method:'get',onRequest:function(){},onSuccess:function(responseTree,responseElements,responseHTML,responseJavaScript){$('simple-modal').removeClass("loading");$('simple-modal').getElement(".contents").set("html",responseHTML);param.onRequestComplete();eval(responseJavaScript)
this._display();this._addEscBehaviour();}.bind(this),onFailure:function(){$('simple-modal').removeClass("loading");$('simple-modal').getElement(".contents").set("html","loading failed")}}).send();}},_scaleImage:function(w,h){var removeH=this.options.lightboxExcessHeight+this.options.offsetTop;var removeW=this.options.lightboxExcessWidth;var width=w;var height=h;var newWidth=window.getSize().x-removeW;var newHeight=window.getSize().y-removeH;ratio=(width<=height)?height/newHeight:width/newWidth;ratio=Math.max(ratio,1.0);w=parseInt(width/ratio);h=parseInt(height/ratio);return{"width":w,"height":h}},_display:function(){try{$("simple-modal-overlay").setStyles({height:window.getCoordinates().height});}catch(err){}
try{var offsetTop=this.options.offsetTop||0;$("simple-modal").setStyles({top:offsetTop,left:((window.getCoordinates().width-$("simple-modal").getCoordinates().width)/2)});}catch(err){}
return;},_addEscBehaviour:function(){if(this.options.keyEsc){this._removeSM=function(e){if(e.key=="esc")this.hide();}.bind(this)
if(this.options.keyEsc){var fixEV=Browser.name!='ie'?"keydown":"onkeydown";window.addEvent(fixEV,this._removeSM);}}},_template:function(s,d){for(var p in d)
s=s.replace(new RegExp('{'+p+'}','g'),d[p]);return s;}});