/***********************************************************************************/
// MapServer2000.js
//
// JavaScript Funktionen zur Darstellung dynamischer Layer mit Zoombox Funktionen
// (c) WIGeoGIS GmbH, (c) ESRI Inc.
//
// Allgemeine Variablen und Einstellungen

var errorHappend = false;
var errorActive = false;

var left;
var right;
var top;
var bottom;
var fullLeft;
var fullRight;
var fullTop;
var fullBottom;
var newLeft;
var newBottom;
var newRight;
var newTop;
//var theImage;

// Kartengroesse
var iWidth;
var iHeight;

// Layer offset
var hspc;
var vspc;

// ermittelt den Browser, Version und Betriebssystem
   var isPlatform = navigator.platform;
   if (navigator.userAgent.indexOf("MSIE")!=-1) {
      var isBrowser='IE';
      var isNS = false;
   } else {
      var isBrowser='NS';
      var isNS = true;
   }
   var isVersion = parseInt(navigator.appVersion);

// Allgemeine Einstellungen
function generalSettings(iw,ih,hs,vs) {
   iWidth = iw;
   iHeight = ih;
   hspc = hs;
   vspc = vs;
   
}


/***********************************************************************************/
// DHTML Layer erzeugen
function createLayer(name, left, top, width, height, visible, content) {
          var layer;
          if ((isNS) && (isVersion<5)) {
            document.writeln('<layer name="' + name + '" left=' + left + ' top=' + top + ' width=' + width + ' height=' + height +  ' visibility=' + (visible ? '"show"' : '"hide"') +  '>');
            document.writeln(content);
            document.writeln('</layer>');
            layer = getLayer(name);
            layer.width = width;
            layer.height = height;
          } else {
            document.writeln('<div id="' + name + '" style="position:absolute; overflow:none; left:' + left + 'px; top:' + top + 'px; width:' + width + 'px; height:' + height + 'px;' + ' visibility:' + (visible ? 'visible;' : 'hidden;') +  '">');
            document.writeln(content);
            document.writeln('</div>');
          }
          clipLayer(name, 0, 0, width, height);
}

//eingefügt : von bg 5.4.02 --> diese Funktion funktioniert genau gleich, wie createLayer, aber man kann noch den 
//zindex (in welcher reihenfolge sollen die layer gezeichnet werden) angeben
// DHTML Layer erzeugen
function createLayerWithIndex(name, left, top, width, height, visible, content, theZindex) {
          var layer;
          if ((isNS) && (isVersion<5)) {
            document.writeln('<layer name="' + name + '" left=' + left + ' top=' + top + ' width=' + width + ' height=' + height + 'px; z-index:' + theZindex + '; visibility=' + (visible ? '"show"' : '"hide"') +  '>');            
            document.writeln(content);
            document.writeln('</layer>');
            layer = getLayer(name);
            layer.width = width;
            layer.height = height;
          } else {
            document.writeln('<div id="' + name + '" style="position:absolute; overflow:none; left:' + left + 'px; top:' + top + 'px; width:' + width + 'px; height:' + height + 'px; z-index:' + theZindex + '; visibility:' + (visible ? 'visible;' : 'hidden;') +  '">');
            //alert('<div id="' + name + '" style="position:absolute; overflow:none; left:' + left + 'px; top:' + top + 'px; width:' + width + 'px; height:' + height + 'px; z-index:' + theZindex + ' visibility:' + (visible ? 'visible;' : 'hidden;') +  '">');i
            document.writeln(content);
            document.writeln('</div>');
          }
          clipLayer(name, 0, 0, width, height);
}


/***********************************************************************************/
// DHTML Layer erzeugen
function createFloatLayer(name, left, top, width, height, visible, content) {
          var layer;
          if ((isNS) && (isVersion<5)) {
            document.writeln('<layer name="' + name + '" left=' + left + ' top=' + top + ' width=' + width + ' height=' + height +  ' visibility=' + (visible ? '"show"' : '"hide"') +  '>');
            document.writeln(content);
            document.writeln('</layer>');
            layer = getLayer(name);
            layer.width = width;
            layer.height = height;
          } else {
            document.writeln('<div id="' + name + '" style="position:absolute; overflow:auto; left:' + left + 'px; top:' + top + 'px; width:' + width + 'px; height:' + height + 'px;' + ' visibility:' + (visible ? 'visible;' : 'hidden;') +  '">');
            document.writeln(content);
            document.writeln('</div>');
          }
          clipLayer(name, 0, 0, width, height);
}

