var BlogArchive = {
  heading: null,
  month: null,
  createHandler: function( type, trigger, htmlElement ) {
    if ( 'month' == type ) {
      this.month = new BlogArchiveMonth( trigger, htmlElement );
    } else {
      this.heading = new BlogArchiveHeading( trigger, htmlElement );
    }
  },
  hideExcept: function( handler ) {
    if ( handler == this.heading )
      this.month.hide();
    else
      this.heading.hide();
  }
}

function BlogArchivePanel( htmlElement, parentHandler ) {
  this.domNode = htmlElement;
  this.parent = parentHandler;
  this.trigger = null;
  this.content_panel = null;

  var tmp = htmlElement.getElementsByTagName( 'a' );
  for( var i=0; i<tmp.length; i++ )
    if ( tmp[ i ].className.indexOf( 'trigger' ) != -1 ) {
      this.trigger = tmp[ i ];
      break;
    }
  if ( this.domNode.tagName == 'ul' ) {
    this.content_panel = this.domNode;
  } else {
    tmp = htmlElement.getElementsByTagName( 'ul' );
    if ( tmp.length > 0 )
      this.content_panel = tmp[ 0 ];
  }

  this.collapsed = ( !isNull( this.trigger ) )
    ? ( this.trigger.className.indexOf( 'selected' ) == -1 ) : false;

  this.onTriggerClick = function( e ) {
    DOMEvent.preventDefault( e );

    if ( isNull( this.content_panel ) ) {
      this.parent.request( getXmlText( this.trigger ), Delegate.create( this, 'onXmlLoad' ) );
    } else {
      if ( this.collapsed ) {
        this.parent.collapseExcept( this );
        this.expand();
      } else {
        this.collapse();
      }
    }
  }
  this.onXmlLoad = function( xmlResponse ) {
    var items = xmlResponse.xml.getElementsByTagName( 'item' );
    if ( items.length > 0 ) {
      if ( isNull( this.content_panel ) )
        this.content_panel = document.createElement( 'ul' );
      this.content_panel.style.display = 'none';
      for( var i=0; i<items.length; i++ ) {
        var li = document.createElement( 'li' );
        var anchor = document.createElement( 'a' );
        var href = items[ i ].getAttribute( 'href' );
        if ( !isNull( href ) )
          anchor.href = href;
        anchor.innerHTML = '<span>&gt;</span> ' + items[ i ].getAttribute( 'label' );
        li.appendChild( anchor );
        this.content_panel.appendChild( li );
        this.content_panel.appendChild( document.createTextNode( '\n' ) );
      }
      if ( this.domNode != this.content_panel )
        this.domNode.appendChild( this.content_panel );
    }
    this.parent.collapseExcept( this );
    this.expand();
  }

  this.collapse = function() {
    if ( !isNull( this.content_panel ) )
      this.content_panel.style.display = 'none';
    this.collapsed = true;
    this.toggleState();
  }
  this.expand = function() {
    if ( !isNull( this.content_panel ) )
      this.content_panel.style.display = 'block';
    this.collapsed = false;
    this.toggleState();
  }

  this.toggleState = function() {
    if ( !isNull( this.trigger ) ) {
      this.trigger.className = 'trigger';
      if ( !this.collapsed )
        this.trigger.className += ' selected';
    }
  }

  if ( !isNull( this.trigger ) )
    addListener( this.trigger, 'click', Delegate.create( this, 'onTriggerClick' ) );

  this.show = function() {
    this.domNode.style.display = 'block';
  }
  this.hide = function() {
    this.domNode.style.display = 'none';
  }
}
function BlogArchiveMonth( trigger, htmlElement ) {
  this.domNode = htmlElement;
  this.trigger = trigger;

  this.panels = new Array();

  var panels = htmlElement.getElementsByTagName( 'div' );
  for( var i=0; i<panels.length; i++ )
    if ( panels[ i ].className.indexOf( 'result_panel' ) != -1 )
      this.panels.push( new BlogArchivePanel( panels[ i ], this ) );

  this.active = ( this.panels.length > 0 );

  this.show = function() {
    for( var i=0; i<this.panels.length; i++ )
      this.panels[ i ].show();
    this.active = true;
    this.trigger.className = 'selected';
  }
  this.hide = function() {
    for( var i=0; i<this.panels.length; i++ )
      this.panels[ i ].hide();
    this.active = false;
    this.trigger.className = '';
  }

  this.collapseExcept = function( panel ) {
    for( var i=0; i<this.panels.length; i++ )
      if ( this.panels[ i ] != panel && !this.panels[ i ].collapsed )
        this.panels[ i ].collapse();
  }

  this.request = function( variable, callback ) {
    var cn = new Remote();
    cn.uri = BASE_URL + 'xml/archivesMois/';
    if ( !isNull( variable ) )
      cn.uri += variable;
  	cn.registerListener( 'onLoad', { handleEvent: function( eventType, eventSource, xmlResponse ) {
  	  callback.call( this, xmlResponse );
  	} } );
    try {
    	cn.send( 'post' );
  	} catch( e ) {
  	}
  }

  this.onXmlLoad = function( xmlResponse ) {
    BlogArchive.hideExcept( this );
    var items = xmlResponse.xml.getElementsByTagName( 'item' );
    for( var i=0; i<items.length; i++ ) {
      var div = document.createElement( 'div' );
      var h4 = document.createElement( 'h4' );
      div.className = 'result_panel';
      var anchor = document.createElement( 'a' );
      anchor.href = '#';
      anchor.className = 'trigger';
      anchor.appendChild( document.createTextNode( items[ i ].getAttribute( 'label' ) ) );
      h4.appendChild( anchor );
      div.appendChild( h4 );
      this.domNode.appendChild( div );
      this.panels.push( new BlogArchivePanel( div, this ) );
    }
    this.show();
  }

  this.onTriggerClick = function( e ) {
    DOMEvent.preventDefault( e );

    if ( this.active ) {
      this.hide();
    } else {
      if ( 0 == this.panels.length ) {
        this.request( null, Delegate.create( this, 'onXmlLoad' ) );
      } else {
        BlogArchive.hideExcept( this );
        this.show();
      }
    }
  }

  addListener( this.trigger, 'click', Delegate.create( this, 'onTriggerClick' ) );
}
function BlogArchiveHeading( trigger, htmlElement ) {
  this.domNode = htmlElement;
  this.trigger = trigger;

  this.panel = null;
  var panels = htmlElement.getElementsByTagName( 'ul' );
  for( var i=0; i<panels.length; i++ )
    if ( panels[ i ].className.indexOf( 'result_panel' ) != -1 ) {
      this.panel = new BlogArchivePanel( panels[ i ], this );
      break;
    }

  this.active = !isNull( this.panel );

  this.show = function() {
    if ( !isNull( this.panel ) )
      this.panel.show();
    this.active = true;
    this.trigger.className = 'selected';
  }
  this.hide = function() {
    if ( !isNull( this.panel ) )
      this.panel.hide();
    this.active = false;
    this.trigger.className = '';
  }

  this.request = function( variable, callback ) {
    var cn = new Remote();
  	cn.uri = BASE_URL + 'xml/archivesRubrique/';
  	cn.registerListener( 'onLoad', { handleEvent: function( eventType, eventSource, xmlResponse ) {
  	  callback.call( this, xmlResponse );
  	} } );
  	cn.send( 'post' );
  }

  //  Il n'y a qu'un seul panneau
  this.collapseExcept = function( panel )
  {}

  this.onXmlLoad = function( xmlResponse ) {
    var panel = document.createElement( 'ul' );
    this.domNode.appendChild( panel );
    this.panel = new BlogArchivePanel( panel, this );
    this.panel.onXmlLoad( xmlResponse );
    BlogArchive.hideExcept( this );
    this.show();
  }

  this.onTriggerClick = function( e ) {
    DOMEvent.preventDefault( e );

    if ( this.active ) {
      this.hide();
    } else {
      if ( isNull( this.panel ) ) {
        this.request( null, Delegate.create( this, 'onXmlLoad' ) );
      } else {
        BlogArchive.hideExcept( this );
        this.show();
      }
    }
  }

  addListener( this.trigger, 'click', Delegate.create( this, 'onTriggerClick' ) );
}

addListener( window, 'load', function() {
  var archive = document.getElementById( 'archives' );
  var archive_selectors = document.getElementById( 'type_selectors' );
  if ( !isNull( archive_selectors ) ) {
    var anchors = archive_selectors.getElementsByTagName( 'a' );
    BlogArchive.createHandler( 'heading', anchors[ 0 ], archive );
    BlogArchive.createHandler( 'month', anchors[ 1 ], archive );
  }
} );