var __emenutop = '';
var __emenuleft = '';
var __emenutitle = '';
var __elastlevel = null;
var __efirstid = null;
var __emenuelements = new Array();
var __emenuelementsparents = new Array();
var __ecountlevelone=0;
var __ecurrentlevelone=0;
var __eproductsparent;
var __eproductshtml = '';
var __econtactsparent;
var __econtactshtml = '';
var __emenusubtop = '';

__eproductsparent = __products_id; 
__econtactsparent = __contacts_id;

var __contacts_count = 0;

function __emenuelement(id,parent,caption,type,article,href,target,level)
{
	if (caption.toLowerCase() == '&lt;br&gt;') caption='<br>';
	this.id=id;this.parent=parent;this.caption=caption;this.type=type;this.article=article;
	this.href=href;this.target=target;this.level=level;this.selected=false;
	if (__efirstid == null)
	{
		__efirstid = id;this.selected = true;
	}
	if (level == 1)
		__ecountlevelone++;
	__emenuelementsparents[id] = new Array();
	if (parent && __emenuelementsparents[parent])
		__emenuelementsparents[parent][__emenuelementsparents[parent].length] = id;
	//if (caption.toLowerCase().replace(/_/,' ') == 'products and services' && level == 1) __eproductsparent = id;
	//if (caption.toLowerCase().replace(/_/,' ') == 'contact' && level == 1) __econtactsparent = id;
}
			
function m(id,parent,caption,type,article,href,target,level)
{
	__emenuelements[id] = new __emenuelement(id,parent,caption,type,article,href,target,level);
}
			
function __emenubuild()
{
	var i;
	i = AM;
	while (__emenuelements[i])
	{
		__emenuelements[i].selected = true;
		i = __emenuelements[i].parent;
	}
	getMenu(__efirstid,1);
	//__emenuleft = '<table cellspacing=1 cellpadding=0 border=0>'+__emenuleft+'</td></tr></table>';
	if (__emenuelements[AM])
		__emenutitle = __emenuelements[AM].caption.toUpperCase();
	__eproductshtml = getProductsMenu(__eproductsparent,1);
	__econtactshtml = getContactsMenu(__econtactsparent,1);
}
			
function getMenu(parentid,level,stop)
{
	var i,id,_class;
				
		if (level > 0 && level < 5)
		{
			var p = null;
			if (__emenuelements[parentid].parent)
				p = __emenuelements[__emenuelements[parentid].parent].parent;
			//addtolayer(__emenuelements[parentid].parent,p,parentid,level,__emenuelements[parentid].caption,getHref(parentid,false));

			for (i=0;i<__emenuelementsparents[parentid].length;i++)
			{
				id = __emenuelementsparents[parentid][i];
				getMenu(id,level+1,true);
			}
		} 
		
	if (stop)
		return;
	for (i=0; i<__emenuelementsparents[parentid].length;i++)
	{
		id = __emenuelementsparents[parentid][i];
		var _m = __emenuelements[id];
		
		if (level===1)
			__emenutop += '<a '+getHref(id)+' onmouseover="showlayer(event,'+id+');" onmouseout="delayhidelayer('+id+');"><img src="img/menu/'+id+((_m.selected)?'-sel':'')+'.png" border=0></a>';
		else if (level===2)
		{
			if (_m.caption && _m.caption != '-' && _m.caption.toLowerCase() != '<br>')
				__emenusubtop += '<a '+getHref(id)+'><img src="img/menu/'+id+((_m.selected)?'-sel':'')+'.png" border=0></a>'; 
			if (_m.selected)
				__emenuleft += '<a '+getHref(id)+' class=menu-sel>'+_m.caption+'</a><br>';
		}
		else if (level > 2)
		{
			__emenuleft += '<table cellspacing=0 cellpadding=0 border=0><tr><td><img src="img/spacer.gif" width='+(6*(level-2))+' height=1></td><td>';
			__emenuleft += '<a '+getHref(id)+' class="'+((_m.selected)?'menu-sel':'menu')+'">'+_m.caption+'</a>';
   			__emenuleft += '</td></tr></table>';
		}
		if (__emenuelementsparents[id].length>0 && _m.selected)
			getMenu(id,level+1);
	}
}
function getHref(id,onlyhref)
{
	var menu = __emenuelements[id],href;
	
	switch (menu.type)
	{
		case 'article': href = 'show.php?id='+menu.article;break;
		case 'url': href = menu.href;break;
		default: href = '#';
	}
	if (onlyhref) return href;

	return 'href="'+href+'" target="'+menu.target+'"';
}

function getProductsMenu(__eproductsparent,level)
{
	if (!__eproductsparent || typeof(__emenuelementsparents[__eproductsparent]) == 'undefined' || __emenuelementsparents[__eproductsparent].length==0)
	{
		if (level == 1)
			return '<sel'+'ect name="products" class="productsdropdown"><option>'+__products_empty+'</option></select>';
		return '';
	}
	var html = '',id,i,add='';
	if (level == 1)
		html += '<sel'+'ect name="products" onchange="if(this.options[this.selectedIndex].value) {window.open(this.options[this.selectedIndex].value,\'_blank\');}" class=productsdropdown><option value="">'+__products_select+'</option><option value="">-------------------</option>';
	for (i=1;i < level;i++)
		add +='&nbsp;&nbsp;';

	for (i=0; i<__emenuelementsparents[__eproductsparent].length;i++)
	{
		id = __emenuelementsparents[__eproductsparent][i];
		html +='<option value="http://www.kalmarind.com/'+getHref(id,true)+'"'+((id==AM && __emenuelements[id].selected)?' selected':'')+'>'+add+__emenuelements[id].caption+'</option>';
		html +=getProductsMenu(id,level+1);
	}
	if (level == 1)
		html += '</select>';
	return html;
}

function getContactsMenu(__econtactsparent,level)
{
	if (!__econtactsparent || typeof(__emenuelementsparents[__econtactsparent]) == 'undefined' || __emenuelementsparents[__econtactsparent].length==0)
	{
		if (level == 1)
			return '<sel'+'ect name="contacts" class="productsdropdown"><option>'+__contacts_empty+'</option></select>';
		return '';
	}
	var html ='',id,i,add = '';
	
	for (i=1;i < level;i++)
		add +='&nbsp;&nbsp;';

	if (level == 1) html += '<sel'+'ect name="contacts" onchange="if(this.options[this.selectedIndex].value) {window.location.href=this.options[this.selectedIndex].value;}" class="productsdropdown"><option value="">'+__contacts_select+'</option><option value="">-------------------</option>';
	for (i=0; i< __emenuelementsparents[__econtactsparent].length;i++)
	{
		__contacts_count++;
		id = __emenuelementsparents[__econtactsparent][i];
		html +='<option value="'+getHref(id,true)+'"'+((id==AM && __emenuelements[id].selected)?' selected':'')+'>'+add+__emenuelements[id].caption+'</option>';
		html +=getContactsMenu(id,level+1);
	}
	if (level == 1)
		html += '</select>';
	if (__contacts_count < 2 && level == 1)
		html = '';
	return html;
}
