//  colmenu.js - collapsable menu script
//  2002/2/6 Makino Takaki

// How to use:

// <style type="text/css">
// .cMenu       {position:relative; width:200px; }
// .cMenuHead   {position:absolute; width:200px; left:0px; top:0px; background-color: #ffffcf;}
// .cMenuBody   {position:absolute; width:200px; left:0px; top:0px; visibility: hidden; background-color: #ffcfcf; margin-left: 15px;} 
// </style>

// <script language="javascript">
// <!--
// function init()
// {
//   cmInitMenu('a');
//   cmInitMenu('aa','a');
// }
//  // -->
// </script>

// <body onload="init()">

// <div id="a" class="cMenu">
//   <div id="ahead" class="cMenuHead">
//     <a onclick="cmFlip('a');" href="#">menu1</a>
//   </div>
//   <div id="abody" class="cMenuBody">

//     <div id="aa" class="cMenu">
//       <div id="aahead" class="cMenuHead">
//         <a onclick="cmFlip('aa');" href="#">menu1-1</a>
//       </div>
//       <div id="aabody" class="cMenuBody">
//         ....
//       </div>
//     </div>

//   </div>
// </div>

var glowitem = new Array();
var glowlimit = 40;
var glowmax = 15;
var glowdiff = 3;
var glowtimerstep = 15;
var menutimerwait = 100;
var glowtimerwait = 60;
var menuitem = new Array();
//var menuheight = new Array();
var shrinkimg = new Image(); shrinkimg.src = "/~t/img/shrink.gif";
var expandimg = new Image(); expandimg.src = "/~t/img/expand.gif";

function cmInitMenu(id, parentid)
{
 // alert("initmenu " + id + " / " + parentid);
	menuitem[id] = new Object();
	menuitem[id].abovstyle = new xbStyle(new xbGetElementById(id+'abov'));
	menuitem[id].lastabove = 0;
	menuitem[id].bodystyle = new xbStyle(new xbGetElementById(id+'body'));
	if( xbGetElementById(id+'belo') )
		menuitem[id].belostyle = new xbStyle(new xbGetElementById(id+'belo'));
	menuitem[id].img = new xbGetElementById(id+'img');
	menuitem[id].expanded = false;
	menuitem[id].delta = 0;
	menuitem[id].height = menuitem[id].bodystyle.getHeight();
	menuitem[id].timer = undefined;
	if( parentid )
		menuitem[id].parentid = parentid;
	menuitem[id].childglow = new Array();
	menuitem[id].childglowindex = 0;
	menuitem[id].childglowheight = 0;

	menuitem[id].bodystyle.setClipBottom( 0 );
	menuitem[id].bodystyle.setTop( menuitem[id].abovstyle.getHeight() );
	if( menuitem[id].belostyle )
		menuitem[id].belostyle.setTop( menuitem[id].abovstyle.getHeight() );
	

	if( !parentid )
	{
		parentid = "!";
		if( ! menuitem["!"] )
		{
			menuitem["!"] = new Object();
			menuitem["!"].bodystyle = new xbStyle(new xbGetElementById("!"));
		}
	}
	var parentheight = menuitem[parentid].lastabove + menuitem[id].abovstyle.getHeight() + (menuitem[id].belostyle ? menuitem[id].belostyle.getHeight() : 0);
	// alert(parentid + "<" + id + " = " + menuitem[parentid].lastabove + " + " + menuitem[id].abovstyle.getHeight() + " + " + (menuitem[id].belostyle ? menuitem[id].belostyle.getHeight() : -1));
	if( menuitem[parentid].height < parentheight )
	{
		menuitem[parentid].height = parentheight;
		//alert(parentid);
		menuitem[parentid].bodystyle.setHeight(menuitem[parentid].height);
		menuitem[parentid].lastabove += menuitem[id].abovstyle.getHeight();
	}
}

