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

    if (GBrowserIsCompatible()) {
    
        this.init(mapDiv, center, zoomLevel, imagesBase, 
                stores);
                
    } else {
        alert("Google maps unable to work on this browser.");
    }
}
            
peapod.DetailsMapController.prototype = {
            
    init:function(mapDiv, center, zoomLevel, imagesBase,
             stores) {
    
        this.imagesBase = imagesBase;
        this.stores = stores;
                
        var canvas = $(mapDiv);
        
        this.map = new GMap2(canvas);
        
        this.map.addControl(new GScaleControl());
        
        var cen = new GLatLng(center.lat, center.lng);
                    
        this.map.setCenter(cen, zoomLevel);
        
        var startPoint = new GLatLng(this.stores[0].lat, this.stores[0].lng);
        var bounds = new GLatLngBounds();
        var offsetBounds = new GLatLngBounds();
        this.map.addControl(new GSmallMapControl());
                    	
        for (var i = 0; i < this.stores.length; i++) { 
        	this.addStoreDetailLocation(this.stores[i], (i + 1), bounds, cen, i);
        }
        	
        if (this.stores.length > 1) {
        	offsetBounds.extend(startPoint);
            offsetBounds.extend(bounds.getCenter());
            this.map.setZoom(Math.round(this.map.getBoundsZoomLevel(bounds)*.95));
            this.map.setCenter(offsetBounds.getCenter());
        }
        
        Event.observe(window, 'unload', GUnload);
    },
                
    addStoreDetailLocation:function(store, filename, bounds, center, num) {
         var point = new GLatLng(store.lat, store.lng);
                  
         if (num == 0) {
        	 var marker = new GMarker(center, {clickable:false});
         } else
         {
        	 var micon = new GIcon();
        	 micon.image = this.imagesBase + store.storeType + '/' + 'store-icon.png';
       		 micon.iconSize= new GSize(24, 24);
        	 micon.iconAnchor= new GPoint(12, 12);
             
             var opts = {clickable:true, icon:micon};
             var marker = new GMarker(point, opts);
             
             GEvent.addListener(marker, "click", function() {
            	 window.location = "/our_stores/locator/store_details.htm?storeNumber=" + store.storeId + "&storeType=" + store.storeType;
             });
         }
         
         bounds.extend(point);
         
         this.map.addOverlay(marker);
      }
};
