﻿loaded = false;

menuLen  = 8; // Edit the number of top level menu links
curMenu  = 0;
subtimer = null;
marqueelen = 10;

	for (var qqw =1; qqw < marqueelen + 1; qqw ++) {
		document.all('m' + qqw).style.visibility='hidden';
		document.all('m' + qqw).stop();
	}

	for (qqw = 1; qqw < marqueelen + 1; qqw ++) {
		if (qqw < marqueelen) {
			document.all('m' + qqw).nextma = 'm' + (qqw+1);
			document.all('m' + qqw).onfinish = scrollnextma;
		}
		if (qqw < 5 || 10 < qqw) { setWidth('m' + qqw); }
	}
	
	document.all('s1').style.left = -1 * (document.all('s1').offsetWidth + 2);
	document.all('s1').style.top = document.all('n1').offsetHeight;
	document.all('n1').style.top = 0; document.all('n1').style.left = 0;

	var oo = document.all('n1').offsetHeight;
	for (i = 2; i < menuLen + 1; i ++) {
		document.all('n' + i).style.top = document.all('n' + (i - 1)).offsetHeight + document.all('n' + (i - 1)).offsetTop + 1;
		document.all('n' + i).style.left = 0;
		document.all('s' + i).style.left = -1 * (document.all('s' + i).offsetWidth + 2);
		document.all('s' + i).style.top = document.all('n' + i).offsetHeight + document.all('n' + i).offsetTop;
		oo += document.all('n' + i).offsetHeight + 1;
	}

	document.all('board').style.width = document.all('board').parentNode.offsetWidth;
	document.all('board').style.left = -1 * (document.all('board').offsetWidth + 1);

function scrollnextma () {
	if (curMenu == 1) { toSlide(event.srcElement.nextma); }
}

function setWidth (iid) {
	var obj = document.all(iid);
	var jj = 0;
	for (var i = 0; i < obj.children.length; i ++) {
		jj += obj.children(i).offsetWidth;
	}
	obj.style.width = jj;
}

function toSlide (iid) {
	document.all(iid).loop=1;
	document.all(iid).behavior='slide';
	document.all(iid).scrollAmount = 40;
	document.all(iid).style.visibility='visible';
}

function scrollall () {
	for (i =1; i < marqueelen + 1; i ++) {
		document.all('m' + i).style.visibility = 'hidden';
		document.all('m' + i).scrollAmount = 3000;
		document.all('m' + i).loop=-1;
		document.all('m' + i).behavior='scroll';
	}
}

function startSlide () {
	toSlide('m1');
}

function imgSwap(div, ison){
	if (div) { var obj = document.all(div); } else { var obj = event.srcElement; }
	
	if ('n' + curMenu == obj.id) { ison = 'on'; }
	
	if (ison == 'on') {
		obj.children(0).style.display = 'none';
		obj.children(1).style.display = 'block';
	} else {
		obj.children(0).style.display = 'block';
		obj.children(1).style.display = 'none';
	}
}

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){
	if(this._w3c){
		return document.getElementById(name);
	}else if(this._iex){
		return document.all[name];
	}else if(this._ns4){
		return this.getObjNS4(document,name);
	}
}
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){
	return (this._ns4) ? obj : obj.style;
}

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(){ // left position of the element
	return parseInt(this._css.left || 0);
}
MakeDiv.prototype.getTop = function(){ // top position of the element
	return parseInt(this._css.top || 0);
}
MakeDiv.prototype.getWidth = function(){ // width of the element
	if(this._ns4){
		 return this._el.document.width;
	}else{
		return this._el.offsetWidth;
	}
}	
MakeDiv.prototype.getHeight = function(){ // height of the element
	if(this._ns4){
		 return this._el.document.height;
	}else{
		return this._el.offsetHeight;
	}
}
MakeDiv.prototype.moveTo = function(x,y){ // move the element to a new position
	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){  // time-based animation
	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();
		if (this._id == 's1') { startSlide(); }
	}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(){ // clear the tweenTo method
	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 = function(name){
	if(name){
		this._inherit = MakeDiv; this._inherit(name);
		this._basex = this.getLeft();
		this._basey = this.getTop();
	}
}
MenuDiv.prototype = new MakeDiv();

function showMenu(num){
	if(! loaded){ return; }
	if (! loaded || curMenu==num || num < 1 || num > menuLen) { return; }

	curMenu = 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++){
		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 - 2, s._basey);
		imgSwap('n'+i, 'off'); // off
	}
	
	document.all('n' + menuLen).parentNode.style.height = oo + document.all('s' + curMenu).offsetHeight;
	document.all('board').style.height = document.all('board').parentNode.offsetHeight;
}

function showSub(num){
	if (num != 1) { scrollall(); }
	var s = eval('API.s'+num);
	var sx = s.getLeft() + 4;
	var sy = s._basey;
	var dx = s.getWidth();
	var dy = 0;
	s.clearTween();
	s.tweenTo(easeOutQuad, [sx,sy], [dx,dy], 5);
	imgSwap('n'+num, 'on'); // on
}

function createObjects(){
	document.all('s1').parentNode.style.visibility = 'visible';
	for(var i=1; i<=menuLen; i++){
		eval('API.n'+i+' = new MenuDiv("n'+i+'")');
		eval('API.s'+i+' = new MenuDiv("s'+i+'")');
		document.all('s' + i).style.visibility = 'visible';
	}
	loaded = true;
	showMenu(1); // open the first menu
}

API = new Detect();

createObjects();

	document.all('board').style.height = document.all('board').parentNode.offsetHeight;

window.onbeforeprint = hideall;
window.onafterprint = showall;

function hideall () {
	for(var i=1; i<=menuLen; i++){
		if (i !=curMenu ) { document.all('s' + i).style.visibility = 'hidden'; }
	}
}

function showall () {
	for(var i=1; i<=menuLen; i++){
		if (i !=curMenu ) { document.all('s' + i).style.visibility = 'visible'; }
	}
}

