/**
 * Used by search store by 'choose address' in homepage.
 * As 'Choose address' already create a map object, then will need a reference to that object.
 */
var MultipleStoresOnMapByAddressChooser = Class.create(MultipleStoresOnMap,{
	initialize: function($super, options){
		$super(options);	
		var options 	=	Object.extend(
				{
					searchFormId: 'address_chooser_form',
					searchUrl: '/storelocator/search/lat-long/',
					map: null
				},
				options || {}
		);
		this.options = Object.extend(this.options,options);
		this.map = this.options.map;
	},
	
	getStoresByLatLong: function(lat,long,searchRadious){
		var option = {
				parameters: {lat:lat,long:long,search_radious:searchRadious},
				method: 'post',
				onLoading: function(){
					$(this.options.searchFormId).disable();	
				}.bind(this),
				onSuccess: function(transport){
					$(this.options.searchFormId).enable();
					if(transport.responseJSON.code == 0){
						this._showSearchResultOnMap(transport.responseJSON.data)
							
					} else if(transport.responseJSON.code == 1 && transport.responseJSON.data != null){
						this._showClosestStore(transport.responseJSON.data);
					} else if(transport.responseJSON.code == 1 && transport.responseJSON.data == null){	
						alert("There is no store");
					} else{
						alert('error: ' + transport.responseJSON.message);
					}
				}.bind(this)
			};
		new Ajax.Request(this.options.searchUrl,option);		
	},
	
	_showClosestStore: function(store){
		alert("There is no store with identified radius.\nThe closest store is about " + store.distance + " kms from your address.");
		this._reset();
		this.addStore(store);
		this._showOneStore();
	},
	
	_showSearchResultOnMap: function(stores){
		this._reset();
		for(i = 0, len = stores.length; i < len; i++){
			this.addStore(stores[i]);
		}
		if(this.stores == null || this.stores.length == 0){
			return;
		}
        if(this.stores.keys().length == 1){
        	this._showOneStore();
        } else{
        	this._showMultipleStores();
        }
	}
});