﻿/* An InfoBox is like an info window, but it displays
 * under the marker, opens quicker, and has flexible styling.
 * @param {GLatLng} latlng Point to place bar at
 * @param {Object} opts Passes configuration options - content, 
 *   offsetVertical, offsetHorizontal, className, height, width
 */
//alert("TEST");
function InfoBox(latlng, opts) 
{
  this.latlng_ = latlng;
  this.content_ = opts.content || "Hello World";
  this.offsetVertical_ = opts.offsetVertical || -5;
  this.offsetHorizontal_ = opts.offsetHorizontal || -5;
  this.className_ = opts.className || "";  
  this.height_ = opts.height || 200;
  this.width_ = opts.width || 300;

  //sds
  //sdsds
}

/* InfoBox extends GOverlay class from the Google Maps API
 */
InfoBox.prototype = new GOverlay();

/* Creates the DIV representing this InfoBox
 * @param {GMap2} map The map to add infobox to
 */
InfoBox.prototype.initialize = function(map) {
  // Create the DIV representing our Bar
  var div = document.createElement("div");

  if (this.className_ != "") {
    div.className = this.className_;
  } else {
    div.style.border = "1px solid #000000";
    div.style.position = "absolute";
    div.style.background = "url('http://ace.imageg.net/images/WIZ_ACE_myStore/mapBubble.png')";
    //div.style.backgroundColor = "#FFFFFF";
    div.style.padding = "2px";
    div.style.width = this.width_ + "px";
    div.style.height = this.height_ + "px";
  } 

  var contentDiv = document.createElement("div");
  contentDiv.innerHTML = this.content_;

  var topDiv = document.createElement("div");
  topDiv.style.textAlign = "right";
  topDiv.style.paddingRight = "12px";
  topDiv.style.paddingTop = "10px";
  var closeImg = document.createElement("img");
  //closeImg.src = "http://www.google.com/intl/en_us/mapfiles/close.gif";
  closeImg.src = "http://dnn5.parkrun.com/DesktopModules/ParkrunGoogle/images/close.png";
  topDiv.appendChild(closeImg);

  function removeInfoBox(ib, m) {
    return function() { 
      GEvent.trigger(ib, "closeclick");
      m.removeOverlay(ib);
    };
  }
  
  GEvent.addDomListener(closeImg, 'click', removeInfoBox(this, map));
 
  div.appendChild(topDiv);
  div.appendChild(contentDiv);
  div.style.display = 'none';

  map.getPane(G_MAP_MARKER_PANE).appendChild(div);

  this.map_ = map;
  this.div_ = div;
}

/* Remove the main DIV from the map pane
 */
InfoBox.prototype.remove = function() {
  this.div_.parentNode.removeChild(this.div_);
}

/* Copy our data to a new InfoBox
 * @return {InfoBox} Copy of infobox
 */
InfoBox.prototype.copy = function() {
  var opts = {};
  opts.latlng = this.latlng_;
  opts.content = this.content_;
  opts.offsetVertical = this.offsetVertical_;
  opts.offsetHorizontal = this.offsetHorizontal_;

  opts.className = this.className_ || "";  
  opts.height = this.height_;
  opts.width = this.width_;
  return new InfoBox(this.latlng, opts);
}

/* Redraw the Bar based on the current projection and zoom level
 * @param {boolean} force Helps decide whether to redraw overlay
 */
