// Configuration

var xmlUrl = "/scripts/get-data.php";
var latFieldID = "latitude";
var lonFieldID = "longitude";
var statusFieldID = "statusField";




// Functions



function getIcon(image,width,height) {

	var icon = new GIcon();
	icon.image = image;
	icon.iconSize = new GSize(width, height);
	icon.iconAnchor = new GPoint(width/2, height/2);
	icon.infoWindowAnchor = new GPoint(width/2, height/2);

	return icon;

}


function getDraggableMarker(point) {

    var marker = new GMarker(point, { draggable: true });
    GEvent.addListener(marker, "dragend", function() {
        latlon = marker.getPoint().toUrlValue().split(',');
        if (window.parent) {
            window.parent.document.getElementById(latFieldID).value = latlon[0];
            window.parent.document.getElementById(lonFieldID).value = latlon[1];
        }
        else {
            document.getElementById(latFieldID).value = latlon[0];
            document.getElementById(lonFieldID).value = latlon[1];
        }
    });

    return marker;
}


function getLocation(searchValue, mapObject) {
    searchValue = utf8_encode(searchValue);
    var request = GXmlHttp.create();
    var search = escape(searchValue);
    var url = xmlUrl + '?location=' + search;
    request.open('GET', url, true);
    request.onreadystatechange = function() {
        if (request.readyState == 4) {
            var xmlDoc = GXml.parse(request.responseText);
            var markers = xmlDoc.documentElement.getElementsByTagName("marker");
            var status = xmlDoc.documentElement.getElementsByTagName("status");
            var statusCode = status[0].getAttribute("code");
            var statusText = status[0].getAttribute("text");
            if (statusCode == 1) {
                var lat = parseFloat(markers[0].getAttribute("lat"));
                var lon = parseFloat(markers[0].getAttribute("lon"));
                var point = new GLatLng(lat, lon);
                var marker = getDraggableMarker(point)
                mapObject.clearOverlays();
                mapObject.setCenter(point);
                mapObject.addOverlay(marker);
                if (window.parent) {
                    window.parent.document.getElementById(latFieldID).value = lat;
                    window.parent.document.getElementById(lonFieldID).value = lon;
                }
                else {
                    document.getElementById(latFieldID).value = lat;
                    document.getElementById(lonFieldID).value = lon;
                }
            }
            if (window.parent) {
                window.parent.document.getElementById(statusFieldID).innerHTML = utf8_decode(statusText);
            }
            else {
                document.getElementById(statusFieldID).innerHTML = utf8_decode(statusText);
            }
        }
    }
    request.send(null);
}
function utf8_encode(string) {
    string = string.replace(/\r\n/g, "\n");
    var utftext = "";

    for (var n = 0; n < string.length; n++) {

        var c = string.charCodeAt(n);

        if (c < 128) {
            utftext += String.fromCharCode(c);
        }
        else if ((c > 127) && (c < 2048)) {
            utftext += String.fromCharCode((c >> 6) | 192);
            utftext += String.fromCharCode((c & 63) | 128);
        }
        else {
            utftext += String.fromCharCode((c >> 12) | 224);
            utftext += String.fromCharCode(((c >> 6) & 63) | 128);
            utftext += String.fromCharCode((c & 63) | 128);
        }

    }

    return utftext;
}
function utf8_decode(utftext) {
    var string = "";
    var i = 0;
    var c = c1 = c2 = 0;

    while (i < utftext.length) {

        c = utftext.charCodeAt(i);

        if (c < 128) {
            string += String.fromCharCode(c);
            i++;
        }
        else if ((c > 191) && (c < 224)) {
            c2 = utftext.charCodeAt(i + 1);
            string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
            i += 2;
        }
        else {
            c2 = utftext.charCodeAt(i + 1);
            c3 = utftext.charCodeAt(i + 2);
            string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
            i += 3;
        }

    }

    return string;
}
