﻿
var _MapView = null;
   
// the control creation needs to be in an external file due to a patch:
//      http://support.microsoft.com/kb/912945
//      http://msdn.microsoft.com/library/default.asp?url=/workshop/author/dhtml/overview/activating_activex.asp   
                      
                      
function CreateMapControl(DivID, URL, Height, Width) { 
    if (IsSVGControlInstalled()) {
        var tag = "<embed src='" + URL + "' id='MapControl' name='MapControl' type='image/svg-xml' width='" + Width + "' height='" + Height + "' style='z-index: 0;' />";
        var MapDiv = document.getElementById(DivID);
	    MapDiv.innerHTML = tag;
	} else {
        var MapDiv = document.getElementById("MapDiv");
        var SVGDownload = document.getElementById("SVGDownload");
        SVGDownload.style.visibility = "visible";
        SVGDownload.style.display = "";
        MapDiv.appendChild(SVGDownload);
	}
}

            
function DisplayMap(URL, Height, Width) {      
    CreateMapControl("MapDiv", URL, Height, Width); 
    ViewControlStarted();
}


function ViewControlStarted() {
    if (_MapView == null) {
        _MapView = document.getElementById("MapControl");
    }
    if (_MapView.readystate != 4) {
        setTimeout(ViewControlStarted, 50);
        return;
    }
    _MapView.window.initializeGWMSVG(_MapView, true, true);
    //gObjMapView.insertMenuItem("Snapshot", "Save Current State", "SaveSnapshotAs", 1, "SaveAs");
}


function IsSVGControlInstalled() {
    var objSVGControl;
    var Success = true;
    
	try {
		objSVGControl = new ActiveXObject("Adobe.SVGCtl");
	}
	catch(e) {
        Success = false;
	}

	return Success;
}


function SaveMyRoute() {
    if (window.sidebar) {
        // Mozilla Firefox Bookmark		
        window.sidebar.addPanel(_SaveMyRouteTitle, _SaveMyRouteURL, "");	
    } else if (window.external) { 
        // IE Favorite		
        window.external.AddFavorite(_SaveMyRouteURL, _SaveMyRouteTitle); 
    } else if (window.opera && window.print) {
        // Opera Hotlist	
        return true; 
    }      
}


function ZoomRectangle() {
    if (_MapView != null) {
        _MapView.cmdCaptureRectangle(ZoomRectangleComplete, "");
    }
    return false;
}


function ZoomRectangleComplete(Coordinates, userInfo) {
    __doPostBack("ZoomRectangle", Coordinates);
}


function ZoomOut() {
    if (_MapView != null) {
        _MapView.cmdCapturePoint(ZoomOutComplete, "");
    }
    return false;
}


function ZoomOutComplete(Coordinates, userInfo) {
    __doPostBack("ZoomOut", Coordinates);
}


function ZoomRectangleStatic() {
    objMapView = document.getElementById("MapDiv");
    objMapView.style.cursor = "crosshair";
	objMapView.onmousedown = GetRectanglePoints;

    return false;
}


function ZoomRectangleStaticComplete(Coordinates) {
    objMapView.style.cursor = "default";
    __doPostBack("ZoomRectangleStatic", Coordinates);
}


function ZoomOutStatic() {
    objMapView = document.getElementById("MapDiv");
    objMapView.style.cursor = "crosshair";
	objMapView.onmousedown = GetPoints;
	
	return false;
}


function ZoomOutStaticComplete(Coordinates) {
    objMapView.style.cursor = "default";
    __doPostBack("ZoomOutStatic", Coordinates);
}


//    objMapView = document.getElementById("MapDiv");
//    objMapView.style.cursor = "crosshair";
//	objMapView.onmousedown = GetPoints;

//function ShowTooltip() {
//    alert("tooltip");
//}


gXRecStart = 0.0;
gYRecStart = 0.0;
gXRecEnd = 0.0;
gYRecEnd = 0.0;
gClicks = 0;
objMapView = null;


function getOffSetTop() {
    var pos = 0;
    
    if (window.pageYOffset) {
	      pos = window.pageYOffset;
    }
    else if (document.documentElement && document.documentElement.scrollTop) {
	    pos = document.documentElement.scrollTop;
    }
    else if (document.body) {
	      pos = document.body.scrollTop;
    }
    
    return pos;
}


function getOffSetLeft() {
    var pos = 0;
    
    if (window.pageXOffset) {
	      pos = window.pageXOffset;
    }
    else if (document.documentElement && document.documentElement.scrollLeft) {
	    pos = document.documentElement.scrollLeft;
    }
    else if (document.body) {
	      pos = document.body.scrollLeft;
    }
    
    return pos;
}


