
menu = [];
menuStat = [];
menuSteps = 10;
menuTimer = 200;
menuStay = 20;
thisZ = 1000;

function setMenuStat(inMenuId,inStat,inId){
	
	found = 0;
	id = 0;
	for(i=0;i<menuStat.length;i++){
		
		if (menuStat[i][0]==inMenuId){
			found = 1;
			id=i;
			break;
		}
	}
	if (found){
		menuStat[id][inId] = inStat;
		return 1;
	}else{
		return 0;
	}
}
function getMenuStat(inMenuId,inId){
	found = 0;
	id = 0;
	for(i=0;i<menuStat.length;i++){
		if (menuStat[i][0]==inMenuId){
			found = 1;
			id=i;
			break;
		}
	}
	if (found){
		return menuStat[id][inId];
	}else{
		return 0;
	}
}
function addMenu(inMenuId,inSubMenuId){
	//set menu width
	
	//obj1 = document.getElementById('menuc_'+inMenuId)
	//obj2 = document.getElementById('menu_'+inMenuId)
	//obj3 = document.getElementById('menu_'+inMenuId+'_'+inSubMenuId)
	//obj1.style.width = obj2.style
	
	
	
	menu.push([inMenuId,inSubMenuId]);
	found = 0;
	for(i=0;i<menuStat.length;i++){
		if (menuStat[i][0]==inMenuId){
			found = 1;
			break;
		}
	}
	if (!found){
		menuStat.push([inMenuId,1,0,0,0,0]);
		
	}
	document.getElementById('menuc_'+inMenuId).style.zIndex=thisZ;
	thisZ++;
}

function showMenu(inMenuId,x,y,inSpeed){
	//loop through all menus and remove
	
	//find if menu is a parent before hiding
	menuTimer = inSpeed;
	
	//window.status = menuTimer;
	
	
	safe = 1000;
	ids = [inMenuId];
	subid = inMenuId;
	for(i=0;i<menu.length;i++){
		safe--;
		if (safe<=0){
			break;
		}
		if (menu[i][1]==subid){
			ids.push(menu[i][0]);
			subid = menu[i][0];
			i=-1;
		}
	}
	
	
	for(i=0;i<menuStat.length;i++){
		if (menuStat[i][0]!=inMenuId){
			f=0;
			for(ii=0;ii<ids.length;ii++){
				if (ids[ii]==menuStat[i][0]){
					f = 1;
				}
			}
			if (f==0){
				hideMenu(menuStat[i][0]);
			}
			//alert(menuStat[i][0]);
		}
	}
	
	
	menuTimerReset(inMenuId);
	if (getMenuStat(inMenuId,1)!=1){
		return;
	}
	
	obj = document.getElementById('menu_' + inMenuId);
	x = moveXbySlicePos (x, obj);
	y = moveYbySlicePos (y, obj);
	//alert("x=" + x + ",y=" + y);
	
	//find menu
	thisMenus = [];
	thismenu = 0;
	for(i=0;i<menu.length;i++){
		if (menu[i][0]==inMenuId){
			thismenu=1;
			thisMenus.push(i);
		}
	}
	//alert(thisMenus.length);
	if (!thismenu){
		return false;
	}
	
	//set props to make them appear
	//obj = document.getElementById('menu_' + menu[thisMenus[0]][0] + '_' + menu[thisMenus[0]][1]+'_1');
	thisx = 0;
	thisy = 0;

	for(i=0;i<thisMenus.length;i++){
		obj = document.getElementById('menu_' + menu[thisMenus[i]][0] + '_' + menu[thisMenus[i]][1]+'_2');
		obj.style.left = thisx + 'px';
		obj.style.top = thisy + 'px';
		
		obj = document.getElementById('menu_' + menu[thisMenus[i]][0] + '_' + menu[thisMenus[i]][1]+'_1');
		obj.style.left = thisx + 'px';
		obj.style.top = thisy + 'px';
		
		thisy+=getPixels(obj.style.height);
	}
	//set stat to expand
	
	setMenuStat(inMenuId,2,1);
	setMenuStat(inMenuId,x,2);
	setMenuStat(inMenuId,y,3);
	
	
}



