window.addEvent('load',function(){
	//loop through array of element in containing div
	$$('#mainContent div.boxContainer').each(function(item,i){
		var eyedee = 'box' + i;
		//dynamically set ID property of containing elements
		item.setProperty('id',eyedee);
		//needed for IE
		item.setStyles({opacity:1,height:0});
	});
	
	//loop through links which expand hidden divs
	$$('div.titleBar a').each(function(item,i){
		//set link id's to use in deep-linking conditional at bottom
		var titleBar = 'title' + i;
		item.setProperty('id',titleBar);
		var box = 'box' + i;
		var question = 'question' + i;
		var height = 276;
		if ($(question)){
			height = $(question).getStyle('height').toInt();
		}
		//add click event to each link
		item.addEvent('click',function(e){
			e = new Event(e).stop();
			var effect;
			//test for state of div, expanded or collapsed
			if($(box).getStyle('height').toInt() > 0){
				effect = fxObj(box,0);
				this.toggleClass('titleBarClick');
				this.removeClass('titleBarSpecial');
			}
			else{
				effect = fxObj(box,height);
				this.toggleClass('titleBarClick');
				this.addClass('titleBarSpecial');
			}
			delete effect;
		});
		
		function fxObj(p,q){
			new Fx.Style(p, 'height',{duration:200,transition:Fx.Transitions.Expo.easeIn}).start(q);
		}
		
		//events for hover states. only one hover state (initial post-click hover) is set in css.
		item.addEvent('mouseover',function(e){
			e = new Event(e).stop;
			this.toggleClass('titleBarActive');
		});
		
		item.addEvent('mouseout',function(e){
			e = new Event(e).stop;
			this.toggleClass('titleBarActive');
		});
	});
	
	//expand all click event
	$$('a.expandAll').each(function(item,i){
		item.addEvent('click',function(e){
			e = new Event(e).stop();
			//loop through links which open collapsed divs
			$$('div.titleBar a').each(function(item,i){
				var box = 'box' + i;
				var question = 'question' + i;
				var height = 276;
				if ($(question)){
					height = $(question).getStyle('height').toInt();
				}
				item.addClass('titleBarClick');
				item.addClass('titleBarActive');
				var expand = new Fx.Style(box, 'height',{duration:200,transition:Fx.Transitions.Expo.easeIn}).start(height);
			});
		});
		//garbage collection
		delete expand;
	});
	
	//hide all click event
	$$('a.hideAll').each(function(item,i){
		item.addEvent('click',function(e){
			e = new Event(e).stop();
			$$('div.titleBar a').each(function(item,i){
				var box = 'box' + i;
				var hide = new Fx.Style(box, 'height',{duration:200,transition:Fx.Transitions.Expo.easeIn}).start(0);
				item.removeClass('titleBarClick');
				item.removeClass('titleBarActive');
				item.removeClass('titleBarSpecial');
			});
		});
		delete hide;
	});
	
	//deep-linking conditional
	if($('boxVal')){
		var expand;
		if($('boxVal').value != ""){
			var boxVar = 'box' + $('boxVal').value;
			var titleVal = 'title' + $('boxVal').value;
			$(titleVal).addClass('titleBarClick');
			$(titleVal).addClass('titleBarSpecial');
			expand = new Fx.Style($(boxVar), 'height',{duration:200,transition:Fx.Transitions.Expo.easeIn}).start(276);
		}
		delete expand;
	}
});