
$(function() {
  
  if (document.cookie.match(/sessionid=/)) {
      $("h1, h2").each(function() {
          var $this = $(this);
          if ($this.attr("editlink")) {
              $this.append(" (<a href='" + $this.attr("editlink") + "'>edit</a>)");
          }
      });
  }

  
  // commenting requires JS. So only show reply-to links if you have JS.
  //$(".reply_to_link").html("reply to this");
  //$("#new_comment").html("create a new comment");
  //$("#comment_active").val("1");
  
  // I resent having to do this. But CSS isn't powerful enough to do what I want.
  function fix_image_links() {
    $('a img').parents("a").addClass("withImage");
  }

  fix_image_links();
  setTimeout( fix_image_links, 1000 ); // the disqus stuff loads in images later.

  // disqus comment count display
  (function(){
    var links = document.getElementsByTagName('a');
    var query = '?';
    for(var i = 0; i < links.length; i++) {
      if(links[i].href.indexOf('#disqus_thread') >= 0) {
        query += 'url' + i + '=' + encodeURIComponent(links[i].href) + '&';
      }
    }
    if (query.length > 1)
      $.getScript("http://disqus.com/forums/jerakeen/get_num_replies.js" + query)
  })();

  // look for delicious bookmarks on items
  // just having a delicious-placeholder element somewhere means that
  // we want to look for links to this page.
  var hashes = [];
  $('.delicious-placeholder').each(function(index, element) {
    var hash = $(element).attr("id").replace(/delicious-/, "");
    hashes.push( "hash=" + hash );
  });
  while (hashes.length) {
    var subsection = hashes.splice(0, 15); // delicious permit a maximum of 15 hashes per request
    $.getScript( "http://badges.del.icio.us/feeds/json/url/data?" + subsection.join("&") + "&callback=gotDeliciousData" );
  }


  // automatically add link tracking to outbound links
  $('a').each(function() {
    var $a = $(this);
    var href = $a.attr('href');
    if ( (href.match(/^http/)) && (! href.match(document.domain)) ) {
      $a.click(function() {
        // if so, register an event
        var category = 'outgoing'; // set this to whatever you want
        var event = 'click'; // set this to whatever you want
        var label = href; // set this to whatever you want
        pageTracker._trackEvent(category, event, href);
      });
    }
  });
  
  $('a.cougar-download').each(function() {
    var $a = $(this);
    var href = $a.attr('href');
    $a.click(function() {
      // if so, register an event
      var category = 'download'; // set this to whatever you want
      var event = 'click'; // set this to whatever you want
      var label = href; // set this to whatever you want
      pageTracker._trackEvent(category, event, href);
    });
  });
  
});

function gotDeliciousData(data) {
  for (var i=0; i<data.length; i++) {
    var d = data[i];
    var total = d.total_posts;
    if ($("#delicious-" + d.hash).attr("mylink")) {
      total -= 1;
    }
    if (total > 0) {
      var template = "%d delicious bookmark";
      if ($("#delicious-" + d.hash).attr("template")) {
        template = $("#delicious-" + d.hash).attr("template");
      } else if (total > 1) {
        template = "%d delicious bookmarks";
      }
      var linktext = template.replace(/%d/, total);
      $("#delicious-" + d.hash).html('<a class="comment-link" href="http://delicious.com/url/' + d.hash + '?show=notes_only">' + linktext + '</a>');
    }
  }
}
  