
/*
Class Accordion
by Eelko Potters
***************************************************************************************************/



function Accordion() {

	this.element = null;	
	
	this.chapters = new Array();
	
	this.openChapters = new Array();
	
	this.depth = 0;
	
	this.initialChapter = "1";
	
	this.maxHeight = 240;
	
	this.showing = false;

	this.prepare = function() {
	
		this.element = $("accordion");
		this.activate(this.element);
		this.element.style.visibility = "visible";
		
		this.translate(controller.portfolio.language);
	};
	
	
	
	this.activate = function(root) {

		var divs = root.getElementsByTagName("div");
		
		for (var i = 0; i < divs.length; i++) {
		
			var div = divs[i];
			
			var parts = div.id.split("-");

			if (parts.length == 2 && parts[0] == "chapter") {
			
				var chapter = div;
				
				chapter.number = parts[1];
				
				chapter.level = chapter.number.split(".").length;				
				chapter.open = false;				
				chapter.hasChildren = ($(chapter.id + ".1")) ? true : false;
				
				if (chapter.level > this.depth) {				
					this.depth = chapter.level;
				}				
				
				chapter.heading = $(chapter.id + "-heading");
				
				chapter.heading.accordion = this;
				chapter.heading.chapter = chapter;

				chapter.heading.onmousedown = function() {
					if (!Element.hasClassName(this, "selected")) {
						this.style.color = "white";
						Element.addClassName(this, "selected");
						this.accordion.open(this.chapter);
					}
					else this.accordion.close(this.chapter);
					this.style.color = "";
					
				};
				
				chapter.heading.onmouseover = function() {
					if (!Element.hasClassName(this, "selected")) {
						/*Element.addClassName(this, "hover");*/
					
					var bgcolorlist = new Array("#00f2e4", "#00f7fb", "#04fff0", "#0afcff", 
							"#06ffb6", "#00eed9", "#00f3f6", "#01ffd8", "#09ffb0", "#07fcff");
							
					this.style.color = bgcolorlist[Math.floor(Math.random() * bgcolorlist.length)];
					}
				};
				
				chapter.heading.onmouseout = function() {
						this.style.color = ""
				};
				
				if ($(chapter.id + "-content")) {
					chapter.content = $(chapter.id + "-content");
				}
				
				this.chapters[this.chapters.length] = chapter;
				
			} else {
				continue;
			}
		}
	};
	
	
	this.open = function(chapter) {	
		for (var i = chapter.level; i < this.openChapters.length; i++) {		
			this.close(this.openChapters[i]);
		}		
		this.openChapters[chapter.level] = chapter;		
		chapter.open = true;
		chapter.content.style.display = (chapter.hasChildren) ? "block" : "block";
		
		var newHeight = Element.getHeight(this.element);
		
		controller.debug("max height: " + this.maxHeight + ", new height: " + newHeight);
		
		if (newHeight > this.maxHeight) {
			chapter.content.style.height = this.maxHeight - 150 + "px";
			chapter.content.style.overflow = "auto";		
			controller.debug("Accordion resized to " + chapter.content.style.height);
		}
	};
	
	
	this.close = function(chapter) {
		if (chapter) {
			Element.removeClassName(chapter.heading, "selected");
			chapter.content.style.display = "none";
			chapter.open = false;
			this.openChapters[chapter.level] = null;
		}
	};

	this.toggle = function(chapter) {	
		if (chapter.open) {
			this.close(chapter);
		} else {
			this.open(chapter);		
		}
	};

	this.openPath = function(chapterNumber) {	
		var parts = chapterNumber.split(".");		
		var chapterId = "chapter-";		
		for (var i = 0; i < parts.length; i++) {		
			chapterId += ((i > 0) ? "." : "") + parts[i];			
			this.open($(chapterId));
		}
	};
	
	this.closeAll = function() {
		for (var i = 0; i < this.openChapters.length; i++) {
			this.close(this.openChapters[i]);
		}
	};
	
	
	this.translate = function(language) {

		var translations = $("accordion-translation-" + language);
		
		var divs = translations.getElementsByTagName("div");
		
		for (var i = 0; i < divs.length; i++) {
		
			var translation = divs[i];
			
			var chapterElementId = translation.id.substring(0, (translation.id.length - 
					controller.portfolio.language.length - 1));

			var chapterElement = $(chapterElementId);
			
			chapterElement.innerHTML = translation.innerHTML;
		}
	};
}

/**************************************************************************************************/


