//////////////////////////////////////////////////////////
// init
//////////////////////////////////////////////////////////

var isNetscape = false;
if(navigator.appName == "Netscape" ){
    isNetscape=true;
}

var mapWidth = 450;
var mapHeight = 350;
var borderSize = 2;

var mode = document.getElementById("mainForm:map_mouse_mode").value;
var paramX1 = document.getElementById("mainForm:map_mouse_x1");
var paramY1 = document.getElementById("mainForm:map_mouse_y1");
var paramX2 = document.getElementById("mainForm:map_mouse_x2");
var paramY2 = document.getElementById("mainForm:map_mouse_y2");

paramX1.value = -1;
paramY1.value = -1;
paramX2.value = -1;
paramY2.value = -1;

var zoomBox = document.getElementById("zoombox");
var panElem = document.getElementById("theMap");

// Mausereignisse über dem Kartenbild abfangen
var mapElem;
if (isNetscape) 
  mapElem = document.getElementById("theMap");
else 
  mapElem = document.getElementById("theMap");
mapElem.onmousedown = chkMouseDown;
document.onmouseup = chkMouseUp;
document.onmousemove = mouseMove;

zoomBox.style.fontSize = 0;  // for IE

var mouseX = 0;
var mouseY = 0;
var x1 = 0;
var x2 = 0;
var y1 = 0;
var y2 = 0;
var screenX1 = 0;
var screenY1 = 0;

var zooming = false;
var panning = false;

hideElem(zoomBox);


//////////////////////////////////////////////////////////
// functions
//////////////////////////////////////////////////////////

function mouseMove(e) {

  var x = getAbsX(e);
  var y = getAbsY(e);

  x2 = x-screenX1+x1;
  y2 = y-screenY1+y1;
    
//  window.status = "x: " + x2 + ", y: " + y2;

  if (zooming)
    updateZoomBox();
  else if (panning)
    updatePanning();
}


function updateZoomBox() {	
  if (x2 < 0)
    x2 = 0;
  else if (x2 > mapWidth-borderSize*2)
    x2 = mapWidth-borderSize*2;

  if (y2 < 0)
    y2 = 0;
  else if (y2 > mapHeight-borderSize*2)
    y2 = mapHeight-borderSize*2;

    var zright = x2;
    var zleft = x1;
    var zbottom = y2;
    var ztop = y1;

    if (x1 > x2) {
        zright = x1;
        zleft = x2;
    }
    if (y1 > y2) {
        zbottom = y1;
        ztop = y2;
    }

    moveAndResizeElem(zoomBox,zleft,ztop,zright,zbottom);
}

function updatePanning() {
  moveElem(panElem, x2-x1, y2-y1); 
  clipElem(panElem, x1-x2, y1-y2, mapWidth-(x2-x1), mapHeight-(y2-y1));
}

function setMouseXY(e) {
  mouseX = getRelX(e);
  mouseY = getRelY(e);
}

function chkMouseUp(e) {
    if (zooming) {
        stopZoomBox(e);
//        clear_mainForm();
        paramX1.value = x1;
        paramY1.value = y1;
        paramX2.value = x2;
        paramY2.value = y2;        
        
        // calculate new extent in realworld-coordinates
        if(document.forms[0].minx && document.forms[0].left){	        	
				var xfactor = (parseFloat(document.forms[0].right.value) - parseFloat(document.forms[0].left.value)) / mapWidth;
				var yfactor = (parseFloat(document.forms[0].top.value) - parseFloat(document.forms[0].bottom.value)) / mapHeight;	
				
	            document.forms[0].minx.value = parseFloat(document.forms[0].left.value) + (x1 * xfactor);
	            document.forms[0].miny.value = parseFloat(document.forms[0].top.value) - (y1 * yfactor);
	            document.forms[0].maxx.value = parseFloat(document.forms[0].left.value) + (x2 * xfactor);
	            document.forms[0].maxy.value = parseFloat(document.forms[0].top.value) - (y2 * yfactor);	                    	
        }
        
        showWaitMessage();
        document.forms[0].submit();  // we must submit here because the mouse event for the mapElem is captured by the zoomBox
    }
    else if (panning) {
      panning = false;
//      clear_mainForm();      
      paramX1.value = x1;
      paramY1.value = y1;
      paramX2.value = x2;
      paramY2.value = y2;
      
      //if (x2 < 0  ||  x2 >= mapWidth  ||  y2 < 0  ||  y2 >= mapHeight) {                  	
      // calculate new extent in realworld-coordinates
        if(document.forms[0].minx && document.forms[0].left){	        						
        	var xdist = (paramX2.value - paramX1.value) * -1;
			var ydist = (paramY2.value - paramY1.value);					
        	       	
			var xfactor = (parseFloat(document.forms[0].right.value) - parseFloat(document.forms[0].left.value)) / mapWidth;
			var yfactor = (parseFloat(document.forms[0].top.value) - parseFloat(document.forms[0].bottom.value)) / mapHeight;	
		
	        document.forms[0].minx.value = parseFloat(document.forms[0].left.value) + (xdist * xfactor);
	        document.forms[0].miny.value = parseFloat(document.forms[0].bottom.value) + (ydist * yfactor);
	        document.forms[0].maxx.value = parseFloat(document.forms[0].right.value) + (xdist * xfactor);
	        document.forms[0].maxy.value = parseFloat(document.forms[0].top.value) + (ydist * yfactor);	    

        }
        showWaitMessage();
        document.forms[0].submit(); // ... redundant; submit is bound to the mapElem's onclick event
      //}
    }
}

