var cbpTimeout;
var cbpInterval=50;
var cbpObjects=[];
var sanityCheck = 0;

function cbp_Object(obj){
	//public properties
	this.id = obj.id;
	this.hLink = obj.firstChild.firstChild.firstChild.firstChild.firstChild;
	this.container = obj.firstChild.nextSibling.firstChild;	//class="cbpContainer1"
	this.body = this.container.firstChild.firstChild;		//class="cbpContent" or class="cbpScrollContent"
	this.animationState = 0;	//0 = static, 1 = expanding, -1 = collapsing
	this.animationRate = .5;

	//this.onOpenClick = null;
	//this.onCloseClick = null;
	//this.onOpened = null;
	//this.onClosed = null;
	//this.targetHeight = null;
	//this.groupName = null;
	
	//initialize prototypes only once
	if (typeof cbp_Object._initialized == 'undefined'){
		cbp_Object.prototype.getTitle = function(){return this.hLink.firstChild.innerHTML;}
		cbp_Object.prototype.setTitle = function(newTitle){this.hLink.firstChild.innerHTML = newTitle;}

		cbp_Object.prototype.getBodyText = function(){	return this.body.innerHTML;}
		cbp_Object.prototype.setBodyText = function(html){this.body.innerHTML = html;}
		cbp_Object.prototype.appendBodyText = function(html){this.body.innerHTML += html;}

		cbp_Object.prototype.getHeight = function(){
			if (this.container.style.height) return parseInt(this.container.style.height);
			if (this.container.offsetHeight) {
				var saveBodyState = this.body.style.display;
				this.body.style.display = 'block';
				this.container.style.height = this.container.offsetHeight + 'px';
				this.body.style.display = saveBodyState;
			}
			return parseInt(this.container.style.height);
		}
		cbp_Object.prototype.setHeight = function(height){
			if (parseInt(height) == 'NaN') {
				this.container.style.height = height;
			} else {
				this.container.style.height = height + 'px';
			}
		}
		
		cbp_Object.prototype.getContentHeight = function(){
			if (this.contentHeight) return this.contentHeight;

			var saveContentState = this.container.style.display;
			var saveBodyState = this.body.style.display;
			var saveContentHeight = this.container.style.height;
			
			this.container.style.display = 'block';
			this.container.style.height = '';
			this.body.style.display = 'block';
			
			var h = this.container.offsetHeight;
			
			this.container.style.display = saveContentState;
			this.body.style.display = saveBodyState;
			this.container.style.height = saveContentHeight;
			
			this.contentHeight = h;
			
			//if (!this.container.style.height && this.container.style.display != 'none') this.container.style.height = h + 'px';
			
			return h;
		}

		cbp_Object.prototype.expand = function(){
			if (this.animationState == 1 || (this.animationState == 0 && this.getState() == 'open')) return;
			this.animationState = 1;
			this.container.style.overflow = 'hidden';
			this.container.style.display = 'block';
			this.body.style.display = 'none';
			this.targetHeight = this.getContentHeight();

			sanityCheck = 0;
			cbp_Animate();
		}
		cbp_Object.prototype.collapse = function(){
			if (this.animationState == -1 || (this.animationState == 0 && this.getState() == 'closed')) return;
			this.animationState = -1;
			this.container.style.overflow = 'hidden';
			this.container.style.display = 'block';
			this.body.style.display = 'none';
			this.targetHeight = 0;

			sanityCheck = 0;
			cbp_Animate();
		}
		
		cbp_Object.prototype.getState = function(){return this.hLink.nextSibling.value;}
		cbp_Object.prototype.setState = function(state){
			switch (state){
				case 'open':
					this.hLink.className = 'open';
					this.body.style.display = 'block';
					this.container.style.display = 'block';
					this.container.style.height = '';
					this.container.style.overflow = 'visible';
					if (this.onOpened) this.onOpened(this);
					break;
				case 'closed':
					this.hLink.className = 'closed';
					this.container.style.display = 'none';
					if (this.onClosed) this.onClosed(this);
					break;
			}
			this.hLink.nextSibling.value = state;
			this.animationState = 0;
			
			//make panel visible if page scrolls - kinda clunky
			//window.location = '#' + this.id;
			//this.hLink.blur();
		}
		
		cbp_Object._initialized = true;
	}
}
function cbp_Animate(){
	sanityCheck++;
	if (sanityCheck > 100) {
		clearTimeout(cbpTimeout);
		alert('oops');
		displayStack();
		return;
	}
	
	if (cbpTimeout) clearTimeout(cbpTimeout);
	var animating = false;
	
	for (var i = 0;i < cbpObjects.length;i++) {
		if (cbpObjects[i] && cbpObjects[i].animationState != 0){
			var panel = cbpObjects[i];
			var h = panel.getHeight();
			var target = panel.targetHeight;
			var diff = Math.ceil(Math.abs(target - h) * panel.animationRate);
	
			panel.setHeight(h + panel.animationState * diff);
			
			if (panel.getHeight() != target) {
				animating = true;
			}	else if (panel.animationState == 1){
				panel.setState('open');
			} else {
				panel.setState('closed');
			}

		}
	}
	if (animating) cbpTimeout = setTimeout('cbp_Animate()',cbpInterval);
}

//gets panel from stack; if not found, returns new panel after adding to stack
function cbp_getPanel(id){
	for (var i = 0;i < cbpObjects.length;i++) {
		if (cbpObjects[i] && cbpObjects[i].id == id) return cbpObjects[i];
	}
	if (!document.getElementById) return;
	
	var obj = document.getElementById(id);
	if (!obj) return;
	var panel = new cbp_Object(obj);
	cbpObjects.push(panel);
	
	return panel;
}
function cbp_Toggle(obj){
	obj.blur();
	//get panel object
	var panel = cbp_getPanel(obj.parentNode.parentNode.parentNode.parentNode.parentNode.id);
	if (!panel) return;
	if (panel.groupName) cbp_collapseGroup(panel.groupName);
	if (panel.getState() == 'open') {
		if (panel.onCloseClick) panel.onCloseClick(panel);
		panel.collapse();
	} else {
		if (panel.onOpenClick) panel.onOpenClick(panel);
		panel.expand();
	}
}

//panel grouping---------------------------------------------
function cbp_setPanelGroup(id,groupName,selected) {
	var panel = cbp_getPanel(id);
	panel.groupName = groupName;
	if (selected) {
		panel.expand();
	} else {
		panel.collapse();	
	}
}

function cbp_collapseGroup(groupName){
	for (var i = 0;i < cbpObjects.length;i++) {
		if (cbpObjects[i]) {
			var panel = cbpObjects[i];
			if (panel.groupName == groupName) panel.collapse();
		}
	}
}
