var RightMenu = Class.create();
RightMenu.prototype = {
	initialize: function(bar) {
		// Get the areas by the class name
		this.areas = $(bar).select('.stoolRbarCollapsableArea'); //document.getElementsByClassName('stoolRbarCollapsableArea', bar);
		this.currentlyOpen = -1;
		// Open the area with the attribute open true
		for (var i = 0; i < this.areas.size(); i++) {
			var isOpen = this.areas[i].attributes.getNamedItem('expanded').value;
			if (isOpen == 'True') {
				this.areas[i].down("div", 0).down("img").scr = 'images/arrowDown.gif';
				this.areas[i].down("div", 0).down("img").attributes.getNamedItem("src").value = 'images/arrowDown.gif';
				this.currentlyOpen = i;
			}
		}
		// Get the position of the top area where the collapsable area will start
		this.topPosition = $(bar).down('div.stoolRbarTopArea').getHeight();
		// Get the default separation between the areas
		this.separationHeight = parseInt($(bar).attributes.getNamedItem('separationheight').value);
	},
	expand: function(area) {
		// get the area, the height to expand and its index
		var areaToOpen = $(area).up().up();
		var heightExpanded = parseInt(areaToOpen.attributes.getNamedItem('expandedheight').value);
		var toOpenIndex = this.areas.indexOf(areaToOpen);
		// get the content to hide
		var content = areaToOpen.down("div", 1);
		// If the area to open is not actually open
		if (toOpenIndex != this.currentlyOpen) {
			this.hideCurrent();
			this.moveAreas(toOpenIndex, content, heightExpanded);
			this.currentlyOpen = toOpenIndex;
			this.showCurrent();
		}
	},
	hideCurrent: function() {
		if (this.currentlyOpen != -1) {
			// Replace the arrow image to show it hidden and do the effects
			//Effect.Pulsate(this.areas[this.currentlyOpen].down("div", 0).down("img"), {pulses: 1, duration: 0.5});
			Effect.Fade(this.areas[this.currentlyOpen].down("div", 1), {duration: 0.5});
			this.areas[this.currentlyOpen].down("div", 0).down("img").scr = 'images/arrowRight.gif';
			this.areas[this.currentlyOpen].down("div", 0).down("img").attributes.getNamedItem("src").value = 'images/arrowRight.gif';
		}
	},
	showCurrent: function() {
		// Replace the arrow to show it opened and do the effects
		//Effect.Pulsate(this.areas[this.currentlyOpen].down("div", 0).down("img"), {pulses: 1, duration: 0.5});
		var div = this.areas[this.currentlyOpen];
       	Effect.Appear(this.areas[this.currentlyOpen].down("div", 1), {queue: 'end',afterFinish: function() {Scroll.showScrollbar(div);}, duration: 0.5});
		this.areas[this.currentlyOpen].down("div", 0).down("img").scr = 'images/arrowDown.gif';
		this.areas[this.currentlyOpen].down("div", 0).down("img").attributes.getNamedItem("src").value = 'images/arrowDown.gif';

	},
	// move the areas
	moveAreas: function(toOpenIndex, content, heightExpanded) {
		var p = this;
		var h = heightExpanded;
		this.areas.each(function(o, i) {
			var newY = p.topPosition + i * p.separationHeight + (i > toOpenIndex ? h : 0);
			/*newY = (i > toOpenIndex ? h : 0);*/
			new Effect.Move(o, {y: newY, mode: 'absolute', duration: 0.4, delay: 0.4});
		});
	},
	restart: function() {
		// Open the area with the attribute open true
		var heightExpanded = 0;
		for (var i = 0; i < this.areas.size(); i++) {
			var isOpen = this.areas[i].attributes.getNamedItem('expanded').value;
			if (isOpen == 'True') {
				this.areas[i].down("div", 0).down("img").scr = 'images/arrowDown.gif';
				this.areas[i].down("div", 0).down("img").attributes.getNamedItem("src").value = 'images/arrowDown.gif';
				this.currentlyOpen = i;
				heightExpanded = parseInt(this.areas[i].attributes.getNamedItem('expandedheight').value);
			}
		}
		var p = this;
		this.areas.each(function(o, i) {
			var newY = p.topPosition + i * p.separationHeight + (i > p.currentlyOpen ? heightExpanded : 0);
			o.style['top'] = newY + 'px';
		});
	},
	// Only expand the area or hide it, move nothing
	expandOnly: function(area) {
		// get the area and its index
		var areaToOpen = $(area).up().up();
		var toOpenIndex = this.areas.indexOf(areaToOpen);
		// If the area is currently open, hide it
		if (this.currentlyOpen == toOpenIndex)
		{
			this.hideCurrent();
			this.currentlyOpen = -1;
		}
		else
		{
			this.currentlyOpen = toOpenIndex;
			this.showCurrent();
		}
	}
};
