var Animation = function() {
	this.count = 0;
	this.interval = null;
	this.onfinish = new Publisher();
}

Animation.prototype.createAbsoluteClone = function(node) {
	var childs = (arguments[1]) ? arguments[1] : false;
	var size = getAbsolutePosition(node, arguments[2]);
	var clone = node.cloneNode(! childs);
	clone.size = size;
	clone.style.position = "absolute";
	clone.style.left = size.x + "px";
	clone.style.top = size.y + "px";
//	clone.style.width = size.clientWidth + "px";
//	clone.style.height = size.clientHeight + "px";
	if (childs) {
		for(var child = node.firstChild; child; child = child.nextSibling) {
			if (child.nodeType == Node.ELEMENT_NODE) {
				clone.appendChild(createAbsoluteClone(child, true, node));
			}
		}
	}
	clone.dx = 0;
	clone.dy = 0;
	clone.dh = 0;
	clone.dw = 0;
	return clone;
}

Animation.prototype.go = function(count, interval) {
	if (this.interval || this.count > 0 || count <= 0 || interval <= 0) {
		return;
	}
	this.count = count;
	this.interval = setInterval(function(owner) {
		return function() { owner.animate.call(owner); }}(this), interval
	);
}

Animation.prototype.animate = function() {
	this.count--;
	if (this.count <= 0) {
		clearInterval(this.interval);
		this.interval = 0;
		this.count = 0;
		this.onfinish.deliver();
	}
}


var Fly = function() {
	Fly.superclass.constructor.call(this);
	this.objects = new Array();
}

extend(Fly, Animation);


Fly.prototype.animate = function() {
	for (var i = 0, len = this.objects.length; i < len; i++) {
		var obj = this.objects[i];
		obj.size.x += obj.dx;
		obj.size.y += obj.dy;
		obj.size.width += obj.dw;
		obj.style.top = obj.size.y + 'px';
		obj.style.left = obj.size.x + 'px';
		obj.style.width = obj.size.width + 'px';
	}
	Fly.superclass.animate.call(this);
}

var Slide = function()
{
	Slide.superclass.constructor.call(this);
	this.box = null;
}

extend(Slide, Animation);

Slide.prototype.animate = function(callback) {
	this.box.size.height += this.box.dh;
	this.box.style.height = this.box.size.height + 'px';
	this.box.size.width += this.box.dw;
	this.box.style.width = this.box.size.width + 'px';
	this.box.size.x += this.box.dx;
	this.box.style.left = this.box.size.x + 'px';
	this.box.size.y += this.box.dy;
	this.box.style.top = this.box.size.y + 'px';
	Slide.superclass.animate.call(this);
}





var getStyle = function(element, cssRule) {
    var value = false;
	if (document.defaultView && document.defaultView.getComputedStyle) {
		var style = document.defaultView.getComputedStyle(element, '');
		value = style.getPropertyValue(
			cssRule.replace( /[A-Z]/g, function(match, char) {
				return "-" + match.toLowerCase();
		}));
	} else if (element.currentStyle) {
		value = element.currentStyle[cssRule];
	};
	return value;
}


var getAbsolutePosition = function(node) {
	var relParent = arguments[1];
	var width = node.offsetWidth;
	var height = node.offsetHeight;
	var x = node.offsetLeft;
	var y = node.offsetTop;
	if (! relParent) {
		for (var parent = node.offsetParent; parent; parent = parent.offsetParent) {
			x += parent.offsetLeft;
			y += parent.offsetTop;
			x += parseInt(getStyle(parent, "paddingLeft")) || 0;
			x += parseInt(getStyle(parent, "borderLeftWidth")) || 0;
			y += parseInt(getStyle(parent, "paddingTop")) || 0;
			y += parseInt(getStyle(parent, "borderTopWidth")) || 0;
		}
	} else {
		var size = getAbsolutePosition(relParent);
		x -= size.x;
		y -= size.y;
		x -= parseInt(getStyle(relParent, "marginLeft")) || 0;
		x -= parseInt(getStyle(relParent, "borderLeftWidth")) || 0;
		y -= parseInt(getStyle(relParent, "marginTop")) || 0;
		y -= parseInt(getStyle(relParent, "borderTopWidth")) || 0;
	}
	var cWidth = width;
	var cHeight = height;
	cWidth -= parseInt(getStyle(node, "marginLeft")) || 0;
	cWidth -= parseInt(getStyle(node, "marginRight")) || 0;
	cWidth -= parseInt(getStyle(node, "paddingLeft")) || 0;
	cWidth -= parseInt(getStyle(node, "paddingRight")) || 0;
	cWidth -= parseInt(getStyle(node, "borderLeftWidth")) || 0;
	cWidth -= parseInt(getStyle(node, "borderRightWidth")) || 0;
	cHeight -= parseInt(getStyle(node, "marginTop")) || 0;
	cHeight -= parseInt(getStyle(node, "marginBottom")) || 0;
	cHeight -= parseInt(getStyle(node, "paddingTop")) || 0;
	cHeight -= parseInt(getStyle(node, "paddingBottom")) || 0;
	cHeight -= parseInt(getStyle(node, "borderTopWidth")) || 0;
	cHeight -= parseInt(getStyle(node, "borderBottomWidth")) || 0;
	return {width: width, height: height, x: x, y: y, clientWidth: cWidth, clientHeight: cHeight};
}

var findChildByTagName = function(node, tagName) {
	if (!tagName || !node) {
		return null;
	} 
/*	if (node.tagName.toLowerCase() == tagName.toLowerCase()) {
		return node;
	}
*/
	for (var child = node.firstChild; child; child = child.nextSibling) {
		if (child.nodeType == Node.ELEMENT_NODE) {
			if (child.tagName.toLowerCase() == tagName.toLowerCase()) {
				return child;
			}
			var result = findChildByTagName(child, tagName);
			if (result) {
				return result;
			}
		}
	}
	return null;
}

var isChildOf = function(node, parentNode) {
	if (! node || ! parentNode) {
		return false;
	}
	for (var parent = node; parent; parent = parent.parentNode) {
		if (parent == parentNode) {
			return true;
		}
	}
	return false;
}

var FLY_COUNT = 30;
var FLY_INTERVAL = 10;

var MainMenu = function() {
	this.slide = new Slide();
	this.fly = new Fly();
	this.preloaded = new Array();
	this.texts = new Array();
}

MainMenu.prototype.onload = function() {
	this.mainBox = document.getElementById("mainMenuBox");
	this.topBox = document.getElementById("topMenu");
	this.body = document.getElementsByTagName("body").item(0);
	this.topSize = getAbsolutePosition(this.topBox);
	this.bodySize = getAbsolutePosition(this.body);

	var divs = document.getElementsByTagName("div");
	var srcImg = null;
	var srcText = null;

	for (var i = 0, len = divs.length; i < len; i++) {
		var div = divs[i];
		if (div.className.indexOf("menuItem") != 0) {
			continue;
		}
		this.onmouseover.subscribe(events.getDOMPublisher(div, "mouseover"), this);
		this.onclick.subscribe(events.getDOMPublisher(div, "click"), this);
		srcImg = findChildByTagName(div, "img");
		srcText = findChildByTagName(div, "div");
		if (! srcImg) {
			continue;
		}
		if (srcText) {
			this.texts.push(srcText);
		}
		var index = srcImg.src.indexOf("image");
		if (index < 0) {
			continue;
		}
		srcImg.baseSrc = srcImg.src.substr(0, index);
		var img = new Image;
		img.src = srcImg.baseSrc + "images/1.gif";
		this.preloaded.push(img);
	}
}

MainMenu.prototype.onmouseover = function(event) {
	for (var n = event.target; n; n = n.parentNode) {
		if (n.className && n.className.indexOf("menuItem") == 0) {
			break;
		}
	}   
	var img = findChildByTagName(n, "img");
    $(img).fadeTo(20, 0.3, function(){$(img).fadeTo("normal", 1);});
}   

