addLoadEvent( function() {
	Nav = new Navigation()
} );



function Navigation()
{
	this.mainNav = document.getElementById( 'mainNav' );
	var li       = this.mainNav.getElementsByTagName( 'li' );
	var cats     = [];
	var hasSubCats, isCurMain, isPhotoPage, isTextPage, parentIsCur, url;
	
	for ( var i = 1; i < li.length; i++ ) { // i = 1 -> Skip homepage
		
		url         = li[i].getElementsByTagName( 'A' )[0].href;
		isPhotoPage = url.indexOf( 'frame.php' ) != -1;
		isTextPage  = url.indexOf( 'text.php' ) != -1;
		isCurMain   = li[i].id == 'curMain';
		isCurSub    = li[i].id == 'curSub';
		hasSubCats  = li[i].getElementsByTagName('UL').length != 0;
		parentIsCur = li[i].parentNode.parentNode.id == 'curMain';
		
		// Previous/next
		if ( !hasSubCats && (isPhotoPage || isTextPage) ) {
			cats.push( li[i] );
		}
				
		if ( isCurMain || isCurSub ) {
			var curIndex = cats.length - 1;
		}
		
		// Flyout menu
		if ( !isCurMain && !parentIsCur ) {
			li[i].onmouseover = li[i].onmouseout = function() {
				toggleCurSubNav();
				toggleHoverSubNav( this );
			}
		}
		
	} // End for
	
	if ( top.location.href.indexOf('frame.php') != -1 ) {
		//this.noWhiteFlash(); // debug
	}
	
	this.getPrevPage = function() { // Not for item-x.php
		var prevPage = cats[curIndex - 1] || cats[cats.length - 1];
		return prevPage.firstChild.href;
	}	
	
	this.getNextPage = function() { // Not for item-x.php
		var nextPage = cats[curIndex + 1] || cats[0];
		return nextPage.firstChild.href;
	}

}



// For text.php, overwritten for item-l.php and item-x.php
function keyLeft() 
{
	top.location.href = Nav.getPrevPage();
}

function keyRight()
{
	top.location.href = Nav.getNextPage();
}



function toggleCurSubNav()
{
	var curMain = document.getElementById( 'curMain' );
	if ( curMain ) {
		var curSubNav = curMain.getElementsByTagName( 'ul' )[0];
		if ( curSubNav ) {
			if ( curSubNav.className != 'fade' ) {
				curSubNav.className = 'fade';
			}
			else {
				curSubNav.className = '';
			}
		}
	}
}



function toggleHoverSubNav( obj )
{
	var hoverSubNav = obj.getElementsByTagName( 'ul' )[0];
	if ( hoverSubNav ) {
		if ( hoverSubNav.className == 'mOver' ) {
			hoverSubNav.className = '';
		}
		else {
			hoverSubNav.className = 'mOver';
		}
	}
}