//
// Map bubble text
//

//	moz = (document.layers)? true:false;
	moz = (window.Event)? true:false;
ie4 = (document.all)? true:false


// Microsoft Check.
if (ie4) {
	if (navigator.userAgent.indexOf('MSIE 5')>0) {
	ie5 = true;
	} else {
	ie5 = false; }
} else {
ie5 = false;
}

if (typeof offsetx == 'undefined') { var offsetx = 15; }
if (typeof offsety == 'undefined') { var offsety = 15; }
if (typeof width == 'undefined') { var width = 200; }
if (typeof height == 'undefined') { var height = 40; }
if (typeof border == 'undefined') { var border = "1"; }
if (typeof backcolor == 'undefined') { var backcolor = "#333399"; }
if (typeof fcolor  == 'undefined') { var fcolor = "#CCCCFF"; }
if (typeof textcolor  == 'undefined') { var textcolor = "#000000"; }

// keep last caption to prevent switching from top.half to bot.half when on border.
if (typeof lasttxt == 'undefined') {var lasttxt = "@";}

var pixrange = 15;	// show bubble if within n pixels.
var maxwidth = 300;	// max width of bubble.
//var prevtxt = "";		// used to see if layer should be updated or not.
// top,left corner of map image.

var basex = 0;
var basey = 0;
//	if (moz) {
//		basex = document.getElementById('mapimg').offsetLeft;
//		basey = document.getElementById('mapimg').style.top;
//	} else if (ie4) {
//		basex = document.all["mapimg"].offsetLeft;
//		basey = document.all["mapimg"].offsetTop;
//	}

// half x,y of image (determines which side bubbles go)
var halfx = 225;
var halfy = 225;
var ux = 0;	// mouse x,y
var uy = 0;
var x = 0;	// pin x,y
var y = 0;
var dir = 1;
var snow = 0;
var sw = 0;
var cnt = 0;
var tr = 1;
if ( (moz) || (ie4) ) {
	//	if (moz) over = document.overDiv
//	alert(frames['mapimg']);
//	if (moz) over = document.getElementById('overDiv').style;
//	if (ie4) over = overDiv.style

//	document.onmousemove = mouseMove
//	document.onmousedown = clickmap

//	if (moz) document.captureEvents(Event.MOUSEMOVE)		;// don't need these?
//	if (moz) document.captureEvents(Event.MOUSEDOWN)
}

// caption
function dcs(text, txtcol) {
	// figure table width
	width = text.length * 10;
	if (width > 200) {width = 200;}

	if (text == "") {
		txt = "";
	} else {
		//if (txtcol==1) {bgcol="#CCCCFF"}
		//if (txtcol==2) {bgcol="#EFCFFF"}
		if (txtcol==1) {bgcol="#CCCCCC"}
		if (txtcol==2) {bgcol="#CCCCFF"}
		//if (txtcol==1) {text = "<U>"+ text +"</U>"}

		txt = "<TABLE WIDTH="+width+" BORDER=0 CELLPADDING="+border+" CELLSPACING=0 BGCOLOR=\""+backcolor+"\"><TR><TD><TABLE WIDTH=100% BORDER=0 CELLPADDING=2 CELLSPACING=0 BGCOLOR=\""+bgcol+"\"><TR><TD><FONT FACE=\"Arial,Helvetica\" COLOR=\""+textcolor+"\" SIZE=\"-2\">"+text+"</FONT></TD></TR></TABLE></TD></TR></TABLE>"
	}
	
	dir = 1; // Left.
	
//	x += offsetx;
	if (x < 0) {x = 0}
	// this 450 has to be width of map img.
	maxw = frames['mapimg'].document.getElementById('mp').width - 2;
	if ((x + width) > maxw) {x = maxw - width}; //{x = x - width - offsetx}
	if (x < 0) {x = 0}
	
	//if ( !(y < halfy) ) {y = y - (height + offsety)}
//	if (y < halfy) {y = y + offsety} else {y = y - (height + offsety)}
	if (y < (25+ (offsety*2))) {y = y + offsety} else {y = y - (height + offsety)}

	disp(x, y);

	// if text has changed AND current layer doesn't contain pin image, update layer.
	if (text != lasttxt) {layerWrite(txt);}
	lasttxt = text;

	return true;
}

// Clears popups if appropriate
function nd(e) {
	//prevtxt = "@";	//this txt will never be a caption.
	lasttxt = "@";	//this txt will never be a caption.
	if ( cnt >= 1 ) { sw = 0 };
		if ( (moz) || (ie4) ) {
			if ( sw == 0 ) {
			snow = 0;
		//	parent.mapimg.document.imgform.bubbleup.checked = false;
//			document.imgform.bubbleup.checked = false;

		//	over = document.getElementById('overDiv').style;
			over = frames['mapimg'].document.getElementById('overDiv').style;

			hideObject(over);
		} else {
			cnt++;
		}
	}
	return true;
}


// Common calls
function disp(x,y) {
	if ( (moz) || (ie4) ) {
		if (snow == 0) {

		//	over = document.getElementById('overDiv').style;
			over = frames['mapimg'].document.getElementById('overDiv').style;

//			alert('moving to '+x+"+"+y)
			moveTo(over, x, y);
			showObject(over);
			snow = 1;
		}
	}
}

// Moves the layer
function mouseMove(e) {
var tmptxt

if (snow) {
//		alert('snow='+snow);
	/*
	// if layer should still be pin image, remove it.
	if (moz) {
	}
	if (ie4) {
		tmptxt = document.all["overDiv"].innerHTML;
		if (tmptxt.indexOf('.gif')>0 ) {document.all["overDiv"].innerHTML = "";}
	}
	*/

	// HERE - figure the closest site & update layer.
	getMouseCoords(e);
	
	// do this here and on showbub().
	i = ClosestSite(ux, uy, pixrange);
	if (i >= 0) {
		captxt = ct[i];
		if (sc[i] == "") {txtcol=1} else {txtcol=2}
	} else {
		captxt = "";
		txtcol = 1;
	}
	
//	basex = document.getElementById('mapimg').offsetLeft;
//	basey = document.getElementById('mapimg').offsetTop;
//	alert(e.clientX+','+e.clientY);

	x = Math.abs(ux - basex + offsetx);
	y = Math.abs(uy - basey + offsety);
	dcs( captxt, txtcol );

//	over = document.getElementById('overDiv').style;
	over = frames['mapimg'].document.getElementById('overDiv').style;

	moveTo(over, x, y);
}
}

// Writes to a layer
function layerWrite(txt) {
	if (moz) {
	//	var lyr = document.overDiv.document
	//	lyr.write(txt)
	//	lyr.close()

	//	document.getElementById('overDiv').innerHTML = txt
		frames['mapimg'].document.getElementById('overDiv').innerHTML = txt
	} else if (ie4) frames['mapimg'].document.all["overDiv"].innerHTML = txt
}

// Make an object visible
function showObject(obj) {
//	if (moz) obj.visibility = "show"
	if (moz) obj.visibility = "visible"
	else if (ie4) obj.visibility = "visible"
}

// Hides an object
function hideObject(obj) {
//	if (moz) obj.visibility = "hide"
	if (moz) obj.visibility = "hidden"
	else if (ie4) obj.visibility = "hidden"
}

// Move a layer
function moveTo(obj,xL,yL) {
obj.left = xL
obj.top = yL
}


// for showing captions over map image
function showbub(e) {

// do this here and on mouseMove.
//	parent.mapimg.document.imgform.bubbleup.checked = true;
//	document.imgform.bubbleup.checked = true;

//	o = document.getElementById('mapimg')
//	o = frames['mapimg'].document.getElementById('mp');
//	alert(e.clientX +"+"+ e.clientY);
//	alert(o.offsetLeft +"+"+ o.offsetTop);
//	basex = o.offsetLeft;
//	basey = o.offsetTop;

//	alert(ux+"+"+uy+"+"+pixrange + ", basex="+basex);

	i = ClosestSite(ux, uy, pixrange);
	if (i >= 0) {
		captxt = ct[i];
		if (sc[i] == "") {txtcol=1} else {txtcol=2}
	} else {
		captxt = "";
		txtcol = 1;
	}
	x = Math.abs(ux - basex + offsetx);
	y = Math.abs(uy - basey + offsety);
	dcs( captxt, txtcol );
}

// click map image
function clickmap(e) {
	getMouseCoords(e);
	
	i = ClosestSite(ux, uy, pixrange);
	if (i >= 0) {
		if ((sc[i] > "") && (tr[i] > "")) {
			anch = sc[i];

			// 2007-02-17: link to site page, not tree
//
//			anchary = anch.split("/");
//			anch = anchary[0] +"-"+ anchary[1] +"-"+ anchary[2];
//		//	newurl = "/f.htm?1=/trees/" + tr[i] + ".HTM+" + anch + "&2=/sites/" + sc[i] + ".HTM";
//
			// retain f vs s domain
			var url = location.href;
			var s1 = url.indexOf('/maps/');
			if (s1 != -1) {
				host = unescape(url.substring(0, s1));
			} else {
				host = "http://www.planetware.com";
			}

//		//	newurl = "/s/treelist/"+ anchary[0] +".HTM?t=" + tr[i] + "&s=" + anch + "";
//			newurl = host +"/treelist/"+ anchary[0] +".HTM?t=" + tr[i] + "&s=" + anch + "";
//
			newurl = host +"/sites/"+ anch +".HTM";
//

		//	parent.document.location.href = newurl;
			document.location.href = newurl;
		} else {
			// if only sitecode specified, link to full page. (i.e. US state portal page)
			if (sc[i] > "") {
				newurl = sc[i];
			//	parent.document.location.href = newurl;
				document.location.href = newurl;
			}
		}
	}
	return false;
}

function getMouseCoords(e) {
	// sets X (ux) & Y (uy) global vars.
//	alert('getmousecoords');
//	if (moz) {ux=e.pageX; uy=e.pageY;}
//	if (ie4) {ux=event.x; uy=event.y;}
//	if (ie5) {ux=event.x+document.body.scrollLeft; uy=event.y+document.body.scrollTop;}
	ux = e.clientX;
	uy = e.clientY;
}

function ClosestSite(ux,uy,pixrange) {
	// go through each coord, see what's closest (within x pixels).
	// returns -1 if none within range.
	var dmin = 1000;
	var d = 0;

//	basex = document.getElementById('mapimg').offsetLeft;
//	basey = document.getElementById('mapimg').offsetTop;

	for (i=0; i < mx.length; i++) {
		dx = Math.abs( Math.abs((ux - basex) - mx[i]));
		dy = Math.abs( Math.abs((uy - basey) - my[i]));
		d = Math.sqrt((dx * dx) + (dy * dy));
		if (d < dmin) {dmin = d; imin = i;}
	}
	if (dmin < pixrange) {
		return imin;
	} else {
		return -1;
	}
}

