
function onViewChanging(map)
{
  if (onLoadScale > 0)
  {
    // Bus Route
    if ( (onLoadRoute != null && onLoadRoute.length > 0)  &&
         (onLoadDirection != null && onLoadDirection.length > 0))
    {
      if (onLoadMCS == true)
        doBusStopGotoXY(onLoadLat, onLoadLon, onLoadScale, onLoadRoute, onLoadDirection)
      else
        doBusStopGoto(onLoadLat, onLoadLon, onLoadScale, onLoadRoute, onLoadDirection)

      onLoadScale = 0;
      return;
    }

    // Just goto
    if (onLoadMarker == null || onLoadMarker.length == 0)
    {
      if (onLoadMCS == true)
        doGotoXY(onLoadLat, onLoadLon, onLoadScale);
      else
        doGoto(onLoadLat, onLoadLon, onLoadScale);

      onLoadScale = 0;
      return;
    }

    // Show Marker
    if (onLoadMCS == true)
      doShowMarkerXY(onLoadLat, onLoadLon, onLoadScale, onLoadMarker, onLoadTips);
    else
      doShowMarker(onLoadLat, onLoadLon, onLoadScale, onLoadMarker, onLoadTips);

      onLoadScale = 0;
  }

  if (isLargeView == true)
  {
   refreshMarker(map, window.opener.markerArray,  window.opener.markerArraySize);
   doSetBusService(map, window.opener.onLoadRoute, window.opener.onLoadDirection);
  }
  else{
   refreshMarker(map, markerArray,  markerArraySize);
  }

//  addV3WaterMark(map,"Markup Layer","Ring","Map Center",'C',0.008,0.008,"SymKey");
  addV3WaterMark(map,"Copyright","Copyright","V3 Teletech",'BL',0.0038,0.021,"orgKey");
  addV3WaterMark(map,"SLA","SLA","www.sla.gov.sg",'BR',0.0035,0.018,"SLAKey");
  addV3WaterMark(map,"V3Logo","Logo","www.v3teletech.com",'TR',0.008,0.009,"LogoKey");
}

function onViewChanged(map)
{

}

function onMapLoaded(map)
{

}

function onDigitizedPoint(map,point)
{

}

var IE4 = (document.all && !document.getElementById) ? true : false;
var NS4 = (document.layers) ? true : false;
var IE5 = (document.all && document.getElementById) ? true : false;
var N6 = (document.getElementById && !document.all) ? true : false;
var IE = IE4||IE5;

var markerArray = new Array(5);
var markerArraySize = 0;
var onLoadLat = 0;
var onLoadLon = 0;
var onLoadScale = 0;
var onLoadMarker = "";
var onLoadTips = "";
var onLoadMCS = false;
var onLoadRoute = "" ;
var onLoadDirection = "" ;
var isLargeView = false;

function getMap()
{
  if (navigator.appName == "Netscape")
  {
    if (NS4) return document.map;
    else if (N6) return document.getElementById("nsmap");
  } else {
      return document.map;
  }
}

function getMapType()
{
  var cookiecontent="";
  if(document.cookie.length>0)
  {
    var cookiename="maptype"+"=";
    var cookiebegin=document.cookie.indexOf(cookiename);
    var cookieend=0;
    if(cookiebegin>-1)
    {
      cookiebegin+=cookiename.length;
      cookieend=document.cookie.indexOf(";",cookiebegin);
      if(cookieend<cookiebegin)
      {
        cookieend=document.cookie.length;
      }
      cookiecontent=document.cookie.substring(cookiebegin,cookieend);
    }
  }
  return cookiecontent;
}

function doSelectfeatures()
{
    var map = getMap();
    map.selectMapObjectsDlg();
} 




