jQuery.fn.boxy=function(A){A=A||{};return this.each(function(){var C=this.nodeName.toLowerCase(),B=this;if(C=="a"){jQuery(this).click(function(){var H=Boxy.linkedTo(this),E=this.getAttribute("href"),G=jQuery.extend({actuator:this,title:this.title},A);if(H){H.show()}else{if(E.indexOf("#")>=0){var F=jQuery(E.substr(E.indexOf("#"))),D=F.clone(true);F.remove();G.unloadOnHide=false;new Boxy(D,G)}else{if(!G.cache){G.unloadOnHide=true}Boxy.load(this.href,G)}}return false})}else{if(C=="form"){jQuery(this).bind("submit.boxy",function(){Boxy.confirm(A.message||"Please confirm:",function(){jQuery(B).unbind("submit.boxy").submit()});return false})}}})};function Boxy(B,A){this.boxy=jQuery(Boxy.WRAPPER);jQuery.data(this.boxy[0],"boxy",this);this.visible=false;this.options=jQuery.extend({},Boxy.DEFAULTS,A||{});if(this.options.modal){this.options=jQuery.extend(this.options,{center:true,draggable:false})}if(this.options.actuator){jQuery.data(this.options.actuator,"active.boxy",this)}this.setContent(B||"<div></div>");this._setupTitleBar();this.boxy.css("display","none").appendTo(document.body);this.toTop();if(this.options.fixed){if(jQuery.browser.msie&&jQuery.browser.version<7){this.options.fixed=false}else{this.boxy.addClass("fixed")}}if(this.options.center&&Boxy._u(this.options.x,this.options.y)){this.center()}else{this.moveTo(Boxy._u(this.options.x)?this.options.x:Boxy.DEFAULT_X,Boxy._u(this.options.y)?this.options.y:Boxy.DEFAULT_Y)}if(this.options.show){this.show()}}Boxy.EF=function(){};jQuery.extend(Boxy,{WRAPPER:"<table cellspacing='0' cellpadding='0' border='0' class='boxy-wrapper'><tr><td class='top-left'></td><td class='top'></td><td class='top-right'></td></tr><tr><td class='left'></td><td class='boxy-inner'></td><td class='right'></td></tr><tr><td class='bottom-left'></td><td class='bottom'></td><td class='bottom-right'></td></tr></table>",DEFAULTS:{title:null,closeable:true,draggable:true,clone:false,actuator:null,center:true,show:true,modal:false,fixed:true,closeText:"[close]",unloadOnHide:false,clickToFront:false,behaviours:Boxy.EF,afterDrop:Boxy.EF,afterShow:Boxy.EF,afterHide:Boxy.EF,beforeUnload:Boxy.EF},DEFAULT_X:50,DEFAULT_Y:50,zIndex:1337,dragConfigured:false,resizeConfigured:false,dragging:null,load:function(B,A){A=A||{};var C={url:B,type:"GET",dataType:"html",cache:false,success:function(D){D=jQuery(D);if(A.filter){D=jQuery(A.filter,D)}new Boxy(D,A)}};jQuery.each(["type","cache"],function(){if(this in A){C[this]=A[this];delete A[this]}});jQuery.ajax(C)},get:function(A){var B=jQuery(A).parents(".boxy-wrapper");return B.length?jQuery.data(B[0],"boxy"):null},linkedTo:function(A){return jQuery.data(A,"active.boxy")},alert:function(B,C,A){return Boxy.ask(B,["OK"],C,A)},confirm:function(B,C,A){return Boxy.ask(B,["OK","Cancel"],function(D){if(D=="OK"){C()}},A)},ask:function(C,F,I,J){J=jQuery.extend({modal:true,closeable:false},J||{},{show:true,unloadOnHide:true});var E=jQuery("<div></div>").append(jQuery('<div class="question"></div>').html(C));var A={},H=[];if(F instanceof Array){for(var D=0;D<F.length;D++){A[F[D]]=F[D];H.push(F[D])}}else{for(var B in F){A[F[B]]=B;H.push(F[B])}}var G=jQuery('<form class="answers"></form>');G.html(jQuery.map(H,function(K){return"<input type='button' class='button' value='"+K+"' />"}).join(" "));jQuery("input[type=button]",G).click(function(){var K=this;Boxy.get(this).hide(function(){if(I){I(A[K.value])}})});E.append(G);new Boxy(E,J)},isModalVisible:function(){return jQuery(".boxy-modal-blackout").length>0},_u:function(){for(var A=0;A<arguments.length;A++){if(typeof arguments[A]!="undefined"){return false}}return true},_handleResize:function(A){var B=jQuery(document);if($.browser.msie&&$.browser.version<7){var C=17}else{var C=0}jQuery(".boxy-modal-blackout").css("display","none").css({width:B.width()-C,height:B.height()}).css("display","block")},_handleDrag:function(A){var B;if(B=Boxy.dragging){B[0].boxy.css({left:A.pageX-B[1],top:A.pageY-B[2]})}},_nextZ:function(){return Boxy.zIndex++},_viewport:function(){var C=document.documentElement,A=document.body,B=window;return jQuery.extend(jQuery.browser.msie?{left:A.scrollLeft||C.scrollLeft,top:A.scrollTop||C.scrollTop}:{left:B.pageXOffset,top:B.pageYOffset},!Boxy._u(B.innerWidth)?{width:B.innerWidth,height:B.innerHeight}:(!Boxy._u(C)&&!Boxy._u(C.clientWidth)&&C.clientWidth!=0?{width:C.clientWidth,height:C.clientHeight}:{width:A.clientWidth,height:A.clientHeight}))}});Boxy.prototype={estimateSize:function(){this.boxy.css({visibility:"hidden",display:"block"});var A=this.getSize();this.boxy.css("display","none").css("visibility","visible");return A},getSize:function(){return[this.boxy.width(),this.boxy.height()]},getContentSize:function(){var A=this.getContent();return[A.width(),A.height()]},getPosition:function(){var A=this.boxy[0];return[A.offsetLeft,A.offsetTop]},getCenter:function(){var B=this.getPosition();var A=this.getSize();return[Math.floor(B[0]+A[0]/2),Math.floor(B[1]+A[1]/2)]},getInner:function(){return jQuery(".boxy-inner",this.boxy)},getContent:function(){return jQuery(".boxy-content",this.boxy)},setContent:function(A){A=jQuery(A).css({display:"block"}).addClass("boxy-content");if(this.options.clone){A=A.clone(true)}this.getContent().remove();this.getInner().append(A);this._setupDefaultBehaviours(A);this.options.behaviours.call(this,A);return this},moveTo:function(A,B){this.moveToX(A).moveToY(B);return this},moveToX:function(A){if(typeof A=="number"){this.boxy.css({left:A})}else{this.centerX()}return this},moveToY:function(A){if(typeof A=="number"){this.boxy.css({top:A})}else{this.centerY()}return this},centerAt:function(A,C){var B=this[this.visible?"getSize":"estimateSize"]();if(typeof A=="number"){this.moveToX(A-B[0]/2)}if(typeof C=="number"){this.moveToY(C-B[1]/2)}return this},centerAtX:function(A){return this.centerAt(A,null)},centerAtY:function(A){return this.centerAt(null,A)},center:function(B){var A=Boxy._viewport();var C=this.options.fixed?[0,0]:[A.left,A.top];if(!B||B=="x"){this.centerAt(C[0]+A.width/2,null)}if(!B||B=="y"){this.centerAt(null,C[1]+A.height/2)}return this},centerX:function(){return this.center("x")},centerY:function(){return this.center("y")},resize:function(B,A,D){if(!this.visible){return }var C=this._getBoundsForResize(B,A);this.boxy.css({left:C[0],top:C[1]});this.getContent().css({width:C[2],height:C[3]});if(D){D(this)}return this},tween:function(C,A,E){if(!this.visible){return }var D=this._getBoundsForResize(C,A);var B=this;this.boxy.stop().animate({left:D[0],top:D[1]});this.getContent().stop().animate({width:D[2],height:D[3]},function(){if(E){E(B)}});return this},isVisible:function(){return this.visible},show:function(){if(this.visible){return }if(this.options.modal){var A=this;if(!Boxy.resizeConfigured){Boxy.resizeConfigured=true;jQuery(window).resize(function(){Boxy._handleResize()})}if($.browser.msie&&$.browser.version<7){var B=17}else{var B=0}this.modalBlackout=jQuery('<div class="boxy-modal-blackout"></div>').css({zIndex:Boxy._nextZ(),opacity:0.7,width:jQuery(document).width()-B,height:jQuery(document).height()}).appendTo(document.body);this.toTop();if(this.options.closeable){jQuery(document.body).bind("keypress.boxy",function(C){var D=C.which||C.keyCode;if(D==27){A.hide();jQuery(document.body).unbind("keypress.boxy")}})}}this.boxy.stop().css({opacity:1}).show();this.visible=true;this._fire("afterShow");return this},hide:function(B){if(!this.visible){return }var A=this;if(this.options.modal){jQuery(document.body).unbind("keypress.boxy");this.modalBlackout.animate({opacity:0},function(){jQuery(this).remove()})}this.boxy.stop().animate({opacity:0},300,function(){A.boxy.css({display:"none"});A.visible=false;A._fire("afterHide");if(B){B(A)}if(A.options.unloadOnHide){A.unload()}});return this},toggle:function(){this[this.visible?"hide":"show"]();return this},hideAndUnload:function(A){this.options.unloadOnHide=true;this.hide(A);return this},unload:function(){this._fire("beforeUnload");this.boxy.remove();if(this.options.actuator){jQuery.data(this.options.actuator,"active.boxy",false)}},toTop:function(){this.boxy.css({zIndex:Boxy._nextZ()});return this},getTitle:function(){return jQuery("> .title-bar h2",this.getInner()).html()},setTitle:function(A){jQuery("> .title-bar h2",this.getInner()).html(A);return this},_getBoundsForResize:function(C,A){var B=this.getContentSize();var E=[C-B[0],A-B[1]];var D=this.getPosition();return[Math.max(D[0]-E[0]/2,0),Math.max(D[1]-E[1]/2,0),C,A]},_setupTitleBar:function(){if(this.options.title){var B=this;var A=jQuery("<div class='title-bar'></div>").html("<h2>"+this.options.title+"</h2>");if(this.options.closeable){A.append(jQuery("<a href='#' class='close'></a>").html(this.options.closeText))}if(this.options.draggable){A[0].onselectstart=function(){return false};A[0].unselectable="on";A[0].style.MozUserSelect="none";if(!Boxy.dragConfigured){jQuery(document).mousemove(Boxy._handleDrag);Boxy.dragConfigured=true}A.mousedown(function(C){B.toTop();Boxy.dragging=[B,C.pageX-B.boxy[0].offsetLeft,C.pageY-B.boxy[0].offsetTop];jQuery(this).addClass("dragging")}).mouseup(function(){jQuery(this).removeClass("dragging");Boxy.dragging=null;B._fire("afterDrop")})}this.getInner().prepend(A);this._setupDefaultBehaviours(A)}},_setupDefaultBehaviours:function(A){var B=this;if(this.options.clickToFront){A.click(function(){B.toTop()})}jQuery(".close",A).click(function(){B.hide();return false}).mousedown(function(C){C.stopPropagation()})},_fire:function(A){this.options[A].call(this)}};
