var scripts = {
	init: function() {
		var nodes=[];
		var counter=0;
		var targetmenu=document.getElementById("menu2");
		targetmenu.removeChild(targetmenu.childNodes[0]);
		var changed=0;
		findmenus(document.getElementById("menu1"));
		addevents();
		colorchanger();
		readcolorfromcookie();

		function findmenus(chunk) {
			for (var i=0; i<chunk.childNodes.length; i++) {
				if (chunk.childNodes[i].nodeName=="LI") {
					nodes[counter]=chunk.childNodes[i];
					counter++;
				} else { // don't go deeper, we just need the main menu's
					findmenus(chunk.childNodes[i]);
				}
			}
		}

		function addevents() {
			for (var i=0; i<nodes.length; i++) {
				nodes[i].onmouseover = function() {
					if (typeof(targetmenu.childNodes[0])!="undefined") {
						nodes[changed].appendChild(targetmenu.childNodes[0]);
					}
					for (var j=0; j<this.childNodes.length; j++) {
						if (this.childNodes[j].nodeName=="UL") {
							if (typeof(this.childNodes[j])!="undefined") {
								for (var i=0; i<nodes.length; i++) {
									if (this==nodes[i]) {
										changed=i;
									}
								}
								targetmenu.appendChild(this.childNodes[j]);
							}
						}
					}
				}
				if (Core.hasClass(nodes[i], "active")) {
					if (typeof(targetmenu.childNodes[0])!="undefined") {
						nodes[i].appendChild(targetmenu.childNodes[0]);
					}
					for (var j=0; j<nodes[i].childNodes.length; j++) {
						if (nodes[i].childNodes[j].nodeName=="UL") {
							if (typeof(nodes[i].childNodes[j])!="undefined") {
								changed=i;
								targetmenu.appendChild(nodes[i].childNodes[j]);
							}
						}
					}
				}
			}
		}

		function colorchanger() {
			var colorbuttons=document.getElementById("colors");
			for (var i=0; i<colorbuttons.childNodes.length; i++) {
				if (colorbuttons.childNodes[i].nodeName=="IMG") {
					colorbuttons.childNodes[i].onclick = function() {
						if (Core.hasClass(document.body, "blue")) {
							Core.removeClass(document.body, "blue");
						}
						if (Core.hasClass(document.body, "green")) {
							Core.removeClass(document.body, "green");
						}
						if (Core.hasClass(document.body, "orange")) {
							Core.removeClass(document.body, "orange");
						}
						Core.addClass(document.body, this.attributes["alt"].value);
						var date=new Date();
						date.setYear(date.getYear()+1);
						document.cookie = "color="+this.attributes['alt'].value+";expires="+date.toGMTString()+";path=/";
					}
				}
			}
		}

		function readcolorfromcookie() {
			var nameEQ = "color=";
			var ca = document.cookie.split(';');
			for (var i=0;i < ca.length;i++) {
				var c = ca[i];
				while (c.charAt(0)==' ') {
					c = c.substring(1,c.length);
				}
				if (c.indexOf(nameEQ) == 0) {
					if (c.substring(nameEQ.length,c.length)=="blue" || c.substring(nameEQ.length,c.length)=="green" || c.substring(nameEQ.length,c.length)=="orange") {
						if (Core.hasClass(document.body, "blue")) {
							Core.removeClass(document.body, "blue");
						}
						if (Core.hasClass(document.body, "green")) {
							Core.removeClass(document.body, "green");
						}
						if (Core.hasClass(document.body, "orange")) {
							Core.removeClass(document.body, "orange");
						}
						Core.addClass(document.body, c.substring(nameEQ.length,c.length));
					}
				}
			}
		}
	}
};

Core.start(scripts);