InfoBox.prototype.redraw = function(force) {
  // We only need to redraw if the coordinate system has changed
  if (!force) return;

  // Calculate the DIV coordinates of two opposite corners of our bounds to
  // get the size and position of our Bar
  var pixPosition = this.map_.fromLatLngToDivPixel(this.latlng_);

  // Now position our DIV based on the DIV coordinates of our bounds
  this.div_.style.width = this.width_ + "px";
  this.div_.style.left = (pixPosition.x + this.offsetHorizontal_) + "px";
  this.div_.style.height = this.height_ + "px";
  this.div_.style.top = (pixPosition.y + this.offsetVertical_) + "px";
  this.div_.style.display = 'block';

  // if we go beyond map, pan map
  var mapWidth = this.map_.getSize().width;
  var mapHeight = this.map_.getSize().height;
  var bounds = this.map_.getBounds();
  var boundsSpan = bounds.toSpan();
  var longSpan = boundsSpan.lng();
  var latSpan = boundsSpan.lat();
  var degWidth = (this.width_/mapWidth) * longSpan;
  var degHeight = (this.height_/mapHeight) * latSpan;

  if (this.latlng_.lng() + degWidth > bounds.getNorthEast().lng()) {
    this.map_.panTo(this.latlng_);
  }   

  var bottompt = new GLatLng( (this.latlng_.lat() - degHeight), this.latlng_.lng());
  if (!bounds.contains(bottompt)) {
    this.map_.panTo(this.latlng_);
  }
  
}

function GetLatLong(lattitude, longitude)
{
//lattitude;
//longitude;
//var point = new GLatLng(lattitude, longitude, 14 );

//var infoBoxOptions = 
//          {
//            "content": "",
//            "offsetHorizontal": -175,
//            "offsetVertical": -430,
//            "height": 450,
//            "width": 330,
//            "className": "infoBox"
//          };
//map = new GMap2(document.getElementById("GoogleMap_Div"));
//var marker = new GMarker(point,{icon:'',draggable:'',title:''});
//var infoBox = new InfoBox(point, infoBoxOptions);
//          marker.infoBox = infoBox;
//GEvent.addListener(marker, "click", function () {
//        
//            if (map.infoBox) {
//            map.removeOverlay(map.infoBox);
//            }
//            map.infoBox = marker.infoBox;
//            map.addOverlay(map.infoBox);
//            
//        }
//        );
map = new GMap2(document.getElementById("GoogleMap_Div"));
//var myIcon_google;
var point = new GLatLng(lattitude, longitude, 14 )
//myIcon_google = new GIcon(G_DEFAULT_ICON);
//            markerOptions = { icon:myIcon_google };
//var marker = new GMarker(point, markerOptions); 
//var iwAnchor = marker.getIcon().infoWindowAnchor;
//  var iconAnchor = marker.getIcon().iconAnchor;
//  var offset = new GSize(iwAnchor.x-iconAnchor.x,
//                         iwAnchor.y-iconAnchor.y);
//  map.openInfoWindow(marker.getLatLng(), '', {pixelOffset:offset}); 
        //GEvent.trigger(marker, "click"); 
//        var marker = new Marker(point, new MarkerOptions({draggable: true}));

//                                         map.addOverlay(marker);
//                                         // prevent the addition of multiple markers
//                    map.removeEventListener(MapMouseEvent.CLICK,onMapClick);
//                                         marker.getLatLng(); 
var baseIcon = new GIcon();
 baseIcon.iconSize=new GSize(32,32);
 baseIcon.shadowSize=new GSize(56,32);
 baseIcon.iconAnchor=new GPoint(16,32);
 baseIcon.infoWindowAnchor=new GPoint(16,0);

var thisicon = new GIcon(baseIcon, "http://itouchmap.com/i/blue-dot.png", null, "http://itouchmap.com/i/msmarker.shadow.png");

 var marker = new GMarker(point,thisicon);
 marker.openInfoWindowHtml('trest');
  GEvent.trigger(marker, "click");

        
}


    //RaiseEvent('MovePushpin','pushpin2');
function RaiseEvent(pEventName,pEventValue)
{
    document.getElementById('<%=hidEventName.ClientID %>').value = pEventName;
    document.getElementById('<%=hidEventValue.ClientID %>').value = pEventValue;
    if(document.getElementById('<%=UpdatePanelXXXYYY.ClientID %>') != null)
    {
        __doPostBack('<%=UpdatePanelXXXYYY.ClientID %>','');
    }
}

// JScript File

