
ie = false;

if ( document.all ) { ie = true; }
else
{
	document.captureEvents(Event.MOUSEMOVE);
	document.onmousemove = getMousePositionNONIE;
	document.addEventListener("mousemove", getMousePositionNONIE, true);
}

function getObj( id )
{
	if ( ie && document.all )
	{
		return document.all[ id ];
	}
	else if ( document.getElementById )
	{
		return document.getElementById( id );
	}
	else if ( document.layers )
	{
		return document.layers[ id ];
	}
	else
	{
		return id;
	}
}

var mouseX = 0;
var mouseY = 0;

function getMousePositionNONIE( e )
{
    mouseX = e.pageX;
    mouseY = e.pageY;
}

//var categoryArray = null;
var linkArray = null;
var MAXLINKS = 5;

/*function newCategory( category )
{
	if ( categoryArray != null )
	{
		categoryArray[ categoryArray.length ] = category;
	}
	else
	{
		categoryArray = new Array( );
	}
}*/

function URLEncodedFormat( url )
{
	return url.replace( "+", "%2B" );
}

function newLink( category, URL, name )
{
	//alert( category + ' and ' + theLink );
	if ( linkArray != null )
	{
		if ( findLinkCategory( category ) )
		{
			for ( i = 0; i < linkArray.length; i++ )
			{
				if ( linkArray[ i ][ 0 ] == category )
				{
					blah = linkArray[ i ].length;
					linkArray[ i ][ blah ] = [ name ];
					linkArray[ i ][ blah ][ 1 ] = [ URL ];
				}
			}
		}
		else
		{
			blah2 = linkArray.length;
			linkArray[ blah2 ] = [ category ];
			linkArray[ blah2 ][ 1 ] = [ name ];
			linkArray[ blah2 ][ 1 ][ 1 ] = [ URL ];
		}
	}
	else
	{
		//linkArray = [ [ "0", [ "00", "01" ] ], [ "1", [ "11", "12" ] ] ];
		//alert( linkArray[ 0 ][ 0 ] + ' and ' + linkArray[ 0 ][ 1 ][ 0 ] );
		//alert( linkArray[ 1 ][ 2 ] + ' and ' + linkArray[ 1 ][ 1 ] );
		linkArray = new Array( );
		linkArray[ 0 ] = [ category ];			
		linkArray[ 0 ][ 1 ] = [ name ];
		linkArray[ 0 ][ 1 ][ 1 ] = [ URL ];
	}
}

function findLinkCategory( category )
{
	if ( linkArray != null )
	{
		for ( k = 0; k < linkArray.length; k++ )
		{
			if ( linkArray[ k ][ 0 ] == category ) return true;
		}
	}
	
	return false;
}

function displayLinks( where )
{
	//alert( linkArray[ 0 ][ 0 ] + ' and ' + linkArray[ 0 ][ 1 ] );
	//alert( linkArray[ 0 ][ 0 ] + ' and ' + linkArray[ 0 ][ 2 ] );
	//alert( linkArray[ 1 ][ 0 ] + ' and ' + linkArray[ 1 ][ 1 ] );
	//alert( linkArray[ 1 ][ 0 ] + ' and ' + linkArray[ 1 ][ 2 ] );
	
	//alert( findLinkCategory( 'dev' ) );
	
/*	
	table = "<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" align=\"center\">"
	table = table + "<tr>";
	table = table + "	<td colspan=\"3\">";
	table = table + "		<table width=\"100%\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\">";
	table = table + "			<tr>";
	table = table + "				<td width=\"23\" height=\"23\"><img src=\"../images/window/top.left.png\" style=\"width: 23px; height: 23px; border: 0;\"></td>";
	table = table + "				<td height=\"19\" background=\"../images/window/top.png\" class=\"bold\" style=\"color: white;\">sparce links v2.31</td>";
	table = table + "				<td width=\"23\" height=\"23\"><img src=\"../images/window/top.right.png\" style=\"width: 23px; height: 23px; border: 0;\"></td>";
	table = table + "			</tr>";
	table = table + "		</table>";
	table = table + "	</td>";
	table = table + "</tr>";
	table = table + "<tr>";
	table = table + "	<td width=\"4\" background=\"../images/window/left.png\"></td>";
	table = table + "	<td background=\"../images/window/spacer.png\">";*/
	table = "		<table border=\"0\" cellpadding=\"0\" class=\"link\" style=\"width: 100%;\">";
	table = table + "			<tr height=\"19\">";
	table = table + "				<td colspan=\"" + 2*MAXLINKS + 3 + "\" align=\"center\"><a href=\"http://indeepthought.org/\" style=\"display: inline; padding: 0px;\"><img src=\"images/indeepthought.png\" alt=\"in Deep Thought\" border=\"0\"></a></td>";
	table = table + "			</tr>";
	for ( i = 0; i < linkArray.length; i++ )
	{
		table = table + "<tr>";
		table = table + "	<td height=\"2\" colspan=\"" + 2*MAXLINKS + 3 + "\" background=\"../images/window/line.seperator.png\"></td>";
		table = table + "</tr>";
		table = table + "<tr height=\"19\">";
		table = table + "	<td class=\"bold\"><a href=\"http://del.icio.us/dharh/" + linkArray[ i ][ 0 ] + "\">" + linkArray[ i ][ 0 ] + "</a></td>";
		for ( j = 1; j <= MAXLINKS; j++ )
		{
			if ( j < linkArray[ i ].length )
			{
				table = table + "	<td> | </td>";
				table = table + "	<td><a href=\"/sparce/delicious/go.cfm?tag=" + URLEncodedFormat(linkArray[ i ][ 0 ]) + "&link=" + linkArray[ i ][ j ][ 1 ] + "\">" + linkArray[ i ][ j ][ 0 ] + "</a></td>";
			}
			else
			{
				table = table + "	<td> | </td>";
				table = table + "	<td></td>";
			}
		}
		table = table + "	<td> | </td>";
		if ( ( linkArray[ i ].length > MAXLINKS + 1 ) )
		{
			table = table + "	<td><a onClick=\"stayVisible( this ); displayMenu( '" + linkArray[ i ][ 0 ] + "', this );\" onMouseOut=\"hideMenu( 'tagMenu' );\" class=\"clickable\" nowrap=\"nowrap\">[ more... ]</a></td>";
		}
		else
		{
			table = table + "	<td style=\"color: #999999;\" nowrap=\"nowrap\"></td>";
		}
		table = table + "</tr>";
	}
	table = table + "		</table>";
	/*table = table + "	</td>";
	table = table + "	<td width=\"4\" background=\"../images/window/right.png\"></td>";
	table = table + "</tr>";
	table = table + "<tr>";
	table = table + "	<td width=\"4\" height=\"4\"><img src=\"../images/window/bottom.left.png\" style=\"width: 4px; height: 4px; border: 0;\"></td>";
	table = table + "	<td height=\"4\" background=\"../images/window/bottom.png\"></td>";
	table = table + "	<td width=\"4\" height=\"4\"><img src=\"../images/window/bottom.right.png\" style=\"width: 4px; height: 4px; border: 0;\"></td>";
	table = table + "</tr>";
	table = table + "</table>";*/
	
	//var el = getObj( where );
	//el.innerHTML = table;
	document.write( table );
}

