// JavaScript Document

var Accordion = Class.create();

Accordion.prototype = {
	initialize: function(options) {
		this.id = options.id; // id of the outer div
		this.varName = options.varName; // variable name of the Accordion instance
		this.duration = options.duration || 0.2;

		this.panelClass = options.panelClass || "panel";
		this.panelHeadClass = options.panelHeadClass || "panelHead";
		this.closedPanelHeadClass = options.closedPanelHeadClass || this.panelHeadClass;
		this.panelBodyClass = options.panelBodyClass || "panelBody";
		this.bodyURL = {};
		
		if (options.onOpen)
		{
			this.onOpen(options.onOpen);
		}
		if (options.onClose)
		{
			this.onClose(options.onClose);
		}
		this.openIndex = options.openIndex || 0; // which panel is initially open
		
		// Map panel IDs to their respective indices
		// so we can open a panel by ID
		var wrappers = $(this.id).getElementsByClassName(this.panelClass);
		this.idMap = {};
		for (var i = 0; i < wrappers.length; ++i)
		{
			var panelId = wrappers[i].id;
			this.idMap[panelId] = i;
			var url = options.bodyURL[panelId];
			if (url)
			{
				this.bodyURL[i] = url;
			}
		}
	
		// if openIndex is a panel ID, map it to an index
		var mapped = this.idMap[this.openIndex];
		if (mapped != undefined)
		{
			this.openIndex = mapped;
		}
		
		this.headers = $(this.id).getElementsByClassName(this.panelHeadClass);
		for (var i = 0; i < this.headers.length; ++i)
		{
			var hdr = this.headers[i];
			hdr.style.cursor = "pointer";
			// setting code property for debugging purposes
			hdr.code = "function() { " + this.varName + ".show(" + i + "); }";
			hdr.onclick = this._returnEvalCode(this.varName, i);
			hdr.className = (i == this.openIndex ? this.panelHeadClass : this.closedPanelHeadClass);
		}

		this.panels = $(this.id).getElementsByClassName(this.panelBodyClass);
		for (var i = 0; i < this.panels.length; ++i)
		{
			var panel = this.panels[i];
			panel.style.display = (i == this.openIndex ? 'block' : 'none');
		}
		this.replaceBody(this.openIndex);
	},

	/* workaround for IE */
	_returnEvalCode: function(varName, i) {
		return function() {
			eval(varName + ".show(" + i + ");");
		}
	},

	refresh: function() {
		this.replaceBody(this.openIndex);
	},
	
	replaceBody: function(i) {
		if (this.bodyURL[i])
		{
			new Ajax.Updater(this.panels[i].id, this.bodyURL[i],
			{
				// force script execution in root Javascript context
				// note that in IE, variables must be predefined in root html
				asynchronous: false,
				// evaluate <script> blocks
				evalScripts: true
			});
		}
	},

	removeBody: function(i) {
		if (this.bodyURL[i])
		{
			// must replace content with a <div>, or we can't use Effect later
			$(this.panels[i].id).innerHTML = '<div></div>';
		}
	},

	show: function(index) {
		// if index is a panel ID, map it to an index (number)
		if (this.idMap[index] != undefined)
		{
			index = this.idMap[index];
		}
	
		if (index == this.openIndex)
		{
			return; // already open
		}
		
		var closeIndex = this.openIndex;
		//this._onClose(closeIndex);
		this.openIndex = index;
		this._onOpen(index);

		var acc = this;		
		var after = function(effect) {
			acc._onClose(closeIndex);
			acc.scrollTo(index); // scroll so tab header is at the top of the main browser window
		};
		new Effect.Parallel(
			[
				new Effect.SlideUp( this.panels[closeIndex],
					{ duration: this.duration } ),
				new Effect.SlideDown( this.panels[this.openIndex],
					{ duration: this.duration } )
			], {
				duration: this.duration,
				afterFinish: after
			}
		);
		
	},
	scrollTo: function(index) {
		var hdr = this.headers[index];
		var offset = Position.cumulativeOffset(hdr);
		var x = f_scrollLeft(); // store old scrollX position
		//alert('hdr=' + hdr + ' offset=' + offset + 'x=' + x);
		scroll(x, offset[1]);
	},
	onOpen: function(cb) {
		this.onOpenCallback = cb;
		//alert("onOpen=" +this.onOpenCallback);
	},
	
	onClose: function(cb) {
		this.onCloseCallback = cb;
	},
	
	_onOpen: function(i) {
		this.replaceBody(i);
		var cb = this.onOpenCallback;
		if (cb) {
			//alert("opencb=" + cb);
			cb(i, this);
		}
		this.headers[i].className = this.panelHeadClass;
	},
	
	_onClose: function(i) {
		//this.removeBody(i); - BUG FIX for IE - clicking MyPlan/Tools/Sleep/Confessional crashes IE
		var cb = this.onCloseCallback;
		if (cb) {
			//alert("closecb=" + cb);
			cb(i, this);
		}
		this.headers[i].className = this.closedPanelHeadClass;
	}
};