// Get Layer mit dem Namen "name"
function getLayer(name) {
          if ((isNS) && (isVersion<5))
            return(document.layers[name]);
          else if (document.all) {
                if ( eval('document.all.' + name) != null) {
                    layer = eval('document.all.' + name + '.style');
                    return(layer);
                } else
                    return(null);
          }
          else
            return(null);
}

// Bewege Layer nach x,y
function moveLayer(name, x, y) {
        var layer = getLayer(name);
        if (layer != null) {
              if ((isNS) && (isVersion<5))
                layer.moveTo(x, y);
                if (document.all) {
                  layer.left = x;
                  layer.top  = y;
                }
        }
}

// Hintergrundfarbe setzen
function setLayerBackgroundColor(name, color) {
       var layer = getLayer(name);
       if (layer != null) {
             if ((isNS) && (isVersion<5))
               layer.bgColor = color;
             else if (document.all)
                layer.backgroundColor = color;
        }
}

// Toggle unsichtbar
function hideLayer(name) {
        var layer = getLayer(name);
        if (layer != null) {
                if ((isNS) && (isVersion<5))
                layer.visibility = "hide";
                if (document.all)
                         layer.visibility = "hidden";
        }
}

// Toggle sichtbar
function showLayer(name) {
        var layer = getLayer(name);
        if (layer != null) {
                if ((isNS) && (isVersion<5))
                layer.visibility = "show";
                if (document.all)
                 layer.visibility = "visible";
        }
}

// Clip Layer nach clipleft, cliptop, clipright, clipbottom
function clipLayer(name, clipleft, cliptop, clipright, clipbottom) {
          var layer = getLayer(name);
          if (layer != null) {
                  if ((isNS) && (isVersion<5)) {
                      layer.clip.left   = clipleft;
                      layer.clip.top    = cliptop;
                      layer.clip.right  = clipright;
                      layer.clip.bottom = clipbottom;
                  } else {
                      layer.clip = 'rect(' + cliptop + ' ' +  clipright + ' ' + clipbottom + ' ' + clipleft +')';
                  }
          }
}

