﻿Type.registerNamespace("GIRO.Web.Mapping.VirtualEarth");

GIRO.Web.Mapping.VirtualEarth.VEMap = function(element) {
   GIRO.Web.Mapping.VirtualEarth.VEMap.initializeBase(this, [element]);

   this._clientStateField=null;
   this._IncreaseLargestMapViewOnChangeView=false;
   this._LargestMapView=null;
   this._MinPushpinZoomLevel=1;
   this._OnChangeViewHooked=false;
   this._OnContextMenuHooked=false;
   this._OnPushpinMouseOverHooked=false;   
   this._PushpinIcons=new Object();
   this._Show3DNavigationControl=true;
   this._ScaleBarDistanceUnit=VEDistanceUnit.Miles;
   this._ZoomOnAll=false;
   this._ZoomOnRectangle=null;
   this._onchangemapstyle$delegate=null;
   this._onchangeview$delegate=null;
   this._onclick$delegate=null;
   this._onmousedown$delegate=null;
   this._onmouseover$delegate=null;
   this._onsubmit$delegate=null;

}    

GIRO.Web.Mapping.VirtualEarth.VEMap.prototype = {

   initialize : function() {
      GIRO.Web.Mapping.VirtualEarth.VEMap.callBaseMethod(this, "initialize");

      if(this._Show3DNavigationControl==false){this._Map.Hide3DNavigationControl();}
      if(!isNaN(this._ScaleBarDistanceUnit)){if(this._ScaleBarDistanceUnit==1){this._ScaleBarDistanceUnit=VEDistanceUnit.Miles;}else{this._ScaleBarDistanceUnit=VEDistanceUnit.Kilometers}}
      this._Map.SetScaleBarDistanceUnit(this._ScaleBarDistanceUnit);
      if(this._ZoomOnAll){this._Map.SetMapView(this._Map.pushpins);}
      if(this._ZoomOnRectangle!=null){this._ZoomOnRectangle=Sys.Serialization.JavaScriptSerializer.deserialize(this._ZoomOnRectangle);var tmpRect=new VELatLongRectangle(new VELatLong(this._ZoomOnRectangle.TopLeftLatLong.Latitude,this._ZoomOnRectangle.TopLeftLatLong.Longitude),new VELatLong(this._ZoomOnRectangle.BottomRightLatLong.Latitude,this._ZoomOnRectangle.BottomRightLatLong.Longitude), null, null);this._Map.SetMapView(tmpRect);}

      /*
      // TODO: Fix non-async postback onsubmit
      if(this._onsubmit$delegate==null)this._onsubmit$delegate=Function.createDelegate(this,this._onsubmit);
      if(typeof(Sys.WebForms) !== "undefined" && typeof(Sys.WebForms.PageRequestManager) !== "undefined")
         {
         Array.add(Sys.WebForms.PageRequestManager.getInstance()._onSubmitStatements,this._onsubmit$delegate);
         $addHandler(document.forms[0],"submit",this._onsubmit$delegate);
         }
      else
         {
         $addHandler(document.forms[0],"submit",this._onsubmit$delegate);
         }
      */
      if(this._onchangemapstyle$delegate==null)this._onchangemapstyle$delegate=Function.createDelegate(this,this._onchangemapstyle);
      this._Map.AttachEvent("onchangemapstyle",this._onchangemapstyle$delegate);
      //

      if(this._OnChangeViewHooked)
         {
         if(this._onchangeview$delegate==null)this._onchangeview$delegate=Function.createDelegate(this,this._onchangeview);
         this._Map.AttachEvent("onchangeview",this._onchangeview$delegate);
         }

      if(this._OnPushpinMouseOverHooked)
         {
         if(this._onmouseover$delegate==null)this._onmouseover$delegate=Function.createDelegate(this,this._onmouseover);
         this._Map.AttachEvent("onmouseover",this._onmouseover$delegate);
         }

      if(this._OnContextMenuHooked)
         {
         if(this._onclick$delegate==null)this._onclick$delegate=Function.createDelegate(this,this._onclick);
         this._Map.AttachEvent('onclick',this._onclick$delegate);
         if(this._onmousedown$delegate==null)this._onmousedown$delegate=Function.createDelegate(this,this._onmousedown);
         this._Map.AttachEvent('onmousedown',this._onmousedown$delegate);
         }

      this.loadClientState();

      this.RefreshPushpins();
   },    

   dispose : function() {
      GIRO.Web.Mapping.VirtualEarth.VEMap.callBaseMethod(this,"dispose");
   },

   loadClientState : function() {
      if (this._clientStateField==null)return;
      var value=this._clientStateField.value;
      var payload=Sys.Serialization.JavaScriptSerializer.deserialize(value);
      if(payload!=null)
         {
         this._MapStyle=payload.MapStyle;
         if(!isNaN(this._MapStyle)){if(this._MapStyle==2){this._MapStyle="a";}else if(this._MapStyle==3){this._MapStyle="h";}else if(this._MapStyle==4){this._MapStyle="o";}else{this._MapStyle="r";}}
         this._Map.SetMapStyle(this._MapStyle);
         }
      },

   saveClientState : function() {
      if(this._clientStateField==null)return;
      var mapStyle=this._Map.GetMapStyle();
      if(mapStyle=="a"){mapStyle=2;}else if(mapStyle=="h"){mapStyle=3;}else if(mapStyle=="o"){mapStyle=4;}else{mapStyle=1;}
      var state={MapStyle:mapStyle};
      var payload = Sys.Serialization.JavaScriptSerializer.serialize(state);
      this._clientStateField.value = payload;
      return(payload);
   },

   add_deleteallpushpinscomplete : function(handler) {
      this.get_events().addHandler('deleteallpushpinscomplete',handler);
   },
   remove_deleteallpushpinscomplete : function(handler) {
      this.get_events().removeHandler('deleteallpushpinscomplete',handler);
   },
   
   get_clientStateField:function(){return this._clientStateField;},
   set_clientStateField:function(value){this._clientStateField = value;},
   get_IncreaseLargestMapViewOnChangeView:function(){return this._IncreaseLargestMapViewOnChangeView;},
   set_IncreaseLargestMapViewOnChangeView:function(value){this._IncreaseLargestMapViewOnChangeView = value;},
   get_MinPushpinZoomLevel:function(){return this._MinPushpinZoomLevel;},
   set_MinPushpinZoomLevel:function(value){this._MinPushpinZoomLevel = value;},
   get_OnChangeViewHooked:function(){return this._OnChangeViewHooked;},
   set_OnChangeViewHooked:function(value){this._OnChangeViewHooked = value;},
   get_OnContextMenuHooked:function(){return this._OnContextMenuHooked;},
   set_OnContextMenuHooked:function(value){this._OnContextMenuHooked = value;},
   get_OnPushpinMouseOverHooked:function(){return this._OnPushpinMouseOverHooked;},
   set_OnPushpinMouseOverHooked:function(value){this._OnPushpinMouseOverHooked = value;},
   get_ScaleBarDistanceUnit:function(){return this._ScaleBarDistanceUnit;},
   set_ScaleBarDistanceUnit:function(value){this._ScaleBarDistanceUnit = value;},
   get_Show3DNavigationControl:function(){return this._Show3DNavigationControl;},
   set_Show3DNavigationControl:function(value){this._Show3DNavigationControl = value;},
   get_ZoomOnAll:function(){return this._ZoomOnAll;},
   set_ZoomOnAll:function(value){this._ZoomOnAll = value;},
   get_ZoomOnRectangle:function(){return this._ZoomOnRectangle;},
   set_ZoomOnRectangle:function(value){this._ZoomOnRectangle = value;},

   get_PushpinIcon:function(pushpinId){if($get(pushpinId)!=null)return($get(pushpinId).getElementsByTagName("img")[0]);else return(null);},   

   GetPushpinLatLong : function(pushpinId) {
      for(i in this._Map.pushpins)
         if(this._Map.pushpins[i].ID==pushpinId)return(this._Map.pushpins[i].LatLong);
      return(null);      
   },
   
   HideContextMenu : function() {
      this._hideContextMenu();
   },

   _getContextMenu : function() {
      return(document.getElementById(this.get_id()+'_ContextMenu'));
   },
   
   _getContextMenuPoint : function() {
      return(document.getElementById(this.get_id()+'_ContextMenuPoint'));
   },
   
   _hideContextMenu : function() {
      this._getContextMenuPoint().style.display='none';
      this._getContextMenu().style.display='none';
   },

   _makePostBack : function(arg) {
      __doPostBack(this.get_id(),arg);
   },

   _onchangemapstyle : function(e) {
      this.saveClientState();
   },

   _onchangeview : function(e) {
      this._hideContextMenu();
      if(this._Map.GetZoomLevel()<this._MinPushpinZoomLevel)
         {
         if(this._LargestMapView!=null)
            {
            this._LargestMapView=null;
            this._Map.DeleteAllPushpins();
            }
         
         this.ondeleteallpushpinscomplete(Sys.EventArgs.Empty);
         return;
         }

      var mapView=this._Map.GetMapView();
      if (this._LargestMapView!=null&&mapView.TopLeftLatLong.Latitude<=this._LargestMapView.TopLeftLatLong.Latitude&&mapView.TopLeftLatLong.Longitude>=this._LargestMapView.TopLeftLatLong.Longitude&&mapView.BottomRightLatLong.Latitude>=this._LargestMapView.BottomRightLatLong.Latitude&&mapView.BottomRightLatLong.Longitude<=this._LargestMapView.BottomRightLatLong.Longitude)
         {
         return;
         }

      if (this._IncreaseLargestMapViewOnChangeView&&this._LargestMapView!=null)
         {
         if (mapView.TopLeftLatLong.Latitude>this._LargestMapView.TopLeftLatLong.Latitude)
            this._LargestMapView.TopLeftLatLong.Latitude=mapView.TopLeftLatLong.Latitude;
         if (mapView.TopLeftLatLong.Longitude<this._LargestMapView.TopLeftLatLong.Longitude)
            this._LargestMapView.TopLeftLatLong.Longitude=mapView.TopLeftLatLong.Longitude;
         if (mapView.BottomRightLatLong.Latitude<this._LargestMapView.BottomRightLatLong.Latitude)
            this._LargestMapView.BottomRightLatLong.Latitude=mapView.BottomRightLatLong.Latitude;
         if (mapView.BottomRightLatLong.Longitude>this._LargestMapView.BottomRightLatLong.Longitude)
            this._LargestMapView.BottomRightLatLong.Longitude=mapView.BottomRightLatLong.Longitude;
         }
      else
         {
         this._LargestMapView=mapView;
         }

      var mapData=new Object();
      mapData.EventName='ChangeView';
      mapData.CurrentMapView=mapView;
      mapData.LargestMapView=this._LargestMapView;

      var arg=Sys.Serialization.JavaScriptSerializer.serialize(mapData);
      this._makePostBack(arg);
   },

   _onclick : function(e) {
      if(e.rightMouseButton)
         {
         this._oncontextmenu(e);
         }
   },

   _oncontextmenu : function(e) {
      var mapData=new Object();
      mapData.EventName='ContextMenu';
      mapData.LatLong=this._Map.PixelToLatLong(new VEPixel(e.mapX,e.mapY));
      var arg = Sys.Serialization.JavaScriptSerializer.serialize(mapData);
      this._makePostBack(arg);
   },

   _onmousedown : function(e) {
      this._hideContextMenu();
   },
      
   ondeleteallpushpinscomplete : function(e) {
      var handler=this.get_events().getHandler('deleteallpushpinscomplete');
      if (handler!=null)handler(this,e);
   },
    
   _onmouseover : function(e) {
      // this._hideContextMenu();
      if (e.elementID)
         {
         var pin=this._Map.GetShapeByID(e.elementID);
         pin.SetTitle('Loading...');
         pin.SetDescription('Loading...');

         var mapData=new Object();
         mapData.EventName='PushpinMouseOver';
         mapData.CurrentMapView=this._Map.GetMapView();
         mapData.LargestMapView=this._LargestMapView;
         mapData.PushpinId=pin.InternalId;
         mapData.ShapeId=pin.GetID();

         var arg=Sys.Serialization.JavaScriptSerializer.serialize(mapData);
         this._makePostBack(arg);
         return(true);
         }
   },

   _onsubmit : function() {
      this.saveClientState();
      return(true);
   },

   _showContextMenu : function(latLong,innerHtml) {
      var pixel = this._Map.LatLongToPixel(latLong);
      var contextMenuPoint=this._getContextMenuPoint();
      var contextMenu=this._getContextMenu();
      contextMenuPoint.style.display='block';
      contextMenuPoint.style.left=pixel.x-5+'px';
      contextMenuPoint.style.top=pixel.y-5+'px';
      contextMenu.style.display='block';
      contextMenu.style.left=pixel.x+10+'px';
      contextMenu.style.top=pixel.y+'px';
      contextMenu.innerHTML=innerHtml;
   },
         
   update : function(e) {
      if (e!=null)
         {
         var data = Sys.Serialization.JavaScriptSerializer.deserialize(e);
         if (data.ShapeId)
            {
            var pushpinData=data;
            var pin=this._Map.GetShapeByID(pushpinData.ShapeId);
            pin.SetTitle(pushpinData.Title);
            pin.SetDescription(pushpinData.Detail);
            this._Map.ShowInfoBox(pin);
            e=null;
            }
         else if (data.LatLong)
            {
            var contextMenuData=data;
            var latLong = new VELatLong(contextMenuData.LatLong.Latitude, contextMenuData.LatLong.Longitude);
            this._showContextMenu(latLong,contextMenuData.Html);
            e=null;
            }
         }

      var baseArguments=new Array();baseArguments.push(e);
      GIRO.Web.Mapping.VirtualEarth.VEMap.callBaseMethod(this,"update",baseArguments);

      // this.loadClientState();
   },
      
   RefreshPushpins : function() {
      if(this._OnChangeViewHooked)this._onchangeview(null);
   },
   
   ZoomOn : function(topLeftLongitude, topLeftLatitude, bottomRightLongitude, bottomRightLatitude) {
      this.get_Map().SetMapView(new VELatLongRectangle(new VELatLong(topLeftLatitude,topLeftLongitude),new VELatLong(bottomRightLatitude,bottomRightLongitude)));
   }
   
}   

GIRO.Web.Mapping.VirtualEarth.VEMap.registerClass("GIRO.Web.Mapping.VirtualEarth.VEMap", PietschSoft.VE.Map);

if(typeof(Sys)!=='undefined')Sys.Application.notifyScriptLoaded();