﻿
//sign up google map: http://code.google.com/apis/maps/signup.html 
//sign up google ajax search: http://code.google.com/apis/ajaxsearch/signup.html

/*****************************************   Google Map Common Functions   *****************************************/          
var map = null;
var geocoder = null;
var points = new Array ();
 zooms = new Array();
            zooms[0] = 156367.87;
            zooms[1] = 78183.93;
            zooms[2] = 39091.97;
            zooms[3] = 19545.98;
            zooms[4] = 9772.99;
            zooms[5] = 4886.50;
            zooms[6] = 2443.25;
            zooms[7] = 1221.62;
            zooms[8] = 610.81;
            zooms[9] = 305.41;
            zooms[10] = 152.70;
            zooms[11] = 76.35;
            zooms[12] = 38.18;
            zooms[13] = 19.09;
            zooms[14] = 9.54;
            zooms[15] = 4.77;
            zooms[16] = 2.39;
            zooms[17] = 1.19;
        
        zooms2 = new Array();
            zooms2[25] = 10;
            zooms2[20] = 10;
            zooms2[15] = 11;
            zooms2[10] = 11;
            zooms2[7] = 12;
            zooms2[5] = 12;
            zooms2[3] = 12;
            zooms2[1] = 13;
            zooms2[0] = 12;
            
            
//               1. centerLatitude = minLatitude + (maxLatitude – minLatitude) / 2  
//   2. centerLongitude = minLongitude + (maxLongitude – minLongitude) / 2  
/**    load static address on the map   ***/
function load() {
  if (GBrowserIsCompatible()) {
     map = new GMap2(document.getElementById("map"));
     map.addControl(new GOverviewMapControl());
     map.enableDoubleClickZoom();
     map.enableScrollWheelZoom();
     map.addControl(new GMapTypeControl());
     map.addControl(new GSmallMapControl());
     map.setCenter(new GLatLng(39.639538, -101.250000), 4);  //usa map
     geocoder = new GClientGeocoder();
  }
}
  

function initalMap(){
    if (GBrowserIsCompatible())  {
        map = new GMap2(document.getElementById("map"));
        //map.addControl(new GSmallMapControl());  // zoom/move  control with no scale
        map.addControl(new GLargeMapControl ());  // zoom/move control with scale
        //map.addControl(new GLargeMapControl3D ());//  zoom/move control with scale  - 3 D look
        //map.addControl(new GSmallZoomControl ());  //  only +-   zoom control
        map.addControl(new GMapTypeControl());
        map.addControl(new GScaleControl());   // scale of map vs screen
    }
}
    

function fitMap(){
       var bounds = new GLatLngBounds();
       for (var i=0; i< points.length; i++) {
          bounds.extend(points[i]);         
       }
       map.setZoom(map.getBoundsZoomLevel(bounds));
       map.setCenter(bounds.getCenter());
}
     
var bounds = new GLatLngBounds();  
function showAddressWithInfoIcon(_addr, _info, _icon) {    
  var point;
  if (geocoder) {
    geocoder.getLatLng(_addr,
      function(point) {
        if (!point) {
         // alert(_addr + "      *************************   " + _info + " not found");
        } 
        else {        
            bounds.extend(point); //add points to the bound                
            map.setCenter(bounds.getCenter());  //set auto cetner to all points     
          if (_info.length != "")     {  
              var marker = new GMarker(point, _icon);
              map.addOverlay(marker);  
              GEvent.addListener(marker, "click",  
                  function() { marker.openInfoWindowHtml(_info);
                });
          }
          else {              
              var marker = new GMarker(point);
              map.addOverlay(marker); 
          }   
            map.setZoom(map.getBoundsZoomLevel(bounds));  //set auto zoop to all points                   
        } //end else for exist point
      }
    );
  }
}    


    

  
  /**   load given address on the map   **/
  function loadFromAddress(_address) {
  if (GBrowserIsCompatible()) {
     map = new GMap2(document.getElementById("map"));
     map.addControl(new GSmallMapControl());
     map.addControl(new GMapTypeControl());
     geocoder = new GClientGeocoder();
     showAddress(_address);
  }
}
        
        
    function showAddress(address, index) {
    
      if (geocoder) {
        geocoder.getLatLng(
          address,
          function(point) {
            if (!point) {
            //  alert(address + " not found");
            } 
            else {
              map.setCenter(point, 17);
              var marker = new GMarker(point);
              map.addOverlay(marker);              
            GEvent.addListener(marker, "click", function() {
                marker.openInfoWindowHtml(address);
            });
              points[index] = point;
              if (addresses.length > 1)
                 map.setZoom(9);
              else
                marker.openInfoWindowHtml(address);
              
            }
          }
        );
      }
    }
    
    function showAddressInfo(address, index, address_info) {
    
      if (geocoder) {
        geocoder.getLatLng(
          address,
          function(point) {
            if (!point) {
              //alert(address + " not found");
            } 
            else {
              map.setCenter(point, 17);
              var marker = new GMarker(point);
              map.addOverlay(marker);              
            GEvent.addListener(marker, "click", function() {
                marker.openInfoWindowHtml(address_info);
            });
              points[index] = point;
              if (addresses.length > 1)
                 map.setZoom(8);
              else
                marker.openInfoWindowHtml(address_info);
              
            }
          }
        );
      }
    }
    
 

