var myTimer = setTimeout("menuTimer()", 200);
var menuList = new Array(20);
var isIE = navigator.userAgent.indexOf("MSIE") != -1;
var currentItem = -2;
var currentTime = 0;

menuItem = function(layerId, textAlign, counter, increment) {
	this.layerId = layerId;
	this.textAlign = textAlign;
	this.counter = counter;
	this.increment = increment;
}

function enterItem(layerId, menuPos) {
	if (menuPos != -1) {
		currentItem = menuPos;
		currentTime = 0;
	}
	if (currentItem >= 0 && !menuList[currentItem])
		menuList[currentItem] = new menuItem(layerId, (currentItem % 2)?"left":"right", 0, 0);
}

function menuTimer() {
	currentTime++;
	if (currentTime > 4 || currentItem == -2)
		startMenuItem(currentItem)
		
	for (var eachItem in menuList) {
		if (menuList[eachItem] && menuList[eachItem].increment)
			sizeMenuItem(menuList[eachItem]);
	}
	myTimer = setTimeout("menuTimer()", 30);
}

function startMenuItem(menuPos) {
	if (menuPos >= 0 && menuPos < menuList.length) {
		if (menuList[menuPos]) {
			if (menuList[menuPos].counter < 6) {
				menuList[menuPos].increment = 1;
			}
		}
	}
	
	var totalCount = 0.0;
	var eachItem
	for (eachItem in menuList) {
		if (eachItem != menuPos && menuList[eachItem] && !menuList[eachItem].counter && menuList[eachItem].increment)
			menuList[eachItem].increment = 0;
		else if (eachItem != menuPos && menuList[eachItem] && menuList[eachItem].counter)
			totalCount += menuList[eachItem].counter;
	}
	for (eachItem in menuList) {
		if (eachItem != menuPos && menuList[eachItem] && menuList[eachItem].counter)
			menuList[eachItem].increment = -totalCount / menuList[eachItem].counter;
	}
}

function sizeMenuItem(myItem) {
	var myLayerRef, myImageRef, myTextRef;
	
	myLayerRef = document.getElementById(myItem.layerId);
	myItem.counter += myItem.increment;
	if (myItem.counter <= 0) {
		myItem.counter = 0;
		myItem.increment = 0;
	} else if (myItem.counter >= 6) {
		myItem.counter = 6;
		myItem.increment = 0;
	}
	
	myLayerRef.style.height = (40 + myItem.counter * 14) + "px";
	myLayerRef.style.fontWeight = (myItem.increment == 1 || myItem.counter == 6)?"bold":"normal";
	
	myImageRef = document.getElementById(myItem.layerId + "menuitem");
	myImageRef.width = myItem.counter * 10 + 40;
	myImageRef.height = myItem.counter * 10 + 40;

	if (myItem.counter) {
		myLayerRef.style.textAlign = "center";
		myLayerRef.style.padding = parseInt(myItem.counter * 4 / 3 + 0.5) + "px 0 " + parseInt(myItem.counter * 4 / 3 + 0.5) + "px 0";
		if (isIE && myItem.increment == 1) {
			ieAdjust = ((myItem.counter == 1 || myItem.counter == 5)?-2:0) - 8;
			myLayerRef.style.margin = "0 0 " + ieAdjust + "px 0";
		}
		
		myImageRef.src = "menuimages/menuitem100_" + myItem.layerId + ".gif";
		if (myItem.textAlign == "left") {
			myImageRef.style.padding = "0 " + (109 - myItem.counter * 14) + "px " + parseInt(myItem.counter * 4 / 3 + 0.5) + "px " + (1 + myItem.counter * 4) + "px";
		} else {
			myImageRef.style.padding = "0 " + (1 + myItem.counter * 4) + "px " + parseInt(myItem.counter * 4 / 3 + 0.5) + "px " + (109 - myItem.counter * 14) + "px";
		}
		
	} else {
		myLayerRef.style.textAlign = myItem.textAlign;
		myLayerRef.style.padding = "0";
		myLayerRef.style.margin = "0 0 -8px 0";
		
		myImageRef.src = "menuimages/menuitem40_" + myItem.layerId + ".gif";
		myImageRef.style.padding = "0";
	}
}

var panelList = new Array();

function openPanel(panelName) {
	if (!panelList[panelName]) {
		panelList[panelName] = new Object;
		panelList[panelName].status = "closed";
		panelList[panelName].currentSize = 0;
		panelList[panelName].closing = false;
	}
	if (panelList[panelName].closing) {
		panelList[panelName].closing = false;
	} else if (panelList[panelName].status == "closed") {
		setTimeout("openPanelOnTimer(\"" + panelName + "\")", 500);
		panelList[panelName].status = "opening";
	}
}

function openPanelOnTimer(panelName) {
	if (panelList[panelName].status == "opening") {
		document.getElementById(panelName).style.display = "block";
		panelList[panelName].currentSize = 3;
		document.getElementById(panelName).style.lineHeight = "3px";
		document.getElementById(panelName).style.color = "#FFF";
		setTimeout("animatePanel(\"" + panelName + "\")", 30);
		panelList[panelName].status = "animating";
	}
}

function animatePanel(panelName) {
	if (panelList[panelName].status == "animating") {
		panelList[panelName].currentSize++;
		document.getElementById(panelName).style.lineHeight = panelList[panelName].currentSize + "px";
		var colour = "FEDCBA9876543210".charAt(panelList[panelName].currentSize - 3);
		colour += colour + colour;
		document.getElementById(panelName).style.color = "#" + colour;
		if (panelList[panelName].currentSize < 12)
			setTimeout("animatePanel(\"" + panelName + "\")", 30);
		else
			panelList[panelName].status = "open";
	}
}

function closePanel(panelName) {
	if (panelList[panelName].status == "opening") {
		panelList[panelName].status = "closed";
		panelList[panelName].closing = false;
	} else if (panelList[panelName].status == "open" || panelList[panelName].status == "animating") {
		setTimeout("closePanelOnTimer(\"" + panelName + "\")", 250);
		panelList[panelName].closing = true;
	}
}

function closePanelOnTimer(panelName) {
	if (panelList[panelName].closing) {
		document.getElementById(panelName).style.display = "none";
		panelList[panelName].status = "closed";
		panelList[panelName].closing = false;
	}
}