/**
 * Toggler.js - Collapse and expand items
 * 
 * @author  Webstores <info at webstores dot nl>
 *         Copyright (c) Webstores internet totaalbureau <http://www.webstores.nl/>
 * 
 * @param {Mixed} el The CSS selector or object reference of the toggler element
 * @param {Object} options Optional parameters
 */
function Toggler(el, options) {
        this.el = $(el);
        this.options = options;
        this.construct();
};
 
Toggler.prototype = {
        /**
         * @constructor
         */
        construct: function() {
               if(this.el.length) {
                       var self = this;
                       
                       this.options = $.extend({
                               togglerClass: 'toggler',
                               collapsedClass: 'collapsed',
                               expandedClass: 'expanded',
                               allowMultiple: true,
                               genericBehavior: false,
                               onReady: null,
                               onBeforeCollapse: null,
                               onAfterCollapse: null,
                               onBeforeExpand: null,
                               onAfterExpand: null
                       }, this.options || {});
                       
                       $('.' + this.options.togglerClass, this.el).each(function() {
                               var toggler = $(this);
                               var item = $(toggler.attr('href'));
                               
                               if(item.attr('id') == window.location.hash.split('#')[1] || item.hasClass(self.options.expandedClass)) {
                                      self.expand(item);
                               }
                               else {
                                      self.collapse(item);
                               }
                               
                               toggler.click(function(e) {
                                      e.preventDefault();
                                      self.togglerClickHandler(item);
                               });
                               
                               // Simulate generic OS dropdown behavior
                               if(self.options.genericBehavior) {
                                      
                                      // Collapse expanded item and expand mouseover item
                                      toggler.mouseover(function() {
                                              var expandedItem = $('.' + self.options.expandedClass, self.el).get(0);
                                              
                                              if(expandedItem) {
                                                     self.collapse(expandedItem);
                                                     self.expand(item);
                                              }
                                      });
                               }
                       });
                       
                       // Simulate generic OS dropdown behavior
                       if(this.options.genericBehavior) {
                               this.options.allowMultiple = false;
                               
                               // Hide expanded item on document mousedown
                               $(document).mousedown(function(e) {
                                      var target = e.target;
                                      
                                      while(target) {
                                              if(target == $('.' + self.options.expandedClass, self.el).get(0)) {
                                                     return;
                                              }
                                              
                                              target = target.parentNode;
                                      }
                                      
                                      $('.' + self.options.expandedClass, self.el).each(function() {
                                              self.collapse(this);
                                      });
                               });
                               
                               // Hide expanded item on escape keydown
                               $(document).keydown(function(e) {
                                      if(e.keyCode == 27) {
                                              $('.' + self.options.expandedClass, self.el).each(function() {
                                                     self.collapse(this);
                                              });
                                      }
                               });
                       }
                       
                       if(typeof this.options.onReady == 'function') {
                               this.options.onReady(this.el);
                       }
               }
        },
        
        /**
         * When a toggler is clicked
         * 
         * @param {Object} toggler The toggler object
         */
        togglerClickHandler: function(item) {
               this.isExpanded(item) ? this.collapse(item) : this.expand(item);
        },
        
        /**
         * Is an item expanded?
         * 
         * @param {Mixed} item The CSS selector or object reference of the toggler
         */
        isExpanded: function(item) {
               return $(item).hasClass(this.options.expandedClass);
        },
        
        /**
         * Collapses an item
         * 
         * @param {Object} item The CSS selector or object reference of the toggler
         */
        collapse: function(item) {
               if(typeof this.options.onBeforeCollapse == 'function') {
                       this.options.onBeforeCollapse(item);
               }
               
               $(item).removeClass(this.options.expandedClass);
               $(item).addClass(this.options.collapsedClass);
               
               if(typeof this.options.onAfterCollapse == 'function') {
                       this.options.onAfterCollapse(item);
               }
        },
        
        /**
         * Expands an item
         * 
         * @param {Object} item The CSS selector or object reference of the toggler
         */
        expand: function(item) {
               var self = this;
               
               if(typeof this.options.onBeforeExpand == 'function') {
                       this.options.onBeforeExpand(item);
               }
               
               if(!this.options.allowMultiple) {
                       $('.' + this.options.expandedClass, this.el).each(function() {
                               self.collapse(this);
                       });
               }
               
               $(item).removeClass(this.options.collapsedClass);
               $(item).addClass(this.options.expandedClass);
               
               if(typeof this.options.onAfterExpand == 'function') {
                       this.options.onAfterExpand(item);
               }
        }
};
 