function hideMenu(inMenuId){
	
	//alert(getMenuStat(inMenuId,1));
	
	if (getMenuStat(inMenuId,1)==3 || getMenuStat(inMenuId,1)==2){
		//alert(inMenuId);
		setMenuStat(inMenuId,5,1);
	}
	

	//alert("hide");
	

	
	
}


function getPixels(inStr){
	len = inStr.length;
	tmp = inStr.substring(0,len-2);
	return parseInt(tmp);
}



function moveXbySlicePos (x, img) { 
	if (!document.layers) {
		var onWindows = navigator.platform ? navigator.platform == "Win32" : false;
		var macIE45 = document.all && !onWindows && getExplorerVersion() == 4.5;
		var par = img;
		var lastOffset = 0;
		while(par){
			if( par.leftMargin && ! onWindows ) x += parseInt(par.leftMargin);
			if( (par.offsetLeft != lastOffset) && par.offsetLeft ) x += parseInt(par.offsetLeft);
			if( par.offsetLeft != 0 ) lastOffset = par.offsetLeft;
			par = macIE45 ? par.parentElement : par.offsetParent;
		}
	} else if (img.x) x += img.x;
	return x;
}

function moveYbySlicePos (y, img) {
	if(!document.layers) {
		var onWindows = navigator.platform ? navigator.platform == "Win32" : false;
		var macIE45 = document.all && !onWindows && getExplorerVersion() == 4.5;
		var par = img;
		var lastOffset = 0;
		while(par){
			if( par.topMargin && !onWindows ) y += parseInt(par.topMargin);
			if( (par.offsetTop != lastOffset) && par.offsetTop ) y += parseInt(par.offsetTop);
			if( par.offsetTop != 0 ) lastOffset = par.offsetTop;
			par = macIE45 ? par.parentElement : par.offsetParent;
		}		
	} else if (img.y >= 0) y += img.y;
	return y;
}