function cmInitLeaf(id, parents, client, lev)
{
	cmInitGlow(id);

	glowitem[id].backstyle = new xbStyle(xbGetElementById(client));
	glowitem[id].colseries = new Array(glowmax+1);

	//alert(id);
	//alert(glowitem[id].style.getColor());
	var forecolor = makergb(glowitem[id].style.getColor());
	var backcolor = makergb(glowitem[id].backstyle.getBackgroundColor());
	if( backcolor == undefined )
		backcolor = backcolors[lev];
	var foreglow  = new rgb(260,270,280);
	var backglow  = new rgb(224,224,240);
	for( var i=0; i<=glowlimit; i++ )
	{
		var glowrate = i / glowmax;
		glowitem[id].colseries[i] = new Object();
		glowitem[id].colseries[i].forergb = (new rgb( 
			forecolor.red + (foreglow.red-forecolor.red)*glowrate, 
			forecolor.green + (foreglow.green-forecolor.green)*glowrate, 
			forecolor.blue + (foreglow.red-forecolor.blue)*glowrate)).makeString(); 
		glowrate = i / glowlimit;
		glowitem[id].colseries[i].backrgb = (new rgb( 
			backcolor.red + (backglow.red-backcolor.red)*glowrate, 
			backcolor.green + (backglow.green-backcolor.green)*glowrate, 
			backcolor.blue + (backglow.red-backcolor.blue)*glowrate)).makeString(); 
	}

	if( parents == "" )
	{
		initglowseq[nInitGlows++] = id;
		if( nInitGlows == 1 )
			setTimeout("cmSeq(0)",200);
	}
	else {
		var idx = menuitem[parents+"!"].childglow.length++;
		menuitem[parents+"!"].childglow[idx] = new Object;
		menuitem[parents+"!"].childglow[idx].gettopper = glowitem[id].backstyle;
		menuitem[parents+"!"].childglow[idx].targetid  = id;
	}
}

function cmParental(id, delta)
{
	menuitem[id].height += delta;
	if( menuitem[id].bodystyle.getClipBottom() + delta < 0 )
	{
		delta = -menuitem[id].bodystyle.getClipBottom();
	}
	menuitem[id].bodystyle.setClipBottom(menuitem[id].bodystyle.getClipBottom()+delta)
	if( menuitem[id].belostyle )
		menuitem[id].belostyle.setTop(menuitem[id].belostyle.getTop() + delta );
	if( menuitem[id].parentid )
		cmParental( menuitem[id].parentid, delta );
}

function cmFlip(id)
{
	with (menuitem[id])
	{
		expanded = ! expanded;
		img.src = (expanded ? expandimg.src : shrinkimg.src);
		bodystyle.setVisibility('inherit');
		if( ! timer )
			timer = setInterval("cmMove('" + id + "');", menutimerwait);
		if( expanded )
		{
			childglowindex = 0;
			childglowheight = 0;
		}
	}
}

function cmMove(id)
{
	with( menuitem[id] )
	{
		var rest;
		if( expanded )
		{
			// expanding
			rest = height - bodystyle.getClipBottom();
			if( rest <= 0 )
			{	// finish moving
				clearInterval( timer );
				timer = undefined;
				delta = 0;
			}
			else
			{
				if( delta < 0 )
					delta += 2;
				else if( rest >= (delta+2)*(delta+1)*0.5 )
					delta += 1;
				else if( delta > 1 )
					delta -= 1;
				height -= delta;
				cmParental(id, delta);

				if( childglow.length > childglowindex && bodystyle.getClipBottom() > childglowheight )
				{
					cmFlash(childglow[childglowindex].targetid);
					childglowheight += childglow[childglowindex].gettopper.getHeight();
					childglowindex++;
				}
			}
		}
		else {
			// shrinking
			rest = bodystyle.getClipBottom();
			if( rest <= 0 )
			{	// finish moving
				clearInterval( timer );
				timer = undefined;
				bodystyle.setVisibility('hidden');
				delta = 0;
			}
			else
			{
				if( delta > 0 )
					delta -= 2;
				else if( rest >= (-delta+2)*(-delta+1)*0.5 )
					delta -= 1;
				else if( delta < -1 )
					delta += 1;
				height -= delta;
				cmParental(id, delta);
			}
		}
	}
}

function rgb(r,g,b)
{
	this.red = r;
	this.green = g;
	this.blue = b;
	this.makeString = 
function() { 
	return "rgb(" + (this.red<0?0:this.red>255?255:Math.floor(this.red+0.5)) + "," + 
		(this.green<0?0:this.green>255?255:Math.floor(this.green+0.5)) + "," + 
		(this.blue<0?0:this.blue>255?255:Math.floor(this.blue+0.5)) + ")"; } ;
}

function makergb(str)
{
	if( str.substring(0,3) == 'rgb' )
		return eval("new "+str);
	else if( str.substring(0,1) == '#' && str.length == 7 )
	{
		return eval("new rgb(0x" + str.substring(1,3) +
				   ",0x" + str.substring(3,5) +
				   ",0x" + str.substring(5,7) + ")");
	}
	else {
		return undefined;
	}
}

