var asteria = new Object();
asteria.open = "transparent url(images/open.png) no-repeat 12px 3px";
asteria.close = "transparent url(images/close.png) no-repeat 12px 3px";
asteria.index = 0;
asteria.menus = [];

function getEl(id)
{
	return document.getElementById(id);
};

function init_menu()
{
	var menu;
	for(i = 0; i < index; i++)
	{
		menu = asteria.menus[i];
	}
};

function LinkDropdown(cntl, menu, config)
{
 	this.menu = menu;
	this.cntl = cntl;
	this.display = menu.style.display;
	if(null != config)
	{
		this.cntl_sel = config.c_sel;
		this.cntl_nsel = config.c_nsel;
		this.close = "transparent url(" + config.close + ") no-repeat "+config.left+" "+config.top;
		this.open = "transparent url(" + config.open + ") no-repeat "+config.left+" "+config.top;
	}else
	{
		/*
		this.cntl_sel = "select";
		this.cntl_nsel = "not_select";
		this.menu_sel = "select2";
		this.menu_nsel = "not_select_2";
		this.close = asteria.close;
		this.open = asteria.open;
		*/
	}
	if("none" == this.display)
	{
		cntl.style.background = this.open;
	}else
	{
		cntl.style.background = this.close;
	}


	asteria.menus[asteria.index] = menu;
	asteria.index++;
	cntl.link = this;
	menu.link = this;
	cntl.onclick = function()
	{
		var i = 0;
		var link = this.link;
		if(link.cntl == this)
		{
			if(link.display == "none")
			{
				link.menu.style.display = "";
				link.display = "";
				this.parentNode.className = link.cntl_sel;
				this.style.background = link.close;
				var tmp = cntl.parentNode.parentNode.firstChild;
				
				for(i = 0; i < asteria.index; i++)
				{
					var link = asteria.menus[i].link;
					if(link.cntl.parentNode.parentNode == this.parentNode.parentNode && link.cntl != this)
					{
						link.menu.style.display = "none";
						link.display = "none";
						link.cntl.style.background = link.open;
						link.cntl.parentNode.className = link.cntl_nsel;
					}
				}
				/*
				while(tmp)
				{
					if(tmp != this.parentNode && tmp.tagName == "LI"){
						tmp.className = link.cntl_nsel;
						if(tmp.firstChild.link.display != "none")
						{
							tmp.firstChild.link.menu.style.display = "none";
							tmp.firstChild.link.display = "none";
						}
					}
					tmp = tmp.nextSibling;
				}
				*/
			}
			else{
				link.menu.style.display = "none";
				link.display = "none";
				this.parentNode.className = link.cntl_nsel;
				this.style.background = link.open;
			}
		}
	};
	cntl.onmouseover = function()
	{
		this.parentNode.className = this.link.cntl_sel;
	};
	cntl.onmouseout = function()
	{
		if(this.link.display == "none")
			this.parentNode.className = this.link.cntl_nsel;
	};
	/*
	li = menu.firstChild;
	while(li){
		li.onclick = function()
		{
			var link = this.parentNode.link;
			for(i = 0; i < asteria.index; i++)
			{
				other = asteria.menus[i].firstChild;
				while(other)
				{
					if(other != li){
						other.className = link.menu_nsel;
					}
					other = other.nextSibling;
				}
				this.className = link.menu_sel;
			}
		};
		li = li.nextSibling;
	}
	*/
}

function getPageSize() {
	var xScroll;
	var yScroll;

	if (document.body.scrollHeight > document.body.offsetHeight) { // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}

	var windowWidth;
	var windowHeight;
	if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}

	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight) {
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth) {
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}

	return {w: windowWidth, h: windowHeight};
}

function setMenu() {
	var elm = document.getElementById('sidebar');
	if (elm.offsetHeight > getPageSize().h) {
		return;
	}
	var style = elm.style;
	var top = document.body.scrollTop || document.documentElement.scrollTop;
	if (top > 70) {
		style.position = 'fixed';
		style.top = '0px';
	} else {
		style.position = 'relative';
	}
}
