//extends of the overview
var x1, y1, x2, y2, width, height, zooming, layer;
var zleft, zright, ztop, zbottom;
var widthOV, heightOV, leftOV, topOV;

function setOverviewBoundingBox() {
    // get width, height and position of the overview
    widthOV = OverviewFrame.document.Overview.width;
    heightOV = OverviewFrame.document.Overview.height;

    leftOV = getLeft (OverviewFrame.document.Overview);
    topOV = getTop (OverviewFrame.document.Overview);
}

function displayOverviewBox(){

    var mScale, upp;
    var xDistanceOV;
    var xDistanceMap, yCenter;    
    var xLL, yLL, xUR, yUR, xMiddle, yMiddle;
    var minxMap, minyMap, maxxMap, maxyMap;
    var borderWidthLeft, borderWidthRight, borderWidthTop, borderWidthBottom;

    var OVBoxTop    = getLayer(OverviewFrame.document,"OVBoxTop");
    var OVBoxBottom = getLayer(OverviewFrame.document,"OVBoxBottom");
    var OVBoxLeft   = getLayer(OverviewFrame.document,"OVBoxLeft");
    var OVBoxRight  = getLayer(OverviewFrame.document,"OVBoxRight");
    var OVBoxMiddle = getLayer(OverviewFrame.document,"OVBoxMiddle");

    // calculate the scale of the overview
	xDistanceOV = Math.abs(top.overviewBoundingBox[2]-top.overviewBoundingBox[0]);  //difference in x coords (map units)
	mScale =  parseFloat(widthOV / xDistanceOV);
	
	// calculate upp of the map    
    xDistanceMap = parseFloat(maxx) - parseFloat(minx);
    upp = xDistanceMap / mWidth;

    // center coords of the requested map    
    xCenter = (parseFloat(maxx) + parseFloat(minx)) /2;
    yCenter = (parseFloat(maxy) + parseFloat(miny)) /2;
    
           
    minxMap = minx;    
    maxxMap = maxx;
    
    maxyMap = yCenter + (upp * mHeight) /2;
    minyMap = yCenter - (upp * mHeight) /2;
        
    // calc box in pixel coords
    
    xLL = parseInt(mScale * (minxMap - top.overviewBoundingBox[0]));    
    yLL = parseInt(mScale * (minyMap - top.overviewBoundingBox[1]));

    xUR = parseInt(mScale * (maxxMap - top.overviewBoundingBox[0]));
    yUR = parseInt(mScale * (maxyMap - top.overviewBoundingBox[1]));
    
    xMiddle = parseInt (mScale * (xCenter - top.overviewBoundingBox[0]))-1;
    yMiddle = parseInt (mScale * (yCenter - top.overviewBoundingBox[1]))+1;
        
    // size of borders
    borderWidthLeft = 1;
    borderWidthRight = 1;
    borderWidthTop = 1;
    borderWidthBottom = 1;
    
    // resize if box is to small   
    if ((xUR - xLL)< 3) { 
        xLL = ((xLL+xUR)/ 2) - 2;
        xUR = ((xLL+xUR)/ 2) + 2;        
    }
    
    if ((yUR - yLL)< 3) { 
        yLL = ((yLL+yUR)/ 2) - 2;
        yUR = ((yLL+yUR)/ 2) + 2;                
    }
    
    // check if bounding box is too big
    if (xLL < 0 || xLL > widthOV) {
        if (xLL < 0) { xLL = 0; }
        if (xLL > widthOV) { xLL = widthOV; }        
        OVBoxLeft.visibility = 'hidden';
    }
    else { OVBoxLeft.visibility = 'visible'; }
    
    if (xUR < 0 || xUR > widthOV) {
        if (xUR < 0) { xUR = 0;}
        if (xUR > widthOV) { xUR = widthOV;}
        OVBoxRight.visibility = 'hidden';
    }
    else { OVBoxRight.visibility = 'visible'; }
    
    if (yLL < 0 || yLL > heightOV) {
        if (yLL < 0) { yLL = 0; }
        if (yLL > heightOV) { yLL = heightOV; }
        OVBoxBottom.visibility = 'hidden';
    }
    else { OVBoxBottom.visibility = 'visible'; }
    
    if ( yUR < 0 || yUR > heightOV){
        if (yUR < 0) { yUR = 0; }
        if (yUR > heightOV) { yUR = heightOV; }
        OVBoxTop.visibility = 'hidden'; 
    }
    else { OVBoxTop.visibility = 'visible'; }
    
    // make center pixel visible
    OVBoxMiddle.visibility = 'visible';
    
    // calculate overviewpixel coords
    xLL = parseInt(leftOV + xLL);
    yLL = parseInt(topOV + heightOV - yLL);
    xUR = parseInt(leftOV + xUR);
    yUR = parseInt(topOV + heightOV - yUR);
              
    //resize the layers    
    OVBoxTop.left = xLL;
    OVBoxTop.top = yUR;
    OVBoxTop.width = Math.abs(xUR - xLL);
    OVBoxTop.height = borderWidthTop;

    OVBoxBottom.left = xLL;
    OVBoxBottom.top = yLL;
    OVBoxBottom.width = Math.abs(xUR - xLL);
    OVBoxBottom.height = borderWidthBottom;

    OVBoxLeft.left = xLL;
    OVBoxLeft.top = yUR;
    OVBoxLeft.width = borderWidthLeft;
    OVBoxLeft.height = Math.abs(yUR - yLL);

    OVBoxRight.left = xUR;
    OVBoxRight.top = yUR;
    OVBoxRight.width = borderWidthRight;
    OVBoxRight.height =  Math.abs(yUR - yLL);
    
    OVBoxMiddle.left = leftOV + xMiddle;
    OVBoxMiddle.top = topOV + heightOV - yMiddle;
    OVBoxMiddle.width = 4;
    OVBoxMiddle.height = 4;
}

