function gmapInitialize(serviceUrl, languageCode) {


    // Map Object and options
    map = new GMap2(document.getElementById(gMapConfig.ContainerId));

    // Markers manager and geocoder
    if (map) {

        map.addControl(new GLargeMapControl());
        map.setCenter(new GLatLng(50.85, 4.3519), 8);

        mgr = new MarkerManager(map);
        geocoder = new GClientGeocoder();

        // Download data for markers
        GDownloadUrl(serviceUrl + "?lang" + languageCode, function (data) {
            var xml = GXml.parse(data);
            var countries = xml.documentElement.getElementsByTagName("country");

            var numItemToShow = $(document).getUrlParam("itemid");

            var address = $(document).getUrlParam("search");

            var itemToShow;
            items = xml.documentElement.getElementsByTagName("Item");

            //showItems(gMapConfig.ItemType);

            // Fill the dropdownlists only for parking elements

            $(items).each(function (i) {
                if ($(this).attr('type') == 'parking') {
                    var item = $(this);

                    var country ='';
                    if ($('country', this).text() == $('region', this).text())
                        country = $('country', this).text();
                    else
                        country = $('country', this).text() + ' - ' + $('region', this).text();
                    
                    var region = $('region', this).text();
                    var city = $('city', this).text();
                    var name = item.attr('name');

                    if (group[country] == undefined) {
                        group[country] = new Array();
                        document.getElementById("countries")[document.getElementById("countries").length] = new Option(country, country);
                    }
                    group[country].push(new Array(city, city));
                     
                    //if (group['R' + region] == undefined) {
                    //    group['R' + region] = new Array();
                    //    document.getElementById("regions")[document.getElementById("regions").length] = new Option(region, region);
                    //}
                    //group['R' + region].push(new Array(city, city));

                    if (group[city] == undefined) {
                        group[city] = new Array();
                        document.getElementById("cities")[document.getElementById("cities").length] = new Option(city, city);
                    }
                    group[city].push(new Array(name, i));

                    // If param itemid is the current item, create marker and set center on it
                    if ((numItemToShow != null) && (numItemToShow == $(this).attr('id'))) {
                        var marker = new GLatLng($(this).attr('lat'), $(this).attr('lon'));
                        map.addOverlay(createMarker(marker, i));
                        map.setCenter(marker, 14);

                        // Show incommingAddress
                        $('address', $('incomingAddresses', item)).each(function (i) {
                            map.addOverlay(createAlternativeMarker(item, $(this)));

                        });
                    }
                }
            });

            if (address != null) {

                showAddress(decodeURIComponent(address), true);
                $('#address').val(decodeURIComponent(address));
            }

            if ((numItemToShow == null) && (address == null)) showItems(gMapConfig.ItemType);
        });
    }
}

// Create our "tiny" marker icon
function createMarker(latlng, number) {
    var item = items[number];
    var caseIcons = new GIcon(G_DEFAULT_ICON);
    caseIcons.image = gMapConfig.IconBaseUrl + $(item).attr('src');
    caseIcons.printImage = gMapConfig.IconBaseUrl + $(item).attr('src');
    caseIcons.printShadow = '';

    // Set up our GMarkerOptions object
    markerOptions = { icon: caseIcons };
    var marker = new GMarker(latlng, markerOptions);
    marker.value = number;
    GEvent.addListener(marker, "click", function () {

        var myHtml = "<div style='width:180px; margin-left:15px;'>";
        myHtml += "<b>" + $(item).attr("name").capitalize() + "</b>:<br />";
        myHtml += $('mainAddress', item).text() + "<br />" + $('zipCode', item).text() + ' ' + $('city', item).text() + "<br />";
        if ($('image', item).length > 0) {
            myHtml += "<img src='" + gMapConfig.ParkingBaseUrl + $('image', item).text() + "' style='border:1px solid black; width:175px; height:125px; margin-bottom:3px;'>";
            myHtml += "<br />";
        }
        $('facility', $('facilities', item)).each(function (i) {
            myHtml += "<img src='" + gMapConfig.ParkingBaseUrl + $(this).attr('src') + "' alt='" + $(this).attr('name') + "' style='margin:0 2px 3px 0;' />";
            if (i == 5) return false;
        });
        var uri = $('URI', item).text();
        var itemTitle = $(item).attr('type') == 'parking' ? 'parking' : 'carwash';
        if (uri != '') {
            myHtml += "<br /><a href='" + gMapConfig.ParkingBaseUrl + uri + "' target='_blank'>voir le " + itemTitle + "</a>";
        }

        myHtml += "</div>";
        map.openInfoWindowHtml(latlng, myHtml);
    });
    return marker;
}


