//Set Global Variables
//setting Global variables
var blnCollapse = true;


function getMap()
{
  if (navigator.appName == "Netscape")
  	return document.map;
  else
    return map;
}



function select_Object(KEY,layerName)
{
   // Get MG Map Object
   
   var map = getMap();

   var mapSel = map.getSelection();
				
   var mapObjects = map.createObject("MGCollection");

   var mapLayer = map.getMapLayer(layerName);

	if (mapLayer != null)
	{
		if (KEY != "NULL")
	   	{
			   var mapObj = mapLayer.getMapObject(KEY);
				
			   if (mapObj != null)
				{  
			   mapObjects.add(mapObj);
				}
			   mapSel.clear();
			   mapSel.addObjectsEx(mapObjects, true);
			   zoomObject = false;
		}
	}
	else
				alert('Map Layer: ' + layerName + '  ... not found.');

}

    	  	  		    	  
function initObs1()
{
    ver = parseFloat(getMap().getApiVersion());
    if (navigator.appName == "Netscape")
    {

        if (ver >= 1.2)
        {
                getMap().setViewChangedObserver(document.obs);
				getMap().setViewChangingObserver(document.obs);
                //getMap().setDoubleClickObserver(document.obs);
            	//getMap().setSelectionChangedObserver(document.obs);
        }

        if (ver > 1.2)
        {
                //getMap().setBusyStateChangedObserver(document.obs);

            	//getMap().setMapLoadedObserver(document.obs);
                 getMap().setViewChangingObserver(document.obs);
                //getMap().setViewDistanceObserver(document.obs);
        }
    }
}


function initObs2()
{
        if (navigator.appName == "Netscape")
        {
            var ver = parseFloat(getMap().getApiVersion());
            if (ver >= 1.3)
        {
                document.write("<APPLET CODE=\"MapGuideObserver6.class\" WIDTH=1 HEIGHT=1 NAME=\"obs\" MAYSCRIPT>");
                document.write("</APPLET>");
        }
            else if (ver == 1.2)
        {
                document.write("<APPLET CODE=\"MapGuideObserver3.class\" WIDTH=1 HEIGHT=1 NAME=\"obs\" MAYSCRIPT>");
                document.write("</APPLET>");
        }
        }
}




function initVB()
{
    ver = parseFloat(getMap().getApiVersion());
    if (navigator.appName == "Microsoft Internet Explorer")
    {
        if (ver >= 1.2)
        {
                document.write('<SCRIPT LANGUAGE="VBScript">\n');

                //document.write('Sub map_onSelectionChanged(map)\n');
            	//document.write('    onSelectionChanged map\n');
                //document.write('End Sub\n\n');
                
                //document.write('Sub map_onDoubleClickObject(mapObj)\n');
            	//document.write('    mapObj.DoubleClickHandled = onDoubleClickObject(mapObj)\n');
                //document.write('End Sub\n\n');

                //document.write('Sub map_onDigitizedPoint(map, point)\n');
            	//document.write('    onDigitizedPoint map, point\n');
                //document.write('End Sub\n\n');

                //document.write('Sub map_onDigitizedPolygon(map, numPoints, points)\n');
            	//document.write('    onDigitizedPolygon map, numPoints, points\n');
                //document.write('End Sub\n\n');

                //document.write('Sub map_onDigitizedPolyline(map, numPoints, points)\n');
            	//document.write('    onDigitizedPolyline map, numPoints, points\n');
                //document.write('End Sub\n\n');

                //document.write('Sub map_onDigitizedCircle(map, units, center, radius)\n');
            	//document.write('    onDigitizedCircle map, units, center, radius\n');
                //document.write('End Sub\n\n');

                //document.write('Sub map_onAddMapLayer(url, mapLayer)\n');
            	//document.write('    onAddMapLayer url, mapLayer\n');
                //document.write('End Sub\n\n');

                document.write('Sub map_onViewChanged(map)\n');
            	document.write('    onViewChanged map\n');
                document.write('End Sub\n\n');
        }   
        
        if (ver > 1.2)
        {
            //document.write('Sub map_onBusyStateChanged(map, isBusy)\n');
            //document.write('    onBusyStateChanged map, isBusy\n');
            //document.write('End Sub\n\n');

            //document.write('Sub map_onDigitizedRectangle(map, anchorPt, endPt)\n');
            //document.write('    onDigitizedRectangle map, anchorPt, endPt\n');
            //document.write('End Sub\n\n');

            //document.write('Sub map_onViewChanging(map)\n');
            //document.write('    onViewChanging map\n');
            //document.write('End Sub\n\n');

            //document.write('Sub map_onMapLoaded(map)\n');
            //document.write('    onMapLoaded map\n');
            //document.write('End Sub\n\n');

            //document.write('Sub map_onViewedDistance(map, totalDistance, distances, units)\n');
            //document.write('    onViewedDistance map, totalDistance, distances, units\n');
            //document.write('End Sub\n\n');
        }

        if (ver >= 1.2)
        {
                document.write('<\/SCRIPT>\n');
        }
    }
}


