/**
 * Setup t-error divs as dialogs, to appear similar to the b4f's current
 * design.
 * 
 * @requires Events from events.js.
 */ 
var ErrorDialogs = {
	altDialogRE:/\bdialog\b/,
	init:Events.attach(window, "load", function() {
		ErrorDialogs.applyDialogs();
		ClickHandler.addHandler("closeDialog", ErrorDialogs.closeDialog);
		Events.detach(ErrorDialogs.init);
	}),
	/**
	 * Create an close button with a divinable label (such as 'ok') and target.
	 * 
	 * @param {String} lbl The label of the button (the text on its' face)
	 * @param {String} trgt The id of the dialog this button targets.
	 */
	okButton:function(lbl,trgt,className) {
		var ok = document.createElement("div");
		ok.className = "button dialog_button " + className;
		var a = document.createElement("a");
		a.href = "#" + trgt;
		a.rel = "closeDialog";
		a.appendChild(document.createTextNode(lbl));
		ok.appendChild(a);
		return ok;
	},
	addToContainer:function(dialog) {
		var container = $("errorDialogContainer");
		if (container == null) {
			var container = document.createElement("div");
			container.id = "errorDialogContainer";
			var overlay = document.createElement("div");
			overlay.className = "overlay";
			var b = document.getElementsByTagName("body")[0];
			b.appendChild(container);
			b.appendChild(overlay);
		}
		container.appendChild(dialog);
	},
	/**
	 * Searches through all the divs and targets those with a classname
	 * 't-error' or 'userMessages' for setting them up as a dialog.
	 */
	applyDialogs:function() {
		var divs = $$("div.dialog");
		for (var i = 0; i < divs.length; i++) {
			var div = divs[i];
			ErrorDialogs.addToContainer(div); //In case the dialog is nested, we rather place it at the top of the page...
			//TODO Hunt and copy the ok-button for creating the 'x'?
			if (!div.hasClassName("t-invisible") && !div.hasClassName("defaultHidden")) this.setBodyState(true);
			else {
				div.addClassName("dialog_CLOSED");
			}
		}
		divs = $$("div.t-error");
		for (var i = 0; i < divs.length; i++) {
			var div = divs[i];
			var hdiv = div.getElementsByTagName("div")[0];
			var header = document.createElement("h3");
			header.appendChild(hdiv.firstChild);
			var ul = div.getElementsByTagName("ul")[0];
			var body = document.createElement("div");
			body.className = "body";
			div.removeChild(hdiv);
			div.appendChild(header);
			div.appendChild(body);
			body.appendChild(ul);
			div.removeClassName("t-error");
			div.addClassName("userMessages errorMessages");
		}
		divs = $$("div.t-error, div.userMessages");
		for (var i = 0; i < divs.length; i++) {
			var div = divs[i];
			var body = div.getElementsByTagName("div")[0];
			var ok = div.lastChild.getElementsByTagName?div.lastChild.getElementsByTagName("a")[0]:null; //lastchild could be a textnode...
			if (ok && ok.rel && ok.rel == "closeDialog") return true; //This dialog has already been processed... 
			ErrorDialogs.addToContainer(div); //In case the dialog is nested, we rather place it at the top of the page...
			/** 
			 * upgrade to Tapestry 5.0.9
			 * T5.0.9 does not initialize the id in the 't-error' anymore. Div's like
			 * <div class='t-error' id="loginform:errors" ...> are no longer available,
			 * so we have to set the id ourselves
			 */
			if (!div.id) {
				div.id=div.parentNode.id+':errors';
			}
			div.appendChild(this.okButton("x", div.id, "dialog_closeButton"));
			body.appendChild(this.okButton("Ok", div.id, "dialog_okButton"));
			div.className += " dialog";
			if (!div.hasClassName("t-invisible") && !div.hasClassName("defaultHidden")) this.setBodyState(true);
			else {
				div.addClassName("dialog_CLOSED");
			}
		}
	},
	/**
	 * Handles clicks on the links generated by the applyDialogs
	 * method.
	 */
	closeDialog:function(target){
		var href = target.href;
		var id = target.href.substr(target.href.indexOf("#") + 1); //Get id
		if (id) {
			var dialog = $(id); //Get dialog
			if (dialog) {
				dialog.addClassName(" dialog_CLOSED"); //Set classname for closed dialogs
				var f = document.getElementById("content")?document.getElementById("content").getElementsByTagName("form")[0]:null;
				if (f) {
					for (var i = 0; i < f.elements.length; i++) {
						if (f.elements[i].className.indexOf("validation-FAILED") > -1) {
							f.elements[i].focus();
							i = f.elements.length;
						}
					}
				}
				ErrorDialogs.setBodyState(false);
			} else {
				alert("No dialog found for id '" + id + "'!");
			}
		} else {
			alert("Dialogs are not supported for elements without an id!")
		}
		return true; //Cancel the default action of the anchor
	},
	/**
	 * Toggle 'state' of open/closed dialogs on the body-tag, this to help solve some
	 * IE6 troubles concerning overlaying 'select'-elements...
	 */
	setBodyState:function(b){
		$$("html, body").each(function(e) {
			if (b) e.addClassName("dialog_OPEN");
			else e.removeClassName("dialog_OPEN");
			document.location.replace("#errorDialogContainer"); //Make sure the error is visable
		});
	}
};

(document.body||document.documentElement).className += " dialog_ENABLED";

var WaitinglistInfobox = {
	init:Events.attach(window, "load", function() {
		var hasInfoBox = false;
		var body = $$("body")[0];
		var html = $$("html")[0];
		var container = document.createElement("div");
		container.id = "waitinglistContainer";
		$$("#waitinglistInfobox2, #waitinglistInfobox").each(function(element) {
			hasInfoBox = true;
			container.appendChild(element);
		});
		if (hasInfoBox) {
			html.addClassName("hasInfoBox");
			body.appendChild(container);
		}
	})
}