// Copyright 2002, 2004 Factor of 4, LLC -- www.factorof4.com
// All Rights Reserved.
//
// Version 2004-07-30 GGL

// This version does rollover highlighting of the main navigation bar
// and optional drop-down menus for subnavigation.
// No state is maintained to visually indicate the current page.

// There are three "kinds" of main navigation:  1 for the home page,
// 2 for the secondary pages, and 3 for the occasional tertiary page.

// Main navigation bar images are kept in images_nav/ and are named
// main_<kind>_<state>.jpg, where <kind> is 1-3, and state is 0-4.
// State corresponds to which element (1 to 4) is highlighted, with
// zero meaning no highlight.

// Initialization are performed by:
// (a) calling navPreLoad(kind, depth) after loading this file, typically in the head element.
// (b) calling navInitialize(kind) in the body onload handler.

// Finalization is done by navFinalize(), which is assigned by navInitialize() as the onunload 
// handler for the document.

// mouseoff events should be handled by navShowDefault();
// mouseover events should be handled by navShow(state) or navOverMain(state).

// navigation will not respond unless (until) both of these are true
var nav_loaded = 0; // true if this file has been completely executed
var nav_inited = 0; // true if navInitialize() has been called

var nav_main_kind; // set by navPreLoad; 1, 2, or 3

var nav_main_current = 0; // which area is the mouse over? (0 means none)

var navTimer; // used to delay responses to mouseoff events.

var nav_main_img; // The image element named "nav_main" (set during initialization).
var nav_main_images; // Preloaded images for the main bar.

// --- sub navigation menus
var nav_sub_div_key = ['PortfolioMenu', 'GalleryMenu', 'CollectorsMenu', 'BuyMenu', 'ContactMenu']; 
	// maps subnav menu div index to key
var nav_sub_div_state_index = [1, 2, 3, 4, 5]; // maps div index to state index
var nav_sub_div; // array, maps div index to div element

if ( typeof(logWrite) == 'undefined' ) { logWrite = function () {} } // default no logging

function navPreLoad (kind, depth) {
	// kind indicates the navigation kind (1: home, 2: secondary, 3: tertiary).
	// depth indicates number of containers: zero for /page, 1 for /dir/page, etc.
	nav_main_kind = kind;
	var prefix = '';
	while ( depth > 0 ) {  prefix = prefix + '../';  depth-- }
	nav_main_images = new Array(); 
	for ( var i = 0; i <= 5; i++ ) { 
		(nav_main_images[i] = new Image()).src = prefix + "nav_images/main_" + nav_main_kind + "_" + i + ".jpg";
	}
}

function navInitialize (kind) { // set up and show default options for page
	if ( typeof(nav_main_images) == "undefined" ) {
		//alert("Page did not call navPreLoad(kind, depth)");
		return;
	}
	// find the navigation elements
	nav_main_img = imageSearchSuffix("nav_main");
	nav_sub_div = 
		navFindSubMenus('nav_main_container', nav_sub_div_key);
	
	// set state and update display
	nav_main_kind = kind;
	nav_inited = 1;
	document.onunload = navFinalize;
	navShowDefault();
}

function navFinalize() {
	// make sure that no further events are processed.
	nav_main_inited = false;	
	clearTimeoutTimeout(navTimer);
}

function imageSearchSuffix (suffix) {
	// search for and return an image for which the name attribute
	// ends in the string suffix.
	// Used to overcome GoLive's habit of changing tag name attributes. 
	for (var i = 0; i < document.images.length; i++ ) {
		var name = document.images[i].name;
		if ( name.substring(name.length - suffix.length, name.length) == suffix ) {
			return document.images[i];
		}
	}
	alert("No matching image found for suffix '" + suffix + "'.");
	return; // no such image
}

function navFindSubMenus (containerId, divIdSuffixes) {
	// searches for an element with id=containerId, and within
	// that element finds div elements whose ids have a suffix
	// in the array divIdSuffixes.   Returns an array whose elements
	// are the div elements corresponding to the elements of divIdSuffixes.
	
	var container = document.getElementById(containerId);
	if ( ! container ) { 
		alert("navFindSubMenus() can't find element with id " + containerId);
		return null;
	}
	var result = new Array;
	var divs = container.getElementsByTagName('div');
	if ( ! divs || ! divs.length) {
		alert("navFindSubMenus() can't get div elements")
		return null;
	}
	var d;
	var s;
	for ( d = 0; d < divs.length; d++ ) {
		var id = divs[d].id;
		if ( typeof(id) == 'string' ) {
			search: for ( s = 0; s < divIdSuffixes.length; s++ ) {
				var suffix = divIdSuffixes[s];
				if ( id.substring(id.length - suffix.length, id.length) == suffix ) {
					result[s] = divs[d];
					break search;
				}
			}
		}
	}
	for ( s = 0; s < divIdSuffixes.length; s++ ) {
		if ( ! result[s] ) {
			alert("navFindSubMenus() can't find div with id suffix " + divIdSuffixes[s]);
		}
	}
	return result;
}

function navSetVisibility(element, visibility) {
/* 	if ( element.style.setProperty ) { */
/* 		element.style.setProperty('visibility', visibility, '') */
/* 	} */
/* 	else  */
	if ( element.style ) {
		element.style.visibility = visibility;
	} 
	else if ( element.visibility ) { // nn4??
		alert("navSetVisibility() nn4?");
		element.visibility = ( visibility == 'visible' ? 'show' : 'hide' );
	}
	else {
alert("navSetVisibility() -- no method");
	}
}

function navShow (main) { // all image changes happen here
	if ( nav_loaded && nav_inited ) {
		logWrite('', 'navShow('+main+') entered');
		clearTimeout(navTimer);
		nav_main_img.src = nav_main_images[main].src;
		nav_main_current = main;
		// open the subnav menu, close others.
		// **** should optimize this ****
/* 		var d; */
/* 		for ( d = 0; d < nav_sub_div_state_index.length; d++ ) { */
/* 			if ( nav_sub_div_state_index[d] == main ) { // show div d */
/* 				navSetVisibility(nav_sub_div[d], 'visible'); */
/* 			} */
/* 		} */
		navShowSub(main);
	}
}

function navShowSub (main) {
	// open the subnav menu, close others.
	if ( nav_loaded && nav_inited && nav_sub_div ) {
		var d;
		for ( d = 0; d < nav_sub_div_state_index.length; d++ ) {
			if ( nav_sub_div_state_index[d] == main ) { // show div d
//alert("navShowSub("+main+") show " + d);
				navSetVisibility(nav_sub_div[d], 'visible');
			}
			else { // hide div d
//alert("navShowSub("+main+") hide " + d);
				navSetVisibility(nav_sub_div[d], 'hidden');
			}
		}
	}
	else {
//alert("navShowSub("+main+") not active");
	}
}

function navShowDefault () { // refresh the default hilighting
	logWrite('', 'navShowDefault() entered');
	clearTimeout(navTimer); // in case there is one already queued.
	navTimer = setTimeout("navTimerEvent()", 500); // delay, to allow other options
}

function navTimerEvent () {
	// when the timer goes off, we reset the nav to the default state.
	logWrite('', 'navTimerEvent() entered');
	navShow(0);
}

function navOverMain (main) { // onmouseover a main option
	logWrite('', 'navOverMain() entered');
//	if ( main != nav_main_current ) {
		navShow(main);
//	}
}


nav_loaded = 1;
/* navShowDefault(); */