function _addMarker(map,layer, symbol, key, tooltips, lat, lon, height, width)
{
  var mgRedLineLayer = map.getMapLayer(layer);
  if (mgRedLineLayer == null)
    mgRedLineLayer = map.createLayer("redline", layer);

  var mgObjConnection = mgRedLineLayer.getMapObjects();
  var mgSymbolObj = mgRedLineLayer.createMapObject(key, tooltips, "");

  if (mgSymbolObj != null)
  {
    mgRedLineLayer.setPriority(100);
    var mgRedlineSetup = map.getRedlineSetup(); //returns MGRedlineSetup Object

    var mgSymbolAttr =  mgRedlineSetup.getSymbolAttr();

    mgSymbolAttr.setHeight(height,"M");
    mgSymbolAttr.setWidth(width,"M");
    mgSymbolAttr.setRotation(0);
    mgSymbolAttr.setSymbol(symbol);

    var mgSymbolPoint = map.createObject("MGPoint");
    mgSymbolPoint.setX(lon);
    mgSymbolPoint.setY(lat);

    var ret = mgSymbolObj.addSymbolPrimitive(mgSymbolPoint,false);
    mgObjConnection.add(mgSymbolObj);
  }
}

function _clearMarker(map, layer)
{
  var mgRedLineLayer = map.getMapLayer(layer);
  if (mgRedLineLayer != null)
  {
    var mgObjConnection = mgRedLineLayer.getMapObjects();
    mgRedLineLayer.removeAllObjects();
    mgObjConnection.removeAll();
  }
}

function addMarker(map,layer, symbol, key, tooltips, lat, lon, height, width)
{
    var markerData = new Array(8);
    markerData[0] = layer;
    markerData[1] = symbol;
    markerData[2] = key;
    markerData[3] = tooltips;
    markerData[4] = lat;
    markerData[5] = lon;
    markerData[6] = height;
    markerData[7] = width;

    markerArray[markerArraySize++] = markerData;
}

function clearMarker(map, layer)
{
  _clearMarker(map, layer);
  markerArraySize = 0;

}



function refreshMarker(map, markerArray, markerArraySize)
{
  if (markerArraySize > 0)
  {
    for(i = 0; i < markerArray.length; i++)
    {
      var markerData = markerArray[i];
      if (markerData != null)
      {
        var layer = markerData[0];
        _clearMarker(map, layer);
      }
    }

    for(i = 0; i < markerArraySize; i++)
    {
      var markerData = markerArray[i];
      if (markerData != null)
      {
        var layer = markerData[0];
        var symbol = markerData[1];
        var key = markerData[2];
        var tooltips = markerData[3];
        var lat = markerData[4];
        var lon = markerData[5];
        var height = markerData[6];
        var width = markerData[7];

        _addMarker(map,layer, symbol, key, tooltips, lat, lon, map.getScale() * height, map.getScale() * width);
      }
    }
  }
}

function doGetKey()
{
    var map = getMap();
    if (map.getSelection().getNumObjects() == 0)
    {
        alert ("Make selection first!");
        return;
    }
    var sel = map.getSelection();
    var objs = sel.getMapObjectsEx(null);
    var cntObjects = objs.size();
    var msg = "Keys of selected features are:\n";
    for (i=0;i<cntObjects;i++)
    {
        var obj=objs.item(i);
        var key=obj.getKey();
        msg=msg + obj.getMapLayer().getName() + " " + key + "\n";
    }
    alert(msg);
}



function doSelect()
{
    var mapObj;
	  mapObj = getMap();
	  if (mapObj != null && getMapType() !="raster")
	  {
	    getMap().selectMode();
	  } else {

	  raster_setMode('select');

  }
}

function doLegend(url, width, height)
{
  window.open(url, 'legend', 'width='+width+',height='+height+',toolbar=0,directories=0,status=0,scrollbars=1,resize=0,menubar=0');
}

function doZoomIn()
{
  var mapObj;
  mapObj = getMap();
  if (mapObj != null && getMapType() !="raster")
  {
    mapObj.zoomInMode();
  } else {
   raster_setMode('zoom-in');
  }
}

function doZoomOut()
{
  var mapObj;

  mapObj = getMap();
  if (mapObj != null && getMapType() !="raster")
  {
    mapObj.zoomOutMode();
  } else {
  raster_setMode('zoom-out');

  }
}

