var windowObject;
var spanWindowObject;
var X_InitC = 0, Y_InitC = 0;
var X_FinC = 0, Y_FinC = 0;
var X1 = 0, Y1 = 0;
var mbPanActive = false;
var mbPanInit = false;
var mbPanSubmit = false;
var mbZoomRectActive = false;
var mbZoomRectInit = false;
var mbZoomRectSubmit = false;
var mbBtnLeft = false;
var mbBtnRight = false;

function currentOp()
{
	var o = getElement("__currentOp");
	return  (isNull(o)) ? ""  :o.value;
}

function IsValidTarget (target)
{
	var bValid = true;
	switch (currentOp())
	{
		case "zoomrect":
			bValid = ((target.id == "tdnotouch") || (target.id == "ventana") || (target.id == "tdventana") || (target.tagName == "AREA") || (target.id == "imgmap"));
			break;
		case "pan":
			bValid = true; //((target.tagName == "AREA") || (target.id = "theMapSpace"));
			break;
	}
	return bValid;
}



function getPanInit() { return (getPanActive() && mbPanInit) || mbPanSubmit; }
function getPanActive() { return ((currentOp() == "pan") && mbPanActive && !mbPanSubmit); }
function getZoomRectInit() { return ((getZoomRectActive() && mbZoomRectInit) || (mbZoomRectSubmit));}
function getZoomRectActive() { return (((currentOp() == "zoomrect") && mbZoomRectActive && !mbZoomRectSubmit)); }
function getMap() { return getElement ("imgmap"); }

function doMapClick (evt, indexArea, tagArea)
{
	if (getZoomRectInit() || getPanInit()) return;
	if (!evt) var evt = event;
	
	var oMap = getMap();
	var x = getOffsetX (evt, oMap);
	var y = getOffsetY (evt, oMap);

	var theform = getForm();
	theform.__mapX.value = x; 
	theform.__mapY.value = y; 
	
	if ((tagArea == "map") && (currentOp() == "ruta"))
	{
		doMarkPoint(x,y);
		if (theform.__ruta_Point1.value == "")
		{
			theform.__ruta_Point1.value = x + "|" + y + "|" + theform.__currentExtent.value;
			return;
		}
	}
	
	theform.__mapAreaIndex.value = -1;
	theform.__mapAreaTag.value =tagArea;
	theform.submit();
	doWaiter();		
}

function doMarkPoint (x,y)
{
	var oDivPoint = getElement("divPoint");
	var oImgMap = getMap();
	var oImgPoint = getElement ("imgPoint");
	oDivPoint.style.left= findPosX(oImgMap) + x - (oImgPoint.clientWidth / 2);
	oDivPoint.style.top = findPosY(oImgMap) + y - (oImgPoint.clientHeight / 2);
	oDivPoint.style.width = oImgPoint.clientWidth +"px";
	oDivPoint.style.height = oImgPoint.clientHeight  +"px";
	oDivPoint.style.display = "block";
	oImgPoint.style.display = "block";
	oImgPoint.style.left = "0px";
	oImgPoint.style.top = "0px";
}

function doMapMouseMove(evt)
{
	if (!evt) var evt = event;
	var oTarget = getTarget (evt);

	showDebug (" Target : " + oTarget.id + " : " + oTarget.tagName + "      Coord : " + getAbsX(evt) + "," + getAbsY(evt) +  "   BtnLeft : " + mbBtnLeft + " " + evt.which + " " + evt.button);
	
	if ((X_InitC==getAbsX(evt)) && (Y_InitC==getAbsY(evt))) return true;
	if ((!IsValidTarget(oTarget)) || (!mbBtnLeft) )
	{
		switch (currentOp())
		{
			case "pan":
				if (getPanInit()) doMapMouseUp(evt);
				break;
			case "zoomrect":
				if (getZoomRectInit())  doMapMouseUp(evt);
				break;
			default:
				break;
		}
		return true;
	}
	
	
	switch (currentOp())
	{
		case "pan":
			if (getPanActive()) 
			{
				if (!mbPanInit) mbPanInit = true;
				var oMap = getMap();
				X_FinC = getAbsX(evt) - X_InitC;
				Y_FinC = getAbsY(evt) - Y_InitC;
				setLeft(oMap,X_FinC);
				setTop (oMap,Y_FinC);
			}
			break;
		case "zoomrect":
			if (getZoomRectActive())
			{
				if (!mbZoomRectInit) mbZoomRectInit = true;
				var w = Math.abs(getAbsX(evt) - X_InitC);
				var h = Math.abs(getAbsY(evt) - Y_InitC);
				setWidth (windowObject, w);
				setHeight (windowObject, h);
				setWidth (spanWindowObject, w);
				setHeight (spanWindowObject, h);
			}
			break;
	}
	return false;
}

function doMapMouseUp(evt) 
{
	mbBtnLeft = false;
	mbBtnRight = false;
	if (!evt) var evt = event;
	switch (currentOp())
	{
		case "pan":
			SubmitPan(evt);
			break;
		case "zoomrect":
			SubmitZoomRect(evt);
			break;
	}
}

function SubmitZoomRect(evt) 
{
	if (!evt) var evt = event;	
	var oTarget = getTarget (evt);
	
	//showDebug ("SubmitZoomRect 0 : " + mbZoomRectSubmit + " " + oTarget.id + " " + oTarget.tagName);
	if ((mbZoomRectSubmit) || (!getZoomRectInit()) && (!IsValidTarget(oTarget))) return true;
	if ((getAbsX(evt) == X_InitC) && (getAbsY(evt) == Y_InitC)) return true;

	
	//if (isOnlyIE) zoomrectObject.releaseCapture() ;
	
	var sTag = "zoomrect";
	var theform = getForm();

	var oMap =getMap();
	var X2 = getOffsetX (evt, oMap);
	var Y2 = getOffsetY (evt, oMap);
	
	theform.__mapX2.value = X2;
	theform.__mapX.value = X1;
	theform.__mapY2.value = Y2;
	theform.__mapY.value = Y1;
	
	theform.__mapAreaIndex.value = -1;
	theform.__mapAreaTag.value = sTag;
	
	X_FinC = 0;
	Y_FinC = 0;
	mbZoomRectSubmit = true;
	theform.submit();
	doWaiter();		
}


