// set the container for the meny

function msetcontainer( container )
{
	document.menucontainer = container;

	if( is_badbrowser() )
	{
		//container.style.position = "absolute";
	}
};

// create a menu with id and class 

function mstart( id , mclass , autodiv )
{
	var i=0;

	if( document.mmenucount )
	{
	    i = document.mmenucount;
		document.mmenucount++;
	}
	else
	{
		document.mmenucount = 1;
		document.menucash = new Array();
		document.menuclose = new Array();
		document.menukeys = new Array();
		document.menuflash = new Array();
	};

	document.menucash[i] = '';
	document.menuclose[i] = '';
	document.menukeys[i] = id;
	document.menuflash[i] = new Array();

	if( autodiv )
	{
		document.menucash[i] = '<DIV id="'+id+'" class="'+mclass+'" onmouseleave="mstarttimer();" onmouseover="mstoptimer();" onmouseout="mstarttimer();"  style="position: absolute; visibility: hidden; margin: none; position: absolute;">';
		document.menuclose[i] = '</DIV>';		
	};
};

// print code in the menu, ac is code stacked at the end. 

function mprint( a , ac )
{
	var i = document.mmenucount - 1;
	
	document.menucash[i] = document.menucash[i] + a;
	
	if( ac ) document.menuclose[i] = a + document.menuclose[i];
};

// print flash movie in the menu.

function mprint_flash( a , id , w , h , alt )
{
	var i = document.mmenucount - 1;
	
	document.menuflash[i][ document.menuflash[i].length ] = id;
	
	mprint( '<OBJECT classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0" WIDTH="'+w+'" HEIGHT="'+h+'" id="'+id+'" ALIGN=""> <PARAM NAME=movie VALUE="'+a+'"> <PARAM NAME=quality VALUE=high> <PARAM NAME=bgcolor VALUE=#FFFFFF> <EMBED src="'+a+'" quality=high bgcolor=#FFFFFF  WIDTH="'+w+'" HEIGHT="'+h+'" NAME="'+id+'" ALIGN="" TYPE="application/x-shockwave-flash" PLUGINSPAGE="http://www.macromedia.com/go/getflashplayer"></EMBED></OBJECT>' );
};

// create the menu from the buffered menu's

function mcreate()
{
	var i;
	var total='';
	
	var cnt = document.getElementById( document.menucontainer ); 

	if( cnt )  
	{
		for( i=0; i<document.mmenucount; i++ )
		{
			total = total + document.menucash[i] + document.menuclose[i];
		};

		cnt.innerHTML = total;	
	};

	document.activemenu = false;
};

// show the menu

function is_badbrowser()
{
	var badbrowser = false;
	var detect = navigator.userAgent.toLowerCase();

	function checkIt(string)
	{
		place = detect.indexOf(string) + 1;
		thestring = string;
		return place;
	}

	if( checkIt("mac") )
	{
		if(checkIt( "msie" ))
		{
			badbrowser = true;
		}
	}

	return badbrowser;
}

function restart_flash( id )
{
	var i=mitemindex( id );

	for( j=0; j<document.menuflash[i].length; j++ )
	{
		var flash=false;

		// MSE kan niet testen of functie Play bestaat.

		var ua = navigator.userAgent.toLowerCase();
		if( (ua.indexOf( 'msie') || ua.indexOf( 'firefox')) && ! ua.indexOf( 'opera') )
		{
			
			if( document.embeds )
			{
				// Andern halen object uit embeds collectie.
							
				flash =	document.embeds[document.menuflash[i][j]];
				
			};
			
			if( ! flash ) flash = document.getElementById( document.menuflash[i][j] );

			if( flash ) 
			{
				flash.Rewind();
				flash.Play();
			};
			
		};
	};
}

function mshow_left( id , left_item , x_offset )
{
	var item = document.getElementById( id );

	if( ! x_offset ) x_offset = 0;

	if( document.activesubmenu == id )
	{
		mstoptimer();
	}
	else
	{
		mhide_submenu( document.activesubmenu );

		if( item )
		{
			restart_flash( id );
	
			document.activesubmenu = id;
		};
	
		var top = 0;
		var left = 0;
		var walk = left_item;

		if( is_badbrowser() )
		{
			// positie van item is relatief, geen correctie voor container.

			top += left_item.offsetTop;
			left += left_item.offsetLeft + xcontainment(left_item) - xcontainment(item) + x_offset;
		}
		else
		{
			while( walk )
			{
				top += walk.offsetTop;
				left += walk.offsetLeft;
				walk = walk.offsetParent;
			};
		};

		item.style.top = top  + 'px';
		item.style.left = left - item.offsetWidth + x_offset + 'px';
	};

	if( item ) item.style.visibility = "visible";
}

