/*
 *      jquery.akordion.js
 *      
 *      Copyright 2011 Ismael Rodríguez <ismael.rodriguez@premper.com>
 *      
 *      This program is free software; you can redistribute it and/or modify
 *      it under the terms of the GNU General Public License as published by
 *      the Free Software Foundation; either version 2 of the License, or
 *      (at your option) any later version.
 *      
 *      This program is distributed in the hope that it will be useful,
 *      but WITHOUT ANY WARRANTY; without even the implied warranty of
 *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *      GNU General Public License for more details.
 *      
 *      You should have received a copy of the GNU General Public License
 *      along with this program; if not, write to the Free Software
 *      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 *      MA 02110-1301, USA.
 */

(function($){
  $.fn.akordion = function(options){
    var defaults = {
      containersClass:        "",          
      activatorVisibleClass:  "opened",
      activatorVisibleText:   "Ocultar detalles",
      activatorHiddenClass:   "closed",
      activatorHiddenText:    "Mostrar detalles"
    }
    var $settings   = $.extend({}, defaults, options);
    var $activators = new Array();
    var $counter    = 0;
    this.each(function(){
      $(this).addClass($settings.activatorHiddenClass);
      $activators[$counter] = $(this);
      $counter++;
    });
    $("." + $settings.containersClass).hide();
    this.click(function(){
      $activator = $(this);    
      $("." + $settings.containersClass + ":visible").slideToggle("fast");
      for(var i = 0 ; i < $activators.length ; i++){
        //$activators[i].html($settings.activatorHiddenText);
        $activators[i].removeClass($settings.activatorVisibleClass + " " + $settings.activatorHiddenClass);
        $activators[i].addClass($settings.activatorHiddenClass);
      }
      $(this).parent().nextAll("." + $settings.containersClass + ":first:hidden").slideToggle(
        "fast",
        function(){
          //$activator.html($settings.activatorVisibleText);
          $activator.removeClass($settings.activatorVisibleClass + " " + $settings.activatorHiddenClass);
          $activator.addClass($settings.activatorVisibleClass);
        }
      );
      return this;
    });
  }
})(jQuery);

