// :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

loaded = false;

// :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
// Utilities

function preload(objname,imgsrc){
	eval(objname+' = new Image()');
	eval(objname+'.src = "'+imgsrc+'"');
}
function imgSwap(imgname,imgobj,divnames){
	if(document.layers && divnames != null){
		divnames = divnames.split(',');
		var d = '';
		for(var i=0; i<divnames.length; i++){
			d += 'document.'+divnames[i]+'.';
		}
		eval(d+'document.images["'+imgname+'"].src = '+imgobj+'.src');
	}else{
		document.images[imgname].src = eval(imgobj+'.src');
	}
}
function setStatus(msg){
	window.status = msg;
	return true;
}

// :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
// Detect Constructor

Detect = function() {
	var agent = navigator.userAgent.toLowerCase(); 
	this._mac = agent.indexOf('mac') != -1;
	this._win = !this._mac;
	this._w3c = document.getElementById;
	this._iex = document.all;
	this._ns4 = document.layers;
}
Detect.prototype.getObj = function(name){
	var ret = null;

	if(this._w3c){
		ret = document.getElementById(name);
	}else if(this._iex){
		ret = document.all[name];
	}else if(this._ns4){
		ret = this.getObjNS4(document,name);
	}
	
	return ret;
}
Detect.prototype.getObjNS4 = function(obj, name){
	var d = obj.layers;
	var result,temp;
	for(var i=0; i<d.length; i++){
		if(d[i].id == name){
		 	result = d[i];
		}else if(d[i].layers.length){
			var temp = this.getObjNS4(d[i],name);
		}
		if(temp){
			result = temp;
		}
	}
	return result;
}
Detect.prototype.getStyle = function(obj){
	var ret = (this._ns4) ? obj : obj.style;
	return ret;
}
Detect.prototype.getWindowWidth = function(){
	var ret = this._iex ? document.body.clientWidth : window.innerWidth;
	return ret;
}
Detect.prototype.getWindowHeight = function(){
	var ret = this._iex ? document.body.clientHeight : window.innerHeight;
	return ret;
}
Detect.prototype.getScrollTop = function(){
	var ret = this._iex ? document.body.scrollTop : window.pageYOffset;
	
	if (ret > 104)
		ret -= 159;
	
	return ret;
}
Detect.prototype.getScrollLeft = function(){
	var ret = this._iex ? document.body.scrollLeft : window.pageXOffset;
	return ret;
}

// :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
// MakeDiv Constructor --> inherit from Detect

MakeDiv = function(name){
	if(name){
		this._inherit = Detect; this._inherit(name);
		this._id  = name;
		this._el  = this.getObj(this._id);
		this._css = this.getStyle(this._el);
		this._obj = name+'Object'; eval(this._obj+'=this');
		this._timer = null;
		this._tweenRunning = false;
		return this;
	}
}
MakeDiv.prototype = new Detect();

MakeDiv.prototype.getLeft = function(){
	var ret = parseInt(this._css.left || 0);
	return ret;
}
MakeDiv.prototype.getTop = function(){
	var ret = parseInt(this._css.top || 0);
	return ret;
}
MakeDiv.prototype.getWidth = function(){
	var ret = null;
	
	if(this._ns4){
		 ret = this._el.document.width;
	}else{
		ret = this._el.offsetWidth;
	}
	
	return ret;
}	
MakeDiv.prototype.getHeight = function(){
	var ret = null;

	if(this._ns4){
		 ret = this._el.document.height;
	}else{
		ret = this._el.offsetHeight;
	}
	
	return ret;
}
MakeDiv.prototype.moveTo = function(x,y){
	if(this._ns4){
		this._el.moveTo(x,y);
	}else{
		this._css.left = x;
		this._css.top  = y;
	}
}
MakeDiv.prototype.tweenTo = function(method, start, distance, time){
	if(!this._tweenRunning){
		this._tweenTime = 0;
		var s = '['+start.toString()+']';
		var d = '['+distance.toString()+']';
		this._timer = setInterval(this._obj+'.tweenTo('+method+','+s+','+d+','+time+')', 100);
		this._tweenRunning = true;
	}
	if(++this._tweenTime > time){
		this.clearTween();
	}else{
		var x = method(this._tweenTime, start[0], distance[0], time);
		var y = method(this._tweenTime, start[1], distance[1], time);
		this.moveTo(x,y);
	}
}
MakeDiv.prototype.clearTween = function(){
	clearInterval(this._timer);
	this._timer = null;
	this._tweenRunning = false;
}
easeOutQuad = function(t, b, c, d){
	t /= d;
	return -c * t*(t-2) + b;
}
easeOutExpo = function(t, b, c, d){
	return c * ( -Math.pow( 2, -10 * t/d ) + 1 ) + b;
}