function displayMenu( category, where )
{
	table = "<table style='border: 0; border-spacing: 0; padding: 0; background-color: #ECE9D8; border-collapse: collapse;'>";
	i = 0;
	while ( i < linkArray.length )
	{
		if ( linkArray[ i ][ 0 ] == category )
		{
			if ( linkArray[ i ].length > MAXLINKS + 1 )
			{
				for ( j = MAXLINKS + 1; j < linkArray[ i ].length; j++ )
				{
					table = table + "<tr>";
					table = table + "<td nowrap>";
					table = table + "<a href=\"/sparce/delicious/go.cfm?tag=" + URLEncodedFormat(linkArray[ i ][ 0 ]) + "&link=" + linkArray[ i ][ j ][ 1 ] + "\">" + linkArray[ i ][ j ][ 0 ] + "</a>";
					table = table + "</td>";
					table = table + "</tr>";
				}
			}
			break;
		}
		i++;
	}
	table = table + "</table>";
	
	if ( !stayingVisible || where == stayVisibleObj )
	{
		var el = getObj( 'tagMenu' );
		el.style.border = "1px solid blue";
		//var el2 = getObj( where );
		
		var top = getAbsoluteOffsetTop( where );
		var left = getAbsoluteOffsetLeft( where ) + getWidth( where );
		
		//if ( getWidth( el ) < getWidth( where ) )
		//{
			//el.style.width = getWidth( where );
		//}
		el.style.display = "block";
		el.style.top = top;
		el.style.left = left;
		el.innerHTML = table;
		
		var i = left + getWidth( el );
		var j = top + getHeight( el );
		//alert( getWidth( el ) );
		
		if ( document.body.clientWidth < i )
		{
			el.style.left = getAbsoluteOffsetLeft( where ) - getWidth( el );
		}
		
		//alert( window.innerHeight + ' ' + getHeight( el ) );
		if ( window.innerHeight < j )
		{
			el.style.top = top - getHeight( el ) + getHeight( where );
		}
	}
}

function hideMenu( obj )
{
	if ( stayingVisible )
		return;
	
	var el = getObj( obj );
	bottom = getAbsoluteOffsetTop( el ) + getHeight( el );
	right = getAbsoluteOffsetLeft( el ) + getWidth( el );

	el.style.top = 0 - bottom; 
	el.style.left = 0 - right;	
	el.style.display = "none";
}

function getAbsoluteOffsetTop( obj )
{
	var top = obj.offsetTop;
	var parent = obj.offsetParent;
	while ( parent && parent != document.body )
	{
		top += parent.offsetTop;
		parent = parent.offsetParent;
	}
	return top;
}

function getAbsoluteOffsetLeft( obj )
{
	var left = obj.offsetLeft;
	var parent = obj.offsetParent;
	while ( parent && parent != document.body )
	{
		left += parent.offsetLeft;
		parent = parent.offsetParent;
	}
	return left;
}

function getWidth( obj )
{
	var width = 0;
	
	width += obj.offsetWidth;
	
	return width;
}

function getHeight( obj )
{
	var height = 0;
	
	height += obj.offsetHeight;
	
	return height;
}

var wasClicked = 0;
var whatWasClickedBefore = 0;

function clicked( )
{
  if ( whatWasClickedBefore )
	{
	  wasClicked = whatWasClickedBefore;
	}
	
	if ( wasClicked )
	{
		wasClicked = 0;
	}
	else
	{
		wasClicked = 1;
	}
	
	whatWasClickedBefore = 0;
}

var stayingVisible = 0;
var stayVisibleObj = null;

function stayVisible( obj )
{
	if ( stayingVisible )
	{
		stayingVisible = 0;
		hideMenu( 'tagMenu' );
		if ( stayVisibleObj != obj ) { stayVisible( obj ); }
	}
	else
	{
		stayingVisible = 1;
		stayVisibleObj = obj;
	}
}


