//
//	requires FX.js
//
//	usage:
//		- create multiple "popExpand" objects (see fx.js)
//		- call setupAccordion, passing in the IDs of all anchor triggers
//
//	"advanced" accordions: this allows you to set the design of the anchor
//		to indicate how far it is from the open tab. so the banner of the open
//		tab can be set to a dark background; the color of the adjacent banner(s)
//		can be set to a middle gray; the color of those once removed can
//		be set to a lighter gray, etc. it's a bit of a custom quirk, for sure.
//
//		to set up an advanced accordion, simply define these specific styles:
//
//			accordAdv, accordAdv1, accordAdv2 (etc)
//
//		e.g.,
//
//			.accordAdv { background-color: #000 }
//			.accordAdv1 { background-color: #454545 }
//			.accordAdv2 { background-color: #7F7F7F }
//
//		where "accordAdv" is applied to the open tab, "accordAdv1" is applied
//		to the tabs adjacent, etc. in the HTML code, put the "accordAdv" style on 
//		every tab -- do NOT pre-apply "accordAdv1" -- all tabs get the
//		baseline class. it will be dynamically adjusted at page load. e.g.:
//
//			<a href="javascript:;" id="accord1" class="popExpand accordAdv"> ... </a>
//
//		if you do not set the class on the anchor tags, the advanced accordion 
//		module will not be loaded (i.e., it degrades to a normal accordion).
//

// for ADVANCED accordions, the IDs MUST be listed in order. otherwise, no matter,
//	but you'll probably put them in order anyway.

function setupAccordion() {

	var i,ct,a,master = null,els = new Array(), ix=0, baseAdvancedClass="accordAdv";
	for (i=0, ct=arguments.length; i<ct; ++i) {
		a = $(arguments[i]);
		if (a) {
			els.push(a);

			// get the initial status
			a.isAccordionOpen = !($(a.id+'Pop').style.display == 'none');

			// tie the anchors together
			if ( !master) {
				master = a;
				master.openAccordion = null;
				master.toggleAccordion = toggleAccordion;	// link up the master function
				master.allAccordionAnchors = els;
			}
			a.accordionMaster = master;
			a.accordionPosition = ix;
			a.currentAdvancedClass = baseAdvancedClass;

			// which one is open to begin with?
			if ( a.isAccordionOpen) {
				master.openAccordion = a;
				master.openIndex = ix;
			}

			// install another click event
			addEvent(a,'click',accordionClick,true);
			++ix;
		}
	}
	
	setupAccordionDistances( master);
}

	// set up the classes for "advanced" accordions (which
	//	have classes to indicate how far from the open tab they are)
	//
	function setupAccordionDistances(master) {
		var i,ct,a,els = master.allAccordionAnchors,baseAdvancedClass="accordAdv",cls,dist;

		for ( i=0, ct=els.length; i<ct; ++i) {
	
			a = els[i];
			
			// how far am I from the open anchor?
			dist = Math.abs(i - master.openIndex);
			if ( dist == 0)
				cls = baseAdvancedClass;
			else
				cls = baseAdvancedClass + dist;
			
			replaceClass(a, a.currentAdvancedClass, cls);
			a.currentAdvancedClass = cls;
			//a.innerHTML = cls;
		}
	}


	// called only on the master
	function toggleAccordion(anc) {
		
		// user closed the open one; nothing else to do
		if ( this.openAccordion == anc) {
			this.openAccordion = null;
		}
		else {
			var i,ct,a;
			for (i=0, ct=this.allAccordionAnchors.length; i<ct; ++i) {
				a = this.allAccordionAnchors[i];
				
				// find the currently open one and close it
				if ( a.isAccordionOpen) {
					EFX.toggleExpand(a.id+'Pop');
					a.isAccordionOpen = !a.isAccordionOpen;
				}
				else if ( a == anc)	{	// was closed, now the open one
					this.openAccordion = a;
					this.openIndex = i;
				}
					
				// the clicked anchor will normally handle its own open/close action
			}
			setupAccordionDistances( this);
		}
	}
	
	function accordionClick(e) {
		this.accordionMaster.toggleAccordion(this);
		this.isAccordionOpen = !this.isAccordionOpen;
		return false;	// keep processing event handlers
	}