var myAccordion;

var kspAccordion  = new Class({
  Extends: Accordion,

  initialize: function(container, toggler_class_name, element_class_name, options) { 
    this.parent(container, toggler_class_name, element_class_name, options);
    this.togglers.each(function(toggler, index){
      langTogglers = toggler.getElements('ul.flags a.lang-toggler');
      langTogglers.each(function(anchor, lIndex) {
        anchor.addEvent('click', this.toggleLang.bind(this, [anchor, toggler]));
      }, this);
    }, this);

  },

  toggleLang: function(anchor, toggler) {
    var reg = /a-(news-\d+)-(\w+)/;
    if (matches = anchor.id.match(reg)) {
      newsContainerCssId = matches[1];
      lang = matches[2];

      newsContainer = $(newsContainerCssId);
      if (!anchor.getParent().hasClass('on')) {
          anchor.getParent().getParent().getChildren('li').each(function(item){
              if ( anchor.getParent() == item ) {
                item.addClass('on');
              } else {
                item.removeClass('on');
              }
          });

          toggler.getElements('.title_lnk').each(function(item) {
            if (item.lang == lang) {
              item.removeClass('off');
            } else {
              item.addClass('off');
            }
          });


          elementToDisplay = $(newsContainer.id + '-' + lang);
          contentDiv = elementToDisplay.getFirst('div');
          if (contentDiv) {
            newHeight = contentDiv.getSize()['y'];
            fx = new Fx.Tween(newsContainer);
            fx.start('height', newHeight);
            $$('#' + newsContainer.id + ' .lang-container').each(function(item){
                if (item == elementToDisplay) {
                    item.removeClass('off');
                } else {
                    item.addClass('off');
                }
            });
          }
      } 
      
      if ($defined(idx = this.togglers.indexOf(toggler))) {
        if (idx != this.previous) {
          this.display(idx);
        }
      }

    }
    return false;
  }

});

/* override 1.2.4.4 mootools Fx.Accordion */
var resultsAccordion  = new Class({
  Extends: Fx.Accordion,

  initialize: function(){
    this.options.display = false;
    this.options.initialDisplayFx = true;
    this.parent.apply(this, arguments);
  },

  display: function(index, useFx, toggleAll) {
    if (!this.check(index, useFx)) return this;
    toggleAll = $pick(toggleAll, false);
    useFx = $pick(useFx, true);
    if (this.options.returnHeightToAuto){
      var prev = this.elements[this.previous];
      if (prev && !this.selfHidden){
        for (var fx in this.effects){
          prev.setStyle(fx, prev[this.effects[fx]]);
        }
      } 
    }
    index = ($type(index) == 'element') ? this.elements.indexOf(index) : index;
    if ((this.timer && this.options.wait) || (index === this.previous && !this.options.alwaysHide)) return this; 
    this.previous = index;
    var obj = {};
    this.elements.each(function(el, i){
      obj[i] = {};
      var hide;
      if (i != index) {
        if (toggleAll) {
          if (this.elementIsOpen(el))
          {
            hide = true;
          }
        } else {
          if (!this.elementIsOpen(el))
          {
            hide = true;
          }
        }
      } else {
          if (this.elementIsOpen(el))
          {
            hide = true;
            this.selfHidden = true;
          }
      }
      this.fireEvent(hide ? 'background' : 'active', [this.togglers[i], el]);
      for (var fx in this.effects) obj[i][fx] = hide ? 0 : el[this.effects[fx]];
    }, this);
    this.internalChain.chain(function(){
      if (this.options.returnHeightToAuto && !this.selfHidden){
        var el = this.elements[index];
        if (el) el.setStyle('height', 'auto');
      };
    }.bind(this));
    return useFx ? this.start(obj) : this.set(obj);

  },

  elementIsOpen: function(el) {
    return ((el.offsetHeight > 0 && this.options.height) || (el.offsetWidth > 0 && this.options.width));
  }
});