/**   load multiple address in the string array  to the map **/    
  function loadFromMultipleAddressWithInfo(_addresses, _address_info) {  
      if (GBrowserIsCompatible()) {
        addresses = _addresses;
        addresses_info = _address_info;
        points = new Array(addresses.length);
        map = new GMap2(document.getElementById("map"));
        //map.addControl(new GSmallMapControl());  // zoom/move  control with no scale
        map.addControl(new GLargeMapControl ());  // zoom/move control with scale
        //map.addControl(new GLargeMapControl3D ());//  zoom/move control with scale  - 3 D look
        //map.addControl(new GSmallZoomControl ());  //  only +-   zoom control
        map.addControl(new GMapTypeControl());
        map.addControl(new GScaleControl());   // scale of map vs screen
      //  map.addControl(new GNavLabelControl ());
       // map.addControl(new GSmallZoomControl ());
        map.setCenter(new GLatLng(42.8487, -73.755), 13);
        geocoder = new GClientGeocoder();
        
        for ( x = 0 ; x < addresses.length  ; x++ )  
            showAddressInfo(addresses[x], x, addresses_info[x]);
      }
}     


        
        
function loadFromAddressCity(address, city) {
  if (GBrowserIsCompatible()) {
     map = new GMap2(document.getElementById("map"));
    map.addControl(new GSmallMapControl());
    map.addControl(new GMapTypeControl());
    map.setCenter(new GLatLng(42.8487, -73.755), 13);
    geocoder = new GClientGeocoder();
    showAddressCity(address, city);
   // showAddressCity("1431 greenway dr", "irving, tx");
  }
} 
    
function showAddressCity(addr, city) {
  var address = addr + ", " + city;
  if (geocoder) {
    geocoder.getLatLng(
      address,
      function(point) {
        if (!point) {
          alert(address + " not found");
        } 
        else {
          map.setCenter(point, 17);
          var marker = new GMarker(point);
          map.addOverlay(marker);
          marker.openInfoWindowHtml(addr +"<br />" + city);
        } //end of for have point
      } //function point
    );  
  }//end of if
}




function loadDriveDirection(from_Address, to_Address){
    var div_directions = document.getElementById("directions");
    gdir = new GDirections(map, document.getElementById("directions"));   
    GEvent.addListener(gdir, "error", handleDirectionErrors);
    fromAddress = from_Address;
    toAddress = to_Address ;
    
    if (fromAddress.length > 0 && toAddress.length > 0)  {
        div_directions.innerHTML = "";
        gdir.load("from: " + fromAddress + " to: " + toAddress);
    }
}
function handleDirectionErrors() {
    if (gdir.getStatus().code == G_GEO_UNKNOWN_ADDRESS)
        alert("No corresponding geographic location could be found for one of the specified addresses. This may be due to the fact that the address is relatively new, or it may be incorrect.\nError code: " + gdir.getStatus().code);
    else if (gdir.getStatus().code == G_GEO_SERVER_ERROR)
        alert("A geocoding or directions request could not be successfully processed, yet the exact reason for the failure is not known.\n Error code: " + gdir.getStatus().code);
    else if (gdir.getStatus().code == G_GEO_MISSING_QUERY)
        alert("The HTTP q parameter was either missing or had no value. For geocoder requests, this means that an empty address was specified as input. For directions requests, this means that no query was specified in the input.\n Error code: " + gdir.getStatus().code);
    // else if (gdir.getStatus().code == G_UNAVAILABLE_ADDRESS) <--- Doc bug... this is either not defined, or Doc is wrong
    // alert("The geocode for the given address or the route for the given directions query cannot be returned due to legal or contractual reasons.\n Error code: " + gdir.getStatus().code);
    else if (gdir.getStatus().code == G_GEO_BAD_KEY)
        alert("The given key is either invalid or does not match the domain for which it was given. \n Error code: " + gdir.getStatus().code);
    else if (gdir.getStatus().code == G_GEO_BAD_REQUEST)
        alert("A directions request could not be successfully parsed.\n Error code: " + gdir.getStatus().code);
    else 
        alert("An unknown error occurred.");
}