function DrawRectangle(e) {
    if (!e) e = window.event;
	if (gClicks > 0) {
		if (gClicks == 1) {
			pDiv = document.createElement("DIV");
			pDiv.id = "redlinebox";
			pDiv.style.position = "absolute";
			pDiv.style.left = gXRecStart + "px";
			pDiv.style.top = gYRecStart + "px";
			pDiv.style.visibility = "visible";
			pDiv.style.width = "1px";
			pDiv.style.height = "1px";
			pDiv.style.border = "2px gray solid";
			pDiv.style.overflow = "hidden";
			pDiv.style.backgroundColor  = "#b0c4de";
			pDiv.style.opacity = 0.5;	
		    pDiv.style.filter = 'alpha(opacity=50)';
			objMapView.insertBefore(pDiv, null);
			gClicks += 1;
			
		} else {
			pixelX = e.clientX + getOffSetLeft();
			pixelY = e.clientY + getOffSetTop();
			
			if ((pixelX < gXRecStart)&&(pixelY < gYRecStart)) {
				pDiv.style.left = pixelX + "px";
				pDiv.style.width = gXRecStart-pixelX + "px";
				pDiv.style.top = pixelY + "px";
				pDiv.style.height = gYRecStart-pixelY + "px";
			} else if ((pixelX < gXRecStart)&&(pixelY > gYRecStart)) {
				pDiv.style.left = pixelX + "px";
				pDiv.style.width = gXRecStart-pixelX + "px";
				pDiv.style.top = gYRecStart + "px";
				pDiv.style.height = pixelY-gYRecStart + "px";
			} else if ((pixelX > gXRecStart)&&(pixelY < gYRecStart)) {
				pDiv.style.left = gXRecStart + "px";
				pDiv.style.width = pixelX-gXRecStart + "px";
				pDiv.style.top = pixelY + "px";
				pDiv.style.height = gYRecStart-pixelY + "px";
			} else if ((pixelX > gXRecStart)&&(pixelY > gYRecStart)) {
				pDiv.style.left = gXRecStart + "px";
				pDiv.style.top = gYRecStart + "px";
				pDiv.style.width = pixelX - gXRecStart + "px";
				pDiv.style.height = pixelY - gYRecStart + "px";
			}
		}
	}
}


function GetRectanglePoints(e) {
    if (!e) e = window.event;
    // if right-click, remove rect and end event handling
    if (e.button > 1) {
	    var pDiv = document.getElementById("redlinebox");
	    if (null != pDiv) {
		    pDiv.parentNode.removeChild(pDiv);
        }
	    gClicks = 0;
	    objMapView.onmousedown = null;
	    objMapView.onmousemove = null;
	    e.cancelBubble = true;
    	
	    return;
    }
 
	gClicks += 1;
	if (gClicks == 1) {
		gXRecStart = e.clientX + getOffSetLeft();
		gYRecStart = e.clientY + getOffSetTop();
		objMapView.onmousemove = DrawRectangle;
	} else if (gClicks > 1) {
		gXRecEnd = e.clientX + getOffSetLeft();
		gYRecEnd = e.clientY + getOffSetTop();
		objMapView.onmousedown = null;
		objMapView.onmousemove = null;
    
		//make sure xy in right order
		if (gXRecStart > gXRecEnd) {
			tmp = gXRecStart;
			gXRecStart = gXRecEnd;
			gXRecEnd = tmp;
		}
		if (gYRecStart > gYRecEnd) {
			tmp = gYRecStart;
			gYRecStart = gYRecEnd;
			gYRecEnd = tmp;
		}

        var AnchorDiv = document.getElementById("MapDiv");
        var coors = findPos(AnchorDiv);
        var MapOffsetX = coors[0];
        var MapOffsetY = coors[1];
        var Coordinates = (gXRecStart - MapOffsetX) + "|" + (gYRecStart - MapOffsetY) + "|" + (gXRecEnd - MapOffsetX) + "|" + (gYRecEnd - MapOffsetY);
        
		//Add red polyline to map		//addrectangle(sRec);
		//var pDiv = document.getElementById("redlinebox");
		//if (null != pDiv)
		//	pDiv.parentNode.removeChild(pDiv);
		gClicks = 0;

		ZoomRectangleStaticComplete(Coordinates);
	}
}


function GetPoints(e) {
    if (!e) e = window.event;

	var MapX = e.clientX + getOffSetLeft();
	var MapY = e.clientY + getOffSetTop();

    var AnchorDiv = document.getElementById("MapDiv");
    var coors = findPos(AnchorDiv);
    var MapOffsetX = coors[0];
    var MapOffsetY = coors[1];
    var Coordinates = (MapX - MapOffsetX) + "|" + (MapY - MapOffsetY);
    
	ZoomOutStaticComplete(Coordinates);
}


