var PC_nav = {
	navconf: [],
	selectednav: false,
	currentnav: false,
	subnavheight: '38px',
	timeout: 0,
	slidedowntimeout: 0,
	slideuptimeout: 0,
	reset_timeout: 2500,
	slideup: false,
	
	init: function() {
		if (!$('navtabs')) { return false; }
		var navtabs = $('navtabs').select('li');
		navtabs.each(function(navtab) {
			var navid = navtab.id.replace(/^navtab-/,'');
			PC_nav.navconf.push({ id: navid, subnav: (($('sub-nav-'+navid)) ? true : false)});
		});
		this.navconf.each( function(item) {
			if ($('navtab-'+item.id).className == 'current-tab') {
				PC_nav.selectednav = item.id;
				var selectednav = PC_nav.get_subnav(item.id);
				if (selectednav.subnav) {
					PC_nav.state('show',selectednav.id);
					PC_nav.currentnav = selectednav.id;
				}
			}
			Event.observe($('navtab-'+item.id), 'mouseover', function() { PC_nav.navhover(1,this.id.replace(/^navtab-/,'')); });
			Event.observe($('navtab-'+item.id), 'mouseout', function() { PC_nav.navhover(0,this.id.replace(/^navtab-/,'')); });
			if (item.subnav) {
				Event.observe($('sub-nav-'+item.id), 'mouseover', function() { PC_nav.subnavhover(1,this.id.replace(/^sub-nav-/,'')); });
				Event.observe($('sub-nav-'+item.id), 'mouseout', function() { PC_nav.subnavhover(0,this.id.replace(/^sub-nav-/,'')); });
			}
		});
		if ($('navtab-login')) {
			Event.observe($('navtab-login'), 'click', function() { PC_nav.login(); });
		}
	},
	
	navhover: function(state,name) {
		var subnav = this.get_subnav(name);
		if (!subnav) { return false; }
		switch(state) {
			case 1:
				if (this.slideup) {
					this.slideup.cancel();
					this.slideup = false;
					//$('sub-nav-'+this.currentnav).hide();
				}
				if (subnav.subnav) {
					if (this.timeout) {
						clearTimeout(this.timeout);
					}
					this.navconf.each( function(item) {
						$('navtab-'+item.id).className = '';
					} );
					$('navtab-'+subnav.id).className = 'current-tab';
					if (!$('sub-nav-'+subnav.id).visible()) {
						var subnav_visible = false;
						this.navconf.each( function(item) {
							if (item.subnav && $('sub-nav-'+item.id).visible()) {
								subnav_visible = item.id;
							}
						} );
						if (subnav_visible) {
							PC_nav.state('replace',[subnav_visible,subnav.id]);
						} else {
							PC_nav.state('down',subnav.id);
						}
					} else {
						this.navconf.each( function(item) {
							if (item.subnav && item.id != subnav.id) {
								$('sub-nav-'+item.id).hide();
								$('sub-nav-'+item.id).style.height = PC_nav.subnavheight;
							}
						} );
					}
					this.currentnav = subnav.id;
				} else {
					this.navconf.each( function(item) {
						$('navtab-'+item.id).className = '';
					} );
					if (this.currentnav) {
						clearTimeout(this.timeout);
						this.state('up',this.currentnav);
					} else {
						if (this.timeout) {
							clearTimeout(this.timeout);
						}
					}
					$('navtab-'+subnav.id).className = 'current-tab';
				}
			break;
			case 0:
				if (subnav.subnav) {
					this.timeout = window.setTimeout(function() {
						if ($('sub-nav-'+subnav.id).visible()) {
							if (PC_nav.selectednav) {
								PC_nav.navconf.each( function(item) { $('navtab-'+item.id).className = ''; } );
								var selectednav = PC_nav.get_subnav(PC_nav.selectednav);
								if (selectednav) {
									$('navtab-'+selectednav.id).className = 'current-tab';
									if (selectednav.subnav) {
										PC_nav.state('replace',[subnav.id,selectednav.id]); 
										PC_nav.currentnav = selectednav.id;
									} else {
										PC_nav.state('up',subnav.id,true);
									}
								}
							} else {
								PC_nav.state('up',subnav.id,true);
								PC_nav.navconf.each( function(item) { $('navtab-'+item.id).className = ''; } );
							}
						}
					},this.reset_timeout);
				} else {
					this.timeout = window.setTimeout(function() {
						if (PC_nav.selectednav) {
							PC_nav.navconf.each( function(item) { $('navtab-'+item.id).className = ''; } );
							var selectednav = PC_nav.get_subnav(PC_nav.selectednav);
							if (selectednav) {
								$('navtab-'+selectednav.id).className = 'current-tab';
								if (selectednav.subnav) {
									PC_nav.state('down',selectednav.id);
									PC_nav.currentnav = selectednav.id;
								}
							}
						} else {
							PC_nav.navconf.each( function(item) { $('navtab-'+item.id).className = ''; } );
						}
					},this.reset_timeout);
				}
			break;
		}
	},
	subnavhover: function(state,name) {
		switch(state) {
			case 1:
				if (this.timeout) {
					clearTimeout(this.timeout);
				}
			break;
			case 0:
				this.navhover(state,name);
			break;
		}
	},
	get_subnav: function(name) {
		var subnav = false;
		this.navconf.each(function(item) { 
			if (item.id == name) { 
				subnav = item;
			}
		});
		if (!subnav) {
			alert('Error: invalid subnav item ('+name+')');
			return false;
		} else {
			return subnav;
		}
	},
	state: function(state,name,slow) {
		switch (state) {
			case "show":
				$('sub-nav-'+name).show();
			break;
			case "hide":
				$('sub-nav-'+name).hide();
			break;
			case "up":
				if (slow) {
					PC_nav.slideup = Effect.SlideUp('sub-nav-'+name, { duration: 0.5 , transition: Effect.Transitions.sinoidal, afterFinish: function() { $('sub-nav-'+name).style.height = PC_nav.subnavheight; } } );
				} else {
					PC_nav.slideup = Effect.SlideUp('sub-nav-'+name, { duration: 0.2, afterFinish: function() { $('sub-nav-'+name).style.height = PC_nav.subnavheight; } } );
				}
			break;
			case "down":
				if (this.slideup) { 
					this.slideup.cancel();
					this.slideup = false;
				}
				PC_nav.navconf.each( function(item) {
					if (item.subnav) {
						$('sub-nav-'+item.id).hide();
						$('sub-nav-'+item.id).style.height = PC_nav.subnavheight;
					}
				} );
				PC_nav.slideup = Effect.SlideDown('sub-nav-'+name, { duration: 0.3, transition: Effect.Transitions.sinoidal, afterFinish: function() { $('sub-nav-'+name).style.height = PC_nav.subnavheight; } });
			break;
			case "replace":
				if (this.slideup) { 
					this.slideup.cancel();
					this.slideup = false;
				}
				$('sub-nav-'+name[0]).hide();
				$('sub-nav-'+name[1]).show();
				$('sub-nav-'+name[1]).style.height = PC_nav.subnavheight;
			break;
		}
	},
	login: function() {
		if (!$('navtab-login')) { return false; }
		if ($('navtab-login').prototip) {
			$('navtab-login').prototip.show();
		} else {
			var boxtitle = $('navbarlogin').select('.title')[0].innerHTML;
			var boxcontent = $('navbarlogin').select('.content')[0];
			new Tip($('navtab-login'), boxcontent, {
				title: boxtitle,
				style: 'ihvn-orange',
				border:2,
				radius: 2,
				stem: 'topRight',
				hook: { target: 'bottomMiddle', tip: 'topRight' },
				offset: { x: 4, y: -7 },
				width: 400,
				showOn: 'click',
				hideOn: { element: 'closeButton', event: 'click' },
				hideOthers: true
			});
			$('navtab-login').prototip.show();
		}
	}
};
document.observe("dom:loaded", function() {
	PC_nav.init();
});
