
(function($){$.fn.togglePuff=function(settings,callback){settings=jQuery.extend({distance:10,effectTime:250},settings);return this.each(function(){var isHidden=$(this).is(":hidden");if(isHidden){$(this).show();}
$(this).animate({top:'-='+settings.distance+'px',opacity:isHidden?1:0},settings.effectTime,function(){if(callback){callback(this);}});});};$.fn.okTips=function(opts){var $popup,$cover,trigger;opts=$.extend({id:"tooltip",popup:"<div id='ui-oktip' class='ui-tooltip'></div>",live:false,triggerEvent:'click',closeTrigger:null,triggerCloses:true,beforeShow:null,afterShow:null,beforeDestroy:null,afterDestroy:null,body:function(){return"<p>"+this.title+"</p>";},top:-20,left:20,hideDelay:500,effectTime:250,distance:10,modal:true,overlayClass:'ui-widget-overlay-clear'},opts);function appendHtml(){var html;if($popup.length===0){$popup=$(opts.popup).appendTo(document.body).hide();if(opts.modal){$cover=$("<div id='ui-oktip-overlay'></div>");$cover.addClass(opts.overlayClass).css({position:'absolute',top:0,left:0,width:"100%",height:$("body").height()+'px',zIndex:"1000"}).appendTo(document.body).hide();}}
$popup.bind('destroy',function(){destroy();});html=typeof(opts.body)=='function'?opts.body.call($popup,trigger):opts.body;if(typeof(html)==="string"){$popup.html(html);}}
function getPosition(e){var scrollTop=$(window).scrollTop(),screenWidth=$(window).width(),left,top;if(e.pageX+$popup.outerWidth()+(opts.left*2)>=screenWidth){left=screenWidth-$popup.width()-opts.left;}
if(e.pageY+(opts.top*2)<=scrollTop){top=scrollTop-(opts.top*2);}
return{top:(top||e.pageY+opts.top)+"px",left:(left||e.pageX+opts.left)+"px"};}
function create(e){var visible,pos,html;appendHtml();visible=$popup.is(":visible");pos=getPosition(e);$popup.stop(true).css({position:'absolute',zIndex:'9999',left:pos.left,top:pos.top});bindDestroyEvent();if(opts.beforeShow){opts.beforeShow.call($popup);}
visible?$popup.show():$popup.togglePuff(opts,function(e){$(e).show();});if(opts.modal){$cover.show();}
if(opts.afterShow){opts.afterShow.call($popup);}
return $popup;}
function bindDestroyEvent(){if(opts.modal){$cover.bind('click',function(e){e.preventDefault();destroy(e);});}else if(opts.triggerCloses){$(trigger).bind('mouseleave',function(e){destroy(e);});}else{$popup.bind('mouseenter',function(e){$popup.unbind('mouseenter').bind('mouseleave',function(e){e.preventDefault();destroy(e);});});}
if(opts.closeTrigger){$(opts.closeTrigger).bind('click',function(e){e.preventDefault();destroy(e);});}
return $popup;}
function destroy(e){if(opts.beforeDestroy){opts.beforeDestroy.call($popup);}
if($popup.is(":visible")){$popup.stop(true).togglePuff(opts,function(e){$(e).hide();});}
if(opts.modal){$popup.unbind('mouseleave');$cover.hide().unbind('click');}
if(opts.afterDestroy){opts.afterDestroy.call(popup,$trigger);}}
return $(this.selector)[opts.live?'live':'bind'](opts.triggerEvent,function(e){$popup=$("#ui-oktip");$cover=$("#ui-oktip-overlay");e.preventDefault();trigger=this;create(e);});};})(jQuery);