//Author Travis Beck Modified by Gary Evand for Prototype 1.5

var SqueezeBox = Class.create();
SqueezeBox.prototype = {
	
	initialize:function(){
		//	Configuration
		this.toggleBtnClass = 'collapsibleButton';
		this.activeToggleBtnClass = 'active';
		this.collapsibleContainerClass = 'collapsibleContent';
		this.animationTime = 0.5; //the time in seconds for the squeezing and expanding motion (0.3 - 1)
		this.accordianStyle = false; //whether or not opening one section automatically closes all others.
		//  -------------
		
		//  Private Vars
		this.toggleBtnArray = [];
		this.containerArray = [];
		this.activeContainerIndex = 0;
		//  ------------
		
		this.attachEvents();
	},
	
	attachEvents: function(){
		if (!document.getElementsByTagName){ return; }
		this.toggleBtnArray = document.getElementsByClassName(this.toggleBtnClass);
		this.containerArray = document.getElementsByClassName(this.collapsibleContainerClass);
		// loop through all toggle buttons
		var toggleBtn;
		var container;
		var i;
		for (i=this.toggleBtnArray.length-1; i>=0; i-=1){
			toggleBtn = this.toggleBtnArray[i];
			container = this.containerArray[i];
			toggleBtn.index = i;
			//attach the click event to the togglebtn
			if(!(container.hasClassName('expanded'))){
			    container.style.height = "0";
		    }
			toggleBtn.onclick = function(){
			    this.blur();
				mySqueezeBox.pressHandler(this); 
				return false;
			};
		}
	},
	
	pressHandler: function(toggleBtn){
		var container = this.containerArray[toggleBtn.index];
		if(!container.locked){
			container.locked = true;
			mySqueezeBox.toggle(container, toggleBtn);
		}
	},
	
	toggle: function(container, toggleBtn){
		if(toggleBtn.hasClassName(this.activeToggleBtnClass)){
			//this container is active squeeze the hell outta it!
			toggleBtn.removeClassName(this.activeToggleBtnClass);
			this.squeeze(container);
		}else{
			//we need to expand this container and contract any others if we're in accordianStyle
			if(this.accordianStyle){
				this.closeOpenContainers();
			}
			toggleBtn.addClassName(this.activeToggleBtnClass);
			this.expand(container);
		}
	},
	
	squeeze: function(container){
		var height = container.getHeight();
		var slideIn = new Effect.Scale(container, 0,{ 
			duration: this.animationTime, 
			scaleContent: false, 
			scaleX: false, 
			scaleY: true, 
			afterFinish:function(){container.locked = false;}
		});
	},
	
	expand: function(container){
		var contentHeight = Number(this.getContentHeight(container));
		container.style.height = 1 + "px";
		var slideOut = new Effect.Scale(container, 100, {
			duration: this.animationTime, 
			scaleMode: {originalHeight: contentHeight},
			scaleFrom: 1,
			scaleContent: false, 
			scaleX: false, 
			scaleY: true, 
			afterFinish:function(){container.locked = false;}
		});
	},
	
	getContentHeight: function(container){
		container.cleanWhitespace();
		var heightArray = [];
		var children = container.childNodes;
		var child;
		for(var i = 0; i<children.length; i++){
			child = children[i];
			heightArray.push(child.getHeight());
		}
		return(Math.max(heightArray.toString()));
	},
	
	closeOpenContainers: function(){
		var toggleBtn;
		var container;
		for(var i = 0; i<this.toggleBtnArray.length; i++){
			toggleBtn = this.toggleBtnArray[i];
			if(toggleBtn.hasClassName(this.activeToggleBtnClass)){
				//this container is active squeeze the hell outta it!
				toggleBtn.removeClassName(this.activeToggleBtnClass);
				container = this.containerArray[toggleBtn.index];
				this.squeeze(container);
			}
		}
	},
	
	expandAll: function(){
		var container;
		var toggleBtn;
		for(var i = 0; i<this.toggleBtnArray.length; i++){
			toggleBtn = this.toggleBtnArray[i];
			if(!toggleBtn.hasClassName(this.activeToggleBtnClass)){
				//this container is active squeeze the hell outta it!
				toggleBtn.addClassName(this.activeToggleBtnClass);
				container = this.containerArray[toggleBtn.index];
				this.expand(container);
			}
		}
	}
	
};


var mySqueezeBox;
Event.onDOMReady(function() {
	mySqueezeBox = new SqueezeBox(); 
});

// ---------------------------------------------------
