var SqueezeBox = new Class({
   Implements: [Options, Events],
   options: {
      toggleBtnClass: 'collapsibleButton',
      activeToggleBtnClass: 'active', 
      collapsibleContainerClass: 'collapsibleContent', 
      animationTime: 0.5, 
      accordianStyle: false
   },
	
	initialize:function(options){
		//	Configuration
		this.setOptions(options);
		this.options.toggleBtnArray = $$('.'+this.options.toggleBtnClass);
		this.options.containerArray = $$('.'+this.options.collapsibleContainerClass);
      if(this.options.toggleBtnArray.length < 1){return false;}//do nothing
		this.options.activeContainerIndex = 0;
		//  ------------
		this.attachEvents();
	},
	
	attachEvents: function(){
		var container;
		var i;
		var that = this;
		this.options.toggleBtnArray.each(function(toggleBtn, i){
			container = that.options.containerArray[i];
			container.setStyle('overflow', 'hidden');
			toggleBtn.index = i;
			//attach the click event to the togglebtn
         if(!(container.hasClass('expanded'))){
            container.origHeight = container.getSize().y;
            container.setStyle('height','0');
         }
			toggleBtn.addEvent("click", that.pressHandler.bindWithEvent(that));
		});
	},
	
	pressHandler: function(e){
	   e.stop();
	   var toggleBtn = $(e.target);
		var container = this.options.containerArray[toggleBtn.index];
		if(!container.locked){
			container.locked = true;
			this.toggle(container, toggleBtn);
		}
	},
	
	toggle: function(container, toggleBtn){
		if(toggleBtn.hasClass(this.options.activeToggleBtnClass)){
			//this container is active squeeze the hell outta it!
			toggleBtn.removeClass(this.options.activeToggleBtnClass);
			this.squeeze(container);
		}else{
			//we need to expand this container and contract any others if we're in accordianStyle
			if(this.options.accordianStyle){
				this.closeOpenContainers();
			}
			toggleBtn.addClass(this.options.activeToggleBtnClass);
			this.expand(container);
		}
	},
	
	squeeze: function(container){
		var height = container.getSize().y;
		container.setStyle('overflow', 'hidden');
		container.set('tween', {
		   onComplete: function(){
		      container.locked = false;
		   }
		});
		container.tween('height', 0);
	},
	
	expand: function(container){
		var contentHeight = container.origHeight;
		container.style.height = 1 + "px";
		container.set('tween', {
		   onComplete: function(){
		      container.locked = false;
		      container.removeProperty('style');
		   }
		});
		container.tween('height', contentHeight);
	},
	
	getContentHeight: function(container){
		var heightArray = [];
		var children = container.childNodes;
		var child;
		for(var i = 0; i<children.length; i++){
			child = children[i];
			heightArray.push(child.getSize().y);
		}
		return(Math.max(heightArray.toString()));
	},
	
	closeOpenContainers: function(){
		var toggleBtn;
		var container;
		for(var i = 0; i<this.options.toggleBtnArray.length; i++){
			toggleBtn = this.toggleBtnArray[i];
			if(toggleBtn.hasClass(this.options.activeToggleBtnClass)){
				//this container is active squeeze the hell outta it!
				toggleBtn.removeClass(this.options.activeToggleBtnClass);
				container = this.containerArray[toggleBtn.index];
				this.squeeze(container);
			}
		}
	},
	
	expandAll: function(){
		var container;
		var toggleBtn;
		for(var i = 0; i<this.options.toggleBtnArray.length; i++){
			toggleBtn = this.toggleBtnArray[i];
			if(!toggleBtn.hasClass(this.options.activeToggleBtnClass)){
				//this container is active squeeze the hell outta it!
				toggleBtn.addClass(this.options.activeToggleBtnClass);
				container = this.containerArray[toggleBtn.index];
				this.expand(container);
			}
		}
	}
	
});
