var iWidth = 0; 	// image width
var iHeight = 0;	// image height
var hspc = 0; 		// horizontal image offset
var vspc = 0; 		// vertical image offset
var ovBoxSize = 1; 	// Zoombox line width
var mapName = "map"; // Map Name in the HTML Page
var zooming = false;

// Global vars to save mouse position
var mouseX=0;
var mouseY=0;
var x1=0;
var y1=0;
var x2=0;
var y2=0;
var zleft=0;
var zright=0;
var ztop=0;
var zbottom=0;

//map specific
var imgExtraTop = 19;
var imgExtraLeft = 64;
var imgExtraRight = 85;
var imgExtraBottom = 49;
var mapStartLongit = 12.5;
var mapEndLongit = 15.5;
var mapStartLatit = -37.5 ;
var mapEndLatit = -34.5 ;

// Global vars for browser type and version
var isNav = (navigator.appName.indexOf("Netscape")>=0);
var isNav4 = false;
var isIE4 = false;
var is5up = false;


function getStartLongitude()
{
  var iWidthDegrees = mapEndLongit - mapStartLongit;
  return mapStartLongit + (((zleft - hspc - imgExtraLeft) * iWidthDegrees) / (iWidth - imgExtraLeft - imgExtraRight));
}
function getEndLongitude()
{

  var iWidthDegrees = mapEndLongit - mapStartLongit;
  return mapStartLongit + (((zright - hspc - imgExtraLeft) * iWidthDegrees) / (iWidth - imgExtraLeft - imgExtraRight));
}
function getStartLatitude()
{
  var iHeightDegrees = mapEndLatit - mapStartLatit;
  return mapStartLatit + (((ztop - vspc - imgExtraTop) * iHeightDegrees) / (iHeight - imgExtraTop - imgExtraBottom));
}
function getEndLatitude()
{
  var iHeightDegrees = mapEndLatit - mapStartLatit;
  return mapStartLatit + (((zbottom - vspc - imgExtraTop)* iHeightDegrees) / (iHeight - imgExtraTop - imgExtraBottom));
}

// change one image whith another
function changeImages(objImg, newImg) {
	document[objImg].src = eval(newImg + ".src");
}

// Restore navigation image to his normal state
function Restore_Default(img){
	changeImages(img, "navig");
}


// get X offset of the map in the page (IE)
function getOffsetX(imgElem) {
	xPos = document[imgElem].offsetLeft;
	tempEl = document[imgElem].offsetParent;
  	while (tempEl != null) {
  		xPos += tempEl.offsetLeft;
  		tempEl = tempEl.offsetParent;
  	}
	return xPos;
}

// get Y offset of the map in the page (IE)
function getOffsetY(imgElem) {
	yPos = document[imgElem].offsetTop;
	tempEl = document[imgElem].offsetParent;
	while (tempEl != null) {
  		yPos += tempEl.offsetTop;
  		tempEl = tempEl.offsetParent;
  	}
	return yPos;
}

//Initialize zoombox handling
function startUp() {

	// Get Browser version
	if (isNav) {
		if (parseFloat(navigator.appVersion)<5) {
			isNav4=true;
		} else {
			is5up = true;
		}
	} else {
		isIE4=true;
		if (navigator.appVersion.indexOf("MSIE 5")>0 || navigator.appVersion.indexOf("MSIE 6")>0) {
			isIE4 = false;
			is5up = true;
		}
	}
}
function startListeners()
{
  document.onmousemove = getMouse;
	document.onmousedown = mapTool;
  // Initialize Event Listener
	if (isNav) {
		document.captureEvents(Event.MOUSEDOWN | Event.MOUSEMOVE);
	}
}
// get the layer object called "name"
function getLayer(name) {
	if (isNav4) {
		return(document.layers[name]);
	} else if (isIE4) {
		layer = eval('document.all.' + name + '.style');
		return(layer);
	} else if (is5up){
		var theObj = document.getElementById(name);
		return theObj.style;
	} else {
		return(null);
	}
}

