var map = null;
var geocoder = null;

//Map location data lists, populated to match each result found on a postal code search
var addressAry = new Array();
var cityAry = new Array();
var provAry = new Array();
var genderAry = new Array();

function initialize() {
    if (GBrowserIsCompatible()) {
        map = new GMap2(document.getElementById("map"));
        map.setCenter(new GLatLng(12.4419, -81.1419), 4);
        map.addControl(new GSmallMapControl());
        map.disableScrollWheelZoom();

        geocoder = new GClientGeocoder();

    }
}

//this is for the Club Details page to display the proper marker
function placeAddress(lat, lng, zoom) {
    map.setCenter(new google.maps.LatLng(parseFloat(lat), parseFloat(lng)), parseFloat(zoom));
    //map.clearOverlays(); 
    //placeMarkersThree(data);
    var latlng = new GLatLng(parseFloat(lat), parseFloat(lng));
    var marker = new GMarker(latlng);

    map.addOverlay(marker);
}
function showAddress(address) {
    //alert (address);
    // $("#mapClub").addClass("validate[required]");
    if (geocoder) {
        geocoder.getLatLng(
          address,
          function(point) {
              if (!point) {
                  //alert(address + " not found");
              } else {
                  map.setCenter(point, 11);
                  var marker = new GMarker(point);
                  // map.addOverlay(marker);
                  // marker.openInfoWindowHtml(address);

                  var lat = map.getCenter().lat();
                  var lng = map.getCenter().lng();
                  var mapZoom = map.getZoom();
                  var mapBounds = map.getBounds();
                  var southWest = mapBounds.getSouthWest();
                  var northEast = mapBounds.getNorthEast();
                  var swX = southWest.lat();
                  var swY = southWest.lng();
                  var neX = northEast.lat();
                  var neY = northEast.lng();




                  //alert ("lat:"+lat+" lng:"+ lng);
                  //getClubLocations(address);

              }
          }
        );
    }
}



function placeClubMarker(xLat, yLon, clubID, clubName, address, city, prov, gender) {

    var latlng = new GLatLng(parseFloat(xLat), parseFloat(yLon));
    var marker = new GMarker(latlng);
    //map.addOverlay(marker);

    GEvent.addListener(marker, "click", function () {
        //hideStuff();
        //top.location.href="provinceSearch.php?id="+provID;
        //cityMarkers(provID);

        //marker.openInfoWindowHtml("<b>" + String(clubName) + "</b>");
        $("#clubDetailsFromMap").html("<h3>" + clubName + "</h3><p>" + address + "<br />" + city + ", " + prov + "<br />" + gender);

        $("#mapClub").val(clubName);
        
        $("#selectedClubID").val(clubID);
        //$('#mapClub').trigger("onchange");

    });
    map.addOverlay(marker);


    /* this code has been moved to the function.js page /*
    //maybe make this only the first three digits of the postal code
    var cleanPostal = escape(address);

		//url for the loading image
    var ajax_load = "<img src='http://goodlifefitness.netkeepers.com/img/ajax-loader.gif' alt='loading...' />";
		
		//this fetch will populate a drop menu of clubs within 40km
    var loadUrl = "http://goodlifefitness.netkeepers.com/scripts/getClubLocations.html?postal="+cleanPostal;
    $("#clubResponse").html(ajax_load).load(loadUrl, null, function(responseText){ 
    //alert("Response:\n" + responseText);
    //Dave, spit back an array that has lat, lng, clubID, clubName so that we may place markers on the map and populate a drop down
			
			//myselect = document.createElement("clubSelectorDrop2");
    //myselect.text = "newText";
    //myselect.value = "newValue";
			
		});
    */

}

function setSelectedClubData(index, selectedClubAddress, selectedClubCity, selectedClubProv, selectedClubGender) {
    addressAry[index] = selectedClubAddress;
    cityAry[index] = selectedClubCity;
    provAry[index] = selectedClubProv;
    genderAry[index] = selectedClubGender;
}

function moveMaptoCenter() {
    //alert ('Gmap move');
    //set the text below the drop menu
    var clubName = $('#mapClub :selected').text();
    var selectedIndex = $('#mapClub :selected').index();
    selectedIndex --;

    $("#clubDetailsFromMap").html("<h3>" + clubName + "</h3><p>" + addressAry[selectedIndex] +
		"<br />" + cityAry[selectedIndex] + ", " + provAry[selectedIndex] + "<br />" + genderAry[selectedIndex]);


    //split the id of the selected and move the map to centre
    //var selectedClub = $('#mapClub :selected').val();
    var clubArray = selectedClub.split('_');

    map.setCenter(new GLatLng(clubArray[0], clubArray[1]), 12);

    //set the hidden field to equal the club ID
    $("#selectedClubID").val(clubArray[2]);


    //testing
    //alert ('Selected Club '+selectedClub);
}
	

	
	
	