// Layerinhalt ersetzen
function replaceLayerContent(name, content) {
          if ((isNS) && (isVersion<5)) {
                    var layer = getLayer(name);
                        if (layer != null) {
                            layer.document.open();
                            layer.document.writeln(content);
                            layer.document.close();
                        }
          }  else if (document.all) {
                        if (eval("document.all." + name) != null) {
                                content = content.replace(/\'/g,"\\'");
                            var str = "document.all." + name + ".innerHTML = '" + content + "'";
                            eval(str);
                        }
          }
}



/***********************************************************************************/
// Zoombox Einstellungen
// um Mauspositionen zu speichern
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;

var mapX = left;
var mapY = top;

var zooming=false;
var bottomBorderHeight = 0;

function setZoomBoxSettings() {
		//alert('hallo');
        // Set up event capture for mouse movement
        if ((isNS) && (isVersion<5)) {
                document.captureEvents(Event.MOUSEMOVE);
                document.captureEvents(Event.MOUSEDOWN);
                document.captureEvents(Event.MOUSEUP);
        }
        document.onmousemove = getMouse;
	//alert('frank');
        document.onmousedown = mapTool;
        document.onmouseup = chkMouseUp;

}

// Mouseup
function chkMouseUp(e) {
        //alert('chekcMouseUp');
	if (zooming && !errorActive) {
               if (mouseX<0)
                       mouseX = 0;
               if (mouseX>iWidth)
                       mouseX = iWidth;
               if (mouseY<0)
                       mouseY = 0;
               if (mouseY>iHeight)
                       mouseY = iHeight;
               mapTool(e);
        }
}

// perform appropriate action with mapTool
function mapTool (e) {
   //alert('maptool');
   if (!errorActive) {
        getImageXY(e);
        if ((!zooming) && (mouseX>=0) && (mouseX<iWidth) && (mouseY>=0) && (mouseY<iHeight)) {
                startZoomBox(e);
                return false;

        } else if (zooming) {
                getMouse(e);
                stopZoomBox(e);
        }
  }
  return true;
}


// Mouseclicks in x/y Koordinaten transformieren
function getMapXY(xIn,yIn) {
         mouseX = xIn;
         var pixelX = (right-left) / iWidth;
         mapX = pixelX * mouseX + left;
         mouseY = iHeight - yIn;
         var pixelY = (top-bottom) / iHeight;
         mapY = pixelY * mouseY + bottom;
}

function getImageXY(e) {
        if ((isNS) && (isVersion<5)) {
            mouseX=e.pageX;
            mouseY=e.pageY;
        } else {
            mouseX=event.clientX + document.body.scrollLeft;
            mouseY=event.clientY + document.body.scrollTop;
        }
        // Offset von links und oben abziehen
        mouseX = mouseX-hspc;
        mouseY = mouseY-vspc;

}



// Rund um Mausklick zoomen
function zoomin(e) {
       //alert('zoomin');
	getMapXY(mouseX,mouseY);
        var zoomFactor = 1;

        var widthHalf = Math.abs(right - left) / 2;
        var heightHalf = Math.abs(top - bottom) / 2;
        newLeft = mapX - (widthHalf / zoomFactor);
        newRight = mapX + (widthHalf / zoomFactor);
        newTop = mapY + (heightHalf / zoomFactor);
        newBottom = mapY - (heightHalf / zoomFactor);

        refreshMap(true);
}


// Koordinaten an der Mausposition
function getMouse(e) {
     //alert('getMouse');
     window.status="";
     getImageXY(e);
     //if (isNS==false)
            //theImage.style.cursor = "crosshair";

        if (zooming) {
                if (mouseX<0)
                        mouseX = 0;
                if (mouseX>iWidth)
                        mouseX = iWidth;
                if (mouseY<0)
                        mouseY = 0;
                if (mouseY>iHeight-bottomBorderHeight)
                        mouseY = iHeight-bottomBorderHeight;
                x2=mouseX;
                y2=mouseY;
                setClip();
                return false;
        } else
                return true;
       return true;
}

// Start ZoomIn
function startZoomBox(e) {
        moveLayer("theMap",hspc,vspc);
        getImageXY(e);
        // Nicht größer als MapImage
        if ((mouseX<iWidth) && (mouseY<iHeight-bottomBorderHeight)) {
                if (!zooming) {
                        x1=mouseX;
                        y1=mouseY
                        x2=x1+1;
                        y2=y1+1;
                        clipLayer("zoomBoxTop",x1,y1,x2,y2);
                        clipLayer("zoomBoxLeft",x1,y1,x2,y2);
                        clipLayer("zoomBoxRight",x1,y1,x2,y2);
                        clipLayer("zoomBoxBottom",x1,y1,x2,y2);
                        zooming=true;
                        showLayer("zoomBoxTop");
                        showLayer("zoomBoxLeft");
                        showLayer("zoomBoxRight");
                        showLayer("zoomBoxBottom");
                }
        } else {
                if (zooming) {
                        stopZoomBox(e);
                }
        }
        return false;
}

// Stop ZoomIn
function stopZoomBox(e) {
        zooming=false;
        if ((zright <zleft+2) && (ztop < zbottom+2)) {
                // Wenn die Zoombox zu klein ist
                zoomin(e);
        } else {
                var width = Math.abs(right - left);
                var height = Math.abs(top - bottom);
                var pixelX = width / iWidth;
                var theY = iHeight - ztop;
                var pixelY = height / iHeight;
                newTop = pixelY * theY + bottom;
                newRight = pixelX * zright + left;
                newLeft = pixelX * zleft + left;
                theY = iHeight - zbottom;
                pixelY = height / iHeight;
                newBottom = pixelY * theY + bottom;

                refreshMap(true);
        }
        return true;
}

// Clip Zoombox an Mauskoordinaten
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 ((x1 != x2) && (y1 != y2)) {
                var ovBoxSize = 1;
                clipLayer("zoomBoxTop",zleft,ztop,zright,ztop+ovBoxSize);
                clipLayer("zoomBoxLeft",zleft,ztop,zleft+ovBoxSize,zbottom);
                clipLayer("zoomBoxRight",zright-ovBoxSize,ztop,zright,zbottom);
                clipLayer("zoomBoxBottom",zleft,zbottom-ovBoxSize,zright,zbottom);
        }
}

function hideZoomBox() {
        hideLayer("zoomBoxTop");
        hideLayer("zoomBoxLeft");
        hideLayer("zoomBoxRight");
        hideLayer("zoomBoxBottom");
}


/***********************************************************************************/
// Karte aufrufen mit den ermittelten X/Y Koordinaten. Werte aus Form auslesen.

function refreshMap(zooming) {
	showLayer("loadLayer");
	s = document.forms[0].elements['STDREQUEST'].value;
	s = s + "&X1=" + x1 + "&Y1=" + y1 + "&X2=" + x2 + "&Y2=" + y2;
	s = s + "&SAK=ZOOM";
	//s = s + "&ZOOM=" + LastAction;
	//window.parent.location = s;
        document.location = s;
}

