function Cluetip(){this.opts={cluezIndex:97};this.linkinfos={};this.close_timeout=null;this.open_timeout=null;this.isActive=false;this.currentLinkInfo=null;if(!jQuery('#cluetip').length){$cluetipInner=jQuery('<div id="cluetip-inner"></div>');$cluetipArrow=jQuery('<div id="cluetip-pointer"></div>');$cluetipFooter=jQuery('<div id="cluetip-footer"><p><a href="#" onclick="return mShots.stop();">Turn Site Previews Off</a></p></div>');$cluetipDiv=jQuery('<div id="cluetip"></div>').css({zIndex:this.opts.cluezIndex}).append($cluetipArrow).append($cluetipInner).append($cluetipFooter).appendTo('body').hide();$cluetipDiv.css({position:'absolute',zIndex:this.opts.cluezIndex});$cluetipInner.css({position:'relative',zIndex:this.opts.cluezIndex+1});var self=this;$cluetipDiv.hover(function(){clearTimeout(self.close_timeout);},function(){self.do_close();});}
this.register=function(strid,linkInfo){this.linkinfos[strid]=linkInfo;}
this.hide=function(){this.isActive=false;this.currentLinkInfo=null;$cluetipDiv.hide();$cluetipDiv.removeClass("cluetip cluetip-dark");}
this.show=function(){var wasActive=this.isActive;this.isActive=true;var fits=this.position();if(fits){$cluetipDiv.show();$cluetipDiv.addClass("cluetip cluetip-dark");}}
this.do_close=function(){var self=this;clearTimeout(this.close_timeout);this.close_timeout=setTimeout(function(){self.hide();},600);}
this.do_open=function(linkInfo){var self=this;clearTimeout(this.open_timeout);this.open_timeout=setTimeout(function(){self.currentLinkInfo=linkInfo;$cluetipInner.empty().append(linkInfo.get_markup());self.show();},750);}
this.acquire=function(linkInfoId){clearTimeout(this.close_timeout);if(this.currentLinkInfo==this.linkinfos[linkInfoId]&&this.isActive){return;}
var linkInfo=this.linkinfos[linkInfoId];linkInfo.reload();this.do_open(linkInfo);}
this.release=function(linkInfoId){clearTimeout(this.open_timeout);this.do_close();}
this.position=function(){if(this.currentLinkInfo==null){return false;}
var tipInnerWidth=430;var tipWidth=tipInnerWidth+(parseInt($cluetipDiv.css('paddingLeft'))||0)+(parseInt($cluetipDiv.css('paddingRight'))||0);var tipHeight=328;var wHeight=jQuery(window).height();var sTop=jQuery(document).scrollTop();var winWidth=jQuery(window).width();var tOffset=25;var lOffset=15;var $this=jQuery(this.currentLinkInfo.link);linkTop=$this.offset().top;linkLeft=$this.offset().left;linkWidth=$this.width();linkHeight=$this.height();mouseX=mShots.pageX;mouseY=mShots.pageY;var arrowWidth=16;var arrowHeight=16;var direction='';$cluetipInner.css({overflow:'visible',height:'auto'});tipY=linkTop;tipX=linkLeft;baseline=sTop+wHeight;if((linkTop>sTop+tipHeight+arrowHeight||linkTop+linkHeight<baseline-tipHeight-arrowHeight)&&winWidth>tipWidth){$cluetipArrow.removeClass('horizontal-left').removeClass('horizontal-right').css({top:null});posX=mouseX-arrowWidth;if(posX+tipWidth>winWidth){posX=posX-tipWidth+3*arrowWidth;$cluetipArrow.css({left:390});}else{$cluetipArrow.css({left:16});}
if(posX<0){posX=0;}
$cluetipDiv.css({left:posX});$cluetipArrow.addClass('vertical-top').removeClass('vertical-bottom');tipY=mouseY-tipHeight-tOffset;if(tipY<baseline-wHeight+2*tOffset){tipY=mouseY+tOffset;$cluetipArrow.removeClass('vertical-top').addClass('vertical-bottom');}
if(tipY+tipHeight>baseline-tOffset){tipY=baseline-tipHeight-tOffset;$cluetipArrow.removeClass('vertical-top').addClass('vertical-bottom');}
if(direction==''){posX<linkLeft?direction='left':direction='right';}
$cluetipDiv.css({top:tipY+'px'});}else if((tipX>tipWidth||tipX<winWidth-tipWidth)&&wHeight>tipHeight){$cluetipArrow.removeClass('vertical-top').removeClass('vertical-bottom').css({left:null});tipY=linkTop-(tipHeight/2);if(tipY<sTop)tipY=sTop-3;if(tipY+tipHeight>baseline)tipY=baseline-tipHeight+3;$cluetipArrow.css({top:linkTop-tipY});$cluetipDiv.css({top:tipY});$cluetipArrow.addClass('horizontal-right');posX=linkLeft+linkWidth+lOffset;if(posX+tipWidth>winWidth){posX=linkLeft-tipWidth-lOffset;$cluetipArrow.addClass('horizontal-left').removeClass('horizontal-right');}
if(posX<0){return false;}else{}
$cluetipDiv.css({left:posX});}else{return false;}
return true;}}
function LinkInfo(link){this.link=jQuery(link);this.retry=0;this.done=false;this.markup=null;this.disable_reload=false;if(link.href.match(/files.wordpress.com.*\.(jpg|png|bmp|gif)/i)){this.rel=link.href+'?w=399';}else{this.rel='http://s.wordpress.com/mshots/v1/'+encodeURIComponent(link.href)+'?w=399';}
if(mShots.link_sharing_sites_opts.handle){for(i=0;i<mShots.link_sharing_sites_opts.domains.length;i++){if(link.host.indexOf(mShots.link_sharing_sites_opts.domains[i])>=0){if(mShots.link_sharing_sites_opts.method=='disable'){return;}else if(mShots.link_sharing_sites_opts.method=='icon'){this.rel='/wp-content/plugins/mshots/archive.png';this.disable_reload=true;}else if(mShots.link_sharing_sites_opts.method=='top-level'){this.rel='http://s.wordpress.com/mshots/v1/'+encodeURIComponent(link.protocol+'//'+link.host+'/')+'?w=399';}}}}
this.preload_image=jQuery(new Image());this.preload_image.attr("class","cluetip_image");this.preload_image[0].linkinfo=this;this.preload_image.load(function(){var self=this.linkinfo;if(this.width==400&&this.height==300&&!self.disable_reload){self.waiting=true;setTimeout(function(){self.waiting=false;self.reload();},5000);}else{self.done=true;}});this.reload=function(){var url=this.rel;if(this.retry>0){url+="&r="+this.retry;}
if((!this.waiting)&&(!this.done)){this.waiting=true;this.preload_image.attr("src",url);this.retry+=1;}}
this.get_markup=function(){if(this.markup==null){this.markup=jQuery('<div style="overflow: hidden; height: 300px;"><a href="'+this.link[0].href+'" style="display:block"></a></div>');jQuery('a',this.markup).append(this.preload_image);this.markup.append(jQuery('<div style="display: none"><img class="trackingImage" src="http://stats.wordpress.com/p.gif?v=mshots&action=hover&t='+new Date().getTime()+'" width="0" height="0"></img></div>'));}
return this.markup;}
this.mouseIn=function(event){mShots.cluetip.acquire(this.href);}
this.mouseOut=function(event){mShots.cluetip.release(this.href);}
this.link.hover(this.mouseIn,this.mouseOut);}
