(function(a){var b={content:"Hello",position:"center center",closeKey:false,closeOverlay:false,useOverlay:true,autoclose:false,easingDuration:300,easingIn:"swing",easingOut:"swing",height:"auto",width:"360px",animate:true,zIndex:100},c,d,e=a(window),f={hide:function(b,h,i){if(f.isActive()){a("#popin-wrapper").stop(true,true);var j=a("#popin-wrapper"),k=j.css("position"),l=0;position=c.position.split(" ");switch(position[0]){case"top":l=(k==="fixed"?-1*j.outerHeight():j.offset().top)-10;break;case"bottom":l=(k==="fixed"?e.height():j.offset().top+j.height())+10;break;default:l=k==="fixed"?(e.height()-j.outerHeight())/2:$o.offset().top+(e.height()-j.outerHeight())/2}switch(position[1]){case"left":xminpos=(k==="fixed"?-1*j.outerWidth():j.offset().left)-10;break;case"right":xminpos=(k==="fixed"?e.width():j.offset().left+j.width())+10;break;default:xminpos=(e.width()-j.outerWidth())/2+e.scrollLeft()}if(c.animate){j.animate({top:l,left:xminpos},c.easingDuration||c.duration,c.easingOut,function(){if(a.browser.msie){a("#popin-overlay").css("display","none")}else{a("#popin-overlay").fadeOut("fast")}j.remove();clearTimeout(d);if(typeof h==="function"){h.call(i)}})}else{if(a.browser.msie){a("#popin-overlay").css("display","none")}else{a("#popin-overlay").fadeOut("fast")}j.remove();clearTimeout(d);if(typeof h==="function"){h.call(i)}}a(document).unbind("keydown",g.enterKeyHandler).unbind("keydown",g.closeKeyHandler).unbind("keydown",g.tabKeyHandler)}},resize:function(b,c,d){var e=a("#popin-wrapper"),h=parseInt(b.width,10),i=parseInt(b.height,10),j=Math.abs(e.outerWidth()-h),k=Math.abs(e.outerHeight()-i);if(f.isActive()&&(j>5||k>5)){e.animate({width:h},function(){a(this).animate({height:i},function(){g.fixPos()})});a("#popin").animate({width:h-94},function(){a(this).animate({height:i-131},function(){if(typeof c==="function"){c.call(d)}})})}},show:function(h,i,j){if(f.isActive()){a.error("Can't create new message with content: \""+h.content+'", past message with content "'+c.content+'" is still active')}else{c=a.extend({},b,h);a('<div id="popin-wrapper"></div>').appendTo("body");var k=a("#popin-wrapper"),l=a("#popin-overlay");k.css({width:c.width,height:c.height,position:"absolute",top:"-9999px",left:"-9999px"}).html('<div id="popin"></div>').find("#popin").html(c.content).css({height:c.height=="auto"?"auto":k.height()-131,width:k.width()-94});var m=function(){k.show();var a=(e.width()-k.outerWidth())/2+e.scrollLeft(),b,g,h=e.height()>k.height()&&e.width()>k.width()?"fixed":"absolute";position=c.position.split(" ");switch(position[0]){case"bottom":b=h==="fixed"?e.height():e.scrollTop()+e.height();g=b-k.outerHeight();break;case"top":g=h==="fixed"?0:e.scrollTop();b=g-k.outerHeight();break;default:b=h==="fixed"?(e.height()-k.outerHeight())/2:l.offset().top+(e.height()-k.outerHeight())/2;g=b}switch(position[1]){case"left":xmaxpos=h==="fixed"?0:e.scrollLeft();xminpos=xmaxpos-k.outerWidth();break;case"right":xminpos=h==="fixed"?e.width():e.scrollLeft()+e.width();xmaxpos=xminpos-k.outerWidth();break;default:xminpos=(e.width()-k.outerWidth())/2+e.scrollLeft();xmaxpos=xminpos}if(c.animate){k.css({left:xminpos,position:h,top:b,"z-index":c.zIndex+1}).animate({top:g,left:xmaxpos},c.easingDuration,c.easingIn,function(){if(typeof i==="function"){i.call(j)}if(c.autoclose){d=setTimeout(f.hide,c.autoclose)}})}else{k.css({left:xmaxpos,position:h,top:g,"z-index":c.zIndex+1})}};if(c.useOverlay){if(a.browser.msie&&a.browser.version<9){l.css({display:"block","z-index":c.zIndex});m()}else{l.css({"z-index":c.zIndex}).fadeIn(m)}}else{m()}a(document).bind("keydown",g.enterKeyHandler).bind("keydown",g.closeKeyHandler).bind("keydown",g.tabKeyHandler);k.find(":input").bind("keydown",function(b){g.unbindKeyHandler();if(b.keyCode===13){console.log(1);a(".popin-button").eq(0).trigger("click")}})}},set:function(a,d,e){for(var f in a){if(b.hasOwnProperty(f)){b[f]=a[f];if(c&&c[f]){c[f]=a[f]}}}if(typeof d==="function"){d.call(e)}},isActive:function(){return!!(a("#popin-wrapper").length>0)},blink:function(){a("#popin-wrapper").fadeOut(100,function(){a(this).fadeIn(100)})},shake:function(){a("#popin-wrapper").stop(true,true).animate({left:"+=20px"},50,function(){a(this).animate({left:"-=40px"},50,function(){a(this).animate({left:"+=30px"},50,function(){a(this).animate({left:"-=20px"},50,function(){a(this).animate({left:"+=10px"},50)})})})})}},g={fixPos:function(){var b=a("#popin-wrapper"),d=b.css("position");if(e.width()>b.outerWidth()&&e.height()>b.outerHeight()){var f=(e.width()-b.outerWidth())/2,g=e.height()-b.outerHeight();switch(c.position){case"center":g=g/2;break;case"bottom":break;default:g=0}if(d=="fixed"){b.animate({left:f},function(){a(this).animate({top:g})})}else{b.css({position:"fixed",left:f,top:g})}}else{var f=(e.width()-b.outerWidth())/2+e.scrollLeft();var g=e.scrollTop();if(d!="fixed"){b.animate({left:f},function(){a(this).animate({top:g})})}else{b.css({position:"absolute",top:g,left:f>0?f:0})}}},tabKeyHandler:function(b){if(b.keyCode===9){a("#popin-wrapper").find(":input").eq(0).focus();g.unbindKeyHandler();b.preventDefault()}},closeKeyHandler:function(a){if(a.keyCode===27&&c.closeKey){f.hide()}},unbindKeyHandler:function(){a(document).unbind("keydown",g.enterKeyHandler).unbind("keydown",g.tabKeyHandler)}};a(document).ready(function(){a("body").append('<div id="popin-overlay"></div>');a("#popin-overlay").bind("click",function(){if(c.closeOverlay){f.hide()}else{f.blink()}})});a(window).resize(function(){if(f.isActive()){g.fixPos()}});a.popin=function(b,c,d){var e=window;if(typeof b==="object"){c=b;b="show"}if(f[b]){if(typeof c==="function"){d=c;c=null}f[b](c,d,e)}else{a.error('Method "'+b+'" does not exist in $.popin')}}})(jQuery)