function IgnoreZeroLatLongs(bIgnore)
{
        //Check if there is any visible pushpin on map.
        var cnt = 0;
        bounds = new GLatLngBounds();
        for(var i=0;i<markers.getLength();i++)
        {
            var ignoremarker = false;
            var point1 = markers.markers[i].getPoint();
            if(bIgnore)
            {
                if((point1.x==0) && (point1.y==0))
                {
                    ignoremarker = true;
                }
            }
            if(!ignoremarker)
            {
                if(!markers.markers[i].isHidden())
                {
                    bounds.extend(markers.markers[i].getPoint());
                    //Increment visible pushpin count
                    cnt++;
                }
            }
            
        }
        var iZoomLevel = map.getBoundsZoomLevel(bounds);
        var point = bounds.getCenter();
        
        map.setZoom(iZoomLevel);
        map.setCenter(point);
    
}

  function ShowFullScreenMap()
  {
 
      var objButton = document.getElementById('btnFullScreen');
      if(objButton.value=='Full Screen')
      {
        var objMap = document.getElementById('GoogleMap_Div');
        var objDiv = document.getElementById('GoogleMap_Div_Container');
        objMap.style.width='100%';
        objMap.style.height='100%';

        objDiv.style.position='absolute';
        objDiv.style.left='0px';
        objDiv.style.top='0px';
        objDiv.style.width='99%';
        objDiv.style.height='95%';
        //objDiv.style.zIndex = '-100';
        objDiv.style.backgroundColor='LightGrey';

        objButton.value='Close Fullscreen';
        DrawGoogleMap();
      }
      else
      {
        var objMap = document.getElementById('GoogleMap_Div');
        var objDiv = document.getElementById('GoogleMap_Div_Container');
        objMap.style.width='<%=GoogleMapObject.Width %>';
        objMap.style.height='<%=GoogleMapObject.Height %>';
        alert('hi -276');
        objDiv.style.position='';
        objDiv.style.left='';
        objDiv.style.top='';
        objDiv.style.width='';
        objDiv.style.height='';

        objButton.value='Full Screen';
        //DrawGoogleMap();
      }
}

var map;
var trafficInfo = null;

//function fListeners()
//{
//    this.listeners = new Array();
//    this.getLength = function() { return this.listeners.length; };
//    this.pushValue = function(v) { this.listeners.push(v); }
//    this.getValue = function(i)  { return this.listeners[i]; }
//}
function fMarkers()
{
    this.markers = new Array();
    this.getLength = function() { return this.markers.length; };
    this.pushValue = function(v) { this.markers.push(v); }
    this.getValue = function(i)  { return this.markers[i]; }
    this.getLastValue = function()  { return this.markers[this.markers.length-1]; }
    this.getValueById = function(ID)  {
                                        var i;
                                        for(i=0;i<this.markers.length;i++)
                                        {
                                            if(this.markers[i].value==ID)
                                            {
                                               // alert('marker found : '+this.markers[i].value);
                                                return this.markers[i];
                                            }
                                        } 
                                        return null; 
                                      }
    this.removeValueById = function(ID)  {
                                        var i;
                                        for(i=0;i<this.markers.length;i++)
                                        {
                                            if(this.markers[i].value==ID)
                                            {
                                               // alert('marker found : '+this.markers[i].value);
                                                this.markers.splice(i,1);
                                                //alert('changed marker removed');

                                            }
                                        } 
                                        return null; 
                                      }
}

function fPolylines()
{
    this.polylines = new Array();
    this.polylinesID = new Array();
    this.getLength = function() { return this.polylines.length; };
    this.pushValue = function(v,ID) {  this.polylines.push(v); this.polylinesID.push(ID); }
    this.getValue = function(i)  { return this.polylines[i]; }
    this.getLastValue = function()  { return this.polylines[this.polylines.length-1]; }
    this.getValueById = function(ID)  {
                                        var i;
                                        for(i=0;i<this.polylinesID.length;i++)
                                        {
                                            if(this.polylinesID[i]==ID)
                                            {
                                               // alert('polyline found : '+this.polylines[i].value);
                                                return this.polylines[i];
                                            }
                                        }
                                        return null;
                                      }
  this.removeValueById = function(ID) {
                                        var i;
                                        for(i=0;i<this.polylinesID.length;i++)
                                        {
                                            if(this.polylinesID[i]==ID)
                                            {
                                                this.polylines.splice(i,1);
                                                this.polylinesID.splice(i,1);
                                            }
                                        }
                                        return null;
                                      }
}