// :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
// MenuDiv Constructor --> inherit from MakeDiv

MenuDiv = function(name){
	if(name){
		this._inherit = MakeDiv; this._inherit(name);
		this._basex = this.getLeft();
		this._basey = this.getTop();
	}
}
MenuDiv.prototype = new MakeDiv();

// :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
// Open Menu Functions

function showMenu(num){
	if(loaded){
		curMenu = (curMenu==num) ? 0 : num;
		clearTimeout(subtimer);
		subtimer = null;
		if(curMenu != 0){
			var subHeight = eval('API.s'+num).getHeight();
			subtimer = setTimeout('showSub('+num+')', 500);
		}
		for(var i=1; i<=menuLen; i++){
			imgSwap('i'+i, 'i'+i+'_off', 'navigation, n'+i);
			var ymove = (i<=num || curMenu==0) ? 0 : subHeight;
			var n = eval('API.n'+i);
			var s = eval('API.s'+i);
			var sx = n._basex;
			var sy = n.getTop();
			var dx = 0;
			var dy = (n._basey-n.getTop())+ymove;
			n.clearTween();
			n.tweenTo(easeOutExpo, [sx,sy], [dx,dy], 5);
			s.clearTween();
			s.moveTo(s._basex, s._basey);
		}
	}
}

// :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
// Scroll Menu Functions

function ieScroll(){
	if(!scrolling){
		scrolling = true;
		scrollMenu();
	}
}
function scrollMenu(){
	if(API.getWindowHeight() >= allowScroll){
		var n  = API.navigation;
		var ty = n._basey+n.getScrollTop();
		var my = n.getTop();

		n._css.visibility = 'hidden'; // fixes redraw problem for nested divs
		n._css.top = my+(ty-my)/3;
		n._css.visibility = 'visible'; // fixes redraw problem for nested divs
		if(API._iex && Math.abs(ty-my)<=3){
			n._css.top = ty;
			clearTimeout(menuTimeout);
			menuTimeout = null;
			scrolling = false;
		}else{
			menuTimeout = setTimeout('scrollMenu()',50);
		}
	}
}
function handleResize(){
	if(API.getWindowHeight() < allowScroll){
		API.navigation._css.top = API.navigation._basey;
		window.scrollTo(0,0);
	}else{
		scrollMenu();
	}
}
function handleErrors(){
	return true;
}


// Build the navigation, called onload

function createObjects(cur){
	menuLen = 1; // the number of top level menu links. Not zero based
	allowScroll = 400; // if the window height is smaller than this number, the menu will not scroll
	curMenu = 0;
	subtimer = null;
	API = new Detect();
	
	// Create 'MenuDiv' objects
	if (navigator.userAgent.toLowerCase().indexOf("mac") != (-1)) 
		if (typeof(document.getElementById) != "undefined")
			return;
	
	API.navigation = new MenuDiv('navigation');
	for(var i=1; i<=menuLen; i++){
		eval('API.n'+i+' = new MenuDiv("n'+i+'")');
		eval('API.s'+i+' = new MenuDiv("s'+i+'")');
	}
	
	// If the client's browser is Explorer, the function 'ieScroll' is assigned to
	// onscroll event handler. This allows the scrolling function to be activated
	// only after the window has been scrolled, and then the setTimout can be cleared
	// when the menu has reached it's target y position.
	// If the client's browser is Netscape, the onscroll event handler is not available,
	// so the function 'scrollMenu' is assigned to a setTimout and never cleared.
	menuTimeout = null;
	scrolling = false;
	if(API._iex){
		document.body.onscroll = ieScroll;
	}else{
		scrollMenu();
	}
		
	// objects created, scripts can now be called
	loaded = true;

	// a menu can be forced open onload by passing it's id number in as an argument
	if(cur != null){
		showMenu(cur);
	}
	
	// fix Mac IE frame scrollbar bug
	if(API._mac && API._iex){
		document.body.scroll = 'yes';	
	}
}

window.onresize = handleResize;
window.onerror = handleErrors;

// :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
