// qAccordion for JQuery
// by CSantos (csantos@quilogy.com) 2009-apr-13
// - Bookmarkable accordion (based on http://plugins.jquery.com/project/accord).
// - Modified for Ortho 2009-jun-17.
// USAGE:
//   Can be used for ol, ul.
//   <ol id="faqs">
//     <li><a href="#q1" name="q1">Test Question 1?</a>
//       <div>my content 1</div>
//     </li>
//     <li><a href="#q2" name="q2">Test Question 2?</a>
//       <div>my content 2</div>
//     </li>
//   </ol>
//   <script type="text/javascript"> /* <![CDATA[ */
//      $('ol#faqs').qAccordion({activeClass:'active',speed:'normal'});
//   /* ]]> */ </script>
(function($){ 
  $.fn.extend({  
    qAccordion: function(options) {       
		var opts = $.extend({}, $.fn.qAccordion.defaults, options);
    return this.each(function() {
      $.each($(this).find('li.'+opts.hasItemsClass+'>div'), function(){
        $(this).hide();
      });
      $.each($(this).find('li.'+opts.activeClass+'>div'), function(){
        $(this).show();
      });
      $.each($(this).find('li.'+opts.hasItemsClass+'>a'), function(){
        $(this).click(function(e){
          activate(e.target);
          if(opts.setHash=='true') {
            location.hash=this.hash;
          };
          return false;
        });
      });
      if(location.hash) {
        var active = $(this).find('a[href=' + location.hash + ']')[0];
        if(active) {
          activate(active);
        };
      };
      
      function activate(el){
        $(el)['parent']('li').siblings().children('div').slideUp(opts.hideSpeed);
        $(el).siblings('div').slideToggle(opts.speed);
        $(el)['parent']('li').toggleClass(opts.activeClass).siblings().removeClass(opts.activeClass);
      }
    });
    } 
  }); 
	$.fn.qAccordion.defaults = {
		activeClass: 'active'
    ,speed:'normal'
    ,hideSpeed:'normal'
    ,setHash:'false'
    ,hasItemsClass:'hassubitems'
	};
})(jQuery);