// check if layer is visible
function isVisible(name) {
	var layer = getLayer(name);
	if (isNav && layer.visibility == "show")
		return(true);
	if (isIE && layer.visibility == "visible")
 		return(true);
	return(false);
}


// move layer to x,y
function moveLayer(name, x, y) {
	var layer = getLayer(name);
	if (isNav4) {
		layer.moveTo(x, y);
	}else {
		layer.left = x + "px";
 		layer.top  = y + "px";
	}
}

// toggle layer to invisible
function hideLayer(name) {
	var layer = getLayer(name);
	if (isNav4)
		layer.visibility = "hide";
	else
		layer.visibility = "hidden";
}

// toggle layer to visible
function showLayer(name) {
	var layer = getLayer(name);
	if (isNav4)
		layer.visibility = "show";
	else
		layer.visibility = "visible";
}

// clip layer display to clipleft, cliptip, clipright, clipbottom
function clipLayer(name, clipleft, cliptop, clipright, clipbottom) {
	var layer = getLayer(name);
	if (isNav4) {
		layer.clip.left   = clipleft;
		layer.clip.top    = cliptop;
		layer.clip.right  = clipright;
		layer.clip.bottom = clipbottom;
	} else {

		var newWidth = clipright - clipleft;
		var newHeight = clipbottom - cliptop;

		layer.height = newHeight;
		layer.width	= newWidth;
    layer.top	= cliptop + "px";
		layer.left	= clipleft + "px";
	}
}

// shown the zoombox
function boxIt(theLeft,theTop,theRight,theBottom) {

	clipLayer("zoomBoxTop",theLeft,theTop,theRight,theTop+ovBoxSize);
	clipLayer("zoomBoxLeft",theLeft,theTop,theLeft+ovBoxSize,theBottom);
	clipLayer("zoomBoxRight",theRight-ovBoxSize,theTop,theRight,theBottom);
	clipLayer("zoomBoxBottom",theLeft,theBottom-ovBoxSize,theRight,theBottom);

  //var midVert = Math.round( (theLeft+theRight ) / 2);
//	clipLayer("zoomBoxCutVert", midVert ,theTop,midVert+ovBoxSize,theBottom);

  //var midHoriz = Math.round( (theTop+theBottom ) / 2);
//	clipLayer("zoomBoxCutHoriz",theLeft,midHoriz,theRight,midHoriz+ovBoxSize);

 	showLayer("zoomBoxTop");
	showLayer("zoomBoxLeft");
	showLayer("zoomBoxRight");
	showLayer("zoomBoxBottom");
 	//showLayer("zoomBoxCutVert");
  //showLayer("zoomBoxCutHoriz");
}
function boxItDegrees(leftLong,topLat,rightLong,bottomLat)
{
  calculateImageSpacings();

  var mapWidthDegrees = mapEndLongit - mapStartLongit;
  var mapWidth = (iWidth - imgExtraLeft - imgExtraRight);

  var mapHeightDegrees = mapEndLatit - mapStartLatit;
  var mapHeight = (iHeight - imgExtraTop - imgExtraBottom);


  zleft = Math.round(((leftLong - mapStartLongit) * mapWidth / mapWidthDegrees) + hspc + imgExtraLeft);


  ztop = Math.round(((topLat - mapStartLatit) * mapHeight / mapHeightDegrees) + vspc + imgExtraTop);


  zright = Math.round(((rightLong - mapStartLongit) * mapWidth / mapWidthDegrees) + hspc + imgExtraLeft);
  zbottom = Math.round(((bottomLat - mapStartLatit) * mapHeight / mapHeightDegrees) + vspc + imgExtraTop);

  boxIt(zleft,ztop,zright,zbottom);
}
// get cursor location
function getImageXY(e) {
  calculateImageSpacings();
	if (isNav) {
		mouseX=e.pageX;
		mouseY=e.pageY;
	} else {
		mouseX=event.clientX + document.body.scrollLeft;
		mouseY=event.clientY + document.body.scrollTop;
	}
	// subtract offsets from page left and top
	mouseX = mouseX-hspc;
	mouseY = mouseY-vspc;

	//return true;
}

