var HCM = {
 map: null,
 iconMAG : null,
 iconBAF  : null,
 
 _createIcon: function(img, shadow) {
        var icon = new GIcon();
        icon.image = img;
        icon.shadow = shadow;
        icon.iconSize = new GSize(26, 22);
        icon.shadowSize = new GSize(26, 22);
        icon.iconAnchor = new GPoint(11, 26);
        icon.infoWindowAnchor = new GPoint(12, 0);
        return icon;
    },
 
 _initRessources: function(xmlRessources) {
        var chemin_ombre = $("#ombre", xmlRessources).attr('chemin');
        var chemin_icone_mag = $("#icone_mag", xmlRessources).attr('chemin');
        HCM.iconMAG = HCM._createIcon(chemin_icone_mag, chemin_ombre);
        var chemin_icone_baf = $("#icone_baf", xmlRessources).attr('chemin');
        HCM.iconBAF = HCM._createIcon(chemin_icone_baf, chemin_ombre);
    },
  
 _createMarker: function (lat, lng, type, html) {
        var point = new GLatLng(lat, lng);
        var icon = (type == "baf") ? HCM.iconBAF : HCM.iconMAG;
        var marker = new GMarker(point, icon);
        GEvent.addListener(marker, "click", function() {
                HCM.map.setCenter(point);
                marker.openInfoWindowHtml(html);
                HCM.map.setZoom(14);
            });
        
        return marker;
    },
 
 updateMap: function(data, responseCode) {
        //console.log("BEGIN HCM.updateMap");
        // XXX tester le code reponse ?

        var xml = GXml.parse(data);
        // recup et initialisation des ressources
        var ressources = xml.documentElement.getElementsByTagName("ressources")[0];
        HCM._initRessources(ressources);
        
        // remplissage de la carte
        var map = HCM.map;
        var selectCP = Array();
        var agences = xml.documentElement.getElementsByTagName("agence");
        var nbAgences = agences.length;

        for (var i = 0; i < nbAgences; i++) {
            var a = agences[i];
            var type = a.getAttribute("type");
            var cp = a.getAttribute("cp");
            var agence = a.getAttribute("agence");
            // XXX Q&D fix, revoir nommage
            var cpville = agence + " (" + cp + ")";
            var lat = parseFloat(a.getAttribute("lat"));
            var lng = parseFloat(a.getAttribute("lng"));
            if (! (lat && lng)) {
                continue;
            }
            var desc = a.getElementsByTagName('description');
            var html = desc.item(0).firstChild.data;
            map.addOverlay(HCM._createMarker(lat, lng, type, html));
            // XXX : faudrait mieux mettre "agence (cp)", non ?
            selectCP.push({cpville:cpville, lat:lat, lng:lng, type:type});
        }   
        
        //  alert(desc.item(0));
        // selecteur (beurk)
        var selectHtml = '<option value="">Sélectionnez un magasin ou un bureau d\'affaires</option>';
        selectCP = selectCP.sort(function(a, b){return a[0]<b[0]});
        for (var i = 0; i < selectCP.length; i++) {
            var opt = selectCP[i];
            //console.dir(opt);
            selectHtml += 
                '<option value="' + opt.lat + '|' + opt.lng + '|12"' +'">'
                + opt.cpville 
                +'</option>'
                ;
        }
        
        selectHtml = 
        '<select onchange="if(this.options[selectedIndex].value !== \'\') HCM.moveTo(this.options[selectedIndex].value);">'
        + selectHtml
        +'</select>'
        ;
        
        document.getElementById("agencesbox").innerHTML = selectHtml;
        //console.log("END HCM.updateMap");
    },

 moveTo: function(destination) {
        var arr = destination.split('|');
        var map = HCM.map;
        if (map.getZoom() == arr[2]) {
            map.panTo(new GLatLng(arr[0],arr[1]));
        }
        else {
            map.setCenter(new GLatLng(arr[0],arr[1]), parseInt(arr[2]));
        }
    },
 
 init : function(urlAgencesXML) {
        //console.log("BEGIN HCM.init");
        var map = new GMap2(document.getElementById("map"));
        map.addControl(new GLargeMapControl());
        map.addControl(new GMapTypeControl());
        map.enableDoubleClickZoom();
        map.enableContinuousZoom();
        map.setCenter(new GLatLng(46.86019101567027, 2.6806640625), 6); // XXX en param ?
        HCM.map = map;
        GDownloadUrl(urlAgencesXML, HCM.updateMap);
        //console.log("END HCM.init");
    },

 __nada: null
};



