var map;
var localSearch = new GlocalSearch();

var icon = new GIcon();
icon.image = "http://www.google.com/mapfiles/marker.png";
icon.shadow = "http://www.google.com/mapfiles/shadow50.png";
icon.iconSize = new GSize(20, 34);
icon.shadowSize = new GSize(37, 34);
icon.iconAnchor = new GPoint(10, 34);

function usePointFromPostcode(postcode, callbackFunction) {

    localSearch.setSearchCompleteCallback(null,
		function() {

		    if (localSearch.results[0]) {
		        var resultLat = localSearch.results[0].lat;
		        var resultLng = localSearch.results[0].lng;
		        var point = new GLatLng(resultLat, resultLng);
		        callbackFunction(point);
		    } else {
		        alert("Postcode not found!");
		    }
		});

    localSearch.execute(postcode + ", UK");
}

function placeMarkerAtPoint(point) {
    var marker = new GMarker(point, icon);
    map.addOverlay(marker);
}

function setCenterToPoint(point) {
    map.setCenter(point, 17);
}

function showPointLatLng(point) {
    //alert("Latitude: " + point.lat() + "\nLongitude: " + point.lng());
    prompt("Position is:", "Latitude: " + point.lat() + "\nLongitude: " + point.lng());
}

function mapLoad() {
    if (GBrowserIsCompatible()) {
        map = new GMap2(document.getElementById("map"));
        map.addControl(new GLargeMapControl());
        map.addControl(new GMapTypeControl());
        // Centre of UK excluding islands
        map.setCenter(new GLatLng(53.825359, -2.420544), 5);
    }
}

function addLoadEvent(func) {
    var oldonload = window.onload;
    if (typeof window.onload != 'function') {
        window.onload = func;
    } else {
        window.onload = function() {
            oldonload();
            func();
        }
    }
}

function addUnLoadEvent(func) {
    var oldonunload = window.onunload;
    if (typeof window.onunload != 'function') {
        window.onunload = func;
    } else {
        window.onunload = function() {
            oldonunload();
            func();
        }
    }
}

// Create a marker whose info window displays the given number.
function createMarker(point, html) {
    var marker = new GMarker(point);

    GEvent.addListener(marker, 'click', function() {
        marker.openInfoWindowHtml(html);
    });

    // The new marker "mouseover" listener
    GEvent.addListener(marker, "mouseover", function() {
        marker.openInfoWindowHtml(html);
    });

    return marker;
}

function designer(name, postcode, latitude, longitude, url) {
    this.name = name;
    this.postcode = postcode;
    this.latitude = latitude;
    this.longitude = longitude;
    this.url = url;
}

function populateMap() {
    GDownloadUrl("courses.xml", function(data, responseCode) {
        var xml = GXml.parse(data);
        var markers = xml.documentElement.getElementsByTagName("marker");
        for (var i = 0; i < markers.length; i++) {

            var lat = markers[i].getAttribute("lat");
            var lng = markers[i].getAttribute("lng");
            var url = markers[i].getAttribute("url");

            var addressLine1;
            var addressLine2;
            var town;
            var county;
            var postCode;
            var country;

            if (markers[i].getAttribute("useOrganisationAddress").toLowerCase() == 'true') {
                addressLine1 = markers[i].getAttribute("addressLine1");
                addressLine2 = markers[i].getAttribute("addressLine2");
                town = markers[i].getAttribute("town");
                county = markers[i].getAttribute("county");
                postCode = markers[i].getAttribute("postCode");
                country = "";
            }
            else {
                addressLine1 = markers[i].getAttribute("courseAddressLine1");
                addressLine2 = markers[i].getAttribute("courseAddressLine2");
                town = markers[i].getAttribute("courseAddressLine3");
                county = "";
                postCode = markers[i].getAttribute("coursePostCode");
                country = markers[i].getAttribute("courseCountry");
            }

            var telephone = markers[i].getAttribute("telephone");
            var email = markers[i].getAttribute("email");
            var businessName = markers[i].getAttribute("businessName");
            var comments = markers[i].getAttribute("comments");

            var html = '';

            html += '<div class="course">' + markers[i].getAttribute("name") + '</div>';

            var address = '';
            if (addressLine1 != '') {
                address += addressLine1 + '<br/>';
            }
            if (addressLine2 != '') {
                address += addressLine2 + '<br/>';
            }
            if (town != '') {
                address += town + '<br/>';
            }
            if (county != '') {
                address += county + '<br/>';
            }
            if (postCode != '') {
                address += postCode + '<br/>';
            }
            if (address != '') {
                html += '<div class="website">' + address + '</div>';
            }
            if (telephone != '') {
                html += '<div class="website">Telephone: ' + telephone + '</div>';
            }
            if (email != '') {
                html += '<div class="website">Email: ' + email + '</div>';
            }
            if (url != '') {
                html += '<div class="website">Website: <a href="http://' + url + '" target="_blank">' + url + '</a></div>';
            }
            if (comments != '') {
                if (comments.length > 550)
                    comments = comments.substring(0, 550) + '...';
                html += '<div class="coursecomments">' + comments + '</div>';
            }

            var point = new GLatLng(parseFloat(lat), parseFloat(lng));

            var marker = createMarker(point, html);

            map.addOverlay(marker);
        }
    });
}

addLoadEvent(mapLoad);
addLoadEvent(populateMap);
addUnLoadEvent(GUnload);
