var menuthread = null;
var animation = null;
var menuwidth = 97;
var dohide = false;
var menu, menusections;
var menuanim = {time:0, begin:0, change:0.0, duration:0.0, timer:null, visible:true};

var menuindent = 211;
var menu_width = 429;
var menuId		= 'menu';

var fadeTime    = 150; //milliseconds

var faded_in    = false;
var faded_out   = false;

var mHeight=0;
var mWidth=0;

function initmenu() {        	
	menu = $(menuId);
	initSetSelectedBackground();
	if(!menu) return;
	

	addEvent(menu, 'mouseover', openMenu);
	addEvent(document, 'click', closeMenu);

	var menuShield=document.createElement('DIV');
		menuShield.id='menu-shield';		
		$('contentWrapper').appendChild(menuShield);
	var menuShieldTopic=document.createElement('H2');
		menuShieldTopic.style.display='none';
		menuShieldTopic.id='menu-shield-topic';
		if(location.href.indexOf('/en/')!==-1){
			menuShieldTopic.innerHTML='<a href="../introduction">ANNUAL REPORT 2007</a>';
		}else{
			menuShieldTopic.innerHTML='<a href="../inledning">ÅRSREDOVISNING 2007</a>';
		}
		menuShield.appendChild(menuShieldTopic);

		var menuItems=menu.getElementsByTagName('SPAN'),i=0,menuItem=null;
		if(!menuItems) return;
		while(menuItem=menuItems[i++]){
			if(menuItem) menuItem.style.display='none';
		}

}

var openMenu=function(){
	if ($('flashobject')) $('flashobject').style.visibility = 'hidden';
	var menuShield=$('menu-shield');
	if(!menuShield) return;
	if(mWidth >= 380){
		$('menu-shield-topic').style.display='block';		
		menuShield.style.height='598px';
		menuShield.style.width='380px';
		mHeight=598;
		mWidth=380;
		clearTimeout(menuthread);
		menuthread=null;
		var menuItems=menu.getElementsByTagName('SPAN'),i=0,menuItem=null;
		if(!menuItems) return;
		while(menuItem=menuItems[i++]){
			if(menuItem) menuItem.style.display='block';
		}
	}else{
		//mHeight+=20;
		mWidth+=20;
		menuShield.style.height = '598px';
		menuShield.style.width = mWidth +'px';
		menuthread = setTimeout('openMenu()', 5);
	}	
	
}

var closeMenu=function(){
	if ($('flashobject')) $('flashobject').style.visibility = 'visible';
	var menuShield=$('menu-shield');
	if(!menuShield) return;
	if($('menu-shield-topic').style.display=='block'){
		$('menu-shield-topic').style.display='none';
		var menuItems=menu.getElementsByTagName('SPAN'),i=0,menuItem=null;
		if(!menuItems) return;
		while(menuItem=menuItems[i++]){
			if(menuItem) menuItem.style.display='none';
		}
	}
	if(mWidth >= 1){
		//mHeight-=40;
		mWidth-=20;
		menuShield.style.height = mHeight +'px';
		menuShield.style.width = mWidth +'px';
		menuthread = setTimeout('closeMenu()', 5);
	}else{
		menuShield.style.height='0px';
		menuShield.style.width='0px';
		//mHeight=0;
		mWidth=0;
		clearTimeout(menuthread);
		menuthread=null;
	}	
}

var initSetSelectedBackground=function(){
	var selectedMenuitem=$('selectedmenu');
	if(!selectedMenuitem) return;
	var SelectedMenuLink=selectedMenuitem.getElementsByTagName('A')[0];
	selectedMenuitem.parentNode.parentNode.style.background='url(../../images/menu/bg-selected-bottom.gif) no-repeat left bottom';
	selectedMenuitem.parentNode.style.background='url(../../images/menu/bg-selected-top.gif) no-repeat -1px top';
	var imgSelected=document.createElement('DIV');		
		imgSelected.id='selected-image';
		imgSelected.style.marginTop='-19px';
		imgSelected.style.marginLeft='-7px';
	selectedMenuitem.appendChild(imgSelected);
		
}

function showmenu(e){
	var objMenu=$(menuId);
	objMenu.style.width = menu_width +'px';
	/*
	dohide = false;
	if (animation == null) {		    			
        fadein();
		clearTimeout(menuthread);		
		menuthread = setTimeout('initexpandmenu()', 10);
	}
	*/		
}

function initexpandmenu() {
	if (menuwidth > 289) return;
	alert(menu_width);
	//move=cubicOut(menuanim.time, menuanim.begin, menuanim.change, menuanim.duration);
	//move=parseInt(move);
	//$('menu').style.width = $('menu').style.width +'px';
	/*
	for (var i = 0; i < menusections.length; i++) {
		menusections[i].style.width	= menu_width + 'px';
		menusections[i].style.left	= (-220) + 'px';
	}
	*/		
}


function hidemenu() {
	var objMenu=$(menuId);
	//objMenu.style.width = '75px';
	//Fade.Out(menuId,100);
	/*
	dohide = true;
	if (animation == null) {
		clearTimeout(menuthread);						
		menuthread = setTimeout('initcollapsemenu()', 10);				
	}
	*/
}

function initcollapsemenu() {
	menuanim.time = 0;
	menuanim.begin = 0;
	menuanim.change = menuindent;	
	for (var i = 0; i < menusections.length; i++) {
		menusections[i].style.width	= '0px';
		menusections[i].style.left	= (-221) + 'px';
	}
	fadeout();	
}

function fadein () {
    if (!faded_in) {
//        fade('fadein.gif');        
        faded_in = true;      
        faded_out = false;  
    }
}

function fadeout() {
    if (!faded_out) {
//        fade('fadeout.gif');        
        faded_in = false;
        faded_out = true;
    }
}
function fadeXXX(imgSrc) {    	    
    div = document.createElement('div');
	div.id = 'fade_box';		
	document.body.appendChild(div);
	img = document.createElement('img');
	img.src = '../../images/menu/' + imgSrc;
	img.style.height    = '506px';
	img.style.width     = '257px';
	div.appendChild(img);	
	setTimeout('document.body.removeChild(document.body.lastChild)', fadeTime);					
}

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;}
}

/* OPACITY FADE
----------------------------------------------------*/
var Fade = {
	In: function(uid,opacity){
		var obj=$(uid);
		if(!obj) return;
		if(obj.style.display=='none') obj.style.display='block';
		if(opacity <= 100){
			Fade.setOpacity(obj, opacity);
			opacity += 20;
			fadeTimer = window.setTimeout('Fade.In("'+ uid +'",'+ opacity +')', 40);
		}else{
			Fade.setOpacity(obj, 100);
			window.clearTimeout(fadeTimer);
		}
	},
	Out: function(uid,opacity){
		var obj=$(uid);
		if(!obj) return;
		if(opacity >= 5) {
			Fade.setOpacity(obj, opacity);
			opacity -= 20;
			fadeTimer=window.setTimeout('Fade.Out("'+ uid +'",'+ opacity +')', 2);
		} else {
			Fade.setOpacity(obj, 0);
			window.clearTimeout(fadeTimer);
		}
	},
	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);
	}
};
