var Accessibility = new function() {
	this.initialized = false;
	this.sectionArray = [];
	
	// Controller
	var Controller = function(section) {
		this.buttonArray = [];
		this.section = section;

		this.getHandler = function(id) {
			var controller = this;
			return function(evt) {
				controller.switchTo(id);
				return false;
			}
		}
		
		this.switchTo = function(id) {
			var len = this.buttonArray.length;
			for (var i=0; i < len; i++) {
				var button = this.buttonArray[i];
				button.className = button.className.replace(/ act/, '');
			}
			if (len > 0) {
				this.buttonArray[id].className += ' act';
			}
			this.section.switchTo(id);
		}
		
		// Controls::contstuctor
//		try {
			var buttonArray = document.getElementById(section.getName()).getElementsByTagName('a');
			var len = buttonArray.length;
			for (var i=0; i < len; i++) {
				buttonArray[i].onclick = this.getHandler(i);
			}
			this.buttonArray = buttonArray;
			this.switchTo(this.section.current);
	//	} catch(e){}
	}

	// Section
	var Section = function(name) {
		this.linkArray = [];
		this.name = name;
		this.current = 0;
		this.controller = null;
		
		// Section::addNewCSS
		this.addNewCSS = function(url) {
/*			var linkNodes = document.getElementsByTagName('link');
			var l = linkNodes.length;
			for (var i=0; i<l; i++) {
				if (linkNodes[i].href.indexOf(url) != -1) {
					this.linkArray.push(linkNodes[i]);
				}
			}
*/			
			var linkObj = (url != null) ? XB.loadCSS(url) : null;
			this.linkArray.push(linkObj);
		}
		
		// Section::switchTo
		this.switchTo = function(id) {
			id = parseInt(id);
			var linkArray = this.linkArray;
			if (id >= linkArray.length) {
				return;
			}
			this.disableCurrentCSS();
			if (linkArray[id] != null) {
				linkArray[id].disabled = false;
			}
			this.current = id;
			XB.setCookie(this.name, id, 3153600000);
		}
		
		// Section::disableCurrentCSS
		this.disableCurrentCSS = function() {
			var linkObj = this.linkArray[this.current];
			if (linkObj != null) {
				linkObj.disabled = true;
			}
		}
		
		this.getName = function() {
			return this.name;
		}
		
		// Section::init
		this.init = function(defID) {
			// initialize CSS
			id = XB.getCookie(this.name);
			id = (id === false) ? defID : parseInt(id);
			this.switchTo(id);
		}
	}
	
	this.addNewSection = function(name) {
		var section = new Section(name);
		this.sectionArray.push(section);
		return section;
	}
	
	// Accessibility::init
	this.init = function() {
		if (!this.initialized) {
			// initialize controls for each section
			for (var i=0; i < this.sectionArray.length; i++) {
				new Controller(this.sectionArray[i]);
			}
			this.initialized = true;
		}
		for (var i=0, l=this.sectionArray.length; i < l; i++) {
			this.sectionArray[i].init(0);
		}
	}
}
window.onload = function(){try{Accessibility.init()}catch(e){}};
window.onpageshow = function(){try{Accessibility.init();}catch(e){}};
