var iDragX = 0;
var iDragY = 0;
var bDrag = false;
////////////////////////////////////////////////////////////////////////////////////////////////
//Add the map HTML/image and the title image
function addMap(str, sTitle, bCenter) {
	isMapZoomed = false;
	getElem("map").innerHTML = str;
	getElem("title").innerHTML = sTitle;
//Do this to center on single element in site view
	if (bCenter) {
		if (getElem("map_container").getElementsByTagName("div").length > 0) {
//			zoomMap("in");
			iLeft = parseInt(getElem("map_container").getElementsByTagName("div")[0].offsetLeft);
			iTop = parseInt(getElem("map_container").getElementsByTagName("div")[0].offsetTop);	
			moveMapTo(iLeft, iTop);
		}
	}
	getElem("map").style.cursor = "move";
	getElem("map").getElementsByTagName("img")[0].onmousemove = dragMap;
	getElem("map").getElementsByTagName("img")[0].onmousedown = startDrag;
	getElem("map").getElementsByTagName("img")[0].onmouseup = cancelDrag;
	getElem("map").getElementsByTagName("img")[0].onmouseout = cancelDrag;
//Add image map....
	tmp = getElem("map_container").getElementsByTagName("img")[0].src;
	if (tmp.slice(tmp.lastIndexOf("/") + 1) == "map_all.jpg") {
		getElem("map_container").getElementsByTagName("img")[0].setAttribute("usemap", "#narf");
		getElem("map_container").getElementsByTagName("img")[0].style.border = "0px";
	}
}
////////////////////////////////////////////////////////////////////////////////////////////////
function dragMap(e) {
	if (bDrag) {
		if (window.event)
			e = window.event;
		if (parseInt(e.clientX) + parseInt(window.pageXOffset) < 
			parseInt(getElem("map").offsetLeft)  || 
			parseInt(e.clientY) + parseInt(window.pageYOffset) < 
			parseInt(getElem("map").offsetTop) ) {
			bDrag = false;
			return false;
		}
		iX = (iDragX - parseInt(e.clientX));
		iY =  (iDragY - parseInt(e.clientY));
		dragMapBy(iX, iY);
		iDragX = parseInt(e.clientX);
		iDragY = parseInt(e.clientY);
	}
	if (e.type == "mouseup" || e.type == "mouseout")
		bDrag = false;
	return false;
}
function startDrag(e) { 
	bDrag = true; 
	if (window.event)
		e = window.event;
	iDragX = parseInt(e.clientX);
	iDragY = parseInt(e.clientY);
	return false;
}
function cancelDrag(e) { 
	bDrag = false; 
	return false;
}
function cancelDrag1(e) { 
	bDrag = false; 
	return false;
}

////////////////////////////////////////////////////////////////////////////////////////////////