function showWaitMessage(){
	//getLayer("loader").visibility="visible";
}

function chkMouseDown(e) {
    // welcher mausbutton wurde gedrückt?
    var theButton = 0;    
    if(isNetscape){
        theButton = e.which;
    }else{
        theButton = window.event.button;
    }    
    
    if (theButton == 1) {
        // linker Mausbutton    
        mode = document.getElementById("mainForm:map_mouse_mode").value;    
        if (mode == "zoom") 
          startZooming(e);
        else if (mode == "pan")
          startPanning(e);
    } 
}

function startPanning(e) {
    setMouseXY(e);  

    screenX1 = getAbsX(e);
    screenY1 = getAbsY(e);
    x1 = mouseX;
    y1 = mouseY;
    x2 = x1 + 1;
    y2 = y1 + 1;
    
    panning = true;
}

// ZoomBox zeichnen
function startZooming(e) {
    setMouseXY(e);	

    screenX1 = getAbsX(e);
    screenY1 = getAbsY(e);
    x1 = mouseX;
    y1 = mouseY;
    x2 = x1 + 1;
    y2 = y1 + 1;
//		window.status = "x1: " + x1 + ",y1: " + y1 + ",x2: " + x2 + ",y2: " + y2;	
    updateZoomBox();        
    showElem(zoomBox);
    zooming = true;
}

function stopZoomBox(e) {
  zooming=false;
  hideElem(zoomBox);	
}

///////////////////////////////////////////////////////////////////////////
//  tool box functions
///////////////////////////////////////////////////////////////////////////

function getEvent(e) {
  if (!isNetscape)
    e = window.event;
    
  return e;
}

function getAbsX(e) {
  if (isNetscape)
    return getEvent(e).screenX;
  else
    return getEvent(e).clientX;
}

function getAbsY(e) {
  if (isNetscape)
    return getEvent(e).screenY;
  else
    return getEvent(e).clientY;
}

function getRelX(e) {
  if (isNetscape)
    return getEvent(e).layerX;
  else
    return getEvent(e).offsetX;
}

function getRelY(e) {
  if (isNetscape)
    return getEvent(e).layerY;
  else
    return getEvent(e).offsetY;
}

function hideElem(elem) {   
    elem.style.visibility = "hidden";
}

function showElem(elem) {   
    elem.style.visibility = "visible";
}

function moveAndResizeElem(elem, left, top, right, bottom) {
  var style = elem.style;
  var newWidth = right - left;
  var newHeight = bottom - top;    
  style.top = top + "px";
  style.left = left + "px";
  style.height = newHeight + "px";
  style.width = newWidth + "px";        
}

function moveElem(elem, left, top) {
  var style = elem.style;
  style.top = top + "px";
  style.left = left + "px";
}

function clipElem(elem, clipleft, cliptop, clipright, clipbottom) {                
    elem.style.clip = "rect(" + cliptop + "px," + clipright + "px," + clipbottom + "px," + clipleft +"px)";
}

function abs(x) {
  if (x < 0)
    return -x;
  else
    return x;
}