/******************************** map.aspx  **************************************/   

function mapDoctor(physician_id)
{ 
    var disp_setting="toolbar=yes,location=no,directories=yes,menubar=yes,resizable=yes,"; 
    disp_setting+="scrollbars=yes,width=840, height=620, left=50, top=25"; 
    var docprint=window.open("map.aspx?id=" + physician_id,"",disp_setting); 
}
function mapDoctorAddr(physician_id, address, city)
{ 
    var disp_setting="toolbar=yes,location=no,directories=yes,menubar=yes,resizable=yes,"; 
    disp_setting+="scrollbars=yes,width=840, height=620, left=50, top=25"; 
    var docprint=window.open("map.aspx?id=" + physician_id + "&address=" + address +"&city=" + city,"",disp_setting); 
}



/******************************** map.aspx  **************************************/   
var addresses = new Array();
var addresses_info = new Array();

function loadFromMultipleAddress(_addresses) {  
  if (GBrowserIsCompatible()) {
    addresses = _addresses;
    points = new Array(addresses.length);
    map = new GMap2(document.getElementById("map"));
    //map.addControl(new GSmallMapControl());  // zoom/move  control with no scale
    map.addControl(new GLargeMapControl ());  // zoom/move control with scale
    //map.addControl(new GLargeMapControl3D ());//  zoom/move control with scale  - 3 D look
    //map.addControl(new GSmallZoomControl ());  //  only +-   zoom control
    map.addControl(new GMapTypeControl());
    map.addControl(new GScaleControl());   // scale of map vs screen
  //  map.addControl(new GNavLabelControl ());
   // map.addControl(new GSmallZoomControl ());
    geocoder = new GClientGeocoder();
    
    for ( x = 0 ; x < addresses.length  ; x++ ) 
        showAddress(addresses[x], x);
  } //end of if
}     


/******************************** Region.aspx Map   **************************************/
var basicIcon = new GIcon();
var featuredIcon = new GIcon();
var exclusiveIcon = new GIcon();

function createFeaturedMarker(){
    //featuredIcon = new GIcon(G_DEFAULT_ICON);
    featuredIcon.image = "./images/FZ-map-marker.png";    
    featuredIcon.iconSize = new GSize(20, 34);;
    //baseIcon.shadow = "http://www.google.com/mapfiles/shadow50.png";
    featuredIcon.shadowSize = new GSize(37, 34);
    featuredIcon.iconAnchor = new GPoint(9, 34);
    featuredIcon.infoWindowAnchor = new GPoint(9, 2);
}
function createExclusiveMarker(){
    //featuredIcon = new GIcon(G_DEFAULT_ICON);
    exclusiveIcon.image = "./images/FZ-map-marker-exclusive.png";    
    exclusiveIcon.iconSize = new GSize(40, 55);;
    //baseIcon.shadow = "http://www.google.com/mapfiles/shadow50.png";
    exclusiveIcon.shadowSize = new GSize(37, 34);
    exclusiveIcon.iconAnchor = new GPoint(9, 34);
    exclusiveIcon.infoWindowAnchor = new GPoint(9, 2);
}
function createBasicMarker(){
    basicIcon.image = "./images/FZ-map-dot.png";
    basicIcon.iconSize = new GSize(16, 16);
    basicIcon.iconAnchor = new GPoint(9, 34);
    basicIcon.infoWindowAnchor = new GPoint(9, 2);
}

function loadRegionPhysicians  (_physician_addr, _physician_info, _physician_pack) {  
      createBasicMarker();
      createFeaturedMarker();
      createExclusiveMarker();
      if (GBrowserIsCompatible()) {
        map = new GMap2(document.getElementById("map"));
        //map.addControl(new GSmallMapControl());  // zoom/move  control with no scale
        map.addControl(new GLargeMapControl ());  // zoom/move control with scale
        //map.addControl(new GLargeMapControl3D ());//  zoom/move control with scale  - 3 D look
        //map.addControl(new GSmallZoomControl ());  //  only +-   zoom control
        map.addControl(new GMapTypeControl());
        map.addControl(new GScaleControl());   // scale of map vs screen
      //  map.addControl(new GNavLabelControl ());
       // map.addControl(new GSmallZoomControl ());
        geocoder = new GClientGeocoder();
        
        for ( x = 0 ; x < _physician_addr.length  ; x++ )  {
            if (_physician_pack[x] == "Featured")
                showAddressWithInfoIcon(_physician_addr[x], _physician_info[x], featuredIcon);
            else if (_physician_pack[x] == "Exclusive")
                showAddressWithInfoIcon(_physician_addr[x], _physician_info[x], exclusiveIcon);
            else
                showAddressWithInfoIcon(_physician_addr[x], _physician_info[x], basicIcon);
         }  
      } //end of if
}     

