var CubicEyes = {};

CubicEyes.register = function(name) {
	var a = name.split(".");
	var cur = CubicEyes;
	for (tmp = 0; tmp<a.length; tmp++) {
		cur[a[tmp]]=cur[a[tmp]] || {};
		cur=cur[a[tmp]];
	}
}

CubicEyes.Controls = {
	customValidationHelper : function(originalValidationFunction, extraArgument) {
		return function(sender, args) {
			return originalValidationFunction(sender, args, extraArgument);
		}
	}
}

CubicEyes.Dom = {
	ifExistsAddText : function(source, sElem, addElem) {
		var x = source.getElementsByTagName(sElem);
		if (x.length > 0 ) {
			addElem.appendChild( document.createTextNode(x.item(0).firstChild.nodeValue));
		}
	},

	exists : function(elem)
	{
		if(typeof(elem) == "undefined") return false; /* standards format ( mozilla messes up here sometimes) */
		if( elem.length == 0) return false; /* This is actually wrong, but needed anyway */

		return true;
	},

	setAttribute : function (node, attribute, val) {
		/* This functions is needed to convince IE (any version) to change classes of anything */
		var att = document.createAttribute(attribute);
		att.value = val; /* We need to convince IE it is string data */
		node.setAttributeNode(att);
	},

	browserClient : function() {
		/* Retrieves a users browser, needed for some hacks. */
	},

	showChildren : function(e, seperator) {
		/* Prints the textContent of an elements DIRECT children */
		var txt = "";
		if(!seperator) seperator = " " ;

		if (e.hasChildNodes()) {
			var k = e.childNodes;
			for (var n = 0; n < k.length; n++) {
				if(k[n].firstChild) txt += seperator +k[n].firstChild.nodeValue;
			}
		}
		return txt;
	}
}

CubicEyes.Util = {
	foldr1 : function (f, list) {
		var a = list.pop();
		return f(a, foldr1(f, list));
	},
	clone : function(elem) {
		// Clones objects and arrays.
		var ret;
		for (x in elem) {
			ret[x] = elem[x];
		}
		return ret;
	},
	clearfield : function(input, defaultvalue) {
		if (input.value == defaultvalue) input.value = '';
	},
	restorefield : function(input, defaultvalue) {
		if (input.value == '') input.value = defaultvalue;
	}
}

CubicEyes.Logger = {
	active : false, // Default behaviour is off.
	messages : [],
	sources : [],
	add : function(source, message) {
		this.messages.push(message);
		this.sources.push(source);
		if (this.active) show(message,sources);
	},

	init : function() {
		var bugDiv = document.createElement("div");
		if (!this.active) CubicEyes.Dom.setAttribute(bugDiv, "style", "display:none");
		CubicEyes.Dom.setAttribute(bugDiv, "id", "bugDiv");

		document.body.appendChild(bugDiv);
	},

	activate : function() {
		var bugDiv = document.getElementsById("bugDiv");
		this.active = true;
		CubicEyes.Dom.setAttribute(bugDiv, "style", "display:block; z-index:10; height: 400px; width: 1024px;" );
		for (n in this.messages) {
			m = this.messages[n];
			s = this.sources[n];
			this.show(m,s);
		}
	},

	show : function(m,s) {
		var bugDiv = document.getElementById("bugDiv");
		var d = document.createElement("div");
		var text = document.createTextNode(m+ " from " + s);

		div.appendChild(text);
		bugDiv.appendChild(div);
	},

	styleToText : function(se)
	{ /* Use on an elementsstyle array */
		var ret = ""
		for (var n in se)
		{
			ret += n + se[n];
		}
	}

}

