$(function() {
  $('.toggler').togglerize();
  $('a.lightbox').lightBox();
})

jQuery.fn.togglerize = function() {
  
  /*
  
  By: Jordan Isip
  The jQueryUI accordion is an overkill for what we needed...
  Example: http://fixieconsulting.com/portfolio.html

  Markup:
  <div class="toggler active">
    <a href="#fixieshipper" class="aux-toggler">+</a>
    <p>Introduction text</p>

    <div class="accordion">
      <p>Section that is hidden by default</p>
    </div>
  </div>
  
  */
  
  // Hide accordions via JS (not CSS) for accessibility
  if($('.accordion').length == 0){
    return false;
  }
  $('.accordion').hide();
  
  $(this)
    .click( function(){
      if($(this).find('.accordion:hidden').length > 0){
        hideToggler($('.toggler.active'));
        showToggler(this);
      }
    })
    .mouseover( function(){
      $(this).find('a.aux-toggler').addClass('hover');
    })
    .mouseout( function(){
      $(this).find('a.aux-toggler').removeClass('hover');
    });


  function showToggler(toggler) {
    // Set hash to set current toggler on refresh
    window.location.hash = $(toggler).find('a:first').attr('hash');

    $(toggler)
      .addClass('active')
      .find('.accordion:hidden').slideDown('slow')
      .end()
        .find('a.aux-toggler').html('-')
          .one( 'click', function() {
            hideToggler($(this).parents('.toggler.active'));
          });
  }

  function hideToggler(toggler) {
    $(toggler)
      .removeClass('active')
      .find('.accordion:visible').slideUp('slow')
      .end().find('a.aux-toggler').html('+');
  }

  // Set toggler based off of hash
  if(location.hash != ""){
    var toggler = $('a[href='+location.hash+']:first').parents('.toggler');
    var pos = toggler.offset().top - 20;
    $(window).scrollTop(pos)

    showToggler(toggler);
  } else {
    showToggler($('.toggler:first'));
  }

  return this;
}