jQuery.fn.boxy=function(a){a=a||{};return this.each(function(){var b=this.nodeName.toLowerCase(),c=this;if(b=="a")jQuery(this).click(function(){var d=Boxy.linkedTo(this),e=this.getAttribute("href"),f=jQuery.extend({actuator:this,title:this.title},a);if(d)d.show();else if(e.indexOf("#")>=0){d=jQuery(e.substr(e.indexOf("#")));e=d.clone(true);d.remove();f.unloadOnHide=false;new Boxy(e,f)}else{if(!f.cache)f.unloadOnHide=true;Boxy.load(this.href,f)}return false});else b=="form"&&jQuery(this).bind("submit.boxy",
function(){Boxy.confirm(a.message||"Please confirm:",function(){jQuery(c).unbind("submit.boxy").submit()});return false})})};
function Boxy(a,b){this.boxy=jQuery(Boxy.WRAPPER);jQuery.data(this.boxy[0],"boxy",this);this.visible=false;this.options=jQuery.extend({},Boxy.DEFAULTS,b||{});if(this.options.modal)this.options=jQuery.extend(this.options,{center:true,draggable:false});this.options.actuator&&jQuery.data(this.options.actuator,"active.boxy",this);this.setContent(a||"<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");this.options.center&&Boxy._u(this.options.x,this.options.y)?this.center():this.moveTo(Boxy._u(this.options.x)?this.options.x:Boxy.DEFAULT_X,Boxy._u(this.options.y)?this.options.y:Boxy.DEFAULT_Y);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(a,b){b=b||{};var c={url:a,type:"GET",dataType:"html",cache:false,success:function(d){d=jQuery(d);if(b.filter)d=jQuery(b.filter,d);new Boxy(d,b)}};jQuery.each(["type","cache"],function(){if(this in b){c[this]=b[this];delete b[this]}});jQuery.ajax(c)},get:function(a){a=
jQuery(a).parents(".boxy-wrapper");return a.length?jQuery.data(a[0],"boxy"):null},linkedTo:function(a){return jQuery.data(a,"active.boxy")},alert:function(a,b,c){return Boxy.ask(a,["OK"],b,c)},confirm:function(a,b,c){return Boxy.ask(a,["OK","Cancel"],function(d){d=="OK"&&b()},c)},ask:function(a,b,c,d){d=jQuery.extend({modal:true,closeable:false},d||{},{show:true,unloadOnHide:true});a=jQuery("<div></div>").append(jQuery('<div class="question"></div>').html(a));var e={},f=[];if(b instanceof Array)for(var g=
0;g<b.length;g++){e[b[g]]=b[g];f.push(b[g])}else for(g in b){e[b[g]]=g;f.push(b[g])}b=jQuery('<form class="answers"></form>');b.html(jQuery.map(f,function(h){return"<input type='button' value='"+h+"' />"}).join(" "));jQuery("input[type=button]",b).click(function(){var h=this;Boxy.get(this).hide(function(){c&&c(e[h.value])})});a.append(b);new Boxy(a,d)},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(){var a=jQuery(document);jQuery(".boxy-modal-blackout").css("display","none").css({width:a.width(),height:a.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 a=document.documentElement,b=document.body,c=window;return jQuery.extend(jQuery.browser.msie?{left:b.scrollLeft||a.scrollLeft,top:b.scrollTop||
a.scrollTop}:{left:c.pageXOffset,top:c.pageYOffset},!Boxy._u(c.innerWidth)?{width:c.innerWidth,height:c.innerHeight}:!Boxy._u(a)&&!Boxy._u(a.clientWidth)&&a.clientWidth!=0?{width:a.clientWidth,height:a.clientHeight}:{width:b.clientWidth,height:b.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 a=this.getPosition(),b=this.getSize();return[Math.floor(a[0]+b[0]/2),Math.floor(a[1]+
b[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){typeof a=="number"?this.boxy.css({left:a}):
this.centerX();return this},moveToY:function(a){typeof a=="number"?this.boxy.css({top:a}):this.centerY();return this},centerAt:function(a,b){var c=this[this.visible?"getSize":"estimateSize"]();typeof a=="number"&&this.moveToX(a-c[0]/2);typeof b=="number"&&this.moveToY(b-c[1]/2);return this},centerAtX:function(a){return this.centerAt(a,null)},centerAtY:function(a){return this.centerAt(null,a)},center:function(a){var b=Boxy._viewport(),c=this.options.fixed?[0,0]:[b.left,b.top];if(!a||a=="x")this.centerAt(c[0]+
b.width/2,null);if(!a||a=="y")this.centerAt(null,c[1]+b.height/2);return this},centerX:function(){return this.center("x")},centerY:function(){return this.center("y")},resize:function(a,b,c){if(this.visible){a=this._getBoundsForResize(a,b);this.boxy.css({left:a[0],top:a[1]});this.getContent().css({width:a[2],height:a[3]});c&&c(this);return this}},tween:function(a,b,c){if(this.visible){a=this._getBoundsForResize(a,b);var d=this;this.boxy.stop().animate({left:a[0],top:a[1]});this.getContent().stop().animate({width:a[2],
height:a[3]},function(){c&&c(d)});return this}},isVisible:function(){return this.visible},show:function(){if(!this.visible){if(this.options.modal){var a=this;if(!Boxy.resizeConfigured){Boxy.resizeConfigured=true;jQuery(window).resize(function(){Boxy._handleResize()})}this.modalBlackout=jQuery('<div class="boxy-modal-blackout"></div>').css({zIndex:Boxy._nextZ(),opacity:0.7,width:jQuery(document).width(),height:jQuery(document).height()}).appendTo(document.body);this.toTop();this.options.closeable&&
jQuery(document.body).bind("keypress.boxy",function(b){if((b.which||b.keyCode)==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(a){if(this.visible){var b=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(){b.boxy.css({display:"none"});
b.visible=false;b._fire("afterHide");a&&a(b);b.options.unloadOnHide&&b.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();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(a,b){var c=this.getContentSize();c=[a-c[0],b-c[1]];var d=this.getPosition();return[Math.max(d[0]-c[0]/2,0),Math.max(d[1]-c[1]/2,0),a,b]},_setupTitleBar:function(){if(this.options.title){var a=this,b=jQuery("<div class='title-bar'></div>").html("<h2>"+unescape(this.options.title)+"</h2>");this.options.closeable&&b.append(jQuery("<a href='#' class='close'></a>").html(this.options.closeText));
if(this.options.draggable){b[0].onselectstart=function(){return false};b[0].unselectable="on";b[0].style.MozUserSelect="none";if(!Boxy.dragConfigured){jQuery(document).mousemove(Boxy._handleDrag);Boxy.dragConfigured=true}b.mousedown(function(c){a.toTop();Boxy.dragging=[a,c.pageX-a.boxy[0].offsetLeft,c.pageY-a.boxy[0].offsetTop];jQuery(this).addClass("dragging")}).mouseup(function(){jQuery(this).removeClass("dragging");Boxy.dragging=null;a._fire("afterDrop")})}this.getInner().prepend(b);this._setupDefaultBehaviours(b)}},
_setupDefaultBehaviours:function(a){var b=this;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)}};
