﻿var bingMap = null;

         var bingShapeLA = null;
         var bingMapLevel=null;

         var pinPoint = null;
         var pinPixel = null;
         var bingdragShape=null;
         
         var shapeTitle=null;
         var shapeDescription=null;
         
         
         
         function MouseHandler(e) 
         { 
             if (e.eventName == "onmousedown" && e.elementID != null) 
             { 
                bingdragShape = bingMap.GetShapeByID(e.elementID); 
                return true; 
             }
             else if (e.eventName == "onmouseup") 
             { 
                bingdragShape = null; 
             }
             else if (e.eventName == "onmousemove" && bingdragShape != null) 
             { 
                
                bingMapX = e.mapX; 
                bingMapY = e.mapY; 
                pinPixel = new VEPixel(bingMapX, bingMapY); 
                
                bingShapeLA = bingMap.PixelToLatLong(pinPixel); 
                
                bingdragShape.SetPoints(bingShapeLA); 
                
                if(document.getElementById("ctl00_ContentPlaceHolderRight_txtLongitude")!=null)
                {                             
                    document.getElementById("ctl00_ContentPlaceHolderRight_txtLongitude").value=bingShapeLA.Longitude;
                }
                if(document.getElementById("ctl00_ContentPlaceHolderRight_txtLatitude")!=null)
                {
                    document.getElementById("ctl00_ContentPlaceHolderRight_txtLatitude").value=bingShapeLA.Latitude;
                }
                if(document.getElementById("ctl00_ContentPlaceHolderRight_txtLevel")!=null)
                {
                    document.getElementById("ctl00_ContentPlaceHolderRight_txtLevel").value=bingMap.GetZoomLevel();
                    
                }   

                return true; 
             } 
         } 
         
         function ChangeViewHandler()
         {
            if(document.getElementById("ctl00_ContentPlaceHolderRight_txtLevel")!=null)
            {
                document.getElementById("ctl00_ContentPlaceHolderRight_txtLevel").value=bingMap.GetZoomLevel();
            }
            
            return true;
         } 
         
                  
         function GetMap(bingMapDivName,ISMark,bingMapX,bingMapY,bingMapLevel,bingShapeTitle,bingShapeDescription)
         {
         
                        
            bingMap = new VEMap(bingMapDivName);   
            
            bingShapeLA = new VELatLong(bingMapX, bingMapY);
            bingMapLevel=bingMapLevel;
            
            bingMap.LoadMap(bingShapeLA, bingMapLevel, VEMapStyle.Road, false, VEMapMode.Mode2D, true, 1);
            
            if(!ISMark)
            {            
                shapeTitle=bingShapeTitle;
                shapeDescription=bingShapeDescription;
            }
                
            AddShape(ISMark);
            
            if(ISMark)
            {   
            
                bingMap.AttachEvent("onmousedown",MouseHandler); 
                bingMap.AttachEvent("onmouseup",MouseHandler); 
                bingMap.AttachEvent("onmousemove",MouseHandler); 
                bingMap.AttachEvent("onchangeview",ChangeViewHandler); 
                
                if(document.getElementById("ctl00_ContentPlaceHolderRight_txtLongitude")!=null && bingMapX!=null && bingMapX!="")
                {                             
                    document.getElementById("ctl00_ContentPlaceHolderRight_txtLongitude").value=bingMapX;
                }
                if(document.getElementById("ctl00_ContentPlaceHolderRight_txtLatitude")!=null && bingMapY!=null && bingMapY!= "")
                {
                    document.getElementById("ctl00_ContentPlaceHolderRight_txtLatitude").value=bingMapY;
                }
                if(document.getElementById("ctl00_ContentPlaceHolderRight_txtLevel")!=null &&  bingMapLevel!=null && bingMapLevel!="")
                {
                    document.getElementById("ctl00_ContentPlaceHolderRight_txtLevel").value=bingMapLevel;                    
                } 
                if(document.getElementById("ctl00_ContentPlaceHolderRight_txtTitle")!=null &&  bingShapeTitle!=null && bingShapeTitle!="")
                {
                    document.getElementById("ctl00_ContentPlaceHolderRight_txtTitle").value=bingShapeTitle;                    
                }
                if(document.getElementById("ctl00_ContentPlaceHolderRight_txtDirections")!=null &&  bingShapeDescription!=null && bingShapeDescription!="")
                {
                    document.getElementById("ctl00_ContentPlaceHolderRight_txtDirections").value=bingShapeDescription;                    
                }  
            }
            
            
         }

                
         
         function AddShape(ISMark)
         {
            
            bingShape = new VEShape(VEShapeType.Pushpin,bingShapeLA);
            if(!ISMark)
            {
                if(shapeTitle!=null && shapeTitle!="")
                {
                    bingShape.SetTitle(shapeTitle);
                }
                if(shapeDescription!=null && shapeDescription!="")
                {
                    bingShape.SetDescription(shapeDescription);
                }
            }
            bingMap.AddShape(bingShape); 
         }
