var is_ns6plus_or_firefox = document.getElementById && !document.all;
var currcolorhex, currcolordec, targetcolordec, changeamountdec, direction, difference;
var hD = "0123456789ABCDEF";
var bgcolor = "#000000";
var RGB = new Array(256);
var hex = new Array("0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F");
var i = 0;
var j = 0;
var k = 0;

nereidFadeObjects = new Object();
nereidFadeTimers = new Object();
ColorFadeTimers = new Object();
ColorTextFadeTimers = new Object();

for (i = 0; i < 16; i++)
{
	for (j = 0; j < 16; j++)
	{
		RGB[k] = hex[i] + hex[j];
		k++;
	}
}

function d2h(d)
{
	var h = hD.substr(d&15, 1);
	
	while (d > 15)
	{
		d >>= 4;
		h = hD.substr(d&15, 1) + h;
	}
	
	return h;
}

function h2d(h)
{
	return parseInt(h, 16);
}

function rgb2hex(rgbvalue)
{	
	var red, green, blue;
	var temp = rgbvalue.indexOf('(');
	var temp2 = rgbvalue.indexOf(',');
	var rr, gg, bb;
	var hexcolor;
	
	red = rgbvalue.substr(temp + 1, temp2 - temp - 1);
	
	rgbvalue = rgbvalue.substr(rgbvalue.indexOf(',') + 2);
	
	temp2 = rgbvalue.indexOf(',');
	blue = rgbvalue.substr(0, temp2);
	
	rgbvalue = rgbvalue.substr(rgbvalue.indexOf(',') + 2);
	
	temp2 = rgbvalue.indexOf(')');
	green = rgbvalue.substr(0, temp2);
	
	rr = RGB[red];
	gg = RGB[blue];
	bb = RGB[green];
	hexcolor = "#" + rr + gg + bb;
	
	return (hexcolor);
}

function nereidFade(object, destOp, rate, delta)
{
	if (object != "[object]")
	{
		setTimeout("nereidFade(" + object + "," + destOp + "," + rate + "," + delta + ")", 0);
		return;
	}

	clearTimeout(nereidFadeTimers[object.sourceIndex]);

	diff = destOp - object.filters.alpha.opacity;
		
	direction = 1;

	if (object.filters.alpha.opacity > destOp)
		direction = -1;

	delta = Math.min(direction * diff, delta);
	
	object.filters.alpha.opacity += direction * delta;

	if (object.filters.alpha.opacity != destOp)
	{
		nereidFadeObjects[object.sourceIndex] = object;
		nereidFadeTimers[object.sourceIndex] = setTimeout("nereidFade(nereidFadeObjects[" + object.sourceIndex + "]," + destOp + "," + rate + "," + delta + ")", rate);
	}
}

function fade(id, opacity, number_of_steps, when_done_callback)
{
	var obj = document.getElementById(id);
		
	var o = parseFloat(obj.getAttribute("opacity"));
	var tid = parseInt(obj.getAttribute("timerid"));
	
	// Defaults
	if (obj.getAttribute("opacity") == null)
		obj.setAttribute("opacity", 1.0);
	if (obj.getAttribute("timerid") == null)
		obj.setAttribute("timerid", -1);
	if (obj.getAttribute("d") == null)
		obj.setAttribute("d", 0);
	if (!number_of_steps)
		number_of_steps = 1;
	if (!when_done_callback)
		when_done_callback = "";
	
	// Get the current direction d
	var d = parseInt(obj.getAttribute("d"));
	
	// Get the desired direction dd
	var dd = 0;
	
	if (opacity - o < 0)
		dd = -1;
	else if (0 < opacity - o)
		dd = 1;
	
	// If the desired direction dd is not the same as the current direction d, then kill the other fade process and use the desired direction.
	if (d != dd)
	{
		if (tid != -1)
			clearTimeout(tid);
		
		tid = -1;
		d = dd;
	}
	
	// Step size
	var ss = 0;
	if (number_of_steps)
		ss = Math.abs(opacity - o) / number_of_steps;
	
	// Step towards the new opacity
	o += d * ss;
	
	// If we are at the end point, terminate.
	if (0 < d)
	{
		if (opacity < o)
		{
			o = opacity;
			d = 0;
		}
	}
	else if (d < 0)
	{
		if (o < opacity)
		{
			o = opacity;
			d = 0;
		}
	}
	
	obj.style.opacity = o;
	obj.style.filter = "alpha(opacity=" + 100 * o + ")";
	
	obj.setAttribute("opacity", o);
	obj.setAttribute("d", d);
	
	number_of_steps -= 1;
	if (d && 0 < number_of_steps)
		tid = setTimeout("fade('" + id + "', " + opacity + ", " + number_of_steps + ", '" + when_done_callback + "')",10);
	else
	{
		tid = -1;
		
		if (when_done_callback)
			eval(when_done_callback);
	}
	
	obj.setAttribute("timerid", tid);
}

function ColorFade(objectid, targetcolorhex, changeamounthex, rate)
{
	if (!is_ns6plus_or_firefox)
		currcolorhex = String(document.getElementById(objectid).style.backgroundColor);
	else
		currcolorhex = rgb2hex(String(document.getElementById(objectid).style.backgroundColor));
		
	currcolordec = h2d(currcolorhex.substring(1, 7));
	targetcolordec = h2d(String(targetcolorhex).substring(1, 7));

	if (currcolordec != targetcolordec)
	{
		changeamountdec = h2d(String(changeamounthex).substring(1, 7));
		
		direction = currcolordec > targetcolordec? -1: 1;
		difference = Math.abs(currcolordec - targetcolordec);
		
		clearTimeout(ColorFadeTimers[document.getElementById(objectid).cellIndex]);
		
		if (difference < changeamountdec)
		{
			difference = difference * direction;
			currcolordec += difference;
		}
		else
		{
			changeamountdec = changeamountdec * direction;
			currcolordec += changeamountdec;
		}
		
		currcolorhex = d2h(currcolordec);
		
		while (currcolorhex.length < 6)
			currcolorhex = "0" + currcolorhex;
			
		currcolorhex = "#" + currcolorhex;
		
		document.getElementById(objectid).style.backgroundColor = currcolorhex;
		ColorFadeTimers[document.getElementById(objectid).cellIndex] = setTimeout("ColorFade('" + objectid + "', '" + targetcolorhex + "', '" + changeamounthex + "', " + rate + ")", rate);
	}
}