function loadRegionAllPhysicians(_featured_addr, _featured_info, _exclusive_addr, _exclusive_info, _basic_addr, _basic_info) {  
      createBasicMarker();
      createFeaturedMarker();
      createExclusiveMarker();
      if (GBrowserIsCompatible()) {
        map = new GMap2(document.getElementById("map"));
        //map.addControl(new GSmallMapControl());  // zoom/move  control with no scale
        map.addControl(new GLargeMapControl ());  // zoom/move control with scale
        //map.addControl(new GLargeMapControl3D ());//  zoom/move control with scale  - 3 D look
        //map.addControl(new GSmallZoomControl ());  //  only +-   zoom control
        map.addControl(new GMapTypeControl());
        map.addControl(new GScaleControl());   // scale of map vs screen
      //  map.addControl(new GNavLabelControl ());
       // map.addControl(new GSmallZoomControl ());
        geocoder = new GClientGeocoder();
        
        for ( x = 0 ; x < _exclusive_addr.length  ; x++ ) 
            showAddressWithInfoIcon(_exclusive_addr[x], _exclusive_info[x], exclusiveIcon);
        for ( x = 0 ; x < _featured_addr.length  ; x++ )  
            showAddressWithInfoIcon(_featured_addr[x], _featured_info[x], featuredIcon);
        for ( x = 0 ; x < _basic_addr.length  ; x++ ) 
            showAddressWithInfoIcon(_basic_addr[x], _basic_info[x], basicIcon);
      } //end of if
}     





/****************************************** gp lead map *******************************************************/

var redIcon = new GIcon();
var blueIcon = new GIcon();
var yellowIcon = new GIcon();
var iconInfo = new Array ();

function createRedDot(){
    redIcon.image = "../images/gp-map-red.jpg";
    redIcon.iconSize = new GSize(20, 34);
    redIcon.iconAnchor = new GPoint(9, 34);
    redIcon.infoWindowAnchor = new GPoint(9, 2);
}
function createBlueDot(){
    blueIcon.image = "../images/gp-map-blue.jpg";
    blueIcon.iconSize = new GSize(40, 55);
    blueIcon.iconAnchor = new GPoint(9, 34);
    blueIcon.infoWindowAnchor = new GPoint(9, 2);
}
function createYellowDot(){
    yellowIcon.image = "../images/gp-map-yellow.jpg";
    yellowIcon.iconSize = new GSize(16, 16);
    yellowIcon.iconAnchor = new GPoint(9, 34);
    yellowIcon.infoWindowAnchor = new GPoint(9, 2);
}


function loadLeadMap(_addr, _info, _icon) {  
      createRedDot();
      createBlueDot();
      createYellowDot();
      if (GBrowserIsCompatible()) {
        map = new GMap2(document.getElementById("map"));
        //map.addControl(new GSmallMapControl());  // zoom/move  control with no scale
        map.addControl(new GLargeMapControl ());  // zoom/move control with scale
        //map.addControl(new GLargeMapControl3D ());//  zoom/move control with scale  - 3 D look
        //map.addControl(new GSmallZoomControl ());  //  only +-   zoom control
        map.addControl(new GMapTypeControl());
        map.addControl(new GScaleControl());   // scale of map vs screen
      //  map.addControl(new GNavLabelControl ());
       // map.addControl(new GSmallZoomControl ());
        geocoder = new GClientGeocoder();
        
        for ( x = 0 ; x < _addr.length  ; x++ )  {
            if (_icon[x] == "Yellow")
                showAddressWithInfoIcon(_addr[x], _info[x], yellowIcon);
            else if (_icon[x] == "Blue")
                showAddressWithInfoIcon(_addr[x], _info[x], blueIcon);
            else
                showAddressWithInfoIcon(_addr[x], _info[x], redIcon);
         }      
    } //end of if 
}       


