﻿var geocoder;
var map;
var leadMarker;
var eIcon;
var lIcon;
var doCenter = 0;
var googleMap;
var googleClubAddress;
var googleClubName;


function GetAddressFromGoogle(lat,lng,clubAdrs,clubNm)
{
    point = new GLatLng(lat,lng);
    marker = createMarker(point, clubAdrs, '<b>Equinox ' + clubNm + '</b>');
    googleMap = new GMap2($('googleMap'), { size: new GSize(750,390) });
    googleMap.setCenter(point, 14);
    googleMap.addControl(new GHierarchicalMapTypeControl ());
    googleMap.addControl(new GOverviewMapControl ());
    googleMap.addControl( new GSmallZoomControl() );
    googleMap.addOverlay(marker);         
    
    marker.openInfoWindowHtml( googleHtmls[0] );
    
    //plot all of the other clubs
    PlotClubPoints();
    

}

function PlotClubPoints()
{
    for ( i = 0; i < plots.length ; i ++ )
    {
        try
        {       
            clubName = plots[i][0] ? plots[i][0] : '';
            googleAddress = plots[i][1] ? plots[i][1] : ''; 
            lat = plots[i][3] ? plots[i][3] : '';
            lng = plots[i][4] ? plots[i][4] : '';
            
            clubId = plots[i][2];
            
            if ( clubName.length > 0 && googleAddress.length > 0
                && lat.length > 0 && lng.length > 0)    
            {    
                var point = new GLatLng(lat, lng)    
                    
                    //alert ('test');
                var marker = createMarker(point, googleAddress, '<b><a href="/Clubs/ClubTour.aspx?clubid=' + clubId + '" style="color:blue">Equinox ' + clubName + '</a></b>'); 
                googleMap.addOverlay(marker);
                 
            }             
        }
        catch (e)
        {
        
        }
        
    }

}


function GetAddressFromGoogle_callback( res )
{
    var clubName;
    var clubAddress;
    
    if ( res.Status.code == '200' )
    {
        Placemark = res.Placemark[0];
        AddressDetails = Placemark.AddressDetails;
        
        lat = Placemark.Point.coordinates[1];
        lng = Placemark.Point.coordinates[0];  
        
        point = new GLatLng(lat, lng)    
            
        marker = createMarker(point, googleClubAddress, '<b>Equinox ' + googleClubName + '</b>'); 
        
        googleMap = new GMap2($('googleMap'), { size: new GSize(750,390) });
        googleMap.setCenter(point, 14);
        googleMap.addControl(new GHierarchicalMapTypeControl ());
        googleMap.addControl(new GOverviewMapControl ());
        googleMap.addControl( new GSmallZoomControl() );
        googleMap.addOverlay(marker);         
        
       // alert ('googleHtmls[0]');
       // alert (googleHtmls[0]);
        marker.openInfoWindowHtml( googleHtmls[0] );    
    }     
    

    
    //plot all of the other clubs
    for ( i = 0; i < plots.length ; i ++ )
    {
        try
        {       
            clubName = plots[i][0];
            clubAddress = plots[i][1]; 
            if ( clubAddress && clubAddress.length > 0 )        
                geocoder.getLocations(clubAddress,  
                       getCallBackFor(plots[i])             
                
                 );        
        }
        catch (e)
        {
        
        }
        
    }
}


function getCallBackFor(club){
return (function(data, response){
getLocationsCallback(club, data, response);
});
} 


