var activeTransitions = new Array();
var speed = 1.0;

function showMenu(m) {
	var t = 'menu_'+m;
	if (!activeTransition(t)) {
		activeTransition(t, true);
		
		var state = $('menustate_'+m).innerHTML;
		switch (state) {
			case '+':
				$('menustate_'+m).innerHTML = '-';
				$('submenu_'+m).blindDown({duration:speed});
				break;
			case '-':
				$('menustate_'+m).innerHTML = '+';
				$('submenu_'+m).blindUp({duration:speed});
				break;
		}
		activeTransition.delay(speed, t, false);
	}
}


function activeTransition(m, t) {
	t = typeof(t) != 'undefined' ? t : 'none';
	if (t != 'none') {
		activeTransitions[m] = t;
	}
	return activeTransitions[m];
}

function initBoxes() {
	var boxes 		= $$('div.expandable');
	
	boxes.each(function(b) {
		var box		= b;
		var bar		= b.select('h1')[0];			//clickable header bar to expand box
		var img  	= b.select('img.box_banner');	//box banner will always be the first img
		if (img != 'undefined' && img != null) {
			img = img[0];
		} else {
			img = false;
		}
		var state 	= b.select('span.state')[0]; 	//state: + or -
		var inners = new Array();					//expandable content sections in contentbox
		//we only want direct descendents as there may be other internal boxes
		var children = b.childElements();
		children.each(function(c) {
			if (c.hasClassName('inner')) {
				inners[inners.length] = c;
			}
		});
		
		//hide all contentbox inner sections
		if (box.hasClassName('contentbox')) {
			inners.each(function(i) {
				i.style.display = 'none';
			});
		}
		
		//add onclick event
		bar.observe('click', function(bar) {
			if (!box.hasClassName('activeTransition')) {
				box.addClassName('activeTransition');
				switch (state.innerHTML) {
					case '+':
						state.innerHTML = '-';
						inners.each(function(i) {
							i.blindDown({duration:speed});
						});
						break;
					case '-':
						state.innerHTML = '+';
						inners.each(function(i) {
							i.blindUp({duration:speed});
						});
						break;
				}
				removeActiveTransition.delay(speed, box); //stop double clicking
			}
			bar.stop();
			return false;
		});
		
		if (img) {
			//add onclick event
			img.observe('click', function(img) {
				if (!box.hasClassName('activeTransition')) {
					box.addClassName('activeTransition');
					switch (state.innerHTML) {
						case '+':
							state.innerHTML = '-';
							inners.each(function(i) {
								i.blindDown({duration:speed});
							});
							break;
						case '-':
							state.innerHTML = '+';
							inners.each(function(i) {
								i.blindUp({duration:speed});
							});
							break;
					}
					removeActiveTransition.delay(speed, box); //stop double clicking
				}
				img.stop();
				return false;
			});
		}
		
	});
}


function removeActiveTransition(e) {
	e.removeClassName('activeTransition');
}

Event.observe(window, 'load', function() {
	initBoxes();
});

