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.getParent().getElements('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_news = 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('.titre').each(function(item) {
            if (item.lang == lang_news) {
              item.removeClass('titre_off');
            } else {
              item.addClass('titre_off');
            }
          });


          elementToDisplay = $(newsContainer.id + '-' + lang_news);
          contentDiv = elementToDisplay.getFirst('div');
          //console.log(elementToDisplay);
          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('lang-container-off');
                } else {
                    item.addClass('lang-container-off');
                }
            });
          }
      } 
      
      if ($defined(idx = this.togglers.indexOf(toggler))) {
        if (idx != this.previous) {
          this.display(idx);
        }
      }

    }
    return false;
  }

});

window.addEvent('domready', function() {
	
	//create our Accordion instance
	//myAccordion = new Accordion($('accordion'), 'div.toggler', 'div.element', {
	myAccordion = new kspAccordion($('accordion'), 'div.toggler', 'div.element', {

		opacity: true,
		display: 0, // remplacer par 0 pour afficher la premiere actu au chargement, 1 pour la suivante etc.
		alwaysHide: true,

		onActive: function(toggler, element){
            toggler.addClass('actualite_open');
		},
		onBackground: function(toggler, element){
			toggler.setStyle('background-position', '0px 0px');
            toggler.removeClass('actualite_open');
		}

	});

});

function SwitchPressrel(container, eltfrom, eltto) {
  $(eltfrom).fade(0);
  $(eltto).fade(1);
}