function getLocationsCallback(club, res, data)
{
    
    
    if ( res.Status.code == '200' )
    {
        var Placemark = res.Placemark[0];
        var AddressDetails = Placemark.AddressDetails;       
        
        
        var address = Placemark.address;
        var name = club[0]; 
        var clubid = club[2];
        
        var lat = Placemark.Point.coordinates[1];
        var lng = Placemark.Point.coordinates[0];
        
        var point = new GLatLng(lat, lng)    
            
        var marker = createMarker(point, address, '<b><a href="/Clubs/ClubTour.aspx?clubid=' + clubid + '" style="color:blue">Equinox ' + name + '</a></b>'); 
        
       // alert('marker');
       // alert(marker);
        
        googleMap.addOverlay(marker);   
    }     
        
}

  // this variable will collect the html which will eventually be placed in the side_bar
  var side_bar_html = "";

  // arrays to hold copies of the markers and html used by the side_bar
  // because the function closure trick doesnt work there
  var gmarkers = [];
  var googleHtmls = [];
  var googleHtmlIdx = 0;
  // arrays to hold variants of the info window html with get direction forms open
  var to_htmls = [];
  var from_htmls = [];

  // A function to create the marker and set up the event window
  function createMarker(point, address, html) 
  {
  
    var iconMole = new GIcon(); 
    iconMole.image = '/favicon.ico';
    iconMole.shadowSize = new GSize(36, 42);
    iconMole.iconAnchor = new GPoint(6, 20);
    iconMole.infoWindowAnchor = new GPoint(5, 1);
    
  
    //alert ( 'createMarker' );
    var marker = new GMarker(point, {icon:iconMole});
    
    // The info window version with the "to here" form open
    to_htmls[googleHtmlIdx] = '<span style="font-family:Arial; font-size:13px; color:black">' + html + 
                    '<br>Directions: <b>To here</b> - <a href="javascript:fromhere(' + googleHtmlIdx + ')" style="color:blue">From here</a>' +
                    '<br>Start address:<form action="http://maps.google.com/maps" method="get" target="_blank">' +
                    '<input type="text" SIZE=40 MAXLENGTH=40 name="saddr" id="saddr" value="" /><br>' +
                    '<INPUT value="Get Directions" TYPE="SUBMIT">' +
                    '<input type="hidden" name="daddr" value="' + address + 
                    '"/></span>';
       
    // The info window version with the "to here" form open
    from_htmls[googleHtmlIdx] = '<span style="font-family:Arial; font-size:13px; color:black">' + html + 
                    '<br>Directions: <a href="javascript:tohere(' + googleHtmlIdx + ')" style="color:blue">To here</a> - <b>From here</b>' +
                    '<br>End address:<form action="http://maps.google.com/maps" method="get"" target="_blank">' +
                    '<input type="text" SIZE=40 MAXLENGTH=40 name="daddr" id="daddr" value="" /><br>' +
                    '<INPUT value="Get Directions" TYPE="SUBMIT">' +
                    '<input type="hidden" name="saddr" value="' + address +
                    '"/></span>';
                    
    // The inactive version of the direction info
    html += '<br>Directions: <a href="javascript:tohere('+googleHtmlIdx+')" style="color:blue">To here</a> - <a href="javascript:fromhere('+googleHtmlIdx+')" style="color:blue">From here</a>';


    GEvent.addListener(marker, "click", function() {
      marker.openInfoWindowHtml('<span style="font-family:Arial; font-size:13px; color:black">' + html + '</span>');
    });
    
    // save the info we need to use later for the side_bar
    gmarkers[googleHtmlIdx] = marker;
    googleHtmls[googleHtmlIdx] = '<span style="font-family:Arial; font-size:13px; color:black">' + html + '</span>';
    
    // add a line to the side_bar html
    side_bar_html += '<a href="javascript:myclick(' + googleHtmlIdx + ')">' + name + '</a><br>';
    googleHtmlIdx++;
    
    return marker;
  }


  // This function picks up the click and opens the corresponding info window
  function myclick(i) {
    gmarkers[i].openInfoWindowHtml('<span style="font-family:Arial; font-size:13px; color:black">' + htmls[i] + '</span>');
  }

  // functions that open the directions forms
  function tohere(i) {
    gmarkers[i].openInfoWindowHtml('<span style="font-family:Arial; font-size:13px; color:black">' + to_htmls[i] + '</span>');
  }
  function fromhere(i) {
    gmarkers[i].openInfoWindowHtml('<span style="font-family:Arial; font-size:13px; color:black">' + from_htmls[i] + '</span>');
  }
      
      
//instantiage the geoCoder
geocoder = new GClientGeocoder();
