inonit.slim.initialize(function() {
	var CV = {
		prefix: "cv",
		uri: "http://www.davidpcaldwell.com/cv"
	};
	
	var top = new function() {
		this.initialize = function() {
			this.getComponent("email").element.innerHTML = "";
			this.getComponent("email").element.appendChild(document.createTextNode("E-mail: "));
			var link = document.createElement("a");
			this.getComponent("email").element.appendChild(link);
			var address = "david" + "@" + "davidpcaldwell.com";
			link.setAttribute("href","");
			link.href = "mailto:" + address;
			link.appendChild(document.createTextNode(address));
			
			var clients = inonit.slim.getDocument().getComponent("clients");
			
			var getChildren = function(node,filter) {
				var list = node.childNodes;
				var rv = [];
				for (var i=0; i<list.length; i++) {
					if (filter(list[i])) {
						rv.push(list[i]);
					}
				}
				return rv;
			}
			
			var toArray = function(nodes) {
				var rv = [];
				for (var i=0; i<nodes.length; i++) {
					rv[i] = nodes[i];
				}
				return rv;
			}
			
			var list = (function() {
				var nodes = clients.element.childNodes;
				for (var i=0; i<nodes.length; i++) {
					if (nodes[i].tagName && nodes[i].tagName.toLowerCase() == "ul") {
						return nodes[i];
					}
				}
			})();

			var getWhat = function(item) {
				return inonit.js.Array.choose(getChildren(item,function(e) {
					if (e.tagName && e.tagName.toLowerCase() == "div") {
						if (inonit.dom.Element.getClasses(e,["what"]).what) {
							return true;
						}
					} else {
						return false;
					}
				}));
			}
			
			toArray(list.children).forEach(function(item) {
				var toDateString = function(attribute) {
					var tokens = attribute.split(".").map(function(token) {
						return Number(token);
					});
					var months = [
						"January","February","March","April","May","June",
						"July","August","September","October","November","December"
					];
					return months[tokens[1]-1] + " " + tokens[0];
				}

				var what = getWhat(item);
				var component = clients.getPrototype("when");
				component.getComponent("start").set(toDateString(inonit.dom.Element.getNsAttribute(what,CV,"startDate")));
				component.getComponent("end").set(toDateString(inonit.dom.Element.getNsAttribute(what,CV,"endDate")));
				inonit.dom.Element.setClasses(component.element,{ prototype: false });
				item.insertBefore(component.element,what);
			});

			var array = toArray(list.children);
			array.sort(function(a,b) {
				var awhat = getWhat(a);
				var bwhat = getWhat(b);
				var astart = inonit.dom.Element.getNsAttribute(awhat,CV,"startDate");				
				var bstart = inonit.dom.Element.getNsAttribute(bwhat,CV,"startDate");
				if (astart < bstart) return 1;
				if (bstart < astart) return -1;
				return 0;
			});
			
			while(list.children.length) {
				list.removeChild(list.children[0]);
			}
			for (var i=0; i<array.length; i++) {
				list.appendChild(array[i]);
			}
			
			var convert = new inonit.slim.server.Action("convert");
			
			var toStaticHtml = function() {
				var destination = (function() {
					var namespace = "http://www.w3.org/1999/xhtml";
					var root = "html";
					if (document.implementation && document.implementation.createDocument) {
						return document.implementation.createDocument(namespace,root,null);
					} else {
						var doc = new ActiveXObject("MSXML2.DOMDocument");
						doc.loadXML("<" + root + " xmlns=\"" + namespace + "\"/>");
						return doc;
					}
				})();

				while(destination.documentElement.childNodes.length) {
					destination.documentElement.removeChild(destination.documentElement.childNodes[0]);
				}

				var transform = function(source) {
					var recurse = arguments.callee;
					if (source.nodeName.toLowerCase() == "script") return;
					if (source.nodeName.toLowerCase() == "style") return;
					if (source.nodeName.toLowerCase() == "link") return;
					if (source.nodeName.toLowerCase() == "input") return;
					if (inonit.dom.Node.isElement(source) && source.getAttribute("id") == "formats") return;
					var rv = inonit.dom.document.importNode(destination,source,false);
					var removeAttributes = [];
					if (rv.attributes) {
						for (var i=0; i<rv.attributes.length; i++) {
							if (/\:/.test(rv.attributes[i].nodeName)) {
								removeAttributes.push(rv.attributes[i].nodeName);
							}
						}
					}
					removeAttributes.forEach(function(name) {
						rv.removeAttribute(name);
					});
					if (inonit.dom.Node.isElement(source)) {
						var style = inonit.dom.Element.getComputedStyle(source);
						var declarations = [];
						for (var x in style) {
							if (!/^\-webkit/.test(x) && !/^-moz/.test(x)) {
								if (typeof(style[x]) == "string" && style[x].length > 0) {
									declarations.push(x + ": " + style[x]);
								}
							}							
						}
						rv.setAttribute("style",declarations.join("; "));
					}
					if (source.childNodes) {
						for (var i=0; i<source.childNodes.length; i++) {
							var transformed = recurse(source.childNodes[i]);
							if (transformed) {
								rv.appendChild(transformed);
							}
						}
					}
					return rv;
				}

				for (var i=0; i<document.documentElement.childNodes.length; i++) {
					destination.documentElement.appendChild(transform(document.documentElement.childNodes[i]));
				}

				return destination;
			}

			//	TODO	controlled on server side? Controlled by appearance of buttons? Probably best would be to make server-side
			//			service that includes format and description ("Microsoft Word") and then build this section dynamically.

			var DYNAMIC_CONVERSION_DISABLED = true;
			
			inonit.slim.getDocument().getComponent( "offline" ).detach();
			inonit.slim.getDocument().getComponent( (DYNAMIC_CONVERSION_DISABLED) ? "dynamic" : "static" ).detach();
			if (DYNAMIC_CONVERSION_DISABLED) {
				inonit.slim.getDocument().getComponent("convert/html").detach();
				inonit.slim.getDocument().getComponent("convert").element.parentNode.style.width = "135px";
			} else {
				inonit.slim.getDocument().getComponent("convert").getChildren().forEach(function(child) {
					child.element.href = "convert";
					this.action(new (function(type) {
						this.child = "convert/"+type,
						this.on = "click";
						this.call = function(e) {
							var node = document.createTextNode(" Working ...");
							try {
								child.element.appendChild(node);
								child.element.href = null;
								var destination = toStaticHtml();
								var result = convert.POST({
									type: type,
									html: inonit.dom.Node.toXml(destination.documentElement)
								});
								inonit.slim.window.open({
									url: "converted?key=" + result.key,
									name: result.key
								});
							} finally {
								child.element.removeChild(node);
								child.element.href = "convert";
								e.preventDefault();
							}
						}
					})(child.getSlimId()));
				},this);
			}
		}
	}
	
	inonit.slim.configuration = top;
});

