/***
dhtml_menu.js - Andy, 1 Oct 2002.
Desc --
Functions and variables for DHTML menu animation.
*/

/***
Customisable Variables --
menu_mousedown - Bool, indicates if a scroll button (up/down) is active or not.
menu_speed - Int, time in milliseconds to loop scrolling function.
menu_chunk - Int, amount of pixels to move menu by on the first call of the scrolling function.
menu_chunk_accelerate - Int, amount of pixels to accelerate by on each repetition of the scrolling function.
menu_padding - Int, amount of pixels space to leave at the bottom of the menu when scrolling.
menu_fade_count - Int, amount of frames to repeat the menu_fade function.
*/
var menu_mousedown = 0;
var menu_speed = 20;
var menu_chunk = 2;
var menu_chunk_accelerate = 0.02;
var menu_padding = 10;
var menu_fade_count = 8;

/***
Script Variables --
menu_fade_timeout_id - Int, used to hold the iTimeoutID returned by window.setTimeout.
menu_scroll_count - Int, how many times the menu_scroll function has run.
user_dom - Bool, indicates if the user is using a DOM enabled browser.
user_mac - Bool, indicates if the user is on a Macintosh.
habitat - String, holds document.location.
*/
var high=0
var menu_fade_timeout_id = 0;
var menu_scroll_count = 0;
var user_dom = (document.getElementById);
var user_mac = (navigator.appVersion.indexOf("Macintosh") > -1);
var habitat = document.location.toString();
var firstTime = true

if (user_mac) { menu_chunk *= 2; }

function menu_jump(id, dir) { 
	/***
	menu_jump - Andy, 1 Oct 2002.
	Desc --
	Lets the user 'jump' to the top or bottom of the menu.
	NOTE: Only works on DOM browsers.
	*/
	if (user_dom) {
		var pos = 0;
		var lyr = document.getElementById(id+"_content");
		var viewheight = document.getElementById(id+"_container").offsetHeight
		if (dir == "down" && (lyr.offsetHeight > viewheight)) {
			pos = lyr.offsetHeight-menu_padding;			
			} 
		lyr.style.top = pos +"px";;
	}
}



function menu_scroll_start(id, dir) {
	/***	Starts the menu scrolling process.	*/	
	 
	if (user_dom) {
		//initialses the layer ypos 
		if (firstTime){
		document.getElementById(id+"_content").style.top=0;
		document.getElementById(id+"_content").style.left=0;
		firstTime= false;
		} 	
	window.clearTimeout(menu_fade_timeout_id); 
	}
	menu_active = 1;
	if (dir=="down" || dir=="up")
		{menu_scroll(id, dir, menu_chunk);}
	else
		{menu_scroll_horizontal(id, dir, menu_chunk);}
	
}

function menu_scroll_end() {
	/***	Ends the menu scrolling process. */
	menu_active = 0;
}

function menu_scroll(id, dir, chunk) {
	/*** Scrolls the DHTML menu up or down (dir).	*/ 
	menu_scroll_count++;
	var lyr = menu_lyr(id);
	var pos = (dir == "down")?(lyr.y - chunk):(lyr.y + chunk);
	
	if (user_dom) {		
		// DOM DHTML..
		var viewheight = document.getElementById(id+"_container").offsetHeight
		var low = (lyr.offsetHeight + menu_padding - viewheight) * -1;	
		
		window.status = lyr.id;
		//alert("lyr.offsetHeight=" + lyr.offsetHeight + ";viewheight=" + viewheight);

		if (lyr.offsetHeight>viewheight){
			if (pos <= high && pos >= low) {
				lyr.style.top = pos +"px";} 
			else {lyr.style.top = (dir == "down")?low + "px":high + "px";}
		}

	} else {
		// NS 4 DHTML..
		if (pos <= high) {
			lyr.y = pos;
		} else {
			lyr.y = high;
		}
	}
	if (menu_active) {
		chunk += menu_chunk_accelerate;
		window.setTimeout("menu_scroll('" + id + "', '" + dir + "', " + chunk + ")", menu_speed);
	}	else {
		menu_fade_timeout_id = window.setTimeout("menu_fade('" + id + "', '" + dir + "', " + chunk + ", 0)", menu_speed)
	}
}

