$(document).ready(function() { 
  
  $('a.report').click(function(){
    var that = $(this);
    var formContainer = that.next();
    formContainer.toggle();
    formContainer.find(".btn-submit").unbind("click").click(function(){
      var form = $(this).parents("form");
      var data = form.serialize();
      var location = form.attr("action");
      $.post(location, data, function(data, status){
        if(status="success"){
          that.html('Thanks for letting us know - Vik will take a closer look');
          that.unbind('click').addClass("inactive").click(function(){
            return false;
          });
          formContainer.hide();
        }
      });
      return false;
    });
    return false;
  });
  
  $('.post-content').each(function(){
    $(this).find('p:first').css('font-weight','bold');
  });
  $('.btn-submit-degraded').hide();
  $('.btn-submit').show().click(function(){
    $(this).parents("form").submit();
    return false;
  });
  $('#search-query').focus(function(){
    if(!this.initial){
      this.initial = this.value;
    }
    if(!this.value||this.initial===this.value){
      this.value = "";
    }
  }).blur(function(){
    if(!this.value){
      this.value = this.initial;
    }
  });
  
  /* ------------- open new windows using 'rel' attribute -----------------*/
  $("a[rel*='external']").each(function() {
    $(this).attr({
      title: "This link will open in a new window"
    });
    // add target attribute to link
    $(this).attr('target', '_blank');
    pageTracker._trackPageview('/outgoing/'+ $(this).attr('href'));
  });
     
  var featuredPosts = $('#featured-post-list li'),
      currentPost = 1,
      timeout = 4500;
      fadeDuration = 450;
  var rotateFeaturedPost = function(){
    if(currentPost==featuredPosts.size()) currentPost = 0;
    var post = $(featuredPosts[currentPost]);
    featuredPosts.not(post).fadeOut(fadeDuration, function(){
      post.fadeIn(fadeDuration+100);
    });
    currentPost = currentPost + 1;
    setTimeout(rotateFeaturedPost, timeout);
  }; setTimeout(rotateFeaturedPost, timeout);
  
  share = function (service, url, title) {
     var url = encodeURIComponent(url || location.href);
     var title = encodeURIComponent(title || document.title);
     var popup = function (url) {
         window.open(url, 'share','scrollbars=yes,toolbar=0,status=0,width=750,height=500');
         return false;
     };  
     var services = {
         facebook : function (url, title) {
             return popup('http://www.facebook.com/sharer.php?u='+url+'&t='+title);
         },
         digg : function (url, title) {
             return popup('http://digg.com/submit?url='+url+'&amp;title='+title+'&amp;media=news&amp;topic=soccer');
         },      
         delicious : function (url, title) {
             return popup('http://delicious.com/save?v=5&amp;noui&amp;jump=close&amp;url='+url+'&amp;title='+title);
         },
         twitter : function (url, title) {
             var msg = title + '%20@%20' + url;
             return popup('http://twitter.com/home?status='+msg);
         }
     };  
     services[service](url, title);
  };
  
});