$(function(){

  // Replace Headers with images
  replaceHeaders();
  
  // Replace Post dates with images
  replaceDates();
  
  // Comandi x ingrandire e rimpicciolire la textarea dei commenti
  commentFormCommands();
  
  // Anchor-Scroll on sidebar
  scrollAnchorLinks();
  
  // Pay me a coke
  $('p.meta a.coke').click(function(e){$('form#paypalCoke').submit();e.preventDefault();});
  
  // New window for all external links
  $("a[href^=http://]:not(.noblank)").attr("target","_blank");
  $("a[href^=http://andrea.zilio.name]").attr("target","");
  
  // New windows even for manually specified links
  $("a.blank").attr("target","_blank");
  
  // Search Form text
  $("input#s").attr("defText",'Searching for...');
  $("input#s").focus(function(){if($(this).val() == $(this).attr("defText")) $(this).val(""); });
  $("input#s").blur(function(){if($(this).val() == "") $(this).val($(this).attr("defText"));});
});

function scrollAnchorLinks(){
  $('ul#sidebar a[href*=#]').click(function(e) {
    if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') && location.hostname == this.hostname) {
      var $target = $(this.hash);
      $target = $target.length && $target || $('[id=' + this.hash.slice(1) +']');
      if ($target.length) {
        var targetOffset = $target.offset().top;
        $('html,body').animate({scrollTop: targetOffset}, 1000);
        
        // To make Back and Forward browser button works!
        var sel = '[id=' + this.hash.slice(1) +']';
        $(sel).attr('id','');
        document.location = this.href;
        $(sel).attr('id',this.hash.slice(1));
        
        e.preventDefault();
      }
    }
  });
}

function commentFormCommands(){
  // Comandi x ingrandire e rimpicciolire la textarea dei commenti
  var $more = $('<span class="more" title="Enlarge the text box">More</span>');
  var $less = $('<span class="less" title="Compact the text box">Less</span>');
  $less.cur = $("div#new-comment textarea").height();
  
  $more.click(function(){
    $("div#new-comment textarea").animate({height: $("div#new-comment textarea").height() + 100},"normal");
  });
  $less.click(function(){
    if($("div#new-comment textarea").height() > $less.cur){
      $("div#new-comment textarea").animate({height: $("div#new-comment textarea").height() - 100},"normal");
    }
  });
  $("div#new-comment small.allowed-tags").prepend($more.add($less));
}


function replaceHeaders(){
  $("div.post h3").each(function(){
    var $h3   = $(this);
    var $a    = $("a",$h3);
    var text  = jQuery.trim($h3.text());
    
    var $img = $("<img />");
    $img.attr("src",themeUrl + "/scripts/post-title.php?style=italic&t=" + encodeURIComponent(text));
    $img.attr("alt",text);
    
    $h3.addClass("replaced-header");
    
    if($a.size()>0){
      $img.attr("title",$a.attr("title") || "");
      $a.empty().prepend($img);
    }else{
      $h3.empty().prepend($img);
    }
  });
}

function replaceDates(){
  $("div.post div.info small.date").each(function(){
    var $t = $(this);
    // Specifico la base per parseInt, altrimenti se la stringa
    // inizia con '0' utilizza base 8 invece che base 10
    var day   = parseInt($("span.day",$t).text(),10);
    var month = parseInt($("span.month",$t).text(),10);
    
    var $img = $('<img />');
    $img.attr("src",themeUrl + "/scripts/date.php?m=" + month + "&n=" + day);
    $img.attr("alt",$t.text());
    
    $t.addClass("replaced-date").empty().prepend($img);
  });
}