function mshow_right( id , left_item , x_offset )
{
	var item = document.getElementById( id );

	if( ! x_offset ) x_offset = 0;

	if( document.activesubmenu == id )
	{
		mstoptimer();
	}
	else
	{
		mhide_submenu( document.activesubmenu );

		if( item )
		{
			restart_flash( id );
	
			document.activesubmenu = id;
		};
	
		var top = 0;
		var left = 0;
		var walk = left_item;

		if( is_badbrowser() )
		{
			top += walk.offsetTop;
			left += walk.offsetLeft + item_left.offsetWidth + x_offset;
		}
		else
		{
			while( walk )
			{
				top += walk.offsetTop;
				left += walk.offsetLeft;
				walk = walk.offsetParent;
			};
		};

		item.style.top = top  + 'px';
		item.style.left = left + item_left.offsetWidth + x_offset + 'px';
	};

	if( item ) item.style.visibility = "visible";
}

function mshow( id , below , backlayer )
{
	var item = document.getElementById( id );

	if( document.activemenu == id )
	{
		mstoptimer();
	}
	else
	{
		mhide();

		if( item )
		{
			restart_flash( id );
	
			document.activemenu = id;
		};
	};

	if( below )
	{	
		if( item )
		{		

			var top = 0;
			var left = 0;
			var walk = below;

			while( walk )
			{
				top += walk.offsetTop;
				left += walk.offsetLeft;
				walk = walk.offsetParent;
			};

			if (is_badbrowser() ) 
			{
				var cnt = document.getElementById( document.menucontainer ); 

				item.style.top = top + 	below.offsetHeight - cnt.offsetTop - ycontainment(cnt) + 'px';
				item.style.left = left - cnt.offsetLeft - xcontainment(cnt) + 'px';
			}
			else
			{
				item.style.top = top + 	below.offsetHeight + 'px';
				item.style.left = left + 'px';
			}
			
		};
	};

	if( item ) item.style.visibility = "visible";
	
};

function mitemindex( id )
{
	var i=0;

	for( i=0; i< document.menuflash.length ; i++ )
	{
		if( document.menukeys[i]==id ) return i;
	};	

	return 0;
};

function mhide( id )
{
	mstoptimer();

	if( id )
	{
		var item = document.getElementById( id );
		item.style.visibility = "hidden";
	}
	else
	{
		var i;

		for( i=0; i<document.mmenucount; i++ )
		{
			var item = document.getElementById( document.menukeys[i] );
			if( item ) item.style.visibility = "hidden";
		};
	};

	document.activemenu = false;
};

function mhide_submenu( id )
{
	if( ! id ) id = document.activesubmenu;

	if( id )
	{
		var item = document.getElementById( id );
		item.style.visibility = "hidden";
	}
	
	document.activesubmenu = false;
};

// internal timers

function mstarttimer()
{
   mstoptimer();
   document.mtimer = setTimeout("mhidetimer()", 500);
};

function mstoptimer()
{
	if( document.mtimer ) 
	{
		clearTimeout(document.mtimer);

		document.mtimer = false;
	};
};

function mhidetimer()
{

	if( document.activemenu )
	{
		var item = document.getElementById( document.activemenu );
		
		if( document.menu_mx < item.offsetLeft || document.menu_mx > item.offsetLeft + item.offsetWidth
			|| document.menu_my < item.offsetTop || document.menu_my > item.offsetTop + item.offsetHeight )
		{
			mhide();
		}
		else
		{
			 mstarttimer();
		}
	};
};

function mmousemove( e )
{
	if( document.all )
	{	
		document.menu_mx = event.x;
		document.menu_my = event.y;
	}
	else
	{
		document.menu_mx = e.pageX;
		document.menu_my = e.pageY;
	}
}

function xcontainment( item )
{
	var x=0;

	walk = item.offsetParent;

	while( walk )
	{
		x += walk.offsetLeft;
		walk = walk.offsetParent;
	};

	return x;
};

function ycontainment( item )
{
	var y=0;

	walk = item.offsetParent;

	while( walk )
	{
		y += walk.offsetTop;
		walk = walk.offsetParent;
	};

	return y;
};