function menu_scroll_horizontal(id, dir, chunk) {
 	menu_scroll_count++;
	var lyr = menu_lyr(id);
	var pos = (dir == "right")?(lyr.x - chunk):(lyr.x + chunk);
	
	if (user_dom) {		
		// DOM DHTML..
		
		var viewwidth = document.getElementById(id+"_container").offsetWidth
		var low = (lyr.offsetWidth + menu_padding - viewwidth) * -1;	
		
		window.status = lyr.id;
		//alert("lyr.offsetWidth=" + lyr.offsetWidth + ";viewwidth=" + viewwidth);
		if (lyr.offsetWidth > viewwidth)
		{
			if (pos <= high && pos >= low) {
				lyr.style.left = pos +"px";} 
			else {lyr.style.left = (dir == "left")?low + "px":high + "px";
			}
		}

	} else {
		// NS 4 DHTML..
		if (pos <= high) {
			lyr.x = pos;
		} else {
			lyr.x = high;
		}
	}
	if (menu_active) {
		chunk += menu_chunk_accelerate;
		window.setTimeout("menu_scroll_horizontal('" + id + "', '" + dir + "', " + chunk + ")", menu_speed);
	}	else {
		menu_fade_timeout_id = window.setTimeout("menu_fade_horizontal('" + id + "', '" + dir + "', " + chunk + ", 0)", menu_speed)
	}
}

function menu_fade(id, dir, chunk, count) {
	/***	Stops the DHTML menu scrolling by decelerating to a stop.	*/
	var lyr = menu_lyr(id);
	var pos = (dir == "down")?(lyr.y - chunk):(lyr.y + chunk);
	if (user_dom) {
		// DOM DHTML..
		var viewheight = document.getElementById(id+"_container").offsetHeight
		var low = (lyr.offsetHeight + menu_padding - viewheight) * -1;	
		
		if (lyr.offsetHeight>viewheight){
			if (pos <= high && pos >= low) {
				lyr.style.top = pos +"px";
			} else {
				lyr.style.top = (dir == "down")?low + "px":high + "px";
			}
		}
	} else {
		// NS 4 DHTML..
		if (pos <= high) {
			lyr.y = pos;
		} else {
			lyr.y = high;
		}
	}
	if (!menu_active) {
		count++;
		if (count < menu_fade_count) {
			menu_fade_timeout_id = window.setTimeout("menu_fade('" + id + "', '" + dir + "', " + chunk + ", " + count + ")", count*menu_speed);
		}
	}
}

function menu_fade_horizontal(id, dir, chunk, count) {
	/***	Stops the DHTML menu scrolling by decelerating to a stop.	*/
	var lyr = menu_lyr(id);
	var pos = (dir == "right")?(lyr.x - chunk):(lyr.x + chunk);
	if (user_dom) {
		// DOM DHTML..
		var viewwidth = document.getElementById(id+"_container").offsetWidth
		var low = (lyr.offsetWidth + menu_padding - viewwidth) * -1;	
		
		if (lyr.offsetWidth>viewwidth){
			if (pos <= high && pos >= low) {
				lyr.style.left = pos +"px";
			} else {
				lyr.style.left = (dir == "right")?low + "px":high + "px";
			}
		}
	} else {
		// NS 4 DHTML..
		if (pos <= high) {
			lyr.x = pos;
		} else {
			lyr.x = high;
		}
	}
	if (!menu_active) {
		count++;
		if (count < menu_fade_count) {
			menu_fade_timeout_id = window.setTimeout("menu_fade_horizontal('" + id + "', '" + dir + "', " + chunk + ", " + count + ")", count*menu_speed);
		}
	}
}

function menu_lyr(id) {
	/***	Returns a custom layer object.	*/
	var lyr = new Object();
	if (user_dom) {
		lyr = document.getElementById(id + "_content");
		//lyr.y = lyr.style.pixelTop;
		lyr.y = parseInt(lyr.style.top)
		lyr.x = parseInt(lyr.style.left)
		//alert (id + "_content;" + "lyr.y=" + lyr.style.top + ";lyr.x=" + lyr.style.left)
		//window.alert("lyr.y="+lyr.y)
	} else {
		lyr = document.eval(id + "_container").document.eval(id + "_content");
		lyr.y = lyr.top;
		lyr.x = lyr.left;
	}
	return(lyr);
}