var Visual={};

Visual.Navigation=Class.create();
Object.extend(Object.extend(Visual.Navigation.prototype), {
	elements: new Array(),
	options: { subPosition:'left-bottom', subLeft:0, subTop:0, eventOn:'over', useIframe:true },
	
	initialize: function() {
		if (arguments.length>1) {
			if (arguments[0].subPosition) this.options.subPosition=arguments[0].subPosition;
			if (arguments[0].subLeft) this.options.subLeft=arguments[0].subLeft;
			if (arguments[0].subTop) this.options.subTop=arguments[0].subTop;
			if (arguments[0].eventOn) this.options.eventOn=arguments[0].eventOn;
			if (arguments[0].useIframe!=undefined) this.options.useIframe=arguments[0].useIframe; else this.options.useIframe=true;
			for(var i=1;i<arguments.length;i++) if ($(arguments[i])) {
				var elm=$(arguments[i]);
				var selm=$('sub'+arguments[i]);
				if (isMSIE && this.options.useIframe) {
					new Insertion.Before(selm.id,'<iframe src="" style="z-index:100;position:absolute;display:none;" frameborder="no" id="ifrm_'+arguments[i]+'"></iframe>');
					var ielm=$('ifrm_'+arguments[i]);
				} else ielm=null;
				elm._eventon=this.options.eventOn;
				this.elements.push({nav: elm, sub: selm, ifrm: ielm, status: false, eff: null});
				selm._position=this.options.subPosition;
				selm._left=this.options.subLeft;
				selm._top=this.options.subTop;
				selm._iframe=this.options.useIframe;
				this.setFunctions(this.elements.length-1);
			}
		}
	},
	
	makePosition: function(pos) {
		Element.makePositioned(this.elements[pos].nav.id);
		switch(this.elements[pos].sub._position) {
			case 'left-bottom':
				Element.setStyle(this.elements[pos].sub.id,{
					left: (this.elements[pos].nav.offsetLeft+this.elements[pos].sub._left)+'px',
					top: (this.elements[pos].nav.offsetTop+this.elements[pos].nav.offsetHeight+this.elements[pos].sub._top)+'px'
				});
				break;
			case 'right-bottom':
				Element.setStyle(this.elements[pos].sub.id,{
					left: (this.elements[pos].nav.offsetLeft+this.elements[pos].nav.offsetWidth+this.elements[pos].sub._left)+'px',
					top: (this.elements[pos].nav.offsetTop+this.elements[pos].nav.offsetHeight+this.elements[pos].sub._top)+'px'
				});
				break;
		}
		if (isMSIE && this.elements[pos].sub._iframe) {
			this.elements[pos].sub.style.display='block';
			Element.setStyle(this.elements[pos].ifrm.id,{
				left: this.elements[pos].sub.style.left,
				top: this.elements[pos].sub.style.top,
				width: this.elements[pos].sub.offsetWidth+'px',
				height: this.elements[pos].sub.offsetHeight+'px'
			});
			this.elements[pos].sub.style.display='none';
		}
		Element.undoPositioned(this.elements[pos].nav.id);
	},
	
	setFunctions: function(i) {
		if (this.elements[i].nav._eventon=='over') {
			eval('this.elements[i].nav.onmouseover=(function onmouseover() { this.show('+i+'); }).bind(this);');
			eval('this.elements[i].nav.onmouseout=(function onmouseover() { this.hide('+i+',2); }).bind(this);');
			eval('this.elements[i].sub.onmouseover=(function onmouseover() { this.show('+i+'); }).bind(this);');
			eval('this.elements[i].sub.onmouseout=(function onmouseover() { this.hide('+i+',2); }).bind(this);');
		} else {
			Element.setStyle(this.elements[i].nav.id,{cursor: 'pointer'});
			eval('this.elements[i].nav.onclick=(function onclick() { this.sw('+i+'); return false; }).bind(this);');
		}
	},
	
	sw: function(pos) {
		if (this.elements[pos].status==false) {
			this.makePosition(pos);
			this.elements[pos].sub.style.display='block';
			if (isMSIE && this.elements[pos].sub._iframe) this.elements[pos].ifrm.style.display='block';
			Element.addClassName(this.elements[pos].nav.id,'_over');
			this.elements[pos].status=true;
		} else {
			this.elements[pos].sub.style.display='none';
			if (isMSIE && this.elements[pos].sub._iframe) this.elements[pos].ifrm.style.display='none';
			Element.removeClassName(this.elements[pos].nav.id,'_over');
			this.elements[pos].status=false;
		}
	},
	
	show: function(pos) {
		for(var i=0;i<this.elements.length;i++) if (i!=pos && this.elements[i].sub) { if (this.elements[i].nav._eventon=='over') this.elements[i].sub.style.display='none'; Element.removeClassName(this.elements[i].nav.id,'_over'); }
		this.makePosition(pos);
		this.elements[pos].sub.style.display='block';
		if (isMSIE && this.elements[pos].sub._iframe) this.elements[pos].ifrm.style.display='block';
		Element.addClassName(this.elements[pos].nav.id,'_over');
		this.elements[pos].status=true;
	},
	
	hide: function(i,sec) {
		if (sec>0) { this.elements[i].status=false; setTimeout((function() {this.hide(i)}).bind(this),1000*sec); }
		else if (this.elements[i].status==false) {
			this.elements[i].sub.style.display='none';
			if (isMSIE && this.elements[i].sub._iframe) this.elements[i].ifrm.style.display='none';
			Element.removeClassName(this.elements[i].nav.id,'_over');
		}
	}
});