function fPolygons()
{
    this.polygons = new Array();
    this.polygonsID = new Array();
    this.getLength = function() { return this.polygons.length; };
    this.pushValue = function(v,ID) {  this.polygons.push(v); this.polygonsID.push(ID); }
    this.getValue = function(i)  { return this.polygons[i]; }
    this.getLastValue = function()  { return this.polygons[this.polygons.length-1]; }
    this.getValueById = function(ID)  {
                                        var i;
                                        for(i=0;i<this.polygonsID.length;i++)
                                        {
                                            if(this.polygonsID[i]==ID)
                                            {
                                                return this.polygons[i];
                                            }
                                        } 
                                        return null;
                                      }
    this.removeValueById = function(ID)  {
                                        var i;
                                        for(i=0;i<this.polygonsID.length;i++)
                                        {
                                            if(this.polygonsID[i]==ID)
                                            {
                                                this.polygons.splice(i,1);
                                                this.polygonsID.splice(i,1);
                                            }
                                        } 
                                        return null; 
                                      }
}

if (GBrowserIsCompatible())
{


map = new GMap2(document.getElementById("GoogleMap_Div"));

var markers = new fMarkers();
var polylines = new fPolylines();
var polygons = new fPolygons();
//var myEventListeners = new fListeners();
                
//function CreateMarker(point,icon1,InfoHTML,bDraggable,sTitle)
//{
//    var marker;
//        marker = new GMarker(point,{icon:icon1,draggable:bDraggable,title: sTitle});
//    if(InfoHTML!='')
//    {
//        GEvent.addListener(marker, "click", function() { this.openInfoWindowHtml(InfoHTML); });
//    }
//        GEvent.addListener(marker, "dragend", function() {  GService.SetLatLon(this.value,this.getLatLng().y,this.getLatLng().x);RaiseEvent('PushpinMoved',this.value);  });
//    return marker;
//}

function CreateMarker(point,icon1,InfoHTML,bDraggable,sTitle, result) 
{
    var marker = new GMarker(point,{icon:icon1,draggable:bDraggable,title: sTitle});
    var infoBoxOptions = 
        {
            "content": InfoHTML,
            "offsetHorizontal": -175,
            "offsetVertical": -430,
            "height": 450,
            "width": 330,
            "className": "infoBox"
        };

    var infoBox = new InfoBox(point, infoBoxOptions);
    marker.infoBox = infoBox;

    GEvent.addListener(marker, "click", function() {
        //if (map.infoBox) 
        //{
        //map.removeOverlay(map.infoBox);
        //}
        //map.infoBox = marker.infoBox;
        //showMapBlowup(InfoHTML);
        //map.openInfoWindowHtml(map.getCenter(),("A Title. Address line 1,Address line 2, City, County. Post Code ")); 
        //this.openInfoWindow(map.infoBox);
        //map.addOverlay(map.infoBox);
        //map.getCenter();
        //map.setCenter(0);

        //comment below lines for custom infobox
        //            this.openInfoWindow(InfoHTML);
        //            map.setZoom(14);

        //uncomment below lines for custom infobox
        marker.openExtInfoWindow
            (
               map, "custom_info_window_red", InfoHTML, { beakOffset: 17 }
            );
        //        alert(point.lat());
        //document.getElementById("GoogleMap_Div").style.top = "200px";
        //        var pointlat = parseFloat(point.lat());
        //        pointlat = pointlat-00.000199;
        //        alert(pointlat);
        //        map.setCenter(new GLatLng(pointlat, point.lng()));
        map.setCenter(new GLatLng(point.lat() + parseFloat(0.15), point.lng()));
        //map.panBy(new GSize(1,300));
        //map.panTo(new GLatLng((point.lat() - 00.000199), point.lng())); //51.411199
        map.setZoom(10);


    }
    );
    map.addOverlay(marker);
    return marker;
}

function OpenInfoWindow(id,InfoHTML)
{
    
    var marker = markers.getValueById(id);
    if(marker!=null)
    {
        marker.openInfoWindowHtml(InfoHTML);
    }
}

function CreatePolyline(points,color,width,isgeodesic)
{
    var polyline;
    if(!isgeodesic)
    {
        polyline = new GPolyline(points,color,width);
    }
    else
    {
        var polyOptions = {geodesic:true};
        polyline = new GPolyline(points,color,width,1,polyOptions);
    }
    return polyline;
}

function CreatePolygon(points,strokecolor,strokeweight,strokeopacity,fillcolor,fillopacity)
{
    var polygon;
    
    var polygon = new GPolygon(points,strokecolor,strokeweight,strokeopacity,fillcolor,fillopacity);
    return polygon;
}

function fGetGoogleObject(result, userContext)
{
    map.setCenter(new GLatLng(result.CenterPoint.Latitude, result.CenterPoint.Longitude), result.ZoomLevel);
    
    if(result.ShowMapTypesControl)
    {
        map.addControl(new GMapTypeControl());
    }

    if(result.ShowZoomControl)
    {
        map.addControl(new GLargeMapControl());
    }
    
    
    map.setMapType(eval(result.MapType));
    
    var i;
    if(markers!=null)
    {
        for(i=0;i<markers.getLength();i++)
        {
            var cmark = markers.getValue(i);
            if(cmark !=null)
            {
                    map.removeOverlay(cmark);
            }
        }
    }
//    if(myEventListeners!=null)
//    {
//        for(i=0;i<myEventListeners.getLength();i++)
//        {
//            var lisnr = myEventListeners.getValue(i);
//            if(lisnr!=null)
//            {
//                GEvent.removeListener(lisnr);
//            }
//        }
//    }  
    markers = new fMarkers();
//    myEventListeners = new fListeners();

    for(i=0;i<result.Points.length;i++)
    {
        var myIcon_google;

        var myPoint = new GLatLng(result.Points[i].Latitude, result.Points[i].Longitude);
        
        myIcon_google = null;
        if(result.Points[i].IconImage!='')
        {
            myIcon_google = new GIcon(G_DEFAULT_ICON);
            markerOptions = { icon:myIcon_google };
            
            myIcon_google.iconSize = new GSize(result.Points[i].IconImageWidth,result.Points[i].IconImageHeight);
            myIcon_google.image = result.Points[i].IconImage;
            myIcon_google.shadow = result.Points[i].IconShadowImage;
            myIcon_google.shadowSize = new GSize(result.Points[i].IconShadowWidth, result.Points[i].IconShadowHeight);
            myIcon_google.iconAnchor =  new GPoint(result.Points[i].IconAnchor_posX, result.Points[i].IconAnchor_posY);
            myIcon_google.infoWindowAnchor = new GPoint(result.Points[i].InfoWindowAnchor_posX, result.Points[i].InfoWindowAnchor_posY);
        }
       
        var marker = CreateMarker(myPoint,myIcon_google,result.Points[i].InfoHTML,result.Points[i].Draggable,result.Points[i].ToolTip);
        marker.value = result.Points[i].ID;
        markers.pushValue(marker);
        map.addOverlay(markers.getLastValue());
    }
    //Add polylines
   // alert('adding polyline');

    polylines = new fPolylines();
    for(i=0;i<result.Polylines.length;i++)
    {
	 var polypoints = new Array();
	 var j;
	 for(j=0;j<result.Polylines[i].Points.length;j++)
 	 {
	 	polypoints.push(new GLatLng(result.Polylines[i].Points[j].Latitude, result.Polylines[i].Points[j].Longitude));
	 }
        var polyline = CreatePolyline(polypoints,result.Polylines[i].ColorCode,result.Polylines[i].Width,result.Polylines[i].Geodesic);
        polylines.pushValue(polyline,result.Polylines[i].ID);
        map.addOverlay(polylines.getLastValue());
    }
// var polypoints = new Array();
// polypoints.push(new GLatLng(43.65669, -79.44268));
// polypoints.push(new GLatLng(43.66619, -79.44268));
// var poly = CreatePolyline(polypoints,"#66FF00",10,true);
// map.addOverlay(poly);

// var polypoints = new Array();
// polypoints.push(new GLatLng(43.65669, -79.44268));
// polypoints.push(new GLatLng(43.66619, -79.44268));
// polypoints.push(new GLatLng(43.67619, -79.44268));
// var directions = new GDirections(map,document.getElementById("directions_canvas")); 
//Clear the mapa nd directions of any old information
//directions.clear();

//Load the map and directions from the specified waypoints
//directions.loadFromWaypoints(polypoints);


    polygons = new fPolygons();
    for(i=0;i<result.Polygons.length;i++)
    {
	 var polypoints = new Array();
	 var j;
	 for(j=0;j<result.Polygons[i].Points.length;j++)
 	 {
	 	polypoints.push(new GLatLng(result.Polygons[i].Points[j].Latitude, result.Polygons[i].Points[j].Longitude));
	 }
        var polygon = CreatePolygon(polypoints,result.Polygons[i].StrokeColor,result.Polygons[i].StrokeWeight,result.Polygons[i].StrokeOpacity,result.Polygons[i].FillColor,result.Polygons[i].FillOpacity);
        polygons.pushValue(polygon,result.Polygons[i].ID);
        map.addOverlay(polygons.getLastValue());
    }

    
    if(result.ShowTraffic)
    {
        trafficInfo = new GTrafficOverlay();
        map.addOverlay(trafficInfo);
    }
    if(result.AutomaticBoundaryAndZoom)
    {
        RecenterAndZoom(true,result);
    }

}

function DrawGoogleMap()
{

    if (GBrowserIsCompatible())
    {
    map = new GMap2(document.getElementById("GoogleMap_Div"));
    geocoder = new GClientGeocoder();
  
     GService.GetGoogleObject(fGetGoogleObject);
    } 
} 

 
function fGetGoogleObjectOptimized(result, userContext)
{

    if(result.RecenterMap)
    {
        map.setCenter(new GLatLng(result.CenterPoint.Latitude, result.CenterPoint.Longitude), result.ZoomLevel);
        GService.RecenterMapComplete();
    }
    
    map.setMapType(eval(result.MapType));

    if(result.ShowTraffic)
    {
        trafficInfo = new GTrafficOverlay();
        map.addOverlay(trafficInfo);
    }
    else
    {
        if(trafficInfo!=null)
        {
            map.removeOverlay(trafficInfo);
            trafficInfo = null;
        }
    }

    var i;
    for(i=0;i<result.Points.length;i++)
    {
        //Create icon
        var myIcon_google;

        var myPoint = new GLatLng(result.Points[i].Latitude, result.Points[i].Longitude);
        
        myIcon_google = null;
        if(result.Points[i].IconImage!='')
        {
            myIcon_google = new GIcon(G_DEFAULT_ICON);
            markerOptions = { icon:myIcon_google };

            myIcon_google.iconSize = new GSize(result.Points[i].IconImageWidth,result.Points[i].IconImageHeight);
            myIcon_google.image = result.Points[i].IconImage;
            myIcon_google.shadow = result.Points[i].IconShadowImage;
            myIcon_google.shadowSize = new GSize(result.Points[i].IconShadowWidth, result.Points[i].IconShadowHeight);
            myIcon_google.iconAnchor =  new GPoint(result.Points[i].IconAnchor_posX, result.Points[i].IconAnchor_posY);
            myIcon_google.infoWindowAnchor = new GPoint(result.Points[i].InfoWindowAnchor_posX, result.Points[i].InfoWindowAnchor_posY);

        }
        //Existing marker, but changed.
        if(result.Points[i].PointStatus=='C')
        {
            var marker = markers.getValueById(result.Points[i].ID);
            if(marker!=null)
            {
                markers.removeValueById(result.Points[i].ID);
                map.removeOverlay(marker);
            }
            var marker = CreateMarker(myPoint,myIcon_google,result.Points[i].InfoHTML,result.Points[i].Draggable,result.Points[i].ToolTip);
            marker.value = result.Points[i].ID;
            markers.pushValue(marker);
            map.addOverlay(markers.getLastValue());
        }
        //New Marker
        if(result.Points[i].PointStatus=='N')
        {
            var marker = CreateMarker(myPoint,myIcon_google,result.Points[i].InfoHTML,result.Points[i].Draggable,result.Points[i].ToolTip);
            marker.value = result.Points[i].ID;
            markers.pushValue(marker);
            map.addOverlay(markers.getLastValue());
        }
        //Existing marker, but deleted.
        if(result.Points[i].PointStatus=='D')
        {
            var marker = markers.getValueById(result.Points[i].ID);
            if(marker!=null)
            {
                markers.removeValueById(result.Points[i].ID);
                map.removeOverlay(marker);
            }
        }
    }
    
    //Get Polylines
    for(i=0;i<result.Polylines.length;i++)
    {
        //Existing marker, but changed.
        
        if(result.Polylines[i].LineStatus=='C')
        {
        
            var polyline = polylines.getValueById(result.Polylines[i].ID);
            if(polyline!=null)
            {
                polylines.removeValueById(result.Polylines[i].ID);
                map.removeOverlay(polyline);
            }
	        var polypoints = new Array();
	        var j;
	        for(j=0;j<result.Polylines[i].Points.length;j++)
 	        {
	 	        polypoints.push(new GLatLng(result.Polylines[i].Points[j].Latitude, result.Polylines[i].Points[j].Longitude));
	        }
            var polyline = CreatePolyline(polypoints,result.Polylines[i].ColorCode,result.Polylines[i].Width,result.Polylines[i].Geodesic);
            polylines.pushValue(polyline,result.Polylines[i].ID);
            map.addOverlay(polylines.getLastValue());
        }
        //New Marker

        if(result.Polylines[i].LineStatus=='N')
        {
	        var polypoints = new Array();
	        var j;
	        for(j=0;j<result.Polylines[i].Points.length;j++)
 	        {
	 	        polypoints.push(new GLatLng(result.Polylines[i].Points[j].Latitude, result.Polylines[i].Points[j].Longitude));
	        }
            var polyline = CreatePolyline(polypoints,result.Polylines[i].ColorCode,result.Polylines[i].Width,result.Polylines[i].Geodesic);
            polylines.pushValue(polyline,result.Polylines[i].ID);
            map.addOverlay(polylines.getLastValue());
        }
        //Existing marker, but deleted.
        if(result.Polylines[i].LineStatus=='D')
        {
            var polyline = polylines.getValueById(result.Polylines[i].ID);
            if(polyline!=null)
            {
                polylines.removeValueById(result.Polylines[i].ID);
                map.removeOverlay(polyline);
            }
        }
    }
    
        //Get Polygons
    for(i=0;i<result.Polygons.length;i++)
    {
        //Existing marker, but changed.

        if(result.Polygons[i].Status=='C')
        {
        
            var polygon = polygons.getValueById(result.Polygons[i].ID);
            if(polygon!=null)
            {
                polygons.removeValueById(result.Polygons[i].ID);
                map.removeOverlay(polygon);
            }
	        var polypoints = new Array();
	        var j;
	        for(j=0;j<result.Polygons[i].Points.length;j++)
 	        {
	 	        polypoints.push(new GLatLng(result.Polygons[i].Points[j].Latitude, result.Polygons[i].Points[j].Longitude));
	        }
            var polygon = CreatePolygon(polypoints,result.Polygons[i].StrokeColor,result.Polygons[i].StrokeWeight,result.Polygons[i].StrokeOpacity,result.Polygons[i].FillColor,result.Polygons[i].FillOpacity);
            polygons.pushValue(polygon,result.Polygons[i].ID);
            map.addOverlay(polygons.getLastValue());
        }
        //New Marker

        if(result.Polygons[i].Status=='N')
        {
	        var polypoints = new Array();
	        var j;
	        for(j=0;j<result.Polygons[i].Points.length;j++)
 	        {
	 	        polypoints.push(new GLatLng(result.Polygons[i].Points[j].Latitude, result.Polygons[i].Points[j].Longitude));
	        }
            var polygon = CreatePolygon(polypoints,result.Polygons[i].StrokeColor,result.Polygons[i].StrokeWeight,result.Polygons[i].StrokeOpacity,result.Polygons[i].FillColor,result.Polygons[i].FillOpacity);
            polygons.pushValue(polygon,result.Polygons[i].ID);
            map.addOverlay(polygons.getLastValue());
        }
        //Existing marker, but deleted.
        if(result.Polygons[i].Status=='D')
        {
            var polygon = polygons.getValueById(result.Polygons[i].ID);
            if(polygon!=null)
            {
                polygons.removeValueById(result.Polygons[i].ID);
                map.removeOverlay(polygon);
            }
        }
    }
    if(result.AutomaticBoundaryAndZoom)
    {
        RecenterAndZoom(true,result);
    }
}
}