function init()
{
// If the plug-in is installed, check to see if latest version is installed
    ver = parseFloat(getMap().getApiVersion());
    
    if (ver < 6.0 && navigator.appName == "Netscape")
    {
        displayDownloadMsg();
    }
    else 
    {
        initObs1();
    }    
} 


function displayDownloadMsg()
{
    msg =  "You do not have the latest version of the Autodesk Mapguide Viewer.\n" +
         "Do you want to download the latest Autodesk MapGuide viewer now? " +
         "Click OK to download the latest viewer. Otherwise, click Cancel to " +
         " proceed with your current viewer. If you click Cancel, some features " + 
         "of the example may not work properly)."

   if (confirm(msg))
       parent.window.location = "http://www.mapguide.com/Downloads/";
}



// The following JavaScript functions process events from both the ActiveX
// Control and the Netscape Plug-in. The ActiveX events are forwarded to these
// functions via VBScript, and the Plug-in events are forwarded to these
// functions via the MapGuideObserver Java applet.


//Function fired when Map View changes
function onViewChanged(map)
{
 if (blnCollapse) 
 { 
   var map = getMap(); 
   var myLayerGroups = map.getMapLayerGroups(); 
   for(var i=0; i < myLayerGroups.size(); i++) 
   { 
     var myLayerGroup = myLayerGroups.item(i); 
     myLayerGroup.expand(); 
    }
   blnCollapse = false; 
  } 
} 

//function onSelectionChanged(map)
//{
//    if (parent.toggleframe.displaySelEvents)
//    {
//        var mapSel = map.getSelection();
//        var numObjs = mapSel.getNumObjects();

//        alert(numObjs + " Object(s) selected");
//    }
//}




//function onDoubleClickObject(mapObj)
//{
//    if (parent.toggleframe.displayDblClickEvents)
//    {
//        alert("\nYou just double clicked on: " + mapObj.getName() + "\n\nLayer: "
//              + mapObj.getMapLayer().getName());

//        return true;
//    }
//    return false;
//}




//function onDigitizedPoint(map, point)
//{
//    alert("You just digitized a point. The coordinates are:\n\nLAT = " + point.getY()
//          + " , " + "LON = " + point.getX());
//}




//function onDigitizedPolygon(map, numPoints, points)
//{
//    alert("You just digitized a polygon with: " + numPoints + " Points");
//    for (i = 0; i < numPoints; i++)
//        alert("Point[" + i + "] = " + points.item(i).getY() + " , " + points.item(i).getX());
//}




//function onDigitizedCircle(map, units, point, r)
//{
//    alert("You just digitized a circle \n"
//          + "centre: LAT=" + point.getY() + " LON=" + point.getX() + "\n"
//          + "radius " + r + " " + units);
//}




//function onDigitizedPolyline(map, numPoints, points)
//{
//    alert("You just digitized a polyline with: " + numPoints + " Points");
//    for (i = 0; i < numPoints; i++)
//        alert("Point[" + i + "] = " + points.item(i).getY() + " , " + points.item(i).getX());
//}




//function onAddMapLayer(url, mapLayer)
//{
//   alert("You just added " + mapLayer.getName() + " from " + url);
//}





//function onDigitizedRectangle(map, anchorPt, endPt)
//{
//    alert("You just digitized a rectangle. The coordinates are:\n\n" 
//          +     "Anchor: LAT = " + anchorPt.getY() + " , " + "LON = " + anchorPt.getX() + "\n" 
//          +     "End: LAT = " + endPt.getY() + " , " + "LON = " + endPt.getX());
//}




//function onBusyStateChanged(map, bIsBusy)
//{
//    if (parent.toggleframe.displayBusyEvents)
//    {
//        alert('map.isBusy: ' + bIsBusy);
//    }
//}

//function onMapLoaded(map)
//{
//if (navigator.appName == "Netscape")
    //var map = getMap();
	//var mapLayer = map.getMapLayer("REMD_PARCELS");
    //mapLayer.setShowInLegend(true);
	//mapLayer.setVisibility(true);
	//mapLayer.setRebuild(true);
   //alert("Juan"); 
//}   
 

//function onViewChanging(map)
//{
 //if (navigator.appName == "Netscape")
   
  //{ alert("Juan"); }
//}


//function onViewedDistance(map, totalDistance, distances, units)
//{
//    alert('view distance completed.\n total distance: ' + totalDistance 
//        + '\n units: ' + units
//        + '\n number of segments: ' + distances.size()
//        + '\n 2nd last seg: ' + distances.item(distances.size()-2)
//        + '\n last seg: ' + distances.item(distances.size()-1)); 
//}}}}}}
