////////////////////////////////////////////////////////////////////////////////////////
var map;
var g_count = 0;
var g_index = 0;
var g_geocoder = new GClientGeocoder();
////////////////////////////////////////////////////////////////////////////////////////
window.onload = init;
window.onunload = GUnload;
////////////////////////////////////////////////////////////////////////////////////////
document.onkeypress = keyhandler;
function keyhandler(e) {

    var charCode;

    if (e && e.which) {
        charCode = e.which;
    } else if (window.event) {
        e = window.event;
        charCode = e.keyCode;
    }

    if (charCode == 13) {
        var zip = document.getElementById('zip').value;
        var city = document.getElementById('city').value.toLowerCase();
        var state = document.getElementById('state').value.toUpperCase();
        if (zip != '') {
            onClickSearchZip();
        }
        else if (state != '') {
            onClickSearchState();
        }
        else {
            onClickSearchCountry();
        }
        return false;
    }
    return true;
}
////////////////////////////////////////////////////////////////////////////////////////
function init() {
   if (document.aspnetForm.zip.value == ""){
	
    document.getElementById("map").style.width = "643px";
	
    map = new GMap2(document.getElementById("map"));
    map.setMapType(G_NORMAL_MAP);
    map.addControl(new GLargeMapControl());
    map.setCenter(new GLatLng(39.3683, -93.691442), 3);
    document.getElementById('sidebar_location').style.display = "none";
    document.getElementById('zip').focus();
    if (g_loc != '') {
        document.getElementById('state').value = g_loc;
        onClickSearchState();
    }
   }
   else{
	//   alert(document.aspnetForm.zip.value);
	    map = new GMap2(document.getElementById("map"));
       map.setMapType(G_NORMAL_MAP);
	   onClickSearchZip();
   }
}
////////////////////////////////////////////////////////////////////////////////////////
function hideMarkers() {
    document.getElementById("map").style.width = "430px";
    map.checkResize();
    for (id in markers) {
        markers[id].visible = false;
        map.clearOverlays();
    }
}
////////////////////////////////////////////////////////////////////////////////////////
function showMarkers() {
    clearList();
    addCountHeader();
    g_index = 0;
    for (id in markers) {
        if (markers[id].visible == true) {
            createMarker(id);
        }
    }
}
////////////////////////////////////////////////////////////////////////////////////////
function onClickSearchZip() {
    var zip = document.getElementById('zip').value;
	
	//alert("1");
	
    
	if (zip == '') {
        alert('Please enter a zip code');
        document.getElementById('zip').focus();
        return;
    }

  document.getElementById('city').value="";
     
//    alert(zip);
    var addr = zip + ',USA';
    var startZoom = 7;
    var radius = document.getElementById('radiuszip').value;

    if (radius == 5) {
        startZoom = 11;
    }
    else if (radius == 10) {
        startZoom = 9;
    }
    else if (radius == 20) {
        startZoom = 8;
    }
    g_geocoder.getLatLng(addr, function(pointaddr) {
        if (pointaddr) {
            hideMarkers();
            map.setCenter(pointaddr, startZoom);
            g_count = 0;
            for (id in markers) {
                var distance = LatLon.distHaversine(pointaddr.lat(), pointaddr.lng(), markers[id].latitude, markers[id].longitude);
                distance = distance * 0.621371192;
                if (distance < radius) {
                    g_count++;
                    markers[id].visible = true;
                    markers[id].distance = distance.toString();
                }
            }
            markers.sort(function(a, b) { return parseInt(a.distance) - parseInt(b.distance) });
            showMarkers();
        }
    });
}
////////////////////////////////////////////////////////////////////////////////////////
function onClickSearchState() {
    var city = document.getElementById('city').value.toLowerCase();
    var state = document.getElementById('state').value.toUpperCase();
    var radius = document.getElementById('radiusstate').value;
    if (state == '') {
        alert('Please select a state');
        document.getElementById('state').focus();
        return;
    }
	if (city == '') {
        alert('Please enter city name');
        document.getElementById('city').focus();
        return;
    }
	document.getElementById('zip').value="";
    var addr = '';
    var startZoom = 0;
    if (city != '') {
        addr = city + ',' + state + ',USA';
        startZoom = 10;
    }
    else if (state != '') {
        addr = state + ',USA';
        startZoom = 6;
    }

    g_geocoder.getLatLng(addr, function(pointaddr) {
        if (pointaddr) {
            hideMarkers();
            map.setCenter(pointaddr, startZoom);
            g_count = 0;
            for (id in markers) {
                var distance = LatLon.distHaversine(pointaddr.lat(), pointaddr.lng(), markers[id].latitude, markers[id].longitude);
                distance = distance * 0.621371192;
                if (city != '' && state != '') {
                    if (distance < radius || (markers[id].state == state && markers[id].city == city)) {
                        g_count++;
                        markers[id].visible = true;
                        markers[id].distance = distance.toString();
                    }
                }
                else if (state != '') {
                    if (distance < radius || markers[id].state == state) {
                        g_count++;
                        markers[id].visible = true;
                        markers[id].distance = distance.toString();
                    }
                }
            }
            if (city != '') {
                markers.sort(function(a, b) { return parseInt(a.distance) - parseInt(b.distance) });
            }
            else {
                markers.sort(function(a, b) { return (a.name > b.name) ? +1 : -1; });
            }
            showMarkers();
        }
    });
}
////////////////////////////////////////////////////////////////////////////////////////
function onClickSearchCountry() {
    var country = document.getElementById('ctl00_ctl00_MainContent_CountryDropDownList').value.toLowerCase();

    // get addr
    var addr = country;
    var startZoom = 3;

    g_geocoder.getLatLng(addr, function(pointaddr) {
        if (pointaddr) {
            hideMarkers();
            map.setCenter(pointaddr, startZoom);
            g_count = 0;
            for (id in markers) {
                var distance = LatLon.distHaversine(pointaddr.lat(), pointaddr.lng(), markers[id].latitude, markers[id].longitude);
                distance = distance * 0.621371192;
                if (markers[id].country == country) {
                    g_count++;
                    markers[id].visible = true;
                    markers[id].distance = distance.toString();
                }
            }
            markers.sort(function(a, b) { return (a.name > b.name) ? +1 : -1; });
            showMarkers();
        }
    });
}
////////////////////////////////////////////////////////////////////////////////////////
function onStateChanged() {
    document.getElementById('zip').value = '';
    document.getElementById('ctl00_ctl00_MainContent_CountryDropDownList').value = '';
}
////////////////////////////////////////////////////////////////////////////////////////
function onCountryChanged() {
    document.getElementById('zip').value = '';
    document.getElementById('city').value = '';
    document.getElementById('state').value = '';
}
////////////////////////////////////////////////////////////////////////////////////////
function createMarker(id) {

    var pointData = markers[id];
    if (pointData.latitude != 0) {
        addMarker(id, new GLatLng(pointData.latitude, pointData.longitude));
    }
}
////////////////////////////////////////////////////////////////////////////////////////
function addMarker(id, point) {

    g_index++;
    var pointData = markers[id];

    var icon = new GIcon();
    var iconimage = '';
    if (g_index <= 25) {
        iconimage = 'locations/images/iconr' + g_index.toString() + '.png';
    }
    else if (g_index <= 50) {
        iconimage = 'locations/images/iconb' + (g_index - 25).toString() + '.png';
    }
    else if (g_index <= 75) {
        iconimage = 'locations/images/icong' + (g_index - 50).toString() + '.png';
    }
    else {
        iconimage = 'imagesx/mapicons/iconr.png';
    }

    icon.image = iconimage;
    icon.iconSize = new GSize(20, 34);
    icon.iconAnchor = new GPoint(16, 16);
    opts = {
        "icon": icon,
        "clickable": true,
        "draggable": false,
        "labelText": ''
    };

    var marker = new LabeledMarker(point, opts);
    map.addOverlay(marker);

    GEvent.addListener(
		marker,
		"mouseover",
		function() {
		    popupLocation(id);
		}
	);

    buildList(id, iconimage);
    return marker;
}
////////////////////////////////////////////////////////////////////////////////////////
function popupLocation(id) {
    var pointData = markers[id];
    var content = getContent(id);
    map.openInfoWindowHtml(new GLatLng(pointData.latitude, pointData.longitude), content);
}
////////////////////////////////////////////////////////////////////////////////////////
function buildList(id, iconimage) {
    var pointData = markers[id];
    var listItem = document.createElement('li');
    var content = ""
    content += "<span onmouseover='javascript:popupLocation(" + id.toString() + ");'>";
    content += "<img src='" + iconimage + "'>";
    content += getContent(id);
    content += "</span>";
    listItem.innerHTML = content;
    document.getElementById('sidebar_location-list').appendChild(listItem);
}
////////////////////////////////////////////////////////////////////////////////////////
function addCountHeader() {
    if (g_count == 0) {
        var listItem = document.createElement('li');
        listItem.innerHTML += "<span style='font-family: Arial, Helvetica, sans-serif; font-size: 11px; font-weight:bold;'>Sorry, currently there are no locations that match your search.   More locations are coming soon, try again in the future.</span>";
        document.getElementById('sidebar_location-list').appendChild(listItem);
    }
    else {
        var listItem = document.createElement('li');
        listItem.innerHTML += "<span style='font-family: Arial, Helvetica, sans-serif; font-size: 11px; font-weight:bold;'>" + g_count.toString() + " Location(s) Found</span>";
        document.getElementById('sidebar_location-list').appendChild(listItem);
    }
}
////////////////////////////////////////////////////////////////////////////////////////
function clearList() {
    document.getElementById('sidebar_location').style.display = "block";
    var ul = document.getElementById('sidebar_location-list');
    var li = ul.firstChild;
    while (li) {
        ul.removeChild(li);
        li = ul.firstChild;
    }
}
////////////////////////////////////////////////////////////////////////////////////////
function lookupAddress(id) {
    var pointData = markers[id];
    g_geocoder.getLatLng(pointData.addr1, function(point) {
        if (!point) {
            g_geocoder.getLatLng(pointData.addr2, function(point) {
                if (point) {
                    s = "update g_info set latitude = '" + point.lat() + "', longitude='" + point.lng() + "' where gymid = " + pointData.placeid + "<br>";
                    document.getElementById('latlng').innerHTML += s;
                    addMarker(id, point);
                }
            });
        }
        else {
            s = "update g_info set latitude = '" + point.lat() + "', longitude='" + point.lng() + "' where gymid = " + pointData.placeid + "<br>";
            document.getElementById('latlng').innerHTML += s;
            addMarker(id, point);
        }
    });
}
////////////////////////////////////////////////////////////////////////////////////////
