
window.addEvent('domready', function(){ //function for when dom is ready to be referenced by js, and create accordion
//alert("acc " + window.addEvent);
	currentSelection=null; //The current selected item
	scrollNum=0; //The number of the currently selected item (if scrolling)
	scrollOn=true; //Whether it is periodically scrolling through them
	manual=false; //Whether the most recent switch was automatic or due to the user clicking the accordion
	fadeDuration = 150;
	
	elementnames=$$(".acc_title"); //list of elements
		lastSelection=null;
	accordion=new Accordion(
		$$(".acc_title"), //set accordion title boxes
		$$(".acc_main"), //set accordion content boxes
		{
			opacity: false, //dont fade during transitions
			wait: true, //dont allow a new item to be clicked during a transition
			transition: Fx.Transitions.cubicIn,
			onActive:function(element) { //when a transition begins
				lastSelection=currentSelection; //lastSelection: the previous selection
				currentSelection=element;
				switchActive(true, element); //set the selection to active class to display active image
			},
			onComplete:function() { //when any transition is completed
				if(manual==true) { //if it was triggered by a user...
					scrollOn=false;  //..stop accordion from automatically scrolling through elements
					setTimeout("scrollOn=true;accNext();", 45000);
				}
				manual=true; //default is true, autoscroll sets back to false each time
				if(lastSelection!=null) { //if this is not the first item to be shown...
					switchActive(false, lastSelection); //set the previous selection to inactive class to display inactive image
				}
				elementnames.each(function(s, index) {
      		if(s.innerHTML != currentSelection.innerHTML) {
      			s.className="acc_title acc_inactive";
      		}
				}); 
					$$(".acc_inactive").each(function(s, index) {
		new Fx.Style(s, 'opacity',{duration:fadeDuration}).start(.5);																
	});
			elementnames.each(function(s, index) {
			s.addEvent('mouseenter', function(){
			if(s.className == "acc_title acc_inactive") {
							new Fx.Style(s, 'opacity',{duration:fadeDuration}).start(1);
				}
			})
			s.addEvent('mouseleave', function(){
			if(s.className == "acc_title acc_inactive") {
							new Fx.Style(s, 'opacity',{duration:fadeDuration}).start(.5);
				}
			})
		});	
			}
		}
	);
	setTimeout("accNext()", 15000); //scroll accordion in 15 secs
});

function accNext() { //function for scrolling accordion
	if(scrollOn==true) { //if scrolling is on...
		manual=false; //this scroll is automatic
		accordion.display((++scrollNum)%4); //scroll to the next item. If already on last item, scroll to first item
		setTimeout("accNext()", 15000); //scroll accordion again in 15 seconds
	}
}

function switchActive(active, header) { //changes [header]s class name based on [active]
	
	for(var i=0; i<elementnames.length; i++) { //for each element name
			if(active==true) {
    		var activity="active";
    		new Fx.Style(header, 'opacity',{duration:fadeDuration}).start(1);
    	} else {
    		var activity="inactive";
				new Fx.Style(header, 'opacity',{duration:fadeDuration}).start(.5);
    	}
			header.className="acc_title acc_" + activity;	//set the appropriate class name
	}
}
