var menuX=0,menuY=0;menuW=0;menuH=0;border=25; 
var oldx=0;oldy=0;
var clickedtagid=0;
var clickedtag="";
var oclass = "";

// show menu
function show( x, y )
{
	var menu = document.getElementById('menu');

	menuX = x; menuY = y;
	
	menu.style.left = x + "px";
	menu.style.top = y + "px";
	menu.style.display = 'block';

	menuW = menu.offsetWidth; menuH = menu.offsetHeight;
}

// hide menu
function hide()
{
	var x = document.getElementById('menu');
	if (x) x.style.display = 'none';
}

// get coords 
function find_mouse( e )
{
	var x=10, y=10;
	
	e = e || window.event;
	
	if (e && e.pageX) 
	{
		// ffox
		x = e.pageX; 
		y = e.pageY; 
	}
	else
	if (e && e.clientX)
	{
		// ie, strict and normal
		var scroll = document.documentElement ? document.documentElement : document.body;

		x = e.clientX + scroll.scrollLeft; 
		y = e.clientY + scroll.scrollTop;
	}
	else
	{
		// unsupported
		x = oldx;
		y = oldy;
	}
		
	return {x: x, y: y};
}

// mouse position tracker 
function track_mouse(e)
{
		var m = find_mouse(e);
		if ((m.x < (menuX - border)) || (m.x > (menuX + menuW + border)) || (m.y < (menuY - border)) || (m.y > (menuY + menuH + border)))
			hide();
		
		if (e)
		{
			oldx = m.x;
			oldy = m.y;
		}	
}

if (document.addEventListener)
	document.addEventListener("mousemove", track_mouse, false);
else if (document.attachEvent)
	document.attachEvent("onmousemove", track_mouse);
else
	document.onmousemove = track_mouse;

// called by your onclick 
function showMenu(e, tagid, tagname)
{
  if (e)
  {
		clickedtagid=tagid;
		clickedtag=tagname;
		prepareMenu(tagid, tagname);
		var m = find_mouse(e);
		show(m.x, m.y);
  }	
}

function setLinkText(id, html)
{
  var x = getObj(id);
  if (x != null)
  	x.innerHTML = html;
}

function prepareMenu(tagid, tagname)
{
	setLinkText("miAbout", "<SPAN style='float: left; height: 30px;'><IMG border='0' SRC='http://gui.tvchix.com/images/pplside.gif'></SPAN> \"<b>" + tagname + "</b>\" describes something About Me, one of my Interests or Groups");
	setLinkText("miLooking", "<SPAN style='float: left; height: 30px;'><IMG border='0' SRC='http://gui.tvchix.com/images/pplpair.gif'></SPAN> \"<b>" + tagname + "</b>\" doesnt describe me, but its something Im Looking For in others");
	setLinkText("miSearch", "<IMG border='0' SRC='http://gui.tvchix.com/images/view.gif'> Search for users with this Tag");
	setLinkText("miRemove", "<IMG border='0' SRC='http://gui.tvchix.com/images/sqcancel.gif'> Remove this Tag from My Profile");
}

function miDoClick(id)
{
	if (id == "miAbout")        makeRequest('addtag', 'ajaxhandler.php?addtag=1&tagtype=0&tagid=' + clickedtagid) 
	else if (id == "miLooking") makeRequest('addtag', 'ajaxhandler.php?addtag=1&tagtype=1&tagid=' + clickedtagid) 
	else if (id == "miRemove")  makeRequest('deltag', 'ajaxhandler.php?deltag=1&tagid=' + clickedtagid + '&refresh=true') 
	hide();
}
		
function getObj (nr) //this function is to maintain cross platform compatibility
{
	if (document.layers) { return document.layers[nr];	}
	else if (document.all)	{	return document.all[nr]; }
	else if (document.getElementById) { return document.getElementById(nr); }
	else { return false;	}
}

function misearchclick(el)
{
	hide();
	if(clickedtag!="")
		document.location.href="http://www.tvchix.com/search.php?st=tg&q="+clickedtag;
	else	
		alert("Sorry, this feature is still in development.\n Be sure to read the News and Announcements for when it becomes available.");
}

function FlashTag(id)
{
	fsteps=4;
	var x = getObj(id);
	if (x != null)
		oclass = x.className;
	setTimeout('CycleTag("' + id + '")', 80);
}

function CycleTag(id)
{
	var x = getObj(id);
	if (x != null)
	{
		var y = x.className;
		var i = y.indexOf("shadex");
		if (i < 0) {
			y = y.replace("shade", "");
			y = y + " shadex1";
		}
		else {
			var s = y.substr(i, 7);
			y = y.substr(0, i);
			if (s=="shadex1")
			  y = y + " shadex2";
			else
				y = y + " shadex1";  
		}
		x.className = y;
		
		fsteps--;
		if (fsteps > 0)
			setTimeout('CycleTag("' + id + '");', 80);
		else {
			x.className = oclass;
			oclass="";	
		}
	}
}