var $_country, $_city;

google.load("maps", "3",{"other_params":"sensor=false&language="+info.language});
                                                                                       

var googleMaps = {
    map: null,
    zoomLevel: 9,
    geocoder: null,
    addressList: null,
    dragging: false,
    infoWindow: null,
    currentBounds: null,
    dealersLoaded: false,
    markers: [],

    init: function() {
        this.addressList = $('#address-list');
        
        this.map = new google.maps.Map(document.getElementById("google-maps-replace"), {
            zoom: 9,
			center: new google.maps.LatLng(52.372206,5.218022),
			mapTypeId: google.maps.MapTypeId.ROADMAP,
			scrollwheel: false,
			navigationControl: true,
			scaleControl: false,
			mapTypeControl: false
        });
		
		this.infoWindow = new google.maps.InfoWindow({
			content: 'hallo'
		});
                
        this.geocoder = new google.maps.Geocoder();
        
        this.search();
               
        google.maps.event.addListener(googleMaps.map, 'bounds_changed', function(){
            if(!googleMaps.dragging) {
                googleMaps.boundsChanged();
            }
        });
        
        google.maps.event.addListener(googleMaps.map, 'dragstart', function(){
            googleMaps.dragging = true;
        }); 
        
        google.maps.event.addListener(googleMaps.map, 'dragend', function(){
            googleMaps.dragging = false;
        }); 
    },
    
    boundsChanged : function() {
        var bounds = googleMaps.map.getBounds();
        
        if(!googleMaps.currentBounds || !googleMaps.currentBounds.equals(bounds)) {
            googleMaps.currentBounds = bounds;        
            var coordinates = {
                'ne_lat' : bounds.getNorthEast().lat(),
                'ne_lng' : bounds.getNorthEast().lng(),
                'sw_lat' : bounds.getSouthWest().lat(),
                'sw_lng' : bounds.getSouthWest().lng(),
                'ce_lat' : googleMaps.map.getCenter().b,
                'ce_lng' : googleMaps.map.getCenter().c
            } 

            $.get('resellers/search/',coordinates,function(data){
                googleMaps.clearAddressList();
                var $i = null;
                for($i=0;$i<data.length;$i++) {
                    googleMaps.placeMarker(data[$i]);
                }
            },"json");  
        }
    },
    
    search: function(){
        this.geocoder.geocode({'address':info.city+' '+info.country},function(results, status){
                if (status == google.maps.GeocoderStatus.OK) {
                    googleMaps.map.setZoom(googleMaps.zoomLevel);
                    googleMaps.map.setCenter(results[0].geometry.location);
                }
                
        });
    },
    
    placeMarker: function(obj) {
        this.addAddress(obj);
        var map = this.map;
        
        var image = new google.maps.MarkerImage('img/gmap_icon.png',
            new google.maps.Size(28, 33),
            new google.maps.Point(0,0),
            new google.maps.Point(10, 28)
        );

        var marker = new google.maps.Marker({
            position: new google.maps.LatLng(obj.lat, obj.lng), 
            map: map,
            icon: image
        });

        google.maps.event.addListener(marker, 'click', function() {
            googleMaps.infoWindow.setContent('<strong>'+obj.name+'</strong><br /><a href="http://'+obj.site+'" target="_blank">'+obj.site+'</a><br />');
            googleMaps.infoWindow.open(googleMaps.map,marker);
        });
        
        this.markers.push(marker);

    },
    
    addAddress: function(obj) {
        var $li = $('<li>');
        $li.append('<strong>'+obj.name+'</strong><br /><br />');
        $li.append(obj.address+'<br />');
        $li.append(obj.postal+' '+obj.city+'<br /><br />');
        $li.append(obj.phone+'<br /><br />');
        if(obj.site != '') {
            $li.append('<a href="http://'+obj.site+'" target="_blank">'+obj.site+'</a><br />');
        }
        if(obj.email != '') {
            $li.append('<a href="mailto:'+obj.email+'">'+obj.email+'</a><br />');
        }
        this.addressList.append($li);
    },
    
    clearAddressList: function() {
        this.addressList.html('');
        var $i;
        for($i=0;$i<this.markers.length;$i++){
            this.markers[$i].setMap(null);
        }
    }

}

google.setOnLoadCallback(function(){ 
   
    $_city = $('#city-value');
    $_country = $('#country-value');
	
	googleMaps.init();
    
    $('#submit-search-request').click(function(){
        info.city = $_city.val(); 
        if($_country.val() != '') {
            info.country = $_country.val();
        }
        googleMaps.search();
        return false;
    });
});