//This function causes Recentering of map. It finds all visible markers on map and decides center point and zoom level based on these markers.
function RecenterAndZoom(bRecenter,result)
{

    if(bRecenter)
    {
        //Check if there is any visible pushpin on map.
        var cnt = 0;
        bounds = new GLatLngBounds();
        var objIgnore = document.getElementById('chkIgnoreZero');
        var bIgnore = false;
        if(objIgnore!=null)
        {
           bIgnore  = objIgnore.checked;
        }    
        bIgnore = result.IgnoreZeroLatLngs;
        for(var i=0;i<markers.getLength();i++)
        {
            var ignoremarker = false;
            if(bIgnore)
            {
                var point1 = markers.markers[i].getPoint();
                if((point1.x==0) && (point1.y==0))
                {
                    ignoremarker = true;
                }
            }
            if(!ignoremarker)
            {
                if(!markers.markers[i].isHidden())
                {
                    bounds.extend(markers.markers[i].getPoint());
                    //Increment visible pushpin count
                    cnt++;
                }
            }
            
        }
        var iZoomLevel = map.getBoundsZoomLevel(bounds);        
        var point = bounds.getCenter();
        
        if(iZoomLevel>14)
        {
            iZoomLevel = 14;
        }
        
        if(cnt<=0)
        {
        
            //alert("IF");
            point = new GLatLng(result.CenterPoint.Latitude,result.CenterPoint.Longitude);
            iZoomLevel =result.ZoomLevel;
        }        
        else        //Add by Girish Sarvaiya
        {
            //alert("else");
            //alert(result.CenterPoint.Latitude);
            //point = new GLatLng(55.429013,-1.933594);
            //point = new GLatLng(53.421150,0.501252);
            //alert(document.getElementById('dnn_ctr371_ViewEvents_hidLat').value);
            //alert(document.getElementById('dnn_ctr371_ViewEvents_hidLng').value);
            
            //Live ID
            point = new GLatLng(document.getElementById('dnn_ctr419_ViewEvents_hidLat').value, document.getElementById('dnn_ctr419_ViewEvents_hidLng').value);

            //Local ID
            //alert(document.getElementById('dnn_ctr371_ViewEvents_hidLat').value);
            
//            if (document.getElementById('dnn_ctr371_ViewEvents_hidLat').value != null)
//                point = new GLatLng(document.getElementById('dnn_ctr371_ViewEvents_hidLat').value, document.getElementById('dnn_ctr371_ViewEvents_hidLng').value);
//            else
//                point = new GLatLng(53.421150, 0.501252);
                
            iZoomLevel =result.ZoomLevel;
        }
        map.setZoom(iZoomLevel);        
        map.setCenter(point);
    }
}
function endRequestHandler(sender, args)
{

    GService.GetOptimizedGoogleObject(fGetGoogleObjectOptimized);
}
function pageLoad()
{
    //alert("Page Load");
    if(!Sys.WebForms.PageRequestManager.getInstance().get_isInAsyncPostBack())
        Sys.WebForms.PageRequestManager.getInstance().add_endRequest(endRequestHandler);
}

