function addslashes(str)
{
   return (str+'').replace(/[\\"']/g, '\\$&').replace(/\u0000/g, '\\0');
}

function shorttitle(str)
{
   if(str.length > 100)
   {
      return str.substr(0,97)+'...';
   }
   return str;
}

/*
<img src="http://l.yimg.com/hr/img/delicious.small.gif" height="10" width="10" alt="Delicious" />
<a href="http://www.delicious.com/save" onclick="window.open('http://www.delicious.com/save?v=5&noui&jump=close&url='+encodeURIComponent(location.href)+'&title='+encodeURIComponent(document.title), 'delicious','toolbar=no,width=550,height=550'); return false;"> Bookmark this on Delicious</a>
*/

var freshRequest = null;
var freshAnswer = null;

function getFresh()
{
   if (freshAnswer) { freshAnswer.abort(); freshRequest = null; freshAnswer = null; }
   if (!freshRequest)
   {
      var hlink = $('#minthink-link a').attr('href');
      var hsafetitle = $('#minthink-link a').html();
      var htitle = Decoder.htmlDecode(hsafetitle);
      $('#minthink-link').html('');
      $('#minthink-link').css({background:'url(\'/imx/loading.gif\') center center no-repeat'});
      if(typeof(hlink) != 'undefined')
      {
         $('#history-log').prepend('<p><a href="'+hlink+'" target="_blank" onclick="_gaq.push([\'_trackEvent\', \'Content\', \'History\']);">'+hsafetitle+'</a> <a href="http://www.facebook.com/sharer.php?t='+encodeURIComponent(htitle+' via minthink.com')+'&u='+encodeURIComponent(hlink)+'" target="_blank" title="Share on Facebook"><img src="/imx/social/facebook.png" /></a> <a href="http://twitter.com/share?text='+encodeURIComponent(shorttitle(htitle))+'&url='+encodeURIComponent(hlink)+'&via=minthink&related=ignlg:developer" target="_blank" title="Share on Twitter"><img src="/imx/social/twitter.gif" /></a> <a href="http://www.delicious.com/save?v=5&noui&jump=close&title='+encodeURIComponent(htitle)+'&url='+encodeURIComponent(hlink)+'" target="_blank" title="Share on Delicious"><img src="/imx/social/delicious.png" /></a></p>');
         var opacity = 8;
         var i = 0;
         $("#history-log p").each(function() {
            if(opacity > 3){opacity-=1;} $(this).css('opacity',(opacity/10));
            if(i > 4){$(this).remove();} i++;
         });
         $("#history").show('fast');
      }
      freshRequest=true;
      freshAnswer=$.getJSON('index.php',{lk:'json'}, function(data)
      {
         $('#minthink-link').css({background:'none'});
         $('#minthink-link').html('<a href="'+data.safeurl+'" target="_blank" title="it\'s fresh!" onclick="_gaq.push([\'_trackEvent\', \'Content\', \'Fresh-link\']);">'+data.safetitle+'</a>');

         $("#sharethis:visible a").fadeTo(100,1).fadeTo(300,0.6);
         $("#sharethis:hidden").show("fast");
         $('#sharefb').attr('href','http://www.facebook.com/sharer.php?t='+encodeURIComponent(data.title+' via minthink.com')+'&u='+encodeURIComponent(data.url));
         $('#sharetw').attr('href','http://twitter.com/share?text='+encodeURIComponent(shorttitle(data.title))+'&url='+encodeURIComponent(data.url)+'&via=minthink&related=ignlg:developer');
         $('#sharedl').attr('href','http://www.delicious.com/save?v=5&noui&jump=close&title='+encodeURIComponent(data.title)+'&url='+encodeURIComponent(data.url));
         freshRequest=null;
      });
   }
   return false;
}

$(document).ready(function(){
   $('#minthink-getfresh').click(function(){getFresh();});
});

