// geoloc

var geoloc = {
    towns: new Array(),
    geocoder: null,
    addressMarker: null,
    map: null,


    get_postal_codes: function (latitude, longitude, zoomlevel) {
        showPopup('geoloc_townselect');
        document.getElementById('geoloc_townselect').innerHTML = "<br /><br /><br /><img src='/img/loading.gif' /><br /><br /><br />";
        for (var i=0; i<1000000; i++) {};
        var req = new Request(
        {
            url:'/geoloc/action-geoloc_get_postal_codes',
            method:'post',
            async:true,
            onSuccess: function(json) {
                var result = JSON.decode(json);
                if (result.error>0) {
                    showAlerte('alerte', result.message);
                    return;
                }
                geoloc.setSelectTown(result.result);
            },
            onFailure: function() {
                showAlerte('alerte', 'Impossible de récuperer position');
                return;
            }
        }
        );
        req.send('latitude='+latitude+'&longitude='+longitude+'&zoomlevel='+zoomlevel);
    },

    setUserLoc: function (i) {
        this.selected_town=this.towns[i];
        this.callback(this.towns[i]);
    },

    load: function (init_latitude, init_longitude) {
        if (GBrowserIsCompatible()) {
            if(!$('map')) {
                divmap=new Element("div", {"id":"map"}).setStyles({'width':'500px','height':'300px'});
                $('map_container').appendChild(divmap);
            }
            this.map = new GMap2($('map'), {draggableCursor: 'crosshair'});
            this.map.setCenter(new GLatLng(init_latitude, init_longitude), 11);
            this.map.addControl(new GLargeMapControl());

            this.geocoder = new GClientGeocoder();

            GEvent.addListener(this.map, 'click', function(overlay, latlng) {
                geoloc.get_postal_codes(latlng.lat(), latlng.lng(), geoloc.map.getZoom());
            });

        }
    },

    setSelectTown: function (result) {
        var selectTown = '<h2>Cliquez sur votre ville</h2>';
        selectTown += '<ul>';
        this.towns=new Array();
        for (var i=0; i<result.length; i++) {
            this.towns[i]=result[i];
            selectTown += "<li onclick=\"geoloc.setUserLoc(" + i + "); return false;\" style=\"cursor:pointer\" onmouseover=\"this.style.background='#EEEEEE'\" onmouseout=\"this.style.background='#f5f5f6'\" >" + this.towns[i].town_name + "(" + this.towns[i].town_postalcode + ")</li>" ;
        }
        selectTown += '</ul>';
        document.getElementById('geoloc_townselect').innerHTML = selectTown;
    },

    showAddress: function (address, countryCode) {
        if (this.geocoder) {
            this.geocoder.setBaseCountryCode(countryCode);
            this.geocoder.getLatLng(address,
            function(point) {
                if (!point) {
                    showAlerte('alerte', 'Impossible de localiser : \n<b>'+address+'</b>');
                } else {
                    if (geoloc.addressMarker) {
                        geoloc.map.removeOverlay(geoloc.addressMarker);
                    }
                    geoloc.addressMarker = new GMarker(point);
                    geoloc.map.setCenter(point,11);
                    geoloc.map.addOverlay(addressMarker);
                }
            }
            );
        }
    },


    geoloc_init: function (site_geoloc) {

        if ( (typeof site_geoloc.geoloc_callback) == 'function') {
            this.callback = site_geoloc.geoloc_callback;
        }
        else {
            this.callback = function () {};
        }

        if ( (typeof site_geoloc.geoloc_init) == 'function') {
            this.init = site_geoloc.geoloc_init;
        }
        else {
            this.init = function () {};
        }

        init_position = this.init();
        if ( (typeof init_position) == 'object') {
            this.load(init_position.init_latitude, init_position.init_longitude);
        }

    }

}