/**
 * StatusStore
 * Dit object gaat de status van in- / uitklapbare informatie opslaan en
 * terughalen. StatusStore is sterk afhanelijk van infotekst.js
 *
 * depends: infotekst.js, events.js, cookiemanager.js
 * v1.0.061222
 * author: Martin Reurings - http://www.iprofs.nl/
 */

var StatusStore = {
	COOKIE:"persistencyStore",
	cm:new CookieManager(),
	/**
	 * Initialisatie van StatusStore. Deze routine checked alle links op
	 * de pagina en bepaalt of open moeten blijven of ingeklapt moeten worden.
	 */
	init:function() {
		//Get all links
		var links = document.getElementsByTagName("a");
		//Get cookie
		var cookie = this.cm.getCookie(this.COOKIE);
		for (var i = 0; i < links.length; i++) {
			var link = links[i];
			var id = StatusToggle.getId(link);
			//If the link has an id and a rel-attribute and that rel-attribute is registered as a collapse method...
			if (id && id.length > 1 && link.rel && InfoTekst.collapsable[link.rel]) {
				var re = this.getRE(id);
				if (!cookie || !re.test(cookie))
					InfoTekst.handlers[link.rel](link);//Initialise and close collapsable element
			}
		}
	},
	/**
	 * creates a regular expression to find an id within the comma-seperated list in the cookie.
	 */
	getRE:function(id) {
		var re = new RegExp(",?" + id + "\\b", "g");
		return re;
	},
	/**
	 * Adds an id to the cookie. This indicates the id has an altered state.
	 */
	add:function(id) {
		var cookie = this.cm.getCookie(this.COOKIE);
		var re = this.getRE(id);
		if (!(cookie && re.test(cookie))) { //If no cookie exists or if the id isn't in there
			if (!cookie) cookie = id; //no cookie exists, the id is the new cookie
			else cookie += ',' + id; //Add id to existing cookie
		}
		this.cm.setCookie(this.COOKIE, cookie);
	},
	/**
	 * Removes an id from the cookie. This indicates the id has an unaltered stat.
	 */
	remove:function(id) {
		//alert("Removing " + id + " to the cookie-store");
		var cookie = this.cm.getCookie(this.COOKIE);
		if (cookie) { //If no cookie exists, why bother
			var re = this.getRE(id);
			if (re.test(cookie)) cookie = cookie.replace(re, "");
			this.cm.setCookie(this.COOKIE, cookie);
		}
	}
}

/**
 * Registering the StatusStore with the onload event of the document. Cleanup
 * is done internally.
 */
StatusStore.onload = Events.attach(window, "load", function() {
	StatusStore.init(); //Initialise the store and restore the view.
	//Cleanup
	Events.detach(StatusStore.onload);
});

/**
 * Helper object to collapse / uncollapse elements. This helper will register
 * the collapsed state with the StatusStore.
 */
StatusToggle = {
	CLASS_COLLAPSE:" collapse",
	collapseRE:/collapse/gi,
	/**
	 * Obtain the targetted id from the link.
	 */
	getId:function(link){
		var indx = link.href.indexOf("#");
		var id = null;
		if (indx >=0) id = link.href.substr(indx + 1);
		return id;
	},
	/**
	 * uncollapse an element. This method requires the link wich was clicked
	 * as well as the target element which should be uncollapsed.
	 * The target element's classname will have the 'collapse' class removed.
	 * The classname of the link's parent element will be set to 'out'.
	 */
	open:function(link, target) {
		link.parentNode.className = "out";
		target.className = target.className.replace(this.collapseRE, "");
		StatusStore.add(this.getId(link));
	},
	/**
	 * collapse an element. This method requires the link wich was clicked
	 * as well as the target element which should be collapsed.
	 * The target element's classname will have the 'collapse' class added.
	 * The classname of the link's parent element will be set to 'in'.
	 */
	close:function(link, target) {
		link.parentNode.className = "in";
		target.className += this.CLASS_COLLAPSE;
		StatusStore.remove(this.getId(link));
	},
	/**
	 * This method will determine whether the target element needs to
	 * be collapsed or uncollapsed.
	 */
	toggle:function(link, target) {
		if (target.className.indexOf("collapse") > -1) {
			this.open(link, target);
		} else {
			this.close(link, target);
		}
	}
};