function newCenterOV (e){
     
    var xPixel, yPixel;
    var mScale, xDistanceOV;
            
    // get current mouse coordinates
	xPixel = e.clientX;	
	yPixel = e.clientY;
    
    xPixel = xPixel - leftOV;
    yPixel = yPixel - topOV;
        
    // current map center
    xCenter = (parseFloat(minx)+parseFloat(maxx))/2;
    yCenter = (parseFloat(miny)+parseFloat(maxy))/2;
    
    // calculate the scale of the overview
	xDistanceOV = Math.abs(top.overviewBoundingBox[2]- top.overviewBoundingBox[0]);
	mScale =  parseFloat(widthOV / xDistanceOV);
	
	// get the new center
	xNew = parseFloat(top.overviewBoundingBox[0]) + (xPixel/ mScale);
	yNew = parseFloat(top.overviewBoundingBox[3]) - (yPixel/mScale)
		
    dx = xCenter - xNew;
    dy = yCenter - yNew;
    
    // new bbox
    minx = parseFloat(minx) - dx;
    miny = parseFloat(miny) - dy;
    maxx = parseFloat(maxx) - dx;
    maxy = parseFloat(maxy) - dy;
    
    // request the map
    getMap();
}
 


// x-pixel-value
function getLeft(l) {
    if (isNS){
        return l.x;
    }
    else if (isNS6) {
        el = document.getElementById('Map');
        temp = document.defaultView.getComputedStyle(el,null).getPropertyValue("left");
        Index = temp.indexOf("px",0);
        return parseInt(temp.slice(0,Index));
    }
    else {
        if (l.offsetParent) return (l.offsetLeft + getLeft(l.offsetParent));
        else return (l.offsetLeft);
    }
}

// y-pixel-value
function getTop(l)  {
    if (isNS){
        return l.y;
    }
    else if (isNS6){
        el = document.getElementById('Map');
        temp = document.defaultView.getComputedStyle(el,null).getPropertyValue("top");
        Index = temp.indexOf("px",0);
        return parseInt(temp.slice(0,Index));
    }
    else {
        if (l.offsetParent) return (l.offsetTop + getTop(l.offsetParent));
        else return (l.offsetTop);
    }
}