/* ----------------------------------------------------------------
 @name:     collapser - open and close boxes
 @version:  0.4
 @release:  2010-08-01
 @type:     jQuery plugin
 @author:   Jan Panschab
---------------------------------------------------------------- */

$(function() {

  $.fn.collapser = function(options) {
    var opts = $.extend({}, $.fn.collapser.defaults, options),
        selector = this.selector;

    return this.each(function() {

      var $box = $(this),
          boxPosition = opts.triggerPosition == 'prev' ? 'next' : 'prev',
          boxHeight = $box.height(),
          $trigger,
          $openBox,
          $hashBox;
      
      // collapser inside other collapser
      if (boxHeight === 0 && $box.parents('.collapser').length) {
        $box.parents('.collapser').show();
        boxHeight = $box.height();
        $box.parents('.collapser').hide();
      }
      $box.data('height', boxHeight);
      
      if (opts.trigger == '') {
        $trigger = $box[opts.triggerPosition](opts.triggerClass);
      } else { // generated
        var triggerTo = opts.triggerPosition == 'prev' ? 'before': 'after';
        $box[triggerTo]($(opts.trigger));
        $trigger = $box[opts.triggerPosition]();
      }

      // inicializace
      $box.hide().css({'height': 0}); // všechno se zavře
      $trigger.css({ 'cursor': 'pointer' }).addClass(opts.triggerCloseClass);
      if (opts.openOnLoad) { // otevře se box podle selectoru
        $openBox = $box.filter(opts.openOnLoad);
        $openBox.css('height', $openBox.data('height') + 'px').show()[opts.triggerPosition](opts.trigger).removeClass(opts.triggerCloseClass).addClass(opts.triggerOpenClass);
      }
      if (document.location.hash) { // otevře se hash z adresy
        $hashBox = $(document.location.hash);
        if ($hashBox.data('height') !== undefined) {
          $hashBox
            .css('height', $hashBox.data('height') + 'px')
            .show()
            [opts.triggerPosition](opts.trigger)
            .removeClass(opts.triggerCloseClass)
            .addClass(opts.triggerOpenClass);
        }
      }

      // event
      $trigger.click(function(e) {
        e.preventDefault();
        var $thisTrigger = $(this),
            $groupTriggers = $(selector)[opts.triggerPosition](),
            $thisBox = $thisTrigger[boxPosition](),
            thisBoxHeight = $thisBox.data('height');
        if (opts.autoClose) { // automaticky se zavírají ostatní boxy
          $groupTriggers.removeClass(opts.triggerOpenClass).addClass(opts.triggerCloseClass).each(function() {
            $(this)[boxPosition]().not($thisBox).animate({height: 0}, opts.duration, opts.easing, function() {
              $(this).hide();
            });
          });
        }
        if ($thisBox.is(':visible')) { // close
          $thisTrigger.removeClass(opts.triggerOpenClass).addClass(opts.triggerCloseClass);
          $thisBox.animate({height: 0}, opts.duration, opts.easing, function() {
            $thisBox.hide();
          });
          opts.onClose.call(this);
        } else { // open
          $thisTrigger.removeClass(opts.triggerCloseClass).addClass(opts.triggerOpenClass);
          $thisBox.show().animate({height: thisBoxHeight + 'px'}, opts.duration, opts.easing);
          opts.onOpen.call(this);
        }
      });

    });
  };
  // collapser defaults
  $.fn.collapser.defaults = {
    triggerClass: '', // [jQuery selector]
    triggerPosition: 'prev', //[prev, next]
    trigger: '', // [jQuery element]
    triggerCloseClass: 'tclose', // [string] třída triggeru při zavřeném boxu
    triggerOpenClass: 'topen', // [string] třída triggeru při otevřeném boxu
    autoClose: false, // [boolean] otevírání jednoho/více boxů
    openOnLoad: false, // [false|jQuery selector] co se otevře při načtení stránky
    duration: 1500, // [string(slow, normal, fast)|number] rychlost otevírání boxů
    easing: 'linear', // [linear, swing, or easing plugin]
    onOpen: function() {}, // [function] triggered after box is opened
    onClose: function() {} // [function] triggered after box is closed
  };

});