function doOverview()
{
  var mapObj;

  mapObj = getMap();
  if (mapObj != null && getMapType() !="raster")
  {
    mapObj.zoomOut();
  } else {
	raster_setMode('overview');
  }
}


function doPan()
{
  var mapObj;
  mapObj = getMap();
  if (mapObj != null && getMapType()!="raster")
  {
    mapObj.panMode();

  } else {
   raster_setMode('pan');
  }
}

function doDistance()
{
  var mapObj;

  mapObj = getMap();
  if (mapObj != null && getMapType()!="raster")
  {
    if(parseFloat(mapObj.getApiVersion()) >= "1.3")
        mapObj.viewDistance("KM");
    else
        alert("This function is not available for Plugin version 3.0 and below.");
  } else {

  }
}

function doPreference()
{
  var mapObj;
  mapObj = getMap();
  if (mapObj != null && getMapType() !="raster")
  {
    mapObj.preferencesDlg();
  } else {

  }
}

function doCopyMap()
{
  var mapObj;

  mapObj = getMap();
  if (mapObj != null&&getMapType()!="raster")
  {
    mapObj.copyMap();
  } else {

  }
}

function doPrintMap()
{
  var mapObj;

  mapObj = getMap();
  if (mapObj != null && getMapType()!="raster")
  {
    var pgSetup = mapObj.getPageSetup();
		pgSetup.setInclude("mg_url",false);
    mapObj.printDlg();
  } else {

   raster_printMap();

  }
}

function doLargeView()
{
  var mapHeight = window.screen.Height * 0.8;
  var mapWidth = window.screen.Width * 0.8;
  winconfig = "left=0, Top=0, width=" + screen.width * 0.9 + ", height=" + screen.height * 0.9 + ", menubar=0, resizable=yes, toolbar=0, scrollbars=1, status=1";
  //var winconfig = "left=0, Top=0, width=" + screen.width * 0.9 + ", height=" + screen.height * 0.9 + ", menubar=1, resizable=yes, toolbar=1, scrollbars=1, status=1,location=1";
  var largeView = window.open("#","largeView", winconfig);

  document.forms['frmLargeView'].mapWidth.value = mapWidth;
  document.forms['frmLargeView'].mapHeight.value = mapHeight;
  document.forms['frmLargeView'].mapScale.value = getMap().getScale();
  document.forms['frmLargeView'].mapLat.value = getMap().getLat();
  document.forms['frmLargeView'].mapLon.value = getMap().getLon();
  document.forms['frmLargeView'].target = "largeView";
  document.forms['frmLargeView'].method = "get";
  document.forms['frmLargeView'].submit();
}

function doHelp(url)
{
  window.location.href = url;
}

function doShowInitialMarker(lat, lon, scale, marker, tooltips, mcs)
{
  var mapObj;
  mapObj = getMap();
  if (mapObj != null && getMapType() !="raster")
  {
  onLoadLat = lat;
  onLoadLon = lon;
  onLoadScale = scale;
  onLoadMarker = marker;
  onLoadTips = tooltips;
  onLoadMCS = mcs;

  } else {
	  raster_showMarker(lat, lon, scale, marker,tooltips);
  }
}

function doShowMarkerXY(y, x, scale, marker, tooltips)
{
  var mapObj;
  mapObj = getMap();
  if (mapObj != null && getMapType() !="raster")
  {
     var latLonPt = map.mcsToLonLat(x, y);
     doShowInitialMarker(latLonPt.getY(), latLonPt.getX() - 0.00167158140218, scale, marker, tooltips,false);
  }else {

	 //alert("don't know how to convert Sing Grid to LL86");
  }
}