function calculateImageSpacings()
{
	iWidth = document[mapName].width;
	iHeight = document[mapName].height;

	if (isNav4) {
		hspc = document[mapName].x;
		vspc = document[mapName].y;
	} else {
		hspc = getOffsetX(mapName);
		vspc = getOffsetY(mapName);
	}
}
// start zoom in.... box displayed
function startZoomBox(e) {
	getImageXY(e);
	// keep it within the MapImage
	if ((mouseX<iWidth) && (mouseY<iHeight)) {
		if (zooming) {
			stopZoomBox(e);
		} else {
			x1=mouseX+hspc;
			y1=mouseY+vspc;
			x2=x1+1;
			y2=y1+1;
      setClip();
			zooming=true;
		}
	}
	return false;
}

// stop zoom box display...
function stopZoomBox(e) {
	zooming=false;
	//return true;
	return false;
}

// get the coords at mouse position
function getMouse(e) {
	getImageXY(e);

	if ((mouseX>iWidth) || (mouseY>iHeight) || (mouseX<=0) ||(mouseY<=0)) {
		if (zooming) stopZoomBox(e);
		return true;
	} else if (zooming) {
		x2=mouseX+hspc;
		y2=mouseY+vspc;
    fillTextboxes();
    setClip();
		return false;
	}
}

// clip zoom box layer to mouse coords
function setClip() {
	var tempX=x1;
	var tempY=y1;
	if (x1>x2) {
		zright=x1;
		zleft=x2;
	} else {
		zleft=x1;
		zright=x2;
	}
	if (y1>y2) {
		zbottom=y1;
		ztop=y2;
	} else {
		ztop=y1;
		zbottom=y2;
	}

  if (ztop - vspc < imgExtraTop)
  {
    ztop = vspc + imgExtraTop;
  }
  if (zleft - hspc < imgExtraLeft)
  {
    zleft = hspc + imgExtraLeft;
  }
  if (zbottom - vspc > iHeight - imgExtraBottom)
  {
    zbottom = vspc - imgExtraBottom + iHeight;
  }
  if (zright - hspc > iWidth - imgExtraRight)
  {
    zright = hspc - imgExtraRight + iWidth ;
  }

  if (zbottom - vspc < imgExtraTop)
  {
    zbottom = vspc + imgExtraTop;
  }
  if (zright - hspc < imgExtraLeft)
  {
    zright = hspc + imgExtraLeft;
  }
  if (ztop - vspc > iHeight - imgExtraBottom)
  {
    ztop = vspc - imgExtraBottom + iHeight;
  }
  if (zleft - hspc > iWidth - imgExtraRight)
  {
    zleft = hspc - imgExtraRight + iWidth ;
  }

  if ((x1 != x2) && (y1 != y2)) {
		boxIt(zleft,ztop,zright,zbottom);
	}
}

// perform appropriate action with mapTool
function mapTool (e) {
    var browser = navigator.appName.substring ( 0, 9 );
    var event_number = 0;
 	var obj = null;
 	
 	//ne prend en compte que les evenement 'clics' gauche
    if (browser=="Microsoft")
        event_number = event.button;
    else if (browser=="Netscape")
        event_number = e.which;

    if ( event_number != 1)
    {
    	return (true);
    }
 	
	if (isNav) obj = e.target;
	else obj = event.srcElement;

	if (isNav4) {
		document.captureEvents(Event.MOUSEUP);
	} 

	if (obj == document[mapName]) 
	{
		startZoomBox(e);
		document.onmouseup = doZoomPlus;
		return false;
	}
	else {
		return true;
	}
	//return false;
}


function doZoomPlus(e) {
	var obj = null;
	if (isNav) obj = e.target;
	else obj = event.srcElement;

	if (obj == document[mapName]) {
		stopZoomBox(e);
		//do here
	}
}
