/*
 * :: MAIN MENU SCRIPTS
 */

function menu_object_onClick(id)
{
	if( this.openMenu!=id )
	{
		this.doOpen(id);
		this.doClose(this.openMenu);
		this.openMenu = id;
	}
}

function menu_object_open(tag,href,target,clk)
{
	if( this.current_submenu ) this.current_submenu.open(tag,href,target,clk);
	else
	{
		var temp = new menu_node(tag,href,target,clk);
		this.items[this.items.length] = temp;
		if( this.itemsByTag ) this.itemsByTag[tag] = temp;
		this.current_submenu = temp;
	}
}

function menu_object_close()
{
	if( this.current_submenu )
	{
		if( this.current_submenu.close() ) this.current_submenu = null;
		return false;
	}
	else return true;
}

function menu_object_add(tag,href,target,clk)
{
	if( this.current_submenu ) this.current_submenu.add(tag,href,target,clk);
	else this.items[this.items.length] = new menu_node(tag,href,target,clk);
}

function menu_object_animate(id)
{
	if( !this.itemsByTag[id] ) return;
	var mnu = this.itemsByTag[id];
	if(mnu)mnu.animate(id);
}

function menu_object_doOpen(id)
{
	if( !this.itemsByTag[id] ) return;
	var mnu = this.itemsByTag[id];
	if(mnu)mnu.doOpen(id);
}

function menu_object_doClose(id)
{
	if( !this.itemsByTag[id] ) return;
	var mnu = this.itemsByTag[id];
	if(mnu)mnu.doClose(id);
}

function menu_node_object_animate(id)
{
	if(this.animating)setTimeout('window.main_menu.animate("'+id+'")',50);
	else return;

	this.currentHeight+=this.velocity;
	this.velocity *= 1.1;//0.90;
	if( this.velocity > 0 && this.velocity < 1 ) this.velocity = 1.0;
	else if( this.velocity < 0 && this.velocity > -1 ) this.velocity = -1.0;

	if( ( this.velocity > 0 ) && ( this.currentHeight >= this.targetHeight ) )
	{
		this.currentHeight = this.targetHeight;
		this.animating = false;
	}
	else if( ( this.velocity < 0 ) && ( this.currentHeight <= this.targetHeight ) )
	{
		this.currentHeight = this.targetHeight;
		this.animating = false;
		this.inner.style.display = 'none';
	}
	var h = Math.floor( this.currentHeight );
	this.outer.style.display = 'block';
//	this.outer.style.clip = 'rect(0px '+h+'px '+this.width+'px 0px)';
	this.outer.style.clip = 'rect(0px 250px '+h+'px 0px)';
	this.outer.style.height = h+'px';
}

function menu_node_object_doOpen(id)
{
	var lnk = document.getElementById( id );
	if( lnk )
	{
		var lnkHTML = lnk.innerHTML;
		lnkHTML = lnkHTML.replace( 'class="mainMenuTopLink"', 'class="mainMenuTopLinkSel"' );
		lnkHTML = lnkHTML.replace( 'class=mainMenuTopLink', 'class=mainMenuTopLinkSel' );
		lnk.innerHTML = lnkHTML;
	}

	this.outer = document.getElementById( id+'_sub' );
	this.inner = document.getElementById( id+'_content' );
	if( !this.animating )
	{
		setTimeout('window.main_menu.animate("'+id+'")',50);
		if( !this.isRendered )
		{
			this.inner.innerHTML = this.render();
			this.isRendered = true;
		}
		this.width = 200;//this.inner.offsetWidth;
		this.currentHeight = 0.0;
		this.outer.style.overflow = 'hidden';
		this.outer.style.clip = 'rect(0px 0px 0px 0px)';
	}
	this.outer.style.display = 'block';
	this.inner.style.display = 'block';
	this.targetHeight = this.inner.offsetHeight;
	if( !this.animating ) this.outer.style.display = 'none';
	this.animating = true;
	this.velocity = 5.0;
}

function menu_node_object_doClose(id)
{
	var lnk = document.getElementById( id );
	if( lnk )
	{
		var lnkHTML = lnk.innerHTML;
		lnkHTML = lnkHTML.replace( 'class="mainMenuTopLinkSel"', 'class="mainMenuTopLink"' );
		lnkHTML = lnkHTML.replace( 'class=mainMenuTopLinkSel', 'class=mainMenuTopLink' );
		lnk.innerHTML = lnkHTML;
	}

	this.outer = document.getElementById( id+'_sub' );
	this.inner = document.getElementById( id+'_content' );
	this.isRendered = true;
	if( !this.animating )
	{
		setTimeout('window.main_menu.animate("'+id+'")',50);
		this.width = 150;//this.inner.offsetWidth;
		this.outer.style.overflow = 'visible';
		this.currentHeight = this.inner.offsetHeight * 1.0;
		this.outer.style.overflow = 'hidden';
		this.inner.style.display = 'block';
	}
	this.targetHeight = 0;
	this.animating = true;
	this.velocity = -5.0;
}

function menu_node_object_render(depth)
{
	if(!depth)depth=0;
	var html = this.getHTML(depth);
	var block = '';
	for(var i in this.items)
	{
		if( block ) block += '<br>';
		block += this.items[i].render(depth+1);
	}
	return this.nestHTML(html,block,depth);
}

function menu_node_object_getHTML(depth)
{
	if( this.href )
	{
		return '<a href="'+this.href+'">'+this.tag+'</a>';
	}
	else return this.tag;
}

function menu_node_object_nestHTML(html,block,depth)
{
	if( depth == 0 ) return block;
	else if( block ) return html+'<br><div class="indent">'+block+'</div>';
	else return html;
}

/*
 * :: OBJECT DEFINITIONS
 */

function menu_object()
{
	this.current_submenu = null;
	this.items = new Array();
	this.itemsByTag = new Array();
	this.openMenu = null;

	this.add = menu_object_add;
	this.open = menu_object_open;
	this.close = menu_object_close;

	this.doOpen = menu_object_doOpen;
	this.doClose = menu_object_doClose;
	this.onClick = menu_object_onClick;
	this.animate = menu_object_animate;
}

function menu_node(tag,href,target,clk)
{
	this.current_submenu = null;
	this.items=new Array();
	this.tag=tag;
	this.href=href;
	this.target=target;
	this.clk=clk;

	this.animating = 0;

	this.add = menu_object_add;
	this.open = menu_object_open;
	this.close = menu_object_close;
	this.render = menu_node_object_render;
	this.getHTML = menu_node_object_getHTML;
	this.nestHTML = menu_node_object_nestHTML;

	this.doOpen = menu_node_object_doOpen;
	this.doClose = menu_node_object_doClose;
	this.animate = menu_node_object_animate;
}

function mnu(id)
{
	if(document.getElementById&&window.main_menu&&window.main_menu.onClick)
	{
		window.main_menu.onClick(id);
		return false;
	}
	else return true;
}


