window.addEvent('load', function() {
    if (typeof cms != "undefined") {
        cms.addEvent('load', function() {
            var dsc = new DealerSearchController();
        });
    } else {
        var dsc = new DealerSearchController();
    }
});

var DealerSearchController = new Class({
    
    initialize: function() {
        this.loadEvents();
        $('mapLocation').setProperty('value', getVars.mapLocation);
        $('mapCountry').setProperty('value', getVars.mapCountry);
        $('mapRadius').setProperty('value', getVars.mapRadius);
        
        if (getVars.mapLocation) {
            //this.search();
        }
    },
    
    loadEvents: function() {
        var inst = this;
        $('dealerLocatorSearchForm').addEvent('submit', function(e) {
            e.stop();
            inst.search();
        });
    },
    
    search: function() {
        var location = $('mapLocation').getProperty('value');
        var country = $('mapCountry').getProperty('value');
        var radius = $('mapRadius').getProperty('value');
        
        if (typeof mapController == "undefined") {
            window.location.href = "index.php?page=dealerlocator&language="+getVars.language+"&mapLocation="+location+"&mapCountry="+country+"&mapRadius="+radius;
            return;
        }
        
        mapController.search(location, country, radius);
    }
    
});
