//Image Tagging Library written by T.J. Lipscomb
//For use on shsrobotics.org
var mX = 0;
var mY = 0;
var img;
var map;
var labels;
var IE = (document.all)?true:false;
var box;
var startX = 0;
var startY = 0;

function tag_init() {
	img = document.getElementById('tagpic');
	if(!img) {
		setTimeout(tag_init,100);
		return;
	}
	
	img.onmouseover=tag_clean;
	
	map = document.getElementById('map');
	labels = document.getElementById('taglabels');
	
	//alert("Tag library initialized");
}

function tag_createtagbox(x,y,w,h,clname) {
	var obj=document.createElement("div");
	obj.className=clname;
	obj.style.position="absolute";
	obj.style.left = (Number(x)+tag_getoffsetleft(img))+"px";
	obj.style.top = (Number(y)+tag_getoffsettop(img))+"px";
	if(w) obj.style.width = w+"px";
	if(h) obj.style.height = h+"px";
	document.body.appendChild(obj);
	return obj;
}

function tag_highlight(id,x,y,w,h) {
	//Clean up and previous boxes
	tag_clean();
	
	img.onmouseover=null;

	//Display a border around their face	
	b=tag_createtagbox(x,y,w,h,"tagbox");
	b.onmouseout=tag_clean;
	if(document.getElementById('user'+id).href) b.onclick=function(){
		window.location=document.getElementById('user'+id).href;
	}
	
	//Display a box with their name
	b2=tag_createtagbox(x+w-2,y+4,0,0,"tagbox2");
	b2.innerHTML=document.getElementById('user'+id).innerHTML;
	b2.onmouseout=tag_clean;
	b2.onmouseover=tag_clean;
	
	document.getElementById('user'+id).onmouseout=b.onmouseout;
	document.getElementById('user'+id).className='highlight';
	
	//Delay bringing back the mouseover because IE doesn't like it.
	setTimeout("img.onmouseover=tag_clean",100);
}

function tag_clean() {
	for(i=0;i<document.body.childNodes.length;i++) {
		if(document.body.childNodes[i].className == "tagbox" || document.body.childNodes[i].className == "tagbox2") {
			//if(document.body.childNodes[i].className == "tagbox2") alert(document.body.childNodes[i].innerHTML);
			//alert(document.body.childNodes[i].innerHTML);
			document.body.removeChild(document.body.childNodes[i]);
			i--;
		}
	}
	
	for(i=0;i<labels.childNodes.length;i++) {
		if(labels.childNodes[i].tagName == "A") labels.childNodes[i].className = "";
	}
}

function tag_getoffsetleft(obj) {
	if(!IE) return obj.offsetLeft;
	r = obj.getBoundingClientRect();
	return r.left;
}
function tag_getoffsettop(obj) {
	return obj.offsetTop;
	r = obj.getBoundingClientRect();
	return r.top;
}

onload=tag_init;

