var menuthread=null;
var ribbonthread=null;
var animation=null;
var menuwidth=43;
var dohide=false;
var showribbon=false;
var menu, menusections, currentribbonid;
var menuanim={time:0, begin:0, change:0.0, duration:0.0, timer:null, visible:false};

var initmenu=function(){

	menu=$('menu');
	if(!menu) return;

	//## SET BODY TO ALLOW DYNAMIC JS-MENU
	document.getElementsByTagName('body')[0].id='doscript';

	//## ADD MOUSE-EVENT ON MENU
	addEvent(menu, 'mouseover', Menu.Show);
	addEvent(menu, 'click', Menu.Show);
	addEvent(menu, 'mouseout', Menu.Hide);
	addEvent(menu, 'mousemove', Menu.Update);
	menu.className='';

	menusections=new Array();
	tmpSections=menu.getElementsByTagName('div');

	var n=tmpSections.length;
	for (var i=0; i<n; i++){
		if (tmpSections[i].id != ''){
			if (tmpSections[i].parentNode.className=='active'){
				currentribbonid=tmpSections[i].id.replace('color', 'ribbon');
			}
			menusections.push(tmpSections[i]);

			//## CREATE A RIBBON FOR THIS MENYITEM
			var ribbonContainer=document.createElement('div');
			ribbonContainer.id=tmpSections[i].id.replace('color', 'ribbon');
			ribbonContainer.className='ribbon';
			$('wrap').appendChild(ribbonContainer);
		}
	}
	
}

var currentMenuItem = -1;

function focusMenuItem(direction) {
	a = menu.getElementsByTagName('a');
	if (currentMenuItem == -1) {
		if (direction == 1)
			currentMenuItem = 0;
		else
			currentMenuItem = a.length - 1;
	}
	else {
		currentMenuItem = currentMenuItem + direction;
		if (currentMenuItem < 0 || currentMenuItem > a.length - 1) {
			currentMenuItem = currentMenuItem - direction;
		}
	}
	a[currentMenuItem].focus();
}

var Menu={
	Show : function(e){
		dohide=false;
		if(animation==null){
			if (e == null) {
				if (menuwidth > 250) {
					this.Hide(null);
					return;
				}
			}
			if (menuwidth > 250 || e == null || (e.clientY < 545 && menuwidth < 250)) {
				clearTimeout(menuthread);
				menuthread=setTimeout('Menu.InitExpand()', 200);
			}
		}
	},
	Hide : function(){
		dohide=true;
		if (animation==null && menuwidth >= 250){
			clearTimeout(menuthread);
			menuthread=setTimeout('Menu.InitCollapse()', 200);
		}		
	},
	Update : function(){
		//menuthread=null;
		//window.status=animation;
	},
	InitExpand : function(){
		if (menuwidth > 250 || dohide) return;
		dohide=false;
		menu.className='visible';
		menuanim.time=0;
		menuanim.begin=0;
		menuanim.change=256;
		menuanim.duration=15;
		Menu.Expand();
		menuthread=null;
	},
	InitCollapse : function(){
		dohide=true;
		menuanim.time=0;
		menuanim.begin=0;
		menuanim.change=256;
		Menu.Collapse();
	},
	Expand : function(){
		if (menuwidth >= 256){
			menuthread=null;
			animation=null;
			currentribbon=$(currentribbonid);
			if(currentribbon){
				currentribbon.style.width='10px';
				currentribbon.style.display='block';
				Menu.ExpandRibbon();
			}
			if(dohide && menuwidth >= 250) Menu.Hide();
		}else{
			move=cubicOut(menuanim.time, menuanim.begin, menuanim.change, menuanim.duration);
			move=parseInt(move);
			for (var i=0; i < menusections.length; i++){
				menusections[i].style.width= move + 'px';
				menusections[i].style.left=(-move) + 'px';
			}
			menuanim.time++;
			menuwidth=move;

			clearTimeout(animation);
			animation=setTimeout('Menu.Expand()', 30);
		}
	},
	Collapse : function(){
		if(menuanim.time > menuanim.duration){
			menuthread=null;
			animation=null;
			dohide=false;
			if($(currentribbonid)){
				$(currentribbonid).style.display='none';
			}
		}else{
			move=cubicIn(menuanim.time, menuanim.begin, menuanim.change, menuanim.duration);
			var n=menusections.length;
			for (var i=0; i < n; i++){
					menusections[i].style.width=(256 - move) + 'px';
					menusections[i].style.left=(move - 256) + 'px';
			}
			menuanim.time++;
			menuwidth=(256 - move);
			clearTimeout(animation);
			animation=setTimeout('Menu.Collapse()', 30);
		}
	},
	ExpandRibbon : function(){
		setOpacity(currentribbon,50);
		if(parseInt(currentribbon.style.width) >= 1000 || (parseInt(currentribbon.style.width)+68) >= 1000){
			currentribbon.style.width='1000px';
			ribbonthread=null;
		}else{
			currentribbon.style.display='block';
			currentribbon.style.width=(parseInt(currentribbon.style.width) + 68) + 'px';
			ribbonthread=setTimeout('Menu.ExpandRibbon()', 30);
		}
	}
}


/* CHANGE OPACITY ON ELEMENTS
----------------------------------------------------*/
var setOpacity=function(obj, opacity){
	if(!obj) return;
	obj.style.filter='alpha(style=0,opacity:' + opacity + ')';
	obj.style.KHTMLOpacity=(opacity/100);
	obj.style.MozOpacity=(opacity/100);
	obj.style.opacity=(opacity/100);
};


/* CODE FOR THE RIBBON HOVER EFFECT 
----------------------------------------------------*/
function linear(t, b, c, d){return c*t/d + b;}
function sineInOut(t, b, c, d){return -c/2 * (Math.cos(Math.PI*t/d) - 1) + b;}
function cubicIn(t, b, c, d){return c*(t/=d)*t*t + b;}
function cubicOut(t, b, c, d){return c*((t=t/d-1)*t*t + 1) + b;}
function cubicInOut(t, b, c, d){
	if ((t/=d/2) < 1) return c/2*t*t*t + b;
	return c/2*((t-=2)*t*t + 2) + b;
}

function bounceOut(t, b, c, d){
	if ((t/=d) < (1/2.75)){
		return c*(7.5625*t*t) + b;
	} else if (t < (2/2.75)){
		return c*(7.5625*(t-=(1.5/2.75))*t + .75) + b;
	} else if (t < (2.5/2.75)){
		return c*(7.5625*(t-=(2.25/2.75))*t + .9375) + b;
	} else {
		return c*(7.5625*(t-=(2.625/2.75))*t + .984375) + b;
	}
}