//Used to move the map around in it's window
function moveMap(sDir, moveRate) {
	if (moveRate == null || isNaN(moveRate))
		moveRate = 50;
	oDiv      = getElem("map_container");
	oWindow   = getElem("map");
	iWinLeft  = parseInt(oWindow.offsetLeft);
	iWinTop  = parseInt(oWindow.offsetTop);
	iWinHeight = parseInt(oWindow.offsetHeight);
	iWinWidth = parseInt(oWindow.offsetWidth);

	iMapLeft  = parseInt(oDiv.offsetLeft);
	iMapRight = parseInt(oDiv.offsetLeft) + parseInt(oDiv.offsetWidth);
	iMapTop  = parseInt(oDiv.offsetTop);
	iMapBottom = parseInt(oDiv.offsetTop) + parseInt(oDiv.offsetHeight);
	switch (sDir) {
		case "left":
			if ((iMapLeft + iWinLeft) < iWinLeft) {
				if (Math.abs(iMapLeft) < moveRate)
					oDiv.style.left = "0px";
				else 
					oDiv.style.left = parseInt(oDiv.offsetLeft) + moveRate;
		}
		break;
		case "right":
			if (iMapRight > iWinWidth) {
				if (Math.abs(iMapRight - iWinWidth) < moveRate)
					oDiv.style.left = iMapLeft - Math.abs(iMapRight - iWinWidth) + "px";
				else 
					oDiv.style.left = parseInt(oDiv.offsetLeft) - moveRate;
		}
		break;
		case "up":
			if ((iMapTop + iWinTop) < iWinTop) {
				if (Math.abs(iMapTop) < moveRate)
					oDiv.style.top = "0px";
				else
					oDiv.style.top = (iMapTop + moveRate) + "px";
		}
		break;
		case "down":
//			alert("Top:" + iMapTop + "\nBottom:" + iMapBottom + "\nWinHeight:" + iWinHeight  );
			if (iMapBottom > iWinHeight) {
				if (Math.abs(iMapBottom - iWinHeight) < moveRate)
					oDiv.style.top = iMapTop - Math.abs(iMapBottom - iWinHeight) + "px";
				else
					oDiv.style.top = (iMapTop - moveRate) + "px";
			}
		break;
	}
}
function zoomMap(sDir) {
	oDiv = getElem("map_container");
	oWindow = getElem("map");
	oImage = getElem("map_image");
	if (parseInt(oWindow.getElementsByTagName("img")[0].offsetHeight) <= 230)
		return; 

	iWinWidth = parseInt(oWindow.offsetWidth);
	iWinHeight = parseInt(oWindow.offsetHeight);
	iMapLeft  = parseInt(oDiv.offsetLeft);
	iMapTop  = parseInt(oDiv.offsetTop);
	
	if (!isMapZoomed && sDir == "in") {
//Set the container and image sizes
		oDiv.style.width =  parseInt(parseInt(oImage.offsetWidth)*2) + "px";
		oDiv.style.height =  parseInt(parseInt(oImage.offsetHeight)*2) + "px";
		oImage.style.width = parseInt(parseInt(oImage.offsetWidth)*2) + "px";
		oImage.style.height = parseInt(parseInt(oImage.offsetHeight)*2) + "px";
//reposition container
		oDiv.style.left = parseInt(oDiv.offsetLeft)*2 - parseInt(iWinWidth/2); 
		oDiv.style.top  = parseInt(oDiv.offsetTop)*2  - parseInt(iWinHeight/2);
		isMapZoomed = true;
//Reposition Subelements (div hotspots)
		arr = oDiv.getElementsByTagName("div");
		for (i=0; i < arr.length; i++) {
			arr[i].style.left = parseInt(parseInt(arr[i].offsetLeft)*2 + 12) + "px";
			arr[i].style.top = parseInt(parseInt(arr[i].offsetTop)*2 + 12) + "px";
		}
	}
//ZOOM  OUT!!!!!
	else if (isMapZoomed && sDir == "out") {
//Reposition Container
		oDiv.style.left = parseInt(parseInt(iMapLeft/2) + parseInt(iWinWidth/2)/2)  + "px";
		oDiv.style.top  = parseInt(parseInt(iMapTop/2)  + parseInt(iWinHeight/2)/2)  +"px";
//Prevent Top and Left underflow
		if (parseInt(oDiv.style.left) > 0)
			oDiv.style.left= "0px";
		if (parseInt(oDiv.style.top) > 0)
			oDiv.style.top = "0px";
//Set the container and image sizes
		oDiv.style.width  = parseInt(parseInt(oImage.offsetWidth)/2) + "px";
		oDiv.style.height  = parseInt(parseInt(oImage.offsetHeight)/2) + "px";
		oImage.style.width = parseInt(parseInt(oImage.offsetWidth)/2) + "px";
		oImage.style.height = parseInt(parseInt(oImage.offsetHeight)/2) + "px";
//prevent right and bottom underflow
		if ( (parseInt(oDiv.offsetLeft) + parseInt(oDiv.offsetWidth)) < iWinWidth) 
			oDiv.style.left = iWinWidth - parseInt(oDiv.offsetWidth);
		if ( (parseInt(oDiv.offsetTop) + parseInt(oDiv.offsetHeight)) < iWinHeight) 
			oDiv.style.top = iWinHeight - parseInt(oDiv.offsetHeight);
		isMapZoomed = false;
//Reposition Subelements
		arr = oDiv.getElementsByTagName("div");
		for (i=0; i < arr.length; i++) {
			arr[i].style.left = parseInt(parseInt(arr[i].offsetLeft)/2 -6) + "px";
			arr[i].style.top = parseInt(parseInt(arr[i].offsetTop)/2 -6) + "px";
		}
	}
}
//////////////////////////////////////////////////////////////////////////////////////////////
//Center map at x,y
function moveMapTo(x, y) {
	oDiv = getElem("map_container");
	oWindow = getElem("map");
	oImage = getElem("map_image");
	iWinWidth = parseInt(oWindow.offsetWidth);
	iWinHeight = parseInt(oWindow.offsetHeight);
	iMapLeft  = parseInt(oDiv.offsetLeft);
	iMapTop  = parseInt(oDiv.offsetTop);
	oDiv.style.left = (parseInt(iWinWidth/2) - x) + "px";
	oDiv.style.top  = (parseInt(iWinHeight/2) - y) + "px";
//prevent image bounding errors
	if (parseInt(oDiv.style.left) > 0) 
		oDiv.style.left = "0px";
	if (parseInt(oDiv.style.top) > 0) 
		oDiv.style.top = "0px";
	if ( (parseInt(oDiv.offsetWidth) + parseInt(oDiv.style.left)) < iWinWidth)
		oDiv.style.left = (-(parseInt(oDiv.offsetWidth) - iWinWidth)) + "px";
	if ( (parseInt(oDiv.offsetHeight) + parseInt(oDiv.style.top)) < iWinHeight)
		oDiv.style.top = iWinHeight - parseInt(oDiv.offsetHeight) + "px";
}
function dragMapBy(x, y) {
	oDiv = getElem("map_container");
	oWindow = getElem("map");
	oImage = getElem("map_image");
	iWinWidth = parseInt(oWindow.offsetWidth);
	iWinHeight = parseInt(oWindow.offsetHeight);
	iMapLeft  = parseInt(oDiv.offsetLeft);
	iMapTop  = parseInt(oDiv.offsetTop);
	oDiv.style.left = (iMapLeft - x) + "px";
	oDiv.style.top  = (iMapTop - y) + "px";
//prevent image bounding errors
	if (parseInt(oDiv.style.left) > 0) 
		oDiv.style.left = "0px";
	if (parseInt(oDiv.style.top) > 0) 
		oDiv.style.top = "0px";
	if ( (parseInt(oDiv.offsetWidth) + parseInt(oDiv.offsetLeft)) < iWinWidth) 
		oDiv.style.left = (-(parseInt(oDiv.offsetWidth) - iWinWidth)) + "px";
	if ( (parseInt(oDiv.offsetHeight) + parseInt(oDiv.offsetTop)) < iWinHeight)
		oDiv.style.top = iWinHeight - parseInt(oDiv.offsetHeight) + "px";
}