function SubmitPan(evt) 
{
	if (!evt) var evt = event;

	var oTarget = getTarget (evt);
	if ((mbPanSubmit) || (!getPanInit())) return true;
		
	if ((getAbsX(evt)== X_InitC) && (getAbsY(evt) == Y_InitC)) return true;

	var sTag = "panIE";
	
	var x, y; 
	
	if ((X_FinC == 0) && (Y_FinC == 0))	
	{
		var oMap = getMap();
		x = getOffsetX (evt, oMap);
		y = getOffsetY (evt, oMap);
		sTag = "map";
	} 
	else
	{
		x = getAbsX(evt) - X_InitC;
		y = getAbsY(evt) - Y_InitC;	
	}
	
	var theform = getForm();
	theform.__mapX.value = x;
	theform.__mapY.value = y;
	theform.__mapAreaIndex.value = -1;
	theform.__mapAreaTag.value = sTag;
	
	X_FinC = 0;
	Y_FinC = 0;
	mbPanSubmit = true;
	theform.submit();
	doWaiter();		
		
}


function doMapMouseDown(evt) 
{
	if (!evt) var evt = window.event;
	var oTarget = getTarget (evt);

	mbBtnLeft = getBtnLeft(evt);
	if (!IsValidTarget (oTarget)) 
	{
		mbPanActive = false;
		mbPanInit = false;
		mbZoomRectActive = false;
		mbZoomRectInit = false;		
		return true;
	}
	
	var oMap = getMap();
	switch (currentOp())
	{
		case "pan":
			mbPanActive = true;
			mbPanInit = false;
			mbPanSubmit = false;
			X_InitC=getAbsX(evt); 
			Y_InitC=getAbsY(evt);		
			break;
		case "zoomrect":
			mbZoomRectActive = true
			mbZoomRectInit = false;
			mbZoomRectSubmit = false;

			X_InitC=getAbsX(evt);
			Y_InitC=getAbsY(evt);
			X1 = getOffsetX (evt, oMap);
			Y1 = getOffsetY (evt, oMap);	
			
			windowObject = getElement("ventana");
			spanWindowObject = getElement("spanVentana");
			
			setWidth (windowObject, 0);
			setHeight (windowObject, 0);
			
			setLeft(spanWindowObject, X_InitC);
			setTop(spanWindowObject, Y_InitC);
			break;
	}
	return true;
}


function getCursorOp()
{
	var cursortype;
	switch (currentOp())
	{
		case "pan": 
			cursortype = "move";
			break;
		default:
			cursortype = "crosshair";
			break;
	}
}

function doCursorOp ()
{
	var imgmap = getElement('imgmap');
	if (imgmap != null) 
	{ 
		var cursortype;
		switch (currentOp())
		{
			case "pan": 
				cursortype = "move";
				break;
			default:
				cursortype = "crosshair";
				break;
		}
		imgmap.style.cursor = cursortype;
	} 
}

function doCursorPointer ()
{
	var imgmap = getElement('imgmap');
	if (imgmap != null) 
	{ 
		imgmap.style.cursor = (isIE) ? 'hand' : 'pointer';
	} 
}

function onCurrentOp( opValue )
{
	var theform = getForm();
	var oldopValue = currentOp();
	mbPanActive = false;
	mbPanInit = false;
	mbZoomRectActive = false;
	mbZoomRectInit = false;
	theform.__currentOp.value = opValue;

	theform.__mapX.value = "-1"; 
	theform.__mapY.value = "-1"; 
	theform.__mapX2.value = "-1";
	theform.__mapY2.value = "-1";
	theform.__mapAreaIndex.value = "-1";
	theform.__mapAreaTag.value = "map";		
	if ((isIE) || (isNS6))
	{
		if (oldopValue == opValue)
		{
			switch (opValue)
			{
				case "pan":
				case "ruta":
				case "zoomrect":
					break;
				default:
					theform.submit();
					doWaiter();		
					break;
			}
		}
		else
		{
			changeImage (oldopValue, false);
			changeImage (opValue, true);
		}
	}
	else 
	{
		theform.submit();
		doWaiter();		
	}
}


function changeImage (imgId, active)
{
	if (document.images)
	{
		var imgmap = document.images[imgId];
		if (imgmap != null)
		{
			var theform = getForm();
			var onoff = (active) ? "on" : "off";
			var urlimg  = theform.__mapImagesPath.value;
			urlimg  += imgId + "_" + onoff + ".gif";
			imgmap.src = urlimg ;
		} 
	} 
}
function guideInit()
{
	document.onmousemove=doMapMouseMove;	
	document.onmouseup=doMapMouseUp;
	document.onmousedown=doMapMouseDown;
	
	if (document.captureEvents) 
	{
		document.captureEvents(Event.MOUSEMOVE || Event.MOUSEDOWN || Event.MOUSEUP);
		document.captureEvents(Event.MOUSEMOVE || Event.MOUSEDOWN || Event.MOUSEUP);
		showDebug ("Capture Events");
	}
	else
	{
		showDebug ("NOT Capture Events");
	}
	
	if (isOpera)
	{
		var panNoTouch = getElement("ventana");
		panNoTouch.style.backgroundColor= "Transparent";
	}
	
}		



