/**
 * InfoTekst
 * Een helper om gemakkelijk infotekst blokken te gebruiken in een pagina.
 * Aangepast om ook collapsable elements te ondersteunen.
 *
 * depends: events.js
 * v1.0.061111
 * author: Martin Reurings - http://www.iprofs.nl/
 */

var InfoTekst = {
	aRE:/^a$/i,
	hRE:/^h[0-9]$/i,
	fieldsetRE:/^fieldset$/i,
	thRE:/^th$/i,
	/**
	 * A hashmap containing wether a rel-value is collapsable or not.
	 * This hashmap is used by StatusStore to determine if it should
	 * check it's cookie for the collapsed status of a link.
	 */
	collapsable:{
		"infotekst":false,
		"fieldset":true,
		"uitVoeringCollapse":false
	},
	/**
	 * A hashmap containing the methods link to certain rel-values.
	 * If a rel-value is found within this hashmap it's method will
	 * be called with the link supplied as an argument.
	 */
	handlers:{
		cssRE:/ ?\binfotekst\b/gi,
		CLASS_OPEN:"infotekst-open",
		openRE:/ ?\binfotekst-open\b/gi,
		CLASS_COLLAPSE:"collapse",
		collapseRE:/ ?\bcollapse\b/gi,
		tbRE:/^tbody$/i,
		trRE:/^tr$/i,
		ipw:null,//popupwindow shim-thing
		/**
		 * This method handles 'infotekst' links. These links should have
		 * a valid target id as 'href' attribute.
		 * If an element with the targetted id exists it will be shown
		 * for as long as the user does not leave the link's area.
		 */
		"infotekst":function(target) {
			var targetId = target.href;
			this.cssRE.test("");//reset
			if (targetId.indexOf("#") >= 0) {
				targetId = targetId.substr(targetId.indexOf("#") + 1);
				//alert(targetId);
			}
			var targetTekst = (targetId.length > 0)?document.getElementById(targetId):target.parentNode.nextSibling;
			if (targetId.length <= 0) { 
				while (targetTekst.nextSibling && !this.cssRE.test(targetTekst.className)) {
					this.cssRE.test("");//reset
					targetTekst = targetTekst.nextSibling;
				}
				this.cssRE.test("");//reset
			}
			if (targetTekst && this.cssRE.test(targetTekst.className)) {
				targetTekst.className += " " + this.CLASS_OPEN;
				var self = this;
				var eventHook = Events.attach(target, "mouseout", function (e) {
					var timeout = window.setTimeout(function(){
						window.clearTimeout(timeout);//cleanup
						targetTekst.className = targetTekst.className.replace(self.openRE, "");
					},500);
					Events.detach(eventHook); //Cleanup
				});
			}
			return true;
		},
		"infopopup":function(target) {
			if (this.ipw && !this.ipw.closed) {
			  this.ipw.close();
			}
			if (this.ipw && !this.ipw.closed) {
			  this.ipw.close();
			}
			var w=350,h=250;
			var leftPosition = (screen.width) ? (screen.width- w)/2 : 0;
			var topPosition = (screen.height) ? (screen.height- h)/2 : 0;
			this.ipw = window.open(target.href, "infoPopup", "scrollbars=yes,width="+w+",height="+h+",menubar=no,toolbar=no,top="+topPosition+",left="+leftPosition+",location=no,directories=no,resizable=yes");
			if (this.ipw == null || this.ipw.closed) {
				alert("Het openen van de hulp-informatie is mislukt. Waarschijnlijk heeft u een actieve popup-blocker. Schakel deze uit en probeer het opnieuw.");
			}
			return true;
		},
		/**
		 * A method for collapsing fieldsets. This method is employed for all
		 * search-related forms in RAP. Target link should contain a valid target
		 * id in it's 'href' attribute (href='#[someValidId]').
		 */
		"fieldset":function(target){
			var targetId = StatusToggle.getId(target);
			var targetFS = document.getElementById(targetId);
			if (targetFS) {
				StatusToggle.toggle(target, targetFS);
			} else {
				alert("ERROR 5282727478.001, Invalid ID");
			}
			return true;
		},
		/**
		 * This method is used for collapsing the uitvoering details tables.
		 * It's href must contain a unique id as this id will be used for
		 * storing it's collapsed status.
		 * The id need not be a target for the 'tbody' element that's to
		 * be collapsed.
		 */
		"uitVoeringCollapse":function(target){
			var targetTB = target.parentNode;
			var targetTR = target.parentNode;
			while (targetTB!=null && !this.tbRE.test(targetTB.nodeName)) {
				targetTB = targetTB.parentNode;
				if (this.trRE.test(targetTB.nodeName)) {
					targetTR = targetTB;
					targetTB = targetTB.parentNode;
				}
			}
			if (targetTB != null) {
				StatusToggle.toggle(target, targetTB);
				targetTR.className += " firstChildIE";
			}
			return true;
		}
	},
	/**
	 * This method is the clickhandler logic which will determine whether or not
	 * to act upon a click-event which is passed to it.
	 */
	clickHandler:function(e){
		var e = e||event;
		var target = e.target||e.srcElement;
		//In case an image is clicked within a link
		if (!this.aRE.test(target.nodeName)) {
			if (this.hRE.test(target.nodeName) || this.fieldsetRE.test(target.nodeName) || this.thRE.test(target.nodeName)) {
				if (target.getElementsByTagName("a").length > 0)
				  target = target.getElementsByTagName("a")[0];
				else
				  target = null;
			} else {
			  target =target.parentNode;
			}
		}
		//Check if a link is clicked and rel is of an infotekst
		if (this.aRE.test(target.nodeName) && target.rel) {
			if (this.handlers[target.rel] && this.handlers[target.rel](target))
			  return Events.cancel(e);
			else
			  return true;
		}
	}
}

/**
 * Registration of the InfoTekst object with the document. All it's event-handlers
 * will be cleaned up internally upon leaving the page.
 */
InfoTekst.onload = Events.attach(window, "load", function() {
	//Initialize
	InfoTekst.onclick = Events.attach(document.getElementsByTagName("body")[0], "click", function(e) {
		InfoTekst.clickHandler(e);
	});
	//Cleanup
	Events.detach(InfoTekst.onload);
	InfoTekst.onunload = Events.attach(window, "unload", function() {
		if (InfoTekst.handlers.ipw && !InfoTekst.handlers.ipw.closed) InfoTekst.handlers.ipw.close(); //Backup in case blur handler didn't work
		Events.detach(InfoTekst.onclick);
		Events.detach(InfoTekst.onunload);
	});
});

