var menu = Class.create();
menu.prototype = {
	initialize: function(element, content, options) {
		this.Menus = [];
		this.select = [];
		this.timer = 0;
		this.cls = [];
		this.cls[1] = [];
		this.cls[2] = [];
		this.cls[3] = [];
		
		this.cls[1][0] = 'menuOff';
		this.cls[1][1] = 'menuOn';
		
		this.cls[2][0] = 'subSub';
		this.cls[2][1] = 'subAct';
		
		var menu = $('topMenu');
		if(!menu) return;
		var zoznam = menu.select('.jsmenu');

		if(zoznam.length>0) {
			var i = 0;
			for(i=0;i<zoznam.length;i++) {
				this.Menus[i] = [];
				var riadok = zoznam[i].select('a.polozka');
				
				var y = 0;
				for(y=0;y<riadok.length;y++) {
					riadok[y].observe('mouseover', this.mouseover.bindAsEventListener(this));
					riadok[y].observe('mouseout', this.mouseout.bindAsEventListener(this));
				}
			}
		}
	},
	
	reset: function(e) {
		if(typeof(curSel) =='undefined') return;
		
		for(i=1;i<=this.Menus.length;i++) {
			var tmp = $('topMenu').select('div.level_'+i);
			var me = this;
			tmp.each(function(hid){
				var temp = hid.select('a.polozka');
				for(j=0;j<temp.length;j++) {
					if(typeof(temp[j])!='undefined'){
						try{
							temp[j].removeClassName(me.cls[i][1])
						}catch(err){};
					}
				}
			});
			
			if (i>1){
				if(tmp.length>0) {
					for(y=0;y<tmp.length;y++) {
						tmp[y].hide();
					}
				}
			}else{
				//alert(tmp);
			}
		}
		for(i=curSel.length;i>=0;i--) {
			if($('parent_'+curSel[i])){
				$('parent_'+curSel[i]).show();
				$('id_'+curSel[i]).addClassName(this.cls[i][1]);
			}
		}
		
		
	},
	
	mouseout: function(e) {
		this.timer = setTimeout(this.reset.bind(this), 1500);
	},
	
	mouseover: function(e) {
		clearTimeout(this.timer);
		var el = Event.element(e);
		if (el.nodeName=='SPAN') el = el.up('a');
		var link = el.up('.subparent');
		var level = parseInt(link.className.replace(/[^0-9]*/, ''));
		var parent = this.getParent(link);

		if(level>0 && typeof(this.cls[level])!='undefined') {
			var me = this;
			for(y=0;y<=me.Menus.length;y++) {
				$('topMenu').select('div.level_'+(level-y)).each(function(ex) {
					var tmp = ex.select('a.polozka');
					var ss = '';
					if(parent=='') ss = el.id;
					else	 ss = 'id_'+parent;
					
					if (level-y > level-1){
						for(i=0;i<tmp.length;i++) {
							try {
								tmp[i].removeClassName(me.cls[level-y][1]);
							} catch(e) {}
							try {
								if(ss==tmp[i].id){
									tmp[i].addClassName(me.cls[level-y][1]);
								}
							} catch(e) {}
						}
					}
				});
			}
		}
		if(level>0) {
			for(i=(level+1);i<=this.Menus.length;i++) {
				var tmp = $('topMenu').select('div.level_'+i);
				if(tmp.length>0) {
					for(y=0;y<tmp.length;y++) {
						tmp[y].hide();
					}
				}
			}
			var myid = parseInt(el.id.replace(/id_/, ''));
			if($('parent_'+myid)) $('parent_'+myid).show();
			if(typeof(this.cls[level])!='undefined') {
				el.addClassName(this.cls[level][1]);
			}
		}
	},
	
	getParent: function(e) {
		return e.id.replace(/parent_/,'');
	}
}


function InitMenu() {
	new menu();
}