/**
*
* class GMAP
*
**/

function GMAP() {
        
    
    var self = this;
    self.map;
    self.geocoder;
    self.markers = new Array();
    self.address_collection = new Array(); 
    self.marker_count = 0;
    
    // init Google Maps
    this.init = function() {
        self.map = new GMap2(document.getElementById("map"));
        self.map.addControl(new GLargeMapControl());
        self.map.addControl(new GMapTypeControl());
        self.map.addControl(new GOverviewMapControl());
        self.map.setCenter(new GLatLng(50.988668, 11.028534), 2);    
        self.map.enableScrollWheelZoom();
        self.geocoder = new GClientGeocoder();
    }
        
    // callback handler for setPOI
    this.addAddressToMap = function(response) {
        if (!response || response.Status.code != 200) {
            // maybe an alert message
        } else {
            // place the marker in the map
            place = response.Placemark[0];
            point = new GLatLng(place.Point.coordinates[1],
            place.Point.coordinates[0]);
            
            var marker_icon = new GIcon(G_DEFAULT_ICON);                       
            
            marker_options = { icon:marker_icon };
            marker_icon.image = "http://www.tecart.de/partner/" + self.address_collection[self.marker_count++][1];
            marker = new GMarker(point, marker_options);
            
            self.markers.push(point); 
            self.map.addOverlay(marker);
            //marker.openInfoWindow(place.address + '<br>' + '<b>Country code:</b> ' + place.AddressDetails.Country.CountryNameCode);
            // update the viewport
            var bounds = new GLatLngBounds();
            for (var i=0; i<self.markers.length; i++) {
                bounds.extend(self.markers[i]);
            }
            self.map.setZoom(self.map.getBoundsZoomLevel(bounds) - 1);
            self.map.setCenter(bounds.getCenter());
        }
        
        
    }
        
    
    // add an adress to the map
    this.setPOI = function(address) {        
        self.geocoder.getLocations(address, self.addAddressToMap);
    }      
}

var gmap = new GMAP();

function showAddresses() 
{
    for (i=0; i < gmap.address_collection.length; i++) 
    {
        gmap.setPOI(gmap.address_collection[i][0]);
    }
}

window.onload = function() 
{
    gmap.init();  
    window.setTimeout(showAddresses, 1000); 
}