function createAlternativeMarker(item, alternativeItem) {
    var caseIcons = new GIcon(G_DEFAULT_ICON);
    caseIcons.image = gMapConfig.IconBaseUrl + item.attr('src');
    caseIcons.printImage = gMapConfig.IconBaseUrl + $(item).attr('src');
    caseIcons.printShadow = '';

    var latlng = new GLatLng(alternativeItem.attr('lat'), alternativeItem.attr('lon'));

    // Set up our GMarkerOptions object
    markerOptions = { icon: caseIcons };
    var marker = new GMarker(latlng, markerOptions);

    GEvent.addListener(marker, "click", function () {

        var myHtml = "<div style='width:180px; margin-left:15px;'>";
        myHtml += "<b>" + alternativeItem.text().capitalize() + "</b>:<br />";
        myHtml += $('mainAddress', item).text() + "<br />" + $('zipCode', item).text() + ' ' + $('city', item).text() + "<br />";
        if ($('image', item).length > 0) {
            myHtml += "<img src='" + gMapConfig.ParkingBaseUrl + $('image', item).text() + "' style='border:1px solid black; width:175px; height:125px; margin-bottom:3px;'>";
            myHtml += "<br />";
        }
        $('facility', $('facilities', item)).each(function (i) {
            myHtml += "<img src='" + gMapConfig.ParkingBaseUrl + $(this).attr('src') + "' alt='" + $(this).attr('name') + "' style='margin:0 2px 3px 0;' />";
            if (i == 5) return false;
        });
        var uri = $('URI', item).text();
        var itemTitle = $(item).attr('type') == 'parking' ? 'parking' : 'carwash';
        if (uri != '') {
            myHtml += "<br /><a href='" + gMapConfig.ParkingBaseUrl + uri + "' target='_blank'>voir le " + itemTitle + "</a>";
        }

        myHtml += "</div>";
        map.openInfoWindowHtml(latlng, myHtml);
    });
    return marker;
}


function showItems(type) {
    if (map) {
        // Map cleaning
        map.clearOverlays();
        mgr.clearMarkers();
        // Push markers
        var markers = [];
        $(items).each(function (i) {
            var marker = new GLatLng($(this).attr('lat'), $(this).attr('lon'));
            if ((type == '') || ($(this).attr('type') == type)) {
                markers.push(createMarker(marker, i));
                var item = $(this);
                $('address', $('incomingAddresses', item)).each(function (i) {
                    markers.push(createAlternativeMarker(item, $(this)));
                });
            }
        });

        // Display through MarkerManager
        mgr.addMarkers(markers, 0);
        mgr.refresh();

        if ($('#address').val() != '') {
            showAddress($('#address').val(), false);
            lastCenter = map.getCenter();
            lastZoom = map.getZoom();
        } else {
            lastCenter = null;
            lastZoom = null;
        }
    }
}

function buildMenu() {
    if (gMapConfig.ItemType == 'fleetwash') {
        $('#mnuMap li.right a').fadeTo("fast", .2);
    } else {
        $('#mnuMap li.right a').fadeTo("fast", 1);
    }
}

Array.prototype.removeDuplicate = function () {
    // Here we remove duplicate values from first array
    var array4 = new Array;
    for (var i = 0; i < this.length; i++) {
        var xx = true;
        for (var j = i + 1; j < this.length; j++) {
            if (this[i] == this[j])
                xx = false;
        }
        if (xx == true)
            array4.push(this[i]);
    }
    return array4;
}

String.prototype.capitalize = function () { //v1.0
    return this.replace(/\w+/g, function (a) {
        return a.charAt(0).toUpperCase() + a.substr(1).toLowerCase();
    });
};

function findNearest(point) {
    tmpHyp = 0;
    for (var i = 0; i < items.length; i++) {
        if (gMapConfig.ItemType == '' || gMapConfig.ItemType == $(items[i]).attr('type')) {
            if ((Math.pow(Math.abs(items[i].getAttribute("lon") - point.x), 2) + Math.pow(Math.abs(items[i].getAttribute("lat") - point.y), 2) <= tmpHyp) || tmpHyp == 0) {
                tmpHyp = Math.pow(Math.abs(items[i].getAttribute("lon") - point.x), 2) + Math.pow(Math.abs(items[i].getAttribute("lat") - point.y), 2);
                marker = new GLatLng(items[i].getAttribute("lat"), items[i].getAttribute("lon"));
                var itemNumber = i;
            }
        }
    }

    map.addOverlay(createMarker(marker, itemNumber));
    //directionsPanel = document.getElementById("route");
    var directionPanel = $('#directionPanel').get(0);
    directions = new GDirections(map, directionPanel); //, directionsPanel
    GEvent.addListener(directions, "addoverlay", onGDirectionsAddoverlay);
    directions.load("from: " + point + " to: " + marker);
}

function onGDirectionsAddoverlay() {
    directions.getMarker(1).hide();
    // Replace the map at the last position of the client
    if (lastCenter != null && lastZoom != null)
        map.setCenter(lastCenter, lastZoom);
}	
