$(document).ready(function() {

  // categories
  var $links = $('#content .aside .categories li a');
    
    $links.filter(':not(.selected)').next('ul').hide();
    
    $links.click(function(){
      if ($(this).hasClass('is-parent'))
      {
        $(this)
          .toggleClass('selected')
          .next('ul')
            .slideToggle('fast')
          ;
        return false;
      }
    });
  
  
  // Hide extra service informations
  var $extra = $('#content .service div.extra');
  $extra.filter(":not(.bound)").hide();
  // if (window.location.hash)
  // {
  //   // $extra.filter(function(){return window.location.hash !== "#"+this.id;}).hide();
  //   $extra.filter(function(){console.log("has class bound", this, $(this).hasClass("bound"));return $(this).hasClass("bound");}).hide();
  // }
  // else
  // {
  //   $extra.hide();
  // }

  $('#content .service a.extra').click(function() {
    var id = $(this).attr('href');
    $extra.filter(':not('+id+')').slideUp('fast');
    $(id).slideToggle('fast');
    return false;
  });
  
  
  // Thumbnails caption hover
  $('.thumbnails li').each(function(index) {
      var $thumbnail = $(this);
      var thumbnailsHeight = $thumbnail.find('img').height();
      var contentHeight  = $thumbnail.find('.details').outerHeight();
      var $details = $thumbnail.find('.details');
  
      $details.css({
        'opacity': 0.8,
        'top': thumbnailsHeight+'px'
      });
  
      $thumbnail.hover(function(){
          $(".details", this).stop().animate({top:thumbnailsHeight-contentHeight+'px'},{queue:false,duration:300});
        }, function() {
          $(".details", this).stop().animate({top:thumbnailsHeight+'px'},{queue:false,duration:300});
        });
      
    });

  // Form utilities
  $(":input.required").each(function(){
    var label = $("label[for="+this.id+"]");
    label.addClass("required").text(label.text()+"*");
  });
  $("textarea").closest("tr").addClass("textarea-row");

});