CubicEyes.StatusManager = {
	closeActions : [],
	hideDialog : function(elid) {
		var attributes = {
			opacity: { from: 1, to: 0 }
		};
		var anim = new YAHOO.util.Anim(elid, attributes, 0.5, null);
		anim.animate();
		anim.onComplete.subscribe(function() {document.getElementById(elid).style.display = 'none';});

		var ca = CubicEyes.StatusManager.closeActions;

		for (var n in ca) {
			ca[n]();
		}
	},
	MessageType : { // Enum als in StatusManager.vb Inclusief coole lazy evaluation hack.
		Info : { classe : "info", header : function() { return CubicEyes.L10n.CubicEyes.strings.InfoHeader; } },
		Prompt : { classe : "prompt" , header : function() { return CubicEyes.L10n.CubicEyes.strings.PromptHeader; } },
		Success : { classe : "success", header : function() { return CubicEyes.L10n.CubicEyes.strings.SuccessHeader; } },
		Warning : { classe : "warning", header : function() { return CubicEyes.L10n.CubicEyes.strings.WarningHeader; } },
		Fatal : { classe : "error", header : function() { return CubicEyes.L10n.CubicEyes.strings.ErrorHeader; } }
	},
	addMessage : function (messageType, message) {
		//Check of er al een statusmanager is.
		var DOM = YAHOO.util.Dom;
		var sm = DOM.getElementsByClassName("StatusMessage");
		var manager;
		var headerTextNode;
		var bodyTextNode;

		//Zo niet, maak aan.
		if (sm.length == 0) {
			manager = document.createElement("div");
			var root = document.getElementById("ScrollContainer");
			root.insertBefore(manager, root.firstChild);
			var header = document.createElement("div");
			CubicEyes.Dom.setAttribute(header, "class", "dialog-header");
			CubicEyes.Dom.setAttribute(manager, "id", "statusManager");
			var closeKnop = document.createElement("div");
			CubicEyes.Dom.setAttribute(closeKnop, "class", "dialog-close");
			CubicEyes.Dom.setAttribute(closeKnop, "onclick", "CubicEyes.StatusManager.hideDialog('statusManager')");
			header.appendChild(closeKnop);

			headerTextNode = document.createTextNode("");
			header.appendChild(headerTextNode);
			manager.appendChild(header);

			var bodyNode = document.createElement("div");
			manager.appendChild(bodyNode);
			CubicEyes.Dom.setAttribute(bodyNode, "class", "dialog-content");
			bodyTextNode = document.createTextNode("");
			bodyNode.appendChild(bodyTextNode);

		} //Zo wel, pas het bericht aan.
		else {
			manager = sm[0];
			headerTextNode = Dom.getElementsByClassName("dialog-header", "div", manager)[0].childNodes[1];
			bodyTextNode = Dom.getElementsByClassName("dialog-content", "div", manager)[0].childNodes[0];
		}
		bodyTextNode.nodeValue = message;
		headerTextNode.nodeValue = messageType.header();
		CubicEyes.Dom.setAttribute(manager, "class", "StatusMessage " + messageType.classe);
		CubicEyes.Dom.setAttribute(manager, "style", "");
	}
}

CubicEyes.fixDefaultButton = function () {
	YAHOO.util.Event.addListener(window, "load", function() {
		window.WebForm_FireDefaultButton = function(event, target) {
			var element = event.target || event.srcElement;
			var isEnter = (event.keyCode === 13);
			// We willen alleen de default button vuren als je in een
			// formulier-veld op enter drukt: dus een input die geen
			// button is, of een select. Geen textarea, want daar wil je
			// een enter in kunnen geven.
			var formElem = (element && ((element.tagName.toLowerCase() === "input" && element.type != "submit") || element.tagName.toLowerCase() === "select"));
			if (isEnter && formElem) {
				var defaultButton;
				if (__nonMSDOMBrowser) {
					defaultButton = document.getElementById(target);
				}
				else {
					defaultButton = document.all[target];
				}
				if (defaultButton && typeof(defaultButton.click) !== "undefined") {
					defaultButton.click();
					event.cancelBubble = true;
					if (event.stopPropagation) {event.stopPropagation();}
					return false;
				}
			}
			return true;
		};
	});
};

var loader = new YAHOO.util.YUILoader({
		base: "/library/yui/",
		require: ["event", "dom", "animation"],
		onSuccess : CubicEyes.fixDefaultButton }
		);
loader.insert();