function doShowMarker(lat, lon, scale, marker, tooltips)
{
  var mapObj;

  mapObj = getMap();
  if (mapObj != null && getMapType() !="raster")
  {
    clearMarker(mapObj, "redline layer");
    addMarker(mapObj, "redline layer", marker, "", tooltips, lat, lon, 0.006, 0.006)
    mapObj.zoomScale(lat, lon, scale);

  } else {

	  raster_showMarker(lat, lon, scale, marker,tooltips);

  }
}

function doShowMarkerXY(y, x, scale, marker, tooltips)
{
  var mapObj;
  mapObj = getMap();
  if (mapObj != null && getMapType() !="raster")
  {
     var latLonPt = map.mcsToLonLat(x, y);
     //alert("name: " + tooltips + " lat: " + latLonPt.getY() + " lon: " + (latLonPt.getX() - 0.00167158140218));
     doShowMarker(latLonPt.getY(), latLonPt.getX() - 0.00167158140218, scale, marker, tooltips);
  }else {
	  alert("don't know how to convert Sing Grid to LL86");
  }

}

function doShowStartStop(sLat, sLon, eLat, eLon, distance, scale, sTooltips, eTooltips)
{
  var mapObj;
  mapObj = getMap();
  if (mapObj != null && getMapType() != "raster")
  {
    clearMarker(mapObj, "redline layer");
    addMarker(mapObj, "redline layer", "marker_start", "", sTooltips, sLat, sLon, 0.011, 0.011);
    addMarker(mapObj, "redline layer", "marker_stop", "", eTooltips, eLat, eLon, 0.011, 0.011);
    var mapLat=(sLat + eLat)/2;
    var mapLon=(sLon + eLon)/2;
    var mapScale = (distance/500) * scale;

    if (mapScale < 2000)
     mapScale = 2000;
     mapObj.zoomScale(mapLat, mapLon, mapScale);

  } else {
    var mapLat=(sLat + eLat)/2;
    var mapLon=(sLon + eLon)/2;
    var mapScale = (distance/500) * scale;
    if (mapScale < 2000)
    mapScale = 2000;
		raster_doShowStartStop(sLat, sLon, eLat, eLon, mapLat,mapLon,mapScale,sTooltips,eTooltips);
  }
}

function doShowStartStopXY(sLat, sLon, y, x, distance, scale, sTooltips, eTooltips)
{
  var mapObj;
  mapObj = getMap();
  if (mapObj != null)
  {
     var latLonPt = map.mcsToLonLat(x, y);
     //alert("name: " + tooltips + " lat: " + latLonPt.getY() + " lon: " + (latLonPt.getX() - 0.00167158140218));
     doShowStartStop(sLat, sLon, latLonPt.getY(), latLonPt.getX() - 0.00167158140218, distance, scale, sTooltips, eTooltips);
  }
}

function doVicinityPopUp(lat, lon, name)
{
  var vicinityPopUp = window.open('', 'VicinityWin','status=yes,menubar=no,toolbar=no,scrollbars=yes,resizable=no,width=416,height=410,screenX=0,screenY=0');

  var url = "/neocan/en/popup.vicinity1.html?name=" + escape(name) + "&lat=" + lat + "&lon=" + lon;
  //alert(url);
  vicinityPopUp.location = url;
}

function doVicinityPopUpXY(y, x, name)
{
  var mapObj;
  mapObj = getMap();
  if (mapObj != null)
  {
     var latLonPt = map.mcsToLonLat(x, y);
     doVicinityPopUp(latLonPt.getY(), latLonPt.getX() - 0.00167158140218, name);
  }
}

function doBusStopGoto(lat, lon, scale, service, direction)
{
  var mapObj;
  mapObj = getMap();
  if (mapObj != null)
  {
		var layer1 = mapObj.getMapLayer("Bus Route");
		var layer2 = mapObj.getMapLayer("Bus Route Text");
    layer1.setVisibility(true);
    layer2.setVisibility(true);
		if (layer1 != null)
		{
		 layer1.setSQLWhere("bus_service = '"+service+"' and direction = '"+direction+"'");
		}

		if (layer2 != null)
		{
		 layer2.setSQLWhere("bus_service = '"+service+"' and direction = '"+direction+"'");
		}
   	 mapObj.zoomScale(lat,lon, scale);
		 mapObj.refresh();
  }
  else
  {
   var select = "";
   raster_showBusStopGoto(lat, lon, scale, service);
  }
}

