/**
Don't use this class directly, extends it instead.
Offer: showing multiple store with info window.
 */
var MultipleStoresOnMap = Class.create();
MultipleStoresOnMap.prototype = {
	options : {
		markerIconImage: '/data/js/customized_modules/storelocator/images/marker.png',
		markerIconImageSelected: '/data/js/customized_modules/storelocator/images/marker_selected.png'
	},
	initialize: function(options){
		this.options 	=	Object.extend(this.options,options || {});
		
		this.stores = new Hash();
		this.minLat = null;
		this.maxLat = null;
		this.minLng = null;
		this.maxLng = null;
		
	},
	
	addStore: function(store){
		var icon = new GIcon(G_DEFAULT_ICON);
		icon.image = this.options.markerIconImage;
		
		var point = new GLatLng(store.lat,store.long);
		var marker = new GMarker(point,icon);
		GEvent.addListener(marker,'click',function(){
			var storeIntroHtml = 
				"<div class='map_info_window'>" + 
				"<a href='/storelocator/store/info/store_id/" + store.store_id + "'>" + store.store_name + "</a><br/>" +
				store.address + ', ';
			if(!store.suburb.empty()){
				storeIntroHtml += store.suburb + ', ';
			}
			storeIntroHtml += store.state_code + '<br>';
			if(!store.phone.empty()){
				storeIntroHtml += "<strong>Phone: </strong> " + store.phone + "<br>";
			}
			
			if(!store.email.empty()){
				storeIntroHtml += "<strong>Email: </strong> " + store.email + "<br>";
			}
			storeIntroHtml += "<br><br><br>";
			
			storeIntroHtml += "<a target='_blank' href='http://maps.google.com.au/maps?f=d&saddr=&daddr=" + store.lat + ", " + store.long + "&hl=en'>Get direction</a>";
			storeIntroHtml += "</div>";
			
			marker.openInfoWindowHtml(storeIntroHtml);
			
		});
		
		store.marker = marker;
		this.stores.set(store.store_id,store);
		
		var latitude = Number(store.lat);
		var longitude = Number(store.long); 
		if(typeof(latitude) == 'number' && typeof(longitude) == 'number') {
			if(this.minLat == null || this.minLat > latitude) {
				this.minLat = latitude;
			}
		
			if(this.maxLat == null || this.maxLat < latitude) {
				this.maxLat = latitude;
			}
		
			if(this.minLng == null || this.minLng > longitude) {
				this.minLng = longitude;
			}
		
			if(this.maxLng == null || this.maxLng < longitude) {
				this.maxLng = longitude;
			}
		}
	},
	
	_reset: function(){
		//remove old store data
		if(this.stores != null && this.stores.length > 0){
			this.stores.each(function(pair){
				var store = pair.value;
				var marker = store.marker;
				this.map.removeOverlay(marker);
			}.bind(this));
		}
		
		this.stores = new Hash();
		
		this.minLat = null;
		this.maxLat = null;
		this.minLng = null;
		this.maxLng = null;
		
	},
	
	/**
	 * This version finding the bound manually, but not so correctly as the marker at bound still doesn't show completely
	 * @learnfrom:
	 * http://aiskahendra.blogspot.com/2009/01/set-zoom-level-of-google-map-base-on.html
	 * http://www.adick.at/2008-12-30,set-zoom-level-of-a-google-map-so-that-all-markers-are-visible/
	 * http://www.mixd.com.br/irineu/gmaps/index.php
	 * 
	 * @return void
	 */
	_showMultipleStores_bk: function(){
		var mapContainer = $(this.options.mapContainerId);
		var mapDisplaySize = Math.min(mapContainer.getWidth(),mapContainer.getHeight());
		
		//avg of the coordinates
		var centerLat = this.minLat + ((this.maxLat - this.minLat) / 2);
		var centerLng = this.minLng + ((this.maxLng - this.minLng) / 2);
		var interval = 0;
		
		if ((this.maxLat - this.minLat) > (this.maxLng - this.minLng)) {
			interval = (this.maxLat - this.minLat) / 2;
			this.minLng = centerLat - interval;
			this.maxLng = centerLat + interval;
		} else {
			interval = (this.maxLng - this.minLng) / 2;
			this.minLat = centerLng - interval;
			this.maxLat = centerLng + interval;
		}
		
		//what's the distance of our coordinates? (in kilometers)
		var dist = (6371 *
						Math.acos(
							Math.sin(this.minLat / 57.2958) *
							Math.sin(this.maxLat / 57.2958) + (
								Math.cos(this.minLat / 57.2958) *
								Math.cos(this.maxLat / 57.2958) *
								Math.cos(this.maxLng / 57.2958 - this.minLng / 57.2958)
							)
						)
					);
		
		var zoom = Math.floor(
						8 - Math.log(1.6446 * dist / Math.sqrt(2 * (mapDisplaySize * mapDisplaySize))) / Math.log (2)
					);

		//add markers for all stores
		this.stores.each(function(pair){
			var store = pair.value;
			var marker = store.marker;
			this.map.addOverlay(marker);			
		}.bind(this));
		
		//center map and set zoomLvl
		this.map.setCenter(new GLatLng(centerLat, centerLng), zoom);
	},
	
	_showMultipleStores: function(){
		//avg of the coordinates
		var centerLat = this.minLat + ((this.maxLat - this.minLat) / 2);
		var centerLng = this.minLng + ((this.maxLng - this.minLng) / 2);
		
		var minPoint = new GLatLng(this.minLat,this.minLng);
		var maxPoint = new GLatLng(this.maxLat,this.maxLng);
		var bounds = new GLatLngBounds(minPoint,maxPoint);
		var zoom = this.map.getBoundsZoomLevel(bounds);
		
		//add markers for all stores
		this.stores.each(function(pair){
			var store = pair.value;
			var marker = store.marker;
			this.map.addOverlay(marker);			
		}.bind(this));
		
		//center map and set zoomLvl
		this.map.setCenter(new GLatLng(centerLat, centerLng), zoom);
	},	
	
	_showOneStore: function(){
		var store = this.stores.values()[0];
		var marker = store.marker;
		this.map.addOverlay(marker);	
		var point = new GLatLng(Number(store.lat), Number(store.long));
		var room = parseInt(store.google_map_zoom_level);
		this.map.setCenter(point, room);
	}	
};