var NavTop = Class.create();
NavTop.prototype = 
{
    initialize: function(o){
		
		this._targetLabel = o.targetLabel;
		this._hideOnOutside = o.hideOnOutside;
		this._viewTypeEvent = o.viewTypeEvent;
		this._onChange = o.onChange;
		
		this.tID = '';
		this.tempo = 500;
		this.tabSelect = new Array();
		$$('#'+this._targetLabel+' li').each(this.addEvent.bindAsEventListener(this));
		
		if (this._hideOnOutside) {
			Event.observe($(this._targetLabel), 'mouseover',this.showEvent.bind(this));
			Event.observe($(this._targetLabel), 'mouseout',this.hideEvent.bind(this));
		}
		
    },

	addEvent: function(e){
		var level = 0;
		var elt = e;
		while(elt.id != this._targetLabel){
			elt = Element.up(elt);
			if(elt.nodeName == "UL" || elt.nodeName == "OL") level +=1;
		}
		e.rel = level;
		
		this.addEventLevel(e);
	},
	
	addEventLevel: function(e){
		var a = e.getElementsByTagName('a')[0];
		
		if(!a) return;
		Event.observe(a, this._viewTypeEvent,this.showLayer.bind(this,e));
		
		if (this._hideOnOutside) {
			Event.observe(Element.up(e), 'mouseover',this.showEvent.bind(this));
			Event.observe(Element.up(e), 'mouseout',this.hideEvent.bind(this));
		}
		
	},
	
	showLayer: function(e){
		if(!e) return;
		this.hideLayer(e);
		
		var a = e.getElementsByTagName('a')[0];
		a.addClassName('view');
		if(Element.next(a)){
			Element.next(a).addClassName('view');
			this.swapImg(e,1);
		}
		else{
			this.swapImg(e,1);
		}
		
		this.tabSelect[e.rel-1] = e;
		
		if (this._onChange) this._onChange(e);		
	},
	
	hideLayer: function(o){
		for(var i=0;i< this.tabSelect.length;i++){
			var e = this.tabSelect[i];
			
			if(e && e.rel >= o.rel ){
				var a = e.getElementsByTagName('a')[0];
				a.removeClassName('view');				
				if(Element.next(a)){
					Element.next(a).removeClassName('view');
				}
				if(e.rel) this.swapImg(e,0);
			}
		}
	},
	
	showEvent: function(o){
		clearTimeout(this.tID);
	},
	
	hideEvent: function(){
		clearTimeout(this.tID);
		this.tID = setTimeout(this.hideAll.bind(this),this.tempo);
	},
	
	hideAll: function(){
		this.hideLayer({rel:1,init:true});
	},
	
	select: function(o){
		$$('#'+this._targetLabel+' li').each(this.showSelect.bindAsEventListener(this,o));
	},
	
	showSelect: function(e,o){
		try {
			if(o.descendantOf(e)){
				var a = e.getElementsByTagName('a')[0];
				if (e.rel == 2 || e.rel == 3) a.up().addClassName('on');
				this.tabSelect[e.rel-1] = e;
				if(Element.next(a)){
					if (Element.next(a).tagName.toLowerCase()=='ol') { Element.next(a).addClassName('view') }
					else { Element.next(a).addClassName('on'); }
				}
				if (this._onChange) this._onChange(e);
			}
		}
		catch(e){}
	},
	
	setSwap:function (swap) {
		this._navToSwap = swap;
	},
	
	swapImg: function(e,stat){
		if (this._navToSwap) this._navToSwap.roll(e,stat);
	}
 };