jQuery(document).ready( function($) {
  // Collapsible divs
  $('.collapsible').addClass('collapsed').each( function() {
    $(this).children(':not(h3:eq(0))').wrapAll('<div class="collapsible-wrapper"></div>').end()
          .children('.collapsible-wrapper').hide().end()
          .children('h3').css({cursor:'pointer'})
            .click( function() {
              $(this).siblings('.collapsible-wrapper').slideToggle();
              $(this).parent().toggleClass('collapsed');
            });
    $(this).children('h3').prependTo($(this)); /* for IE6 */
  });
  
  
  
  // form labels
  $('.form-item input, .form-item textarea').each( function() {
    if ( $(this).val() != '' ) {
      $(this).css({zIndex: 15})
            .parents('.form-item').find('label').hide(); // this is for the benefit of IE7
    }
  });
  $('label').click( function() {
    $(this).hide().parent().find('input, textarea').css({zIndex: 15}).focus();
  });
  $('.form-item input, .form-item textarea').focus( function() {
    $(this).css({zIndex: 15})
            .parents('.form-item').find('label').hide(); // this is for the benefit of IE7
  });
  $('.form-item input, .form-item textarea').blur( function() {
    if ( $(this).val() == '' ) {
      $(this).css({zIndex: 5})
            .parents('.form-item').find('label').show(); // this is for the benefit of IE7
    }
  });
  
  //Repeat the above for comment form -- easier than adjusting the HTML
  $('#commentform input, #commentform textarea').each( function() {
    if ( $(this).val() != '' ) {
      $(this).css({zIndex: 15})
            .parents('p').find('label').hide(); // this is for the benefit of IE7
    }
  });
  $('#commentform label').click( function() {
    $(this).hide().parent().find('input, textarea').css({zIndex: 15}).focus();
  });
  $('#commentform input, #commentform textarea').focus( function() {
    $(this).css({zIndex: 15})
            .parents('p').find('label').hide(); // this is for the benefit of IE7
  });
  $('#commentform input, #commentform textarea').blur( function() {
    if ( $(this).val() == '' ) {
      $(this).css({zIndex: 5})
            .parents('p').find('label').show(); // this is for the benefit of IE7
    }
  });
  
  $('#comments').addClass('collapsible').addClass('collapsed')
          .children(':not("#comments-title")').wrapAll('<div class="collapsible-wrapper"></div>')
          .parent().hide();
  $('#comments.collapsible h2').click( function() {
    $(this).siblings('.collapsible-wrapper').slideToggle();
    $(this).parent().toggleClass('collapsed');
  });
  
  $("#sidebar .sub-menu-wrapper .current_page_item, #sidebar .sub-menu-wrapper .current_page_parent, #sidebar .sub-menu-wrapper .current_page_ancestor")
          .addClass('selectedLava').children('a').css({background: 'transparent'});
  $("#sidebar .sub-menu-wrapper li").css({position: 'relative', zIndex: 20});
  if ( $('#sidebar .sub-menu-wrapper .selectedLava').length > 0 ) {
    $("#sidebar .sub-menu-wrapper").lavaLamp({
      startItem: 0
    }) ;
  }
  
  
  $('.gallery .thumbnail img').fadeTo(0, 0.55);
  $('.gallery .gallery-item:first-child').addClass('active').find('.thumbnail img').fadeTo(0, 1);
  $('.gallery .thumbnail img').click( function() {
    if ( !$(this).parents('.gallery-item').hasClass('active') ) {
      $(this).parents('.gallery').find('.gallery-item.active')
              .removeClass('active')
              .find('.thumbnail img').fadeTo(250, 0.55).end()
              .find('.large').fadeOut(250);
      $(this).fadeTo(250, 1)
              .parents('.gallery-item').addClass('active')
              .find('.large').fadeTo(250, 1);
    }
  });
});