(function(a){a.ui=a.ui||{};var c=/left|center|right/,d=/top|center|bottom/,b="center",e=a.fn.position,f=a.fn.offset;a.fn.position=function(f){if(!f||!f.of)return e.apply(this,arguments);f=a.extend({},f);var i=a(f.of),m=i[0],l=(f.collision||"flip").split(" "),h=f.offset?f.offset.split(" "):[0,0],k,j,g;if(m.nodeType===9){k=i.width();j=i.height();g={top:0,left:0}}else if(m.setTimeout){k=i.width();j=i.height();g={top:i.scrollTop(),left:i.scrollLeft()}}else if(m.preventDefault){f.at="left top";k=j=0;g={top:f.of.pageY,left:f.of.pageX}}else{k=i.outerWidth();j=i.outerHeight();g=i.offset()}a.each(["my","at"],function(){var a=(f[this]||"").split(" ");if(a.length===1)a=c.test(a[0])?a.concat([b]):d.test(a[0])?[b].concat(a):[b,b];a[0]=c.test(a[0])?a[0]:b;a[1]=d.test(a[1])?a[1]:b;f[this]=a});if(l.length===1)l[1]=l[0];h[0]=parseInt(h[0],10)||0;if(h.length===1)h[1]=h[0];h[1]=parseInt(h[1],10)||0;if(f.at[0]==="right")g.left+=k;else if(f.at[0]===b)g.left+=k/2;if(f.at[1]==="bottom")g.top+=j;else if(f.at[1]===b)g.top+=j/2;g.left+=h[0];g.top+=h[1];return this.each(function(){var i=a(this),e=i.outerWidth(),d=i.outerHeight(),n=parseInt(a.curCSS(this,"marginLeft",true))||0,o=parseInt(a.curCSS(this,"marginTop",true))||0,q=e+n+parseInt(a.curCSS(this,"marginRight",true))||0,p=d+o+parseInt(a.curCSS(this,"marginBottom",true))||0,c=a.extend({},g),m;if(f.my[0]==="right")c.left-=e;else if(f.my[0]===b)c.left-=e/2;if(f.my[1]==="bottom")c.top-=d;else if(f.my[1]===b)c.top-=d/2;c.left=parseInt(c.left);c.top=parseInt(c.top);m={left:c.left-n,top:c.top-o};a.each(["left","top"],function(b,g){a.ui.position[l[b]]&&a.ui.position[l[b]][g](c,{targetWidth:k,targetHeight:j,elemWidth:e,elemHeight:d,collisionPosition:m,collisionWidth:q,collisionHeight:p,offset:h,my:f.my,at:f.at})});a.fn.bgiframe&&i.bgiframe();i.offset(a.extend(c,{using:f.using}))})};a.ui.position={fit:{left:function(b,c){var e=a(window),d=c.collisionPosition.left+c.collisionWidth-e.width()-e.scrollLeft();b.left=d>0?b.left-d:Math.max(b.left-c.collisionPosition.left,b.left)},top:function(b,c){var e=a(window),d=c.collisionPosition.top+c.collisionHeight-e.height()-e.scrollTop();b.top=d>0?b.top-d:Math.max(b.top-c.collisionPosition.top,b.top)}},flip:{left:function(h,c){if(c.at[0]===b)return;var g=a(window),i=c.collisionPosition.left+c.collisionWidth-g.width()-g.scrollLeft(),e=c.my[0]==="left"?-c.elemWidth:c.my[0]==="right"?c.elemWidth:0,d=c.at[0]==="left"?c.targetWidth:-c.targetWidth,f=-2*c.offset[0];h.left+=c.collisionPosition.left<0?e+d+f:i>0?e+d+f:0},top:function(h,c){if(c.at[1]===b)return;var g=a(window),i=c.collisionPosition.top+c.collisionHeight-g.height()-g.scrollTop(),e=c.my[1]==="top"?-c.elemHeight:c.my[1]==="bottom"?c.elemHeight:0,d=c.at[1]==="top"?c.targetHeight:-c.targetHeight,f=-2*c.offset[1];h.top+=c.collisionPosition.top<0?e+d+f:i>0?e+d+f:0}}};if(!a.offset.setOffset){a.offset.setOffset=function(b,c){if(/static/.test(a.curCSS(b,"position")))b.style.position="relative";var e=a(b),d=e.offset(),h=parseInt(a.curCSS(b,"top",true),10)||0,g=parseInt(a.curCSS(b,"left",true),10)||0,f={top:c.top-d.top+h,left:c.left-d.left+g};if("using"in c)c.using.call(b,f);else e.css(f)};a.fn.offset=function(b){var c=this[0];return!c||!c.ownerDocument?null:b?this.each(function(){a.offset.setOffset(this,b)}):f.call(this)}}})(jQuery)