var map;
var newOV;
var incH = new GIcon();
var incM= new GIcon();
var incL= new GIcon();
var incU= new GIcon();
var cmE = new GIcon();
var cmETF = new GIcon();
var sgnBN = new GIcon();
var sgnBS = new GIcon();
var sgnBE = new GIcon();
var sgnBW = new GIcon();
var sgnGN = new GIcon();
var sgnGS = new GIcon();
var sgnGE = new GIcon();
var sgnGW = new GIcon();
var detST = new GIcon();
var streamingVideoIcon = new GIcon();

var _iconDir = _iDr + "MapSubSystem/icons/";

if (GBrowserIsCompatible())
{	
    function CreateGrpsAndMrks(input) {
        UpdatePage(true); //Make an ajax call to the web 	 
    }
	 
    function SetMap(xCn,yCn,zZm)
    {
        detST.image = _iconDir + "circle.gif";
       detST.iconSize = new GSize(8, 8);
       detST.iconAnchor = new GPoint(4,4);
       detST.infoWindowAnchor = new GPoint(21, 21);
       
       incH.image=_iconDir + "iconEmergency.gif";
       incH.iconSize = new GSize(21, 21);
       incH.iconAnchor = new GPoint(11, 11);
       incH.infoWindowAnchor = new GPoint(11, 11);

        incM.image=_iconDir + "iconIncident.gif";
        incM.iconSize = new GSize(21, 21);
        incM.iconAnchor = new GPoint(11, 11);
        incM.infoWindowAnchor = new GPoint(11, 11);

        incL.image=_iconDir + "iconSpecial.gif";
        incL.iconSize = new GSize(21, 21);
        incL.iconAnchor = new GPoint(11, 11);
        incL.infoWindowAnchor = new GPoint(11, 11);

        incU.image=_iconDir + "iconScheduled.gif";
        incU.iconSize = new GSize(18, 21);
        incU.iconAnchor = new GPoint(11, 9);
        incU.infoWindowAnchor = new GPoint(11, 9);

        cmE.image = _iconDir + "iconSSTV.gif";
        cmE.iconSize = new GSize(16, 6);
        cmE.iconAnchor = new GPoint(8,3);
        cmE.infoWindowAnchor = new GPoint(8, 3);

        cmETF.image = _iconDir + "iconCCTV_s.gif";
        cmETF.iconSize = new GSize(16, 6);
        cmETF.iconAnchor = new GPoint(8, 3);
        cmETF.infoWindowAnchor = new GPoint(8, 3);

        sgnBN.image=_iconDir + "iconDMSblank_N.gif";
        sgnBN.iconSize = new GSize(17,9);
        sgnBN.iconAnchor = new GPoint(8,4);
        sgnBN.infoWindowAnchor = new GPoint(8,4);

        sgnBS.image=_iconDir + "iconDMSblank_S.gif";
        sgnBS.iconSize = new GSize(17,9);
        sgnBS.iconAnchor = new GPoint(8,4);
        sgnBS.infoWindowAnchor = new GPoint(8,4);

        sgnBE.image=_iconDir + "iconDMSblank_E.gif";
        sgnBE.iconSize = new GSize(9, 17);
        sgnBE.iconAnchor = new GPoint(4,8);
        sgnBE.infoWindowAnchor = new GPoint(4,8);

        sgnBW.image=_iconDir + "iconDMSblank_W.gif";
        sgnBW.iconSize = new GSize(9,17);
        sgnBW.iconAnchor = new GPoint(4,8);
        sgnBW.infoWindowAnchor = new GPoint(4,8);

        sgnGN.image=_iconDir+ "iconDMS_N.gif";
        sgnGN.iconSize = new GSize(17,9);
        sgnGN.iconAnchor = new GPoint(8,4);
        sgnGN.infoWindowAnchor = new GPoint(8,4);

        sgnGS.image=_iconDir + "iconDMS_S.gif";
        sgnGS.iconSize = new GSize(17,9);
        sgnGS.iconAnchor = new GPoint(8,4);
        sgnGS.infoWindowAnchor = new GPoint(8,4);

        sgnGE.image=_iconDir + "iconDMS_E.gif";
        sgnGE.iconSize = new GSize(9,17);
        sgnGE.iconAnchor = new GPoint(4,8);
        sgnGE.infoWindowAnchor = new GPoint(4,8);

        sgnGW.image=_iconDir + "iconDMS_W.gif";
        sgnGW.iconSize = new GSize(9,17);
        sgnGW.iconAnchor = new GPoint(4,8);
        sgnGW.infoWindowAnchor = new GPoint(4,8);

        streamingVideoIcon.image = _iconDir + "iconTour.gif";
        streamingVideoIcon.iconSize = new GSize(21, 21);
        streamingVideoIcon.iconAnchor = new GPoint(4, 8);
        streamingVideoIcon.infoWindowAnchor = new GPoint(4, 8);
        
        map = new GMap2(_tc$("map"));
        map.addControl(new GSmallMapControl());
        map.addControl(new GMapTypeControl());
        map.addControl(new GScaleControl());
       //newOV= new GOverviewMapControl(new GSize(200,150))
      // map.addControl(newOV);

        map.addMapType(G_PHYSICAL_MAP); //Add terrain option
        map.removeMapType(G_SATELLITE_MAP); //Remove satelitte option
        
        GEvent.addListener(map,'zoomend',function(oldZ,newZ){CheckZoom(oldZ,newZ);}); 
        map.setCenter(new GLatLng(yCn, xCn), zZm);
        GEvent.addListener(map,'load',CreateGrpsAndMrks('GETNEWDATA'));
        GEvent.addListener(map,'moveend',CheckCenter);    
        map.disableDoubleClickZoom(); 
        map.enableContinuousZoom();
        //map.enableScrollWheelZoom();

        map.setMapType(G_NORMAL_MAP);
    }
   
   
    if (GBrowserIsCompatible())
    {	
	      function TgSpd(st)
	    {
		    if(_aPolyLines&&st==1)
		    {
			    for(var t=0;t<_aPolyLines.length;t++)
			    {
				    try
				    {
					    map.addOverlay(_aPolyLines[t]);
					    //_bTgSpd=true;
					    //_aPolyLines[t].display[true]; //needed for version 1
				    }
				    catch(e)
				    {
					    alert(e.message + " - TgSpd");
				    }
			    }
		    }
		    else if(_aPolyLines&&st==0)
		    {
			    for(var t=0;t<_aPolyLines.length;t++)
			    {
				    try
				    {
					    //_aPolyLines[t].display[false];//needed for version 1
					    map.removeOverlay(_aPolyLines[t]);
					    //_bTgSpd=true;
				    }
				    catch(e)
				    {
					    alert(e.message + " - TgSpd");
				    }
			    }
		    }
	        try
	        {
	           // _bSpdVs=!_bSpdVs;
	        }
	        catch(e)
	        {
	            alert(e.message + " - TgSpd");
	        }
	    }
	    
	    function TgDetectorStation(){
            
            try {
                if(typeof(detSTGrp)!="undefined")
                   detSTGrp.tgMrksVisible();
            }
            catch(e){
                alert(e.message + " - TgDetSt");
            }	
	   }  

	    function TgIncident()
	    {
	        try
	        {
                if(typeof(incMGrp)!="undefined")
	               incMGrp.tgMrksVisible();
	        }
	        catch(e)
	        {
	            alert(e.message + " - TgIncident");
	        }	
	    }
    	function TgSpecial()
	    {
	        try
	        {
                if(typeof(incLGrp)!="undefined")
	               incLGrp.tgMrksVisible();
	        }
	        catch(e)
	        {
	            alert(e.message + " - TgSpecial");
	        }	
	    }
	   function TgScheduled()
	    {
	        try
	        {
                if(typeof(incUGrp)!="undefined")
	               incUGrp.tgMrksVisible();
	        }
	        catch(e)
	        {
	            alert(e.message + " - TgScheduled");
	        }	
	    } 
	    function TgCCTV()
	    {
	        try
	        {
                 if(typeof(cmEGrp)!="undefined")
                     cmEGrp.tgMrksVisible();

                 if (typeof (cmETrafLandGrp) != "undefined")
                     cmETrafLandGrp.tgMrksVisible();   
	        }
	        catch(e)
	        {
	            alert(e.message + " - TgCCTV");
	        }	
	    }
    	
	    function TgDMS()
	    {
	        
	        try {
	            if (typeof (sgnGNGrp) != "undefined")
	                sgnGNGrp.tgMrksVisible();
	        }
	        catch (e) {
	            alert(e.message + " - TgDMS");
	        }
	        try {
	            if (typeof (sgnGSGrp) != "undefined")
	                sgnGSGrp.tgMrksVisible();
	        }
	        catch (e) {
	            alert(e.message + " - TgDMS");
	        }
	        try {
	            if (typeof (sgnGEGrp) != "undefined")
	                sgnGEGrp.tgMrksVisible();
	        }
	        catch (e) {
	            alert(e.message + " - TgDMS");
	        }
	        try {
	            if (typeof (sgnGWGrp) != "undefined")
	                sgnGWGrp.tgMrksVisible();
	        }
	        catch (e) {
	            alert(e.message + " - TgDMS");
	        }

	        try {
	            if (typeof (sgnBNGrp) != "undefined")
	                sgnBNGrp.tgMrksVisible();
	        }
	        catch (e) {
	            alert(e.message + " - TgDMSblank");
	        }
	        try {
	            if (typeof (sgnBSGrp) != "undefined")
	                sgnBSGrp.tgMrksVisible();
	        }
	        catch (e) {
	            alert(e.message + " - TgDMSblank");
	        }
	        try {
	            if (typeof (sgnBEGrp) != "undefined")
	                sgnBEGrp.tgMrksVisible();
	        }
	        catch (e) {
	            alert(e.message + " - TgDMSblank");
	        }
	        try {
	            if (typeof (sgnBWGrp) != "undefined")
	                sgnBWGrp.tgMrksVisible();
	        }
	        catch (e) {
	            alert(e.message + " - TgDMSblank");
	        }	
	    }

	    function TgStreamingVideoTours() {

	        try {
	            if (typeof (streamingVideoMarkerGroup) != "undefined")
	                streamingVideoMarkerGroup.tgMrksVisible();
	        }
	        catch (e) {
	            alert(e.message + " - TgStreamingVideoTours()");
	        }
	    }
}
}
else
{
    alert("Your browser is not compatible with Goolgle Maps. Acceptable browsers include Internet Explorer 6 or better and Firefox 1.5 or better.");
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
//  Purpose:
//      This is used to set up the map to use custom tiling. The custom tiling is used to draw the traffic lines
//      on the map. This is being done as an optimization since the polylines were too slow due to there being so
//      many points.
//
//
//
////////////////////////////////////////////////////////////////////////////////////////////////////////////////

function setupMapForCustomTiling() {
    //Create the tile layer object
    var detailLayer = new GTileLayer(new GCopyrightCollection(''));

    // Callback method to retrieve the URL of the tile
    detailLayer.getTileUrl = GoogleMapFileCallback;

    detailLayer.isPng = function() {
        //This uses PNG's
        return true;
    }

    //add your tiles to the normal map projection

    G_PHYSICAL_MAP.getTileLayers().push(detailLayer);
    G_NORMAL_MAP.getTileLayers().push(detailLayer);
    G_HYBRID_MAP.getTileLayers().push(detailLayer);


    // By using the add overlay method the tiles can be refreshed with the rest of the maps.
    map.addOverlay(new GTileLayerOverlay(detailLayer));

}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
//  Purpose:
//      This is the callback method that the Google Map will call in order to get the URL for the tile that will
//      be placed over the google map.
//
//  Parameters:
//      tile:
//          This holds the x and y coordinates of the tile that will be retrieved from the server.
//
//      zoom:
//          This holds the zoom level for the tile that is being displayed.
//
//  Return:
//      The url of the web page that will return of the tile. This includes any arguments.
//
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function GoogleMapFileCallback(tile, zoom) {
    //var tileURL = "GoogleMapsFile.aspx?x=" + tile.x + "&y=" + tile.y + "&zoom=" + zoom + "&z=" + Math.random().toString();
    //var tileURL = "http://atltimmcmillan/Work/GoogleTilesWebSite/Default.aspx?x=" + tile.x + "&y=" + tile.y + "&zoom=" + zoom + "&z=" + Math.random().toString();
    var tileURL = _googleTileServer + "?type=trafficlinks&x=" + tile.x + "&y=" + tile.y + "&zoom=" + zoom + "&z=" + Math.random().toString();

    return tileURL;
}

