(function(a){a.modal=function(b,c){a.modal.loading();if(b.ajax)h(b.ajax);else if(b.image)g(b.image);else if(b.div)f(b.div);else if(a.isFunction(b))b.call(a);else a.modal.reveal(b,c)};a.extend(a.modal,{settings:{opacity:.5,overlay:true,loadingImage:"/static/i/loading.gif",closeImage:"/static/js/modal/closelabel.gif",imageTypes:["png","jpg","jpeg","gif"],modalHtml:'    <div id="modal" style="display:none;">       <div class="popup">         <table>           <tbody>             <tr>               <td class="tl"/><td class="b"/><td class="tr"/>             </tr>             <tr>               <td class="b"/>               <td class="body">                 <div class="content">                 </div>                 <div class="footer">                   <a href="#" class="close closePop">                     cerrar                   </a>                 </div>               </td>               <td class="b"/>             </tr>             <tr>               <td class="bl"/><td class="b"/><td class="br"/>             </tr>           </tbody>         </table>       </div>     </div>'},loading:function(){b();if(a("#modal .loading").length==1)return true;j();a("#modal .content").empty();a("#modal .body").children().hide().end().append('<div class="loading"><img src="'+a.modal.settings.loadingImage+'"/></div>');a("#modal").css({top:c()[1]+d()/5,left:385.5}).show();a(document).bind("keydown.modal",function(b){if(b.keyCode==27)a.modal.close();return true});a(document).trigger("loading.modal")},reveal:function(b,c){a(document).trigger("beforeReveal.modal");if(c)a("#modal .content").addClass(c);a("#modal .content").append(b);a("#modal .loading").remove();a("#modal .body").children().fadeIn("normal");a("#modal").css("left",a(window).width()/2-a("#modal .popup").width()/2);a(document).trigger("reveal.modal").trigger("afterReveal.modal")},close:function(){a(document).trigger("close.modal");return false}});a.fn.modal=function(c){b(c);function d(){a.modal.loading(true);var b=this.rel.match(/modal\[?\.(\w+)\]?/);if(b)b=b[1];f(this.href,b);return false}return this.click(d)};function b(b){if(a.modal.settings.inited)return true;else a.modal.settings.inited=true;a(document).trigger("init.modal");e();var c=a.modal.settings.imageTypes.join("|");a.modal.settings.imageTypesRegexp=new RegExp("."+c+"$","i");if(b)a.extend(a.modal.settings,b);a("body").append(a.modal.settings.modalHtml);var d=[new Image,new Image];d[0].src=a.modal.settings.closeImage;d[1].src=a.modal.settings.loadingImage;a("#modal").find(".b:first, .bl, .br, .tl, .tr").each(function(){d.push(new Image);d.slice(-1).src=a(this).css("background-image").replace(/url\((.+)\)/,"$1")});a("#modal .close").click(a.modal.close);a("#modal .closePop").attr("src",a.modal.settings.closeImage)}function c(){var a,b;if(self.pageYOffset){b=self.pageYOffset;a=self.pageXOffset}else if(document.documentElement&&document.documentElement.scrollTop){b=document.documentElement.scrollTop;a=document.documentElement.scrollLeft}else if(document.body){b=document.body.scrollTop;a=document.body.scrollLeft}return new Array(a,b)}function d(){var a;if(self.innerHeight)a=self.innerHeight;else if(document.documentElement&&document.documentElement.clientHeight)a=document.documentElement.clientHeight;else if(document.body)a=document.body.clientHeight;return a}function e(){var b=a.modal.settings;b.loadingImage=b.loading_image||b.loadingImage;b.closeImage=b.closePop||b.closeImage;b.imageTypes=b.image_types||b.imageTypes;b.modalHtml=b.modal_html||b.modalHtml}function f(b,c){if(b.match(/#/)){var d=window.location.href.split("#")[0];var e=b.replace(d,"");a.modal.reveal(a(e).clone().show(),c)}else if(b.match(a.modal.settings.imageTypesRegexp))g(b,c);else h(b,c)}function g(b,c){var d=new Image;d.onload=function(){a.modal.reveal('<div class="image"><img src="'+d.src+'" /></div>',c)};d.src=b}function h(b,c){a.get(b,function(b){a.modal.reveal(b,c)})}function i(){return a.modal.settings.overlay==false||a.modal.settings.opacity===null}function j(){if(i())return;if(a("modal_overlay").length==0)a("body").append('<div id="modal_overlay" class="modal_hide"></div>');a("#modal_overlay").hide().addClass("modal_overlayBG").css("opacity",a.modal.settings.opacity).click(function(){a(document).trigger("close.modal")}).fadeIn(200);return false}function k(){if(i())return;a("#modal_overlay").fadeOut(200,function(){a("#modal_overlay").removeClass("modal_overlayBG");a("#modal_overlay").addClass("modal_hide");a("#modal_overlay").remove()});return false}a(document).bind("close.modal",function(){a(document).unbind("keydown.modal");a("#modal").fadeOut(function(){a("#modal .content").removeClass().addClass("content");k();a("#modal .loading").remove()})})})(jQuery)
