jQuery.noConflict();

var geoCoder = null;
var map = null;
var distributors = new Array();

var mapLongtitude = 8;
var mapLattitude = 18;

jQuery(document).ready(function(){
    if(jQuery('#googlemap').length > 0) {
        loadMapMultiple();
    }
});

jQuery(window).unload(function(){
    if(jQuery('#googlemap').length > 0)
        GUnload();
});

function loadMapMultiple() {
    if (GBrowserIsCompatible()) {
        map = new GMap2(document.getElementById("googlemap"));
        map.setCenter(new GLatLng(mapLattitude, mapLongtitude), 1, G_SATELLITE_MAP);
        map.addControl(new GLargeMapControl());
        map.addControl(new GMapTypeControl());
        geoCoder = new GClientGeocoder();
        for (i=0;i<distributors.length;i++) {
            showLocations(distributors[i].name, distributors[i].mapLocation, distributors[i].mapPointX, distributors[i].mapPointY, distributors[i].telephone, distributors[i].fax, distributors[i].website, distributors[i].contactMail, distributors[i].internLink);
        }
    }
}

function showLocations(distributor, address, pointX, pointY, telephone, fax, website, contactMail, readMore) {
    var point = new GLatLng(pointX,pointY);
    var marker = new GMarker(point);
    GEvent.addListener(marker, "click", function() {
        var infoWindowContents = '<h4>' + distributor + '</h4>' /*+ address*/;
        if(telephone)
            infoWindowContents += '<br/>T: ' + telephone;
        if(fax)
            infoWindowContents += '<br/>F: ' + fax;
        if(website) {
            if(website.substr(0,7).toLowerCase() == 'http://' || website.substr(0,8).toLowerCase() == 'https://')
                infoWindowContents += '<br/>W: <a href=\"' + website + '\" target="_blank">' + website + '</a>';
            else
                infoWindowContents += '<br/>W: <a href=\"http://' + website + '\" target="_blank">' + website + '</a>';
        }
        if(contactMail)
            infoWindowContents += '<br/>M: <a href=\"mailto:' + contactMail + '\">' + contactMail + '</a>';
        if(readMore)
            infoWindowContents += '<br/><a href=\"' + readMore + '\">Read more</a>';
        marker.openInfoWindowHtml(infoWindowContents);
    });
    map.addOverlay(marker);
}