function zoomSelected()
{
    var map = getMap();
    var selected = map.getSelection().getMapObjectsEx(null);
    if (selected.size()>0)
        map.zoomSelected();
    else
        alert("nothing selected!");
} 




function showObjectCount()
{
    if (legendSet)
        return;
    var map = getMap();
    if (!map.isBusy())
    {
        var layers = map.getMapLayersEx();
        var cnt = layers.size();
        var i;
        var msg = "";
        for (i = 0; i<cnt; i++)
        {
            var layer = layers.item(i);
            var objectCount = layer.getMapObjectsEx().size();
            var label = layer.getLegendLabel();
            label = label + " " + objectCount + " features";
            layer.setLegendLabel(label);
        }
    }
    legendSet = true;
}    



function layerToggle(name)
{
    var map = getMap();
    var layer = map.getMapLayer(name);
    
    if (layer == null)
        alert("layer not found.");
    else
        layer.setVisibility(!layer.getVisibility());
        
    map.refresh();
} 



function doGetKey()
{
    var map = getMap();
    if (map.getSelection().getNumObjects() == 0)
    {
        alert ("Make selection first!");
        return;
    }
    var sel = map.getSelection();
    var objs = sel.getMapObjectsEx(null);
    var cntObjects = objs.size();
    var msg = "Vous avez selectioné :\n";
    for (i=0;i<cntObjects;i++)
    {
        var obj=objs.item(i);
        var key=obj.getKey();
        msg=msg + obj.getMapLayer().getName() + " " + key + "\n";
    }
    alert(msg);
}




function listLayers()
{
    var map = getMap();
    var layers = map.getMapLayersEx();
    var cnt = layers.size();
    var i;
    var msg = "";
    for (i = 0; i<cnt; i++)
    {
        var layer = layers.item(i);
        msg = msg + layer.getName() + "\n";
    }
    alert(msg);
}  



function countLayers()
{ 
    var map = getMap();
    var layers = map.getMapLayersEx();
    var cnt = layers.size();
    alert(cnt);
}



function doSetBusService(mapObj, service, direction)
{
  if (mapObj != null)
  {
		var layer1 = mapObj.getMapLayer("Bus Route");
		var layer2 = mapObj.getMapLayer("Bus Route Text");
    layer1.setVisibility(true);
    layer2.setVisibility(true);
		if (layer1 != null)
		{
		 layer1.setSQLWhere("bus_service = '"+service+"' and direction = '"+direction+"'");
		}

		if (layer2 != null)
		{
		 layer2.setSQLWhere("bus_service = '"+service+"' and direction = '"+direction+"'");
		}
		 mapObj.refresh();
  }
else{
alert("Raster Map");
}
}

function doBusStopGotoXY(lat, lon, scale, service, direction)
{
  var mapObj;
  mapObj = getMap();
  if (mapObj != null)
  {
     var latLonPt = map.mcsToLonLat(x, y);
     doBusStopGoto(latLonPt.getY(), latLonPt.getX() - 0.00167158140218, scale, service, direction);
  }
else
{
 alert("Raster Map");
}
}

function doGoto(lat, lon, scale)
{
  var mapObj;
  mapObj = getMap();
  if (mapObj != null)
  {
		mapObj.zoomScale(lat,lon, scale);
  }
}

function doGotoXY(y, x, scale)
{
  var mapObj;
  mapObj = getMap();
  if (mapObj != null)
  {
     var latLonPt = map.mcsToLonLat(x, y);
     doGoto(latLonPt.getY(), latLonPt.getX() - 0.00167158140218, scale);
  }
}

//Used to change the map type.
function doChangeMaptype(maptype,url)
{
  document.cookie="mapstate="+maptype+";path=/";
  window.location= url;
}
