﻿ /*
    1 = Address
    2 = HTML for Windows
    3 = 1 or 0 if is default enterprise
    4 = ID
    5 = Current ID in map
    6 = Title
    7 = HTML Simple
    8 = Distance in Km
  */
    var listAddress      = new Array();
    var address          = new Array();
    var map              = null;
    var mgr;
    var geocoder         = null;
    var position         = 1;
    
    var baseIcon = new GIcon(G_DEFAULT_ICON);
    baseIcon.shadow = "http://www.google.com/mapfiles/shadow50.png";
    baseIcon.iconSize = new GSize(9, 15);
    baseIcon.shadowSize = new GSize(9, 15);
    baseIcon.iconAnchor = new GPoint(9, 15);
    baseIcon.infoWindowAnchor = new GPoint(9, 2);
      
    function load()
    {
      if (GBrowserIsCompatible()) 
      {
        map = new GMap2(document.getElementById("map"));
        map.addControl(new GLargeMapControl());
        map.addControl(new GOverviewMapControl());
        map.setCenter(new GLatLng(latitude, longitude), 7);
      
        geocoder = new GClientGeocoder();
       
        ShowAllAddress();      
       
        GEvent.addListener(map, "moveend", function() {
        var center = map.getCenter();
        });

        map.enableDoubleClickZoom();
        map.setUIToDefault();
        
        mgr = new MarkerManager(map);
       }
    }
    
   function SortedCustom(a, b)
   { return (a[8] - b[8]); }

    
    function ShowDirection(from, to)
    {
      if ( map != null && map != undefined )
      {     
        map.clearOverlays();
        position = 0;
        ShowAllAddress();
        
        var directionsPanel       = document.getElementById("route");
        directionsPanel.innerHTML = "";
        
        var directions = new GDirections(map, directionsPanel);
        directions.clear(); 
        directions.load("from: " + from + " to: " + to, {locale: locale});     
        directionsPanel.style.display = "block";
      }
    }
  
    function CalculateDistanceAllAddress()
    {
      geocoder.getLatLng(listAddress[position][1],
            function(point)
            {            
              if (point)
              {
                var km = 0;
                if ( listAddress[position][3] == "0" )
                {
                  var startPoint = new LatLong(latitude, longitude);
                  var endPoint   = new LatLong(point.lat(), point.lng());
                  km             = LatLong.distHaversine(startPoint, endPoint);
                }                     
                address[address.length++] = new InfoAddress(listAddress[position][1], listAddress[position][2], listAddress[position][3], listAddress[position][4], listAddress[position][5], listAddress[position][6], listAddress[position][7], km);        
              }
              
              position = position + 1; 
              
              if (position <= listAddress.length-1)
              { CalculateDistanceAllAddress(); }
            }
       );
    }
    
   function SortByDistance(a, b)
   {
     var x = a.Km;
     var y = b.Km;
     return ((x < y) ? -1 : ((x > y) ? 1 : 0));
   }
    
   function InfoAddress(address, htmlWindow, isDefault, id, currentId, title, htmlSimple, km)
   {
     this.Address    = address;
     this.HtmlWindow = htmlWindow;
     this.IsDefault  = isDefault;
     this.ID         = id;
     this.CurrentID  = currentId;
     this.Title      = title;
     this.HtmlSimple = htmlSimple;
     this.Km         = parseInt(parseFloat(km));
   }

   function ShowAllAddress()
    {   
      geocoder.getLatLng(listAddress[position][1],
            function(point)
            {            
              if (point)
              {
                var html       = listAddress[position][2];  
                var htmlLeft = "";
                    
                    if ( position % 2 )
                    { htmlLeft += " <div style='border-bottom: 1px solid #d0d0d0; font-size: 11px; width: 100%; float: left;'>"; }
                    else
                    { htmlLeft += " <div class='background-color: #eaf1f9; border-bottom: 1px solid #d0d0d0; float: left; padding: 3px 0px 3px 3px; width: 243px;'>"; }
                                        
                    htmlLeft += "  <div class='img'>";
                     
                    if ( listAddress[position][3] == "1" )
                    { htmlLeft += "   <img style='width: 25px; height: 33px;' alt='' src='Maps/pin/redPin25.png' />"; }
                    else
                    { htmlLeft += "   <img style='width: 25px; height: 34px;' alt='' src='Maps/pin/pin.png' />"; }
                    
                    htmlLeft += "  </div>";
                    htmlLeft += "  <div style='width: 200px; font-size: 11px;'>";
                    htmlLeft += "   <a style='color: #6e6e6e; font-weight: bold; font-size: 11px; text-decoration: none;' onclick=\"javascript:OpenInfoResult('mapItem" + position + "')\" href='#'>" + listAddress[position][6].toUpperCase() + "</a>";
                    htmlLeft += "  </div>";
                                        
                    if ( position % 2 )
                    { htmlLeft += "  <div id='mapItem" + position + "' style='display: none; border-top: 1px solid #d0d0d0; clear: both; width: 100%; font-size: 11px;'>"; }
                    else
                    { htmlLeft += "  <div id='mapItem" + position + "' style='display: none; background-color: #eaf1f9; border-top: 1px solid #d0d0d0;  font-size: 11px;clear: both; width: 100%;'>"; }
                    
                    htmlLeft += listAddress[position][7];                   
                    htmlLeft += "  </div>";           
                    htmlLeft += " </div>";
                    
            //    document.getElementById("fich").innerHTML = document.getElementById("fich").innerHTML + htmlLeft;
                                              
                var numberIcon = new GIcon(baseIcon);
                if (  listAddress[position][3] == "1" )
                { numberIcon.image = "Maps/pin/redPin25.png"; }
                else
                { numberIcon.image = "Maps/pin/pin.png"; }
                                
                markerOptions = { icon:numberIcon };
                
                var marker = new GMarker(point, markerOptions);
                map.addOverlay(marker);
                
                GEvent.addListener(marker, 'click', function() { marker.openInfoWindowHtml(html); } );                                 
              }
              // document.getElementById("numberFich").innerHTML = position + " ";
               position = position + 1; 
              
              if (position <= listAddress.length-1)
              { ShowAllAddress(); }
            }
          );
    }
       
    LatLong.llToRad = function(brng) {
         if (!isNaN(brng)) return brng * Math.PI / 180; 
       
         brng = brng.replace(/[\s]*$/,'');               
         var dir = brng.slice(-1).toUpperCase();         
         if (!/[NSEW]/.test(dir)) return NaN;           
         brng = brng.slice(0,-1);                       
         var dms = brng.split(/[\s:,Â°Âºâ€²\'â€³\"]/);         
         switch (dms.length) {                           
           case 3:                                       
             var deg = dms[0]/1 + dms[1]/60 + dms[2]/3600; break;
           case 2:                                       
             var deg = dms[0]/1 + dms[1]/60; break;
           case 1:                                       
             if (/[NS]/.test(dir)) brng = '0' + brng;   
             var deg = brng.slice(0,3)/1 + brng.slice(3,5)/60 + brng.slice(5)/3600; break;
           default: return NaN;
         }
         if (/[WS]/.test(dir)) deg = -deg;               
         return deg * Math.PI / 180;                     
    }
       
    LatLong.distHaversine = function(p1, p2) {
         var R = 6371;
         var dLat  = p2.lat - p1.lat;
         var dLong = p2.lon - p1.lon;

         var a = Math.sin(dLat/2) * Math.sin(dLat/2) +
                 Math.cos(p1.lat) * Math.cos(p2.lat) * Math.sin(dLong/2) * Math.sin(dLong/2);
         var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
         var d = R * c;
       
         return d;
    }
    
    function LatLong(degLat, degLong) 
    {
      this.lat = LatLong.llToRad(degLat);
      this.lon = LatLong.llToRad(degLong);
    }    
    
    function ShowGoogleMap(value)
    {
      ShowElement("map", value);
      //ShowElement("menuMember", value);
    }
    
    function ShowElement(elementName, value)
    {
      var element = document.getElementById(elementName);
      if ( element )
      {
        if ( value == 1 )
        { element.style.display = "block"; }
        else
        { element.style.display = "none"; }
      }    
    }
