$(document).ready(function(){
  $("#new_comment").validate();
  // Code Copier
  if (!$.browser.msie) { $(".textmate-source").codeCopier(); }
  // Some clever shizzle from Simon to adjust the padding of #secondary
  adjustSecondaryPadding();
});

function adjustSecondaryPadding(){
  var singleLineHeight = 31;
  var $title = $('h1');
  if($title.length){
    var titleHeight = $title.outerHeight();
    if(titleHeight > singleLineHeight){
      var $secondary = $('#secondary');
      if($secondary.length){
          var currentPaddingTop = $secondary.css('padding-top');
          currentPaddingTop = currentPaddingTop.replace(/px/, '');
          var newPaddingTop = (parseInt(currentPaddingTop) + parseInt(titleHeight - singleLineHeight)) + 'px';
          $secondary.css('padding-top', newPaddingTop);
      }
    }
  }
}

// Code Copier
(function($) {  
  jQuery.fn.codeCopier = function() {
    return this.each(function(){
      $this = jQuery(this);
      $this.wrap('<div class="code_helper"></div>');
      $codewrapper = $this.parent();
      
      // Create the plain text codeblock
      $original = $this.children().clone();
      
      // Remove line numbering
      $this.find(".linenum").remove();
      
      // Save plain text
      var plain_text = $this.text();
      
      // Create a textarea that is hidden
      $codewrapper.append('<textarea>'+plain_text+'</textarea>');
      $codewrapper.children("textarea").css("height",$this.height()).hide();
      
      // Remove source code and re-inject into DOM from original backup
      
      $this.children().remove();
      $this.html($original);
      
      // Create the toggle link
      var link = document.createElement("a");
      link.setAttribute("href","#");
      var link_text = document.createTextNode("Toggle fancy/plain code");
      link.appendChild(link_text);
      link.onclick = function() {
        toggleCode(this);
        return false;
        }
        
      // Insert after code block
      $codewrapper.prepend(link);

    });
    
    function toggleCode(element) {
      $link = jQuery(element);
      $codewrapper = $link.parent();
      $codewrapper.children("pre").toggle();
      $codewrapper.children("textarea").toggle();
      if ($codewrapper.children("textarea").attr("display") != "none") {
        $codewrapper.children("textarea").focus();
      }
    }
  }
})(jQuery);