var minLat = 0;
var minLng = 0;
var maxLat = 0;
var maxLng = 0;
var pointCount = 0;
var central_marker = null;
var setCenter = false;
var initial_zoom = 13;

function initialize() {
    if (GBrowserIsCompatible()) {
        map = new GMap2(document.getElementById("gmap"));
        map.addControl(new GLargeMapControl());
        var calcCenter = AddPoints();

        //if (calcCenter == true) {
            var sw = new GLatLng(minLat, minLng);
            var ne = new GLatLng(maxLat, maxLng);
            //map.setMapType(G_PHYSICAL_MAP);
            //alert (sw.lat() + ", " + sw.lng());
            var bounds = new GLatLngBounds(sw, ne);
            //alert (bounds.getSouthWest().lat() + ", " + bounds.getSouthWest().lng());
            //alert (bounds.getNorthEast().lat() + ", " + bounds.getNorthEast().lng());
            var zoom = map.getBoundsZoomLevel(bounds);
            if (zoom > 8) 
            {
                zoom -= 1;
            }
            var center = bounds.getCenter();
            map.setCenter(center, zoom);
       // }
       // else 
        //{
        //    if (setCenter == true) 
        //    {
         //       map.setCenter(central_marker, initial_zoom);
        //    }
       // }
    }
}


// Creates a marker at the given point with the given number label
function createMarker(point, text) {
    var marker = new GMarker(point, { title: "School" });
    GEvent.addListener(marker, "click", function() { marker.openInfoWindowHtml(text); });
    return marker;
}



// Creates a marker at the given point with the given number label
function createKeyLocMarker(point, text, title) 
{

    var blueIcon = new GIcon(G_DEFAULT_ICON, "http://www.google.com/intl/en_us/mapfiles/ms/micons/blue-dot.png");
    blueIcon.iconSize = new GSize(32, 32);
    var marker = new GMarker(point, { title: title, icon: blueIcon });
    GEvent.addListener(marker, "click", function() { marker.openInfoWindowHtml(text); });
    //marker.openInfoWindowHtml(text);
    return marker;
}

function GetCountriesFromRegion() {
    var frm = document.frmUserSearch;
    var region = frm.rgn.options[frm.rgn.selectedIndex].value;
    //alert (encodeURIComponent(phrase));
    var cntry = document.getElementById("cntry");
    cntry.style.backgroundColor = "#333333";
    AjaxStartRequest("/_services/ajaxsvc.php?r=" + region, "countriesfromregion", "callback", "", "")
}

function GetPlacesFromCountry() {
    var frm = document.frmUserSearch;
    var country = frm.cntry.options[frm.cntry.selectedIndex].value;
    //alert (encodeURIComponent(phrase));
    var place = document.getElementById("place");
    place.style.backgroundColor = "#333333";
    AjaxStartRequest("/_services/ajaxsvc.php?c=" + country, "placesfromcountry", "callback", "", "")
}


function SearchByLoc() {
    var frm = document.frmSchoolSearch;
    frm.a.value = "loc";
    frm.submit();
    //    var region = frm.rgn.options[frm.rgn.selectedIndex].value;
    //    var country = frm.cntry.options[frm.cntry.selectedIndex].value;
    //    var place = frm.place.options[frm.place.selectedIndex].value;
    //    
}

//function SearchByPhrase()
//{
//    var frm = document.frmUserSearch;
//    frm.a.value="phr";
//    frm.submit();
////    var region = frm.rgn.options[frm.rgn.selectedIndex].value;
////    var country = frm.cntry.options[frm.cntry.selectedIndex].value;
////    var place = frm.place.options[frm.place.selectedIndex].value;
////    
//}


function AjaxCallBack(action, result) {
    //alert(action + ", " + result);
    switch (action) {
        case "countriesfromregion":
            PopulateCountries(result);
            break;

        case "placesfromcountry":
            PopulatePlaces(result);
            break;
    }
}



function PopulateCountries(data) {
    var fld = document.getElementById("cntry");
    var plc = document.getElementById("place");
    ClearListBox("cntry");
    ClearListBox("place");

    var popt = new Option("- All places -", "");
    plc.options[0] = popt;

    var dataArray = data.split(";");
    var curOpt = 0;
    var opt = new Option("- All countries -", "");
    fld.options[curOpt++] = opt;
    for (var i = 0; i < dataArray.length; i++) {
        var thisOpt = dataArray[i].split("|");
        var opt = new Option(thisOpt[1], thisOpt[0]);
        if (thisOpt[0] != '') {
            fld.options[curOpt++] = opt;
        }
    }
    fld.style.backgroundColor = "black";
}



function PopulatePlaces(data) {
    var fld = document.getElementById("place");
    ClearListBox("place");
    var dataArray = data.split(";");
    var curOpt = 0;
    var opt = new Option("- All places -", "");
    fld.options[curOpt++] = opt;
    for (var i = 0; i < dataArray.length; i++) {
        var thisOpt = dataArray[i];
        var opt = new Option(thisOpt, thisOpt);
        if (thisOpt != '') {
            fld.options[curOpt++] = opt;
        }
    }
    fld.style.backgroundColor = "black";
}

