var s_height = 0;
var s_top = 0;
var s_increment = 0;
var curmenu = 0;
var issamepage = 0;

function ShowMenu(section, subpage, samepage) //subpage in this case is the currently loaded subage if selected
{
	if (section == curmenu){return;}
	if(!section || isNaN(section)){section = 0;}
	if(!subpage || isNaN(subpage)){subpage = 0;}
	if(!samepage || isNaN(samepage)){samepage = 0;}
	
	issamepage = samepage;
	SetButton(section);
	StartMenuScroll(section, subpage);
}

function GetMenu(section, subpage)
{
	var xmlHttp;
	try {xmlHttp=new XMLHttpRequest();}
	catch (e) {try {xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");}
	catch (e) {try {xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");}
	catch (e) {alert("Your browser does not support AJAX!"); return false;}}}
	xmlHttp.onreadystatechange=function()
	{
		if (xmlHttp.readyState==4)
		{
			document.getElementById('sbarnav').innerHTML=xmlHttp.responseText;
			if (document.getElementById('sbarbtns'))
			{
				document.getElementById('sbarnav').style.height = document.getElementById('sbarbtns').offsetHeight+'px';
				ScrollMenuDown();
			}else
			{
				document.getElementById('sbarnav').style.height = '16px';
			}
		}
	}
	xmlHttp.open("GET",'ajaxsbar.php?s='+section+'&p='+subpage+'&a='+issamepage, true);xmlHttp.send(null); 
}

function StartMenuScroll(section, subpage)
{
	if (document.getElementById('sbarbtns') && !issamepage)
	{
		var sbdiv = document.getElementById('sbarbtns');
		s_height = sbdiv.offsetHeight - 16;
		s_increment = parseInt((s_height / 16).toString());
		s_top = parseInt(sbdiv.style.top.replace(/\D/g, ""));
		curmenu = section;
		setTimeout("ScrollMenuUp("+section+','+subpage+")", 10);
	}else
	{
		GetMenu(section, subpage);
	}
}

function ScrollMenuUp(section, subpage)
{
	if (document.getElementById('sbarbtns'))
	{
		var sbdiv = document.getElementById('sbarbtns');
		s_top -= s_increment;
		if (s_top > (4 - s_height))
		{
			sbdiv.style.top = s_top.toString()+"px";
			setTimeout("ScrollMenuUp("+section+", "+subpage+")", 10);
		}else
		{
			s_top = 4 - s_height;
			sbdiv.style.top = s_top.toString()+"px";
			GetMenu(section, subpage);
		}
	}
}

function ScrollMenuDown()
{
	if (document.getElementById('sbarbtns'))
	{
		var sbdiv = document.getElementById('sbarbtns');
		s_top += s_increment;
		if (s_top < 0)
		{
			sbdiv.style.top = s_top.toString()+"px";
			setTimeout("ScrollMenuDown()", 31);
		}else
		{
			s_top = 0;
			sbdiv.style.top = s_top.toString()+"px";
		}
	}
}

function SetButton(btnid)
{
	bidstr = (btnid < 10) ? '0'+btnid.toString() : btnid.toString();
	for (i = 1; i < 10; i++)
	{
		cidstr = (i < 10) ? '0'+i.toString() : i.toString();
		if (i == btnid)
		{
			if (document.getElementById('btn'+bidstr)){document.getElementById('btn'+bidstr).style.backgroundPosition = '0 -22px';}
		}else
		{
			if (document.getElementById('btn'+cidstr)){document.getElementById('btn'+cidstr).style.backgroundPosition = '0 0';}
		}
	}
}