//create main loop
setTimeout("mainLoop();",1000/60);
function mainLoop(){



	//window.status = getMenuStat(105,1);
	//if(!menuloaded){
	//alert("here");
		//return;
	//}
	
	//document.writeln('help<br>');
	for(mi=0;mi<menuStat.length;mi++){
		if (menuStat[mi][1]==2){
			//do expand
			
			
			
			menuId = menuStat[mi][0];
			obj = document.getElementById('menuc_' + menuId);
			thisheight = getPixels(obj.style.height);
			
			
			//find menu
			thisMenus = [];
			thismenu = 0;
			
		
			
			for(ti=0;ti<menu.length;ti++){
				
				if (menu[ti][0]==menuId){
				
					
					thismenu=1;
					thisMenus.push(ti);
				}
				
			}
			
			
			
			//alert("here");
		
			if (!thismenu){
				continue;
			}
			//alert("here");
			
			
			
			obj2 = document.getElementById('menu_' + menu[thisMenus[0]][0] + '_' + menu[thisMenus[0]][1]+'_2');
	
	
			//if (obj2){
			//	alert(obj2.style.height);
			//	return;
			//}
	
	
			maxheight = getPixels(obj2.style.height)*thisMenus.length;
			maxheight--;
			thisheight += Math.round(maxheight/menuSteps);
			
			//window.status = thisheight;
			
			obj.style.height = thisheight + 'px';
			
			x = getMenuStat(menuId,2);
			y = getMenuStat(menuId,3);
			
			obj.style.top = (y) + 'px';
			obj.style.left = x + 'px';
			
			obj.style.display = 'block';
			
			
			
			if (thisheight>=maxheight){
				obj.style.top = (y) + 'px';
				obj.style.height = maxheight + 'px';
				setMenuStat(menuId,3,1);
				setMenuStat(menuId,menuTimer,4);
			}
			
			//window.status = obj.style.top;
			
			
			
		}
		if (menuStat[mi][1]==3){
			menuId = menuStat[mi][0];
			
			t = getMenuStat(menuId,4);
			
			//t--;
			setMenuStat(menuId,t,4)
			if (t<=0){
				menuStat[mi][1]=5;
			}
			
			if (menuId==175){
				//window.status = 't'+t;
			}
		}
		
		if (menuStat[mi][1]==4){
			
			
			c = getMenuStat(menuId,5);
			
			if (c==0){
				c = menuStay;
			}
			c--;
			
			setMenuStat(menuId,c,5);
			if (c<=1){
				menuStat[mi][1]=5;
			}
			
			
		}
		//menuId = menuStat[mi][0];
		//if (menuId==175){
		//	window.status = 't' + menuStat[mi][1];
		//}
		//window.status='help';
		
		if (menuStat[mi][1]==5){
			//do contract
			
			
			
			menuId = menuStat[mi][0];
			obj = document.getElementById('menuc_' + menuId);
			thisheight = getPixels(obj.style.height);
			
			
			//find menu
			thisMenus = [];
			thismenu = 0;
			
			
			
			for(ti=0;ti<menu.length;ti++){
				
				if (menu[ti][0]==menuId){
				
					
					thismenu=1;
					thisMenus.push(ti);
				}
				
			}
			
			
			
			//alert("here");
		
			if (!thismenu){
				continue;
			}
			//alert("here");
			
			
			
			obj2 = document.getElementById('menu_' + menu[thisMenus[0]][0] + '_' + menu[thisMenus[0]][1]+'_2');
	
			maxheight = getPixels(obj2.style.height)*thisMenus.length;
			maxheight--;
			thisheight -= Math.round(maxheight/menuSteps);
			
			//window.status = thisheight;
			if (thisheight>=0){
				obj.style.height = thisheight + 'px';
			}
			x = getMenuStat(menuId,2);
			y = getMenuStat(menuId,3);
			
			obj.style.top = (y) + 'px';
			obj.style.left = x + 'px';
			
			obj.style.display = 'block';
			
			
			
			if (thisheight<=0){
			
				obj.style.top = (y) + 'px';
				obj.style.height = 0 + 'px';
				
				setMenuStat(menuId,1,1);
				
				obj.style.display = 'none';
				
			}
			
			//window.status = obj.style.top;
			
			
			
		}
		
	}
	
	setTimeout("mainLoop();",1000/60);
	
	
	
}

function menuTimerReset(inMenuId){
	
	if (getMenuStat(inMenuId,1)==3){
		setMenuStat(inMenuId,menuTimer,4);
		//window.status = 't'+menuTimer;
	}
	
}


function menuClose(inMenuId){
	
	if (getMenuStat(inMenuId,1)==3 || getMenuStat(inMenuId,1)==2){
		
		
		if (getMenuStat(inMenuId,1)==2){
				
			setTimeout("menuRemove('" + inMenuId + "');",1000);
			
		}else{
		setMenuStat(inMenuId,4,1);
		
		setMenuStat(inMenuId,0,5);
		}
		//window.status = 't'+inMenuId;
		
		
		
		
	}
	
}
function menuRemove(inMenuId){
		setMenuStat(inMenuId,4,1);
		
		setMenuStat(inMenuId,0,5);
		
	
		
	
}

function menuOpen(inMenuId){
	
	if (getMenuStat(inMenuId,1)==4){
		
		setMenuStat(inMenuId,3,1);
		
		
		
		
		window.status = 't'+inMenuId;
	}
	
}


//var objImageContainer = document.createElement("div");
//objImageContainer.setAttribute('id','imageContainer');
//objOuterImageContainer.appendChild(objImageContainer);