if (typeof peapod == 'undefined') {
    peapod = new Object;
}
            
peapod.MapController = function(mapDiv, center, zoomLevel, 
        imagesBase, stores, popupDivs, largeMap) {

    if (GBrowserIsCompatible()) {
    
        this.init(mapDiv, center, zoomLevel, imagesBase, 
                stores, popupDivs, largeMap);
                
    } else {
        alert("Google maps unable to work on this browser.");
    }
}
            
peapod.MapController.prototype = {
            
    init:function(mapDiv, center, zoomLevel, imagesBase,
             stores, popupDivs, largeMap) {
    
        this.popupDivs = popupDivs; 
        this.imagesBase = imagesBase;
        this.stores = stores;
                
        var canvas = $(mapDiv);
                    
        this.map = new GMap2(canvas);
        
        if (largeMap) {
        
            this.map.addControl(new GLargeMapControl());
            this.map.addControl(new GMapTypeControl());
            
        } else {
            this.map.addControl(new GSmallMapControl());
        } 
        
        this.map.addControl(new GScaleControl());
                    
        var cen = new GLatLng(center.lat, center.lng);
                    
        this.map.setCenter(cen, zoomLevel);
        var marker = new GMarker(cen, {clickable:false});
        this.map.addOverlay(marker);
                    
        for (var i = 0; i < this.stores.length; i++) {
            this.addStoreLocation(this.stores[i], (i + 1));
        }
        
        Event.observe(window, 'unload', GUnload);
    },
                
    addStoreLocation:function(store, num) {
       var point = new GLatLng(store.lat, store.lng);
       
       var micon = new GIcon();
       micon.image = this.imagesBase + num + ".gif";
       micon.iconSize= new GSize(23, 15);
       micon.iconAnchor= new GPoint(10, 10);
       micon.infoWindowAnchor= new GPoint(5, 1);
       var opts = {clickable:true, icon:micon};
       var marker = new GMarker(point, opts);
       marker.storeId = store.storeId;
                   
       GEvent.addListener(marker, 'click', 
                this.onclick.bind(this, marker));
                            
       var linkId = "lnk_" + marker.storeId; 
       $(linkId).onclick = this.onclick.bind(this, marker);
                   
       this.map.addOverlay(marker);
    },
                
    onclick:function() {
        var marker = $A(arguments)[0];
        marker.openExtInfoWindow(this.map, "blue_border_window",
                 this.popupDivs[marker.storeId], {beakOffset:1});
        
        var top = $('map_top');
        
        if (top) {
            top.scrollTo();
        }
    } 
};