function ColorTextFade(objectid, targetcolorhex, changeamounthex, rate)
{
	if (!is_ns6plus_or_firefox)
		currcolorhex = String(document.getElementById(objectid).style.color);
	else
		currcolorhex = rgb2hex(String(document.getElementById(objectid).style.color));

	currcolordec = h2d(currcolorhex.substring(1, 7));
	targetcolordec = h2d(String(targetcolorhex).substring(1, 7));
	
	if (currcolordec != targetcolordec)
	{
		changeamountdec = h2d(String(changeamounthex).substring(1, 7));
		
		direction = currcolordec > targetcolordec? -1: 1;
		difference = Math.abs(currcolordec - targetcolordec);
		
		clearTimeout(ColorTextFadeTimers[document.getElementById(objectid).cellIndex]);
		
		if (difference < changeamountdec)
		{
			difference = difference * direction;
			currcolordec += difference;
		}
		else
		{
			changeamountdec = changeamountdec * direction;
			currcolordec += changeamountdec;
		}
		
		currcolorhex = d2h(currcolordec);
		
		while (currcolorhex.length < 6)
			currcolorhex = "0" + currcolorhex;
			
		currcolorhex = "#" + currcolorhex;
		
		document.getElementById(objectid).style.color = currcolorhex;
		ColorTextFadeTimers[document.getElementById(objectid).cellIndex] = setTimeout("ColorTextFade('" + objectid + "', '" + targetcolorhex + "', '" + changeamounthex + "', " + rate + ")", rate);
	}
}

function pointer(obj)
{
	obj.style.cursor = 'pointer';
}

var activemenuid = "";
var activeddmenuid = "";
var ClearMenuTimers = new Object();

function ShowMenu(menuid)
{
	if (activemenuid != "")
	{
		HideMenu(activemenuid);
		clearTimeout(ClearMenuTimers[document.getElementById(activemenuid).getAttribute('timerid')]);
	}
	
	activemenuid = menuid;
	
	document.getElementById(menuid).className = 'menufont menupadding menuhighlight';

	//ColorFade(menuid, '#FFFFFF', '#111111', 15);
	//ColorTextFade(menuid, '#000000', '#111111', 15);
}

function ShowDDMenu(ddmenuid)
{
	if (activeddmenuid != "")
	{
		HideDDMenu(activeddmenuid);
		clearTimeout(ClearMenuTimers[document.getElementById(activeddmenuid).getAttribute('timerid')]);
	}
		
	activeddmenuid = ddmenuid;
	
	document.getElementById(ddmenuid + 'container').style.display = "block";

	if (!is_ns6plus_or_firefox)
	{
		nereidFade(document.getElementById(ddmenuid), 100, 10, 20);
		nereidFade(document.getElementById(ddmenuid + 'bg'), 90, 10, 20);
	}
	else
	{
		fade(ddmenuid, 1, 10);
		fade(ddmenuid + 'bg', 0.9, 10);
	}
}

function HideMenu(menuid)
{
	document.getElementById(menuid).className = 'menufont menupadding menuunhighlight';
	//ColorFade(menuid, '#333333', '#111111', 15);
	//ColorTextFade(menuid, '#FFFFFF', '#111111', 15);
}

function HideDDMenu(ddmenuid)
{
	if (ddmenuid != "")
	{
		if (!is_ns6plus_or_firefox)
		{
			nereidFade(document.getElementById(ddmenuid), 0, 10, 20);
			nereidFade(document.getElementById(ddmenuid + 'bg'), 0, 10, 20);
		}
		else
		{
			fade(ddmenuid, 0, 10);
			fade(ddmenuid + 'bg', 0, 10);
		}
		
		document.getElementById(ddmenuid + 'container').style.display = "none";
	}
}

function SetHideMenu(menuid)
{
	//ClearMenuTimers[document.getElementById(menuid).getAttribute('timerid')] = setTimeout("ColorFade('" + menuid + "', '#333333', '#111111', 15); ColorTextFade('" + menuid + "', '#FFFFFF', '#111111', 15);", 500);
	ClearMenuTimers[document.getElementById(menuid).getAttribute('timerid')] = setTimeout("document.getElementById('" + menuid + "').className = 'menufont menupadding menuunhighlight';", 500);
}

function SetHideDDMenu(ddmenuid)
{
	ClearMenuTimers[document.getElementById(ddmenuid).getAttribute('timerid')] = setTimeout("HideDDMenu('" + ddmenuid + "')", 500);
}

function ClearHideMenu(menuid)
{
	clearTimeout(ClearMenuTimers[document.getElementById(menuid).getAttribute('timerid')]);
}

function ClearHideDDMenu(ddmenuid)
{
	clearTimeout(ClearMenuTimers[document.getElementById(ddmenuid).getAttribute('timerid')]);
}

function HighlightDDMenu(ddmenuid)
{
	document.getElementById(ddmenuid).style.background = "#336699";
}

function UnhighlightDDMenu(ddmenuid)
{
	document.getElementById(ddmenuid).style.background = "#003471";
}

function nav(url)
{
	window.location.href = url;
}