var nInitGlows = 0;
var defcolseries = new Array(glowlimit+1);
var glowlevels = new Array(glowmax - glowdiff, glowmax, glowmax, glowlimit);
{
	var forecolor = new rgb(0x66,0x66,0xff);
	var backcolor = new rgb(0x00,0x00,0x33);
	var foreglow  = new rgb(260,270,280);
	var backglow  = new rgb(224,224,240);
	for( var i=0; i<=glowlimit; i++ )
	{
		var glowrate = i / glowmax;
		defcolseries[i] = new Object();
		defcolseries[i].forergb = (new rgb( 
			forecolor.red + (foreglow.red-forecolor.red)*glowrate, 
			forecolor.green + (foreglow.green-forecolor.green)*glowrate, 
			forecolor.blue + (foreglow.red-forecolor.blue)*glowrate)).makeString(); 
		glowrate = i / glowlimit;
		defcolseries[i].backrgb = (new rgb( 
			backcolor.red + (backglow.red-backcolor.red)*glowrate, 
			backcolor.green + (backglow.green-backcolor.green)*glowrate, 
			backcolor.blue + (backglow.red-backcolor.blue)*glowrate)).makeString(); 
	}
	defcolseries[0].backrgb = "transparent";
}
var backcolors = new Array( 0, new rgb(0,51,153), new rgb(0,0,102), new rgb(0,0,24), new rgb(0,0,0) );
var initglowseq = new Array();

function cmSeq(ncnt)
{
	if( initglowseq[ncnt] )
	{
		setTimeout("cmSeq(" + (ncnt+1) + ")", 200);
		cmFlash( initglowseq[ncnt] );
	}
}

function cmInitGlow(id, parents, client, lev)
{
//	alert("id="+id+" parents="+parents);
	elem = new xbGetElementById(id);
	glowitem[id] = new Object();
	glowitem[id].style = new xbStyle(elem);
	glowitem[id].backstyle = glowitem[id].style;
	glowitem[id].glowing = 0;  // 0:fade 1:flash 2:glow 3:click
	glowitem[id].glowup = 0;
	glowitem[id].glowlevel = 0;
	glowitem[id].timer = undefined;
	glowitem[id].colseries = defcolseries;
}


function cmFlash(id)
{
	if( ! glowitem[id] )
		cmInitGlow(id);
	glowitem[id].glowing = 1;
	glowitem[id].glowup = glowtimerstep;
	if( ! glowitem[id].timer )
		glowitem[id].timer = setInterval("cmGlowFade('" + id + "');", glowtimerwait);
//	glowitem[id].style.setInnerHTML(glowitem[id].glowlevel+","+id);
}

function cmClick(id)
{
	if( ! glowitem[id] )
		cmInitGlow(id);
	glowitem[id].glowing = 3;
	glowitem[id].glowup = (glowlimit - glowitem[id].glowlevel+3)/3;
	if( ! glowitem[id].timer )
		glowitem[id].timer = setInterval("cmGlowFade('" + id + "');", glowtimerwait);
}

function cmGlow(id)
{
	if( ! glowitem[id] )
		cmInitGlow(id);
	glowitem[id].glowing = 2;
	glowitem[id].glowup = glowtimerstep;
	if( ! glowitem[id].timer )
		glowitem[id].timer = setInterval("cmGlowFade('" + id + "');", glowtimerwait);
}

function cmFade(id)
{
	if( ! glowitem[id] )
		cmInitGlow(id);
	glowitem[id].glowing = 0;
	if( glowitem[id].glowlevel > 0 )
	{
		if( ! glowitem[id].timer )
			glowitem[id].timer = setInterval("cmGlowFade('" + id + "');", glowtimerwait);
		if( glowitem[id].glowup < glowdiff )
			 glowitem[id].glowup = glowdiff;
	}
//	glowitem[id].style.setInnerHTML(glowitem[id].glowlevel+","+glowitem[id].glowing+","+glowitem[id].glowup);
}

function cmGlowFade(id)
{
	with( glowitem[id] )
	{
		if( glowup )
		{	//  glowing up
			if( -- glowup <= 0 )
			{
				if( glowing >= 2 )
				{
					clearInterval(timer);
					timer = undefined;
				} 
			}
			else {
				if( glowlevel < glowlevels[glowing] )
				{
					if( glowing >= 3 )
					{
						glowlevel += 3;
						if( glowlevel > glowlevels[3] )
							glowlevel = glowlevels[3];
					}
					else
						glowlevel++;
				}
			}
		}
		else 
		{	// glowing down
			if( --glowlevel <= 0 )
			{
				clearInterval(timer);
				timer = undefined;
				glowlevel = 0;
			}
		}
	
		style.setColor( colseries[glowlevel].forergb );
		backstyle.setBackgroundColor( colseries[glowlevel].backrgb );
	}
}
