if (typeof Effect == 'undefined') 
	throw("accordion.js requires script.aculo.us' effects.js library!");

var accordion = Class.create();
accordion.prototype = {

  current_accordion: undefined,
  show_accordion: undefined,
  animating: undefined,
  
  initialize: function(container,options) {
    
    var th = this;
    
    if( !$(container) ) return;
    
    th.accordion_count = null;
    
    th.options = Object.extend({
			resizeSpeed : 8,
			classNames : {
				toggle : 'toggle',
				toggleActive : 'toggle_active',
				content : 'content'
			},
			direction : 'vertical',
			firstOpen: true,
			Slide: {
        duration: 0.3,
        sync: true
      }
		}, options || {});
		
		th.options.container = container;
		
		var first = true;
		var effects = new Array();
		var url_hash = window.location.hash;
		var hash_anchor = undefined;
		
    $$('.'+th.options.classNames.toggle).each(function(toggle_elm){
      
      toggle_elm.observe('click',th.activate.bindAsEventListener(th,toggle_elm));
      
      var content_elm = toggle_elm.next();
      if( (!th.options.firstOpen && first && !url_hash) || (th.options.firstOpen && !first) || ( first != toggle_elm && url_hash ) ) {
        content_elm.hide();
      }
      
      if( (th.options.firstOpen && first && !url_hash) || first == toggle_elm ) {
        toggle_elm.addClassName(th.options.classNames.toggleActive);
        th.current_accordion = toggle_elm;
        th.show_accordion = toggle_elm;
        first = false;
      }      
      
    });
		
		if( url_hash ) {
      th.go_to_article(undefined,url_hash,th);
    }
    
    $$('a').each(function(elm){
      if( elm.hash && window.location.pathname == elm.pathname ) {
        elm.observe('click',th.go_to_article.bindAsEventListener(th,elm.hash));
      }
    });
		
  },
  
  activate: function(e,accordion) {
    
    var th = this;
    
    if( th.animating ) return;

    var hide_accordion = th.current_accordion;
    
    th.current_accordion = accordion;
    th.show_accordion = accordion;
    
    if( th.show_accordion == hide_accordion ) {
      th.show_accordion = undefined;
      th.current_accordion = undefined;
    } 
    
    var effects = new Array();
    var options = th.options.Slide;
    
    if( hide_accordion ) {
      
      var accordion_content_hide = hide_accordion.next();
      hide_accordion.removeClassName(th.options.classNames.toggleActive);
      
      effects.push(
        new Effect.SlideUp(accordion_content_hide,options)
      );
      
    }
    
    if( th.show_accordion ) {
      
      var accordion_content = th.show_accordion.next();
      th.show_accordion.addClassName(th.options.classNames.toggleActive);
      
      effects.push(
        new Effect.SlideDown(accordion_content,options)
      );
      
    }
    
    new Effect.Parallel(effects,{ 
      duration: 0.4,
      beforeStart: function() {
			  th.animating = true;
		  },
		  afterFinish: function() {
		    th.animating = false;
		  } 
		});
    
  },
  
  go_to_article: function(e,url_hash,th) {
    
    if( !th ) th = this;
    
    if( e ) Event.stop(e);
    
    url_hash = url_hash.replace('#','');
	  var hash_anchor = $$('a[name='+url_hash+']').first();
	  
    var title = hash_anchor.next();
    if( th.current_accordion != title ) {
      th.activate(e,title);
      Element.scrollTo.delay(0.8,hash_anchor);
    } else {
      Element.scrollTo(hash_anchor);
    }
    
    
     
  }
  
}
	