MainMenu.prototype.onclick = function(event) {
	var node = null;
	for (var node = event.target; node; node = node.parentNode) {
		if (node.className && node.className.indexOf("menuItem") == 0) {
			break;
		}
	}
    var url = $(node).children("a").attr("href");
	if (this.slide.count == 0) {
		this.slide.box = this.slide.createAbsoluteClone(this.mainBox);
		this.mainBox.style.visibility = "hidden";

		this.slide.box.style.overflow = "hidden";
		this.slide.box.dh = (-this.slide.box.size.clientHeight - 6) / FLY_COUNT;
		this.body.appendChild(this.slide.box);
		this.slide1finished.subscribe(this.slide.onfinish, this);
		this.slide.go(FLY_COUNT, FLY_INTERVAL);
	}
	if (this.fly.count == 0) {
		this.topBox.innerHTML = "";
		var flyX = (this.bodySize.width - 108 * this.texts.length) / 2;
		for (var i = 0, len = this.texts.length; i < len; i++) {
			var text = this.texts[i];
			var flyText = this.fly.createAbsoluteClone(text);
			flyText.style.margin = "0px";
			flyText.dx = (flyX + 108 * i - flyText.size.x) / FLY_COUNT;
			flyText.dy = (this.topSize.y - flyText.size.y) / FLY_COUNT;
			flyText.dw = (107 - flyText.size.width) / FLY_COUNT;
			flyText.style.fontSize = "1em";
			flyText.style.textAlign = "center";
			flyText.style.paddingLeft = "0px";
			flyText.style.backgroundImage = "none";
//			flyText.style.backgroundPosition = "10px center";
			this.body.appendChild(flyText);
			this.fly.objects.push(flyText);
		}
		this.fly.go(FLY_COUNT, FLY_INTERVAL);
	}
	/*var id = node.id.substr(5);
	var url = "/oid/" + id + "/property/content";
	var r = new ajax.Request(url);
	this.pageLoaded.subscribe(r.onsuccess, this);
	ajax.RequestManager.send(r);
	event.preventDefault();
	event.stopPropagation();*/
    /*redirect(url);*/
    setTimeout(function() {redirect(url)},500);
}

redirect = function(url) {
    window.location=url;
}

MainMenu.prototype.slide1finished = function() {
//	this.slide.box = this.slide.createAbsoluteClone(this.mainBox);
//	this.mainBox.style.visibility = "hidden";
//	this.slide.box.style.overflow = "hidden";
	this.body.removeChild(this.slide.box);
	this.div = document.createElement("div");
	/*this.body.insertBefore(this.div, this.mainBox);*/
//	this.slide.box.dh = 0;
//	var d = (608 - this.slide.box.size.clientWidth) / 20;
//	this.slide.box.dw = d * 2;
//	this.slide.box.dx = -d;
//	this.body.appendChild(this.slide.box);
	this.slide1finished.unsubscribe(this.slide.onfinish, this);
//	this.slide2finished.subscribe(this.slide.onfinish, this);
//	this.slide.go(10, FLY_INTERVAL);

	this.boxReady = true;
	if (this.data) {
		this.slideDown();
	}
}

/*MainMenu.prototype.slide2finished = function() {
	this.boxReady = true;
	if (this.data) {
		this.slideDown();
	}
//	alert(1);
}*/

MainMenu.prototype.pageLoaded = function(response) {
//	this.data = response.xml.documentElement;
	this.data = response.data;
	if (this.boxReady) {
		this.slideDown();
	}
//	alert(response.data);
}

MainMenu.prototype.slideDown = function() {
	if (! this.data || ! this.boxReady) {
		return;
	}
//	this.slide2finished.unsubscribe(this.slide.onfinish, this);
//	this.slide.box.dx = 0;
//	this.slide.box.dw = 0;
//	this.slide.box.dh = 3.15;
	this.div.innerHTML = this.data;
//	this.slide.box.innerHTML = this.data;
/*	for (var n = this.data.firstChild; n; n = n.nextSibling) {
		var clone = document.importNode(n.cloneNode(true), true);
		this.slide.box.appendChild(clone);
		clone.style.display = 'block';
	}*/
//	this.slide.go(100, FLY_INTERVAL);
}





var menu = null;
var mainonload = function() {
	menu = new MainMenu();
	menu.onload();
};
mainonload.subscribe(ajax.onload);
