/* configuration */
var HeaderTag = 'H5';
var SubMenuClass = ''
var menu_width = 164;

var FANCY = (navigator.userAgent.toLowerCase().indexOf("msie")>-1);
FANCY = false;
//menu nodes. Each represents one LI tag. Must contain either text(link; one child only!), or H5 an UL tag for submenu
var MenuNode = Class.create ();
MenuNode.prototype = {
	objType : 'node',
	element : null,

	initialize : function (element, parentObj, mainObj) {
		this.element = element;
		this.parentObj = parentObj;
		this.mainObj = mainObj;
		this.level = this.parentObj.level;
		//let's seek if there's Header Tag as child of the element
		var i = 0;
		while( i < this.element.childNodes.length && this.element.childNodes[i].tagName != HeaderTag)
			i++;
		var HasSub = (i < this.element.childNodes.length);

		if (!HasSub) { // assuming it hasn't submenus
			Element.addClassName (this.element, 'menu_item');
			this.header_element = this.element;
		} else {
			Element.addClassName (this.element, 'menu_item_with_sub');
			//so... i points to the header tag. Now seek the UL tag
			Element.addClassName (this.element.childNodes[i], 'menu_header');
			this.header_element = this.element.childNodes[i];
			while (i < this.element.childNodes.length &&
				this.element.childNodes[i] &&
				this.element.childNodes[i].tagName != 'UL') {
				i++;
			}
			var submenu = this.element.childNodes[i];
			this.subMenu = new MenuNodesContainer (submenu, this, this.mainObj);
		}
		this.element.onmouseover = this.hover.bindAsEventListener(this);
		this.element.onmouseout = this.unhover.bindAsEventListener(this);
	},

	hover : function () {
		Element.addClassName (this.element, 'menu_item_hover');
		Element.addClassName (this.header_element, 'menu_header_hover');
		if (this.subMenu)
			this.subMenu.show();
	},
	unhover : function () {
		Element.removeClassName (this.element, 'menu_item_hover');
		Element.removeClassName (this.header_element, 'menu_header_hover');
		if (this.subMenu)
			this.subMenu.hide();
	}
}

//menu nodes container. Each represents UL tag
var MenuNodesContainer = Class.create();
MenuNodesContainer.prototype = {
	objType : 'container',

	initialize: function (element, parentObj, mainObj) {
		this.element = element;
		this.parentObj = parentObj;
		this.mainObj = mainObj;
		this.children = new Array ();
		this.level = this.parentObj.level+1;
		//this.timer = new Timer(this);

		if (this.parentObj.level>=0) {
			Element.addClassName (this.element, 'menu');//close submenu except parent :);
			this.element.style.display = 'none';
			this.element.style.top = '-1px';
			this.element.style.left = menu_width + 'px';

			/*this.opacity = 0.0;
			this.element.style.MozOpacity = 0.0;
			this.element.style.opacity = 0.0;
			this.element.style.filter = 'alpha(opacity : 0)';*/
			//debugger;
		}
		this.element.style.width = menu_width + 'px';

		this.element.onmouseover = this.hover.bindAsEventListener(this);

		for (var i = 0; i < this.element.childNodes.length; i ++) {
			if (this.element.childNodes[i].tagName == 'LI')
				this.children.push (new MenuNode (this.element.childNodes[i], this, this.mainObj ) )
		}
	},

	hover: function () {
		/*if (this.tid) this.timer.clearTimeout(this.tid);
		this.element.style.MozOpacity = 1.0;
		this.element.style.opacity = 1.0;
		this.opacity = 1.0;*/
		this.element.style.display = 'block';
		//this.element.style.filter = 'alpha(opacity : 100)';
	},

	unhover: function () {

	},

	show: function () {
		if (this.mainObj.currDisplayed != null)
			this.mainObj.currDisplayed.hide;

		this.mainObj.currDisplayed = this;
		if (FANCY) {
			if (this.element.filters[0]) this.element.filters[0].Stop();
			this.element.filters[0].Apply();
		}
		this.element.style.display = 'block';
		if (FANCY) {
			this.element.filters[0].Play();
		}
		//this.tid = this.timer.setTimeout ("appear", 30);
		//this.effect = 'appear';
	},

	hide: function () {
		//if (this.opacity <= 0.0) return;
		//this.tid = this.timer.setTimeout ("fade", 30);
		//this.effect = 'fade';
		this.mainObj.currDisplayed = null;
		if (FANCY)
			if (this.element.filters[0]) this.element.filters[0].Stop();
		this.element.style.display='none';
	},

	fade: function () {
		if (this.effect == 'appear') return;
		this.opacity = this.opacity - 0.1;
		this.element.style.MozOpacity = this.opacity;
		this.element.style.opacity = this.opacity;
		this.element.style.filter = 'alpha(opacity : ' + parseInt(this.opacity*100) + ')';

		if (this.opacity > 0.0)
			this.tid = this.timer.setTimeout ("fade", 30)
		else {
			if (this.tid) this.timer.clearTimeout(this.tid);
			this.element.style.display = 'none';
		}
	},

	appear: function () {
		if (this.effect == 'fade') {
			if (this.tid) this.timer.clearTimeout(this.tid);
			return;
		}
		this.opacity = this.opacity + 0.1;
		this.element.style.MozOpacity = this.opacity;
		this.element.style.opacity = this.opacity;
		this.element.style.filter = 'alpha(opacity : ' + parseInt(this.opacity*100) + ')';
		if (this.opacity < 1.0)
			this.tid = this.timer.setTimeout ("appear", 30)
		else
			if (this.tid) this.timer.clearTimeout(this.tid);
	}

}

var MenuClass = Class.create();
MenuClass.prototype = {
	objType : 'main',
	baseNode : null,
	currDisplayed : null,
	level : -1,

	initialize : function (element) {
		this.element = element;

		baseNode = new MenuNodesContainer (this.element, this, this);
		this.element.style.display='block';
	}

}

/* Timer Class */
function Timer(){
	this.obj = (arguments.length)?arguments[0]:window;
	return this;
}

Timer.prototype.setTimeout = function(func, msec){
	var i = Timer.getNew();
	Timer.buildCall(this.obj, i, arguments);
	Timer.set[i].timer = window.setTimeout("Timer.callOnce("+i+");",msec);
	return i;
}

Timer.prototype.clearTimeout = function(i){
	if(!Timer.set[i]) return;
	window.clearTimeout(Timer.set[i].timer);
	Timer.set[i] = null;
}
Timer.set = new Array();
Timer.buildCall = function(obj, i, args){
	var t = "";
	Timer.set[i] = new Array();
	if(obj != window){
		Timer.set[i].obj = obj;
		t = "Timer.set["+i+"].obj.";
	}
	t += args[0]+"(";
	if(args.length > 2){
		Timer.set[i][0] = args[2];
		t += "Timer.set["+i+"][0]";
		for(var j=1; (j+2)<args.length; j++){
			Timer.set[i][j] = args[j+2];
			t += ", Timer.set["+i+"]["+j+"]";
		}}
	t += ");";
	Timer.set[i].call = t;
	return t;
}

Timer.callOnce = function(i){
	if(!Timer.set[i]) return;
	eval(Timer.set[i].call);
	Timer.set[i] = null;
}

Timer.getNew = function(){
	var i = 0;
	while(Timer.set[i]) i++;
	return i;
}