

var pr_active;
var pr_inactive;
var topx = 482344;
var topy = 187948;

function loadImages () {
	pr_active = new Image();
	pr_active.src="../images/prred.gif";
	pr_inactive= new Image();	
	pr_inactive.src="../images/prblack.gif";
}

function prClicked() {
	document.getElementById("details" + event.srcElement.jid).click();
}

var prText=0;
var prevSrcElement=0;
var offsetX=0;
var offsetY=0;
function mouseOut() {
	if (prText) {
		prText.style.display = 'none';
	}
	prevSrcElement=0;
}

function mouseOver() {
	if (prevSrcElement == event.srcElement) {
		return;
	}
	prevSrcElement = event.srcElement;
	var x = a[prevSrcElement.pid][3];
	var y = a[prevSrcElement.pid][4];
	var txtList=[];
	for (var i=1;i<a.length;i++) {
	    if (a[i]) {
		if ((Math.abs(x-a[i][3]) < 4) && (Math.abs(y-a[i][4]) < 4)) {
			txtList.push(a[i][0]);
		}
                  }
	}
	if (txtList.length) {
		if (prText == 0 ) {
			prText = document.getElementById('prtext');
		}
		/*
		if (offsetX==0) {
			offsetX = document.getElementById('map').clientLeft;
			offsetY = document.getElementById('map').clientTop;
		}
		*/
	/*	prText.style.left=x+100+offsetX;
		prText.style.top=y+60+offsetY-(5+txtList.length*15);       */
		prText.style.left=x+5;
		prText.style.top=y-(5+txtList.length*15)-5;       
		prText.innerHTML = txtList.sort().join("<br>");
		prText.style.display='block';
	}		
}

var mapImg=0;
var prImg=0;
var curx,cury;
function newPractice(prid,practicename,x,y,colour,jid) {
	if (prImg==0) {prImg = document.getElementById("pr");}
	var newPr = prImg.cloneNode(false);
	newPr.src=pr_active.src;
	curx = Math.round((x-topx)/174)-1;
	cury = Math.round(479-(y-topy)/174)-1;
	newPr.style.left = curx;
	newPr.style.top= cury;
	newPr.name="PR" + prid;
	newPr.jid = jid;
	newPr.pid = prid;
	newPr.onmouseout = mouseOut;
	newPr.onmouseover = mouseOver;
//	newPr.title = practicename;
	newPr.style.display="inline";
	newPr.style.zIndex = 3 + colour;
	if (colour) {
		newPr.onclick = prClicked;
	}
	newPr.id = "PRTYPE" + colour;
	newPr.src = colour?pr_active.src:pr_inactive.src;
	if (mapImg==0) {mapImg = document.getElementById("map");}
	mapImg.appendChild(newPr);
}

function mapDisplay() {
	var im = document.getElementById("mapButton");
	var dm = document.getElementById("maparea");
	var dl = document.getElementById("listarea");
	if (dm.style.display=="block") {
		im.src = "../images/btn_showmap.gif";
		dm.style.display="none";
		dl.style.display="block";
	} else {
		im.src = "../images/btn_showlist.gif";
		dm.style.display="block";
		dl.style.display="none";
	}
}

function showPractices () {
	for (var i=0;i < jobloc.length; i++) {
		var pid=jobloc[i][0];
		if (a[pid][0].length) {
			newPractice(pid,a[pid][0],a[pid][1],a[pid][2],1,jobloc[i][1]);
			a[pid][3] = curx;
			a[pid][4] = cury;
		}
	}
}

function otherPractices() {
	// toggles display of the other practices on the map
	// See if they are being shown or not
	var obj = document.getElementById("showother");
	if (obj.innerText == "Show All Other Practices") {
		obj.innerText = "Hide All Other Practices";
		for (var pid=0;pid < a.length; pid++) {
			if (a[pid]) { // if an entry
				newPractice(pid,a[pid][0],a[pid][1],a[pid][2],0,0);
				a[pid][3] = curx;
				a[pid][4] = cury;
			}
		}
	} else 	{			
		obj.innerText = "Show All Other Practices";
		while (p = document.getElementById('PRTYPE0')) {
			mapImg.removeChild(p);
		}
		for (var pid=0;pid < a.length; pid++) {
			if (a[pid]) { // if an entry
				a[pid][3] = 0;
				a[pid][4] = 0;
			}
		}
		showPractices()
	}
}
		

function initMap() {  
	 if (document.getElementById("map") != null) {
		 loadImages();
		 /* loadArray();  */
		 showPractices();
	 }
 }

window.onload=initMap;


