seatuk.DealerLocator = function(options){

	var scope = this;
	this.markers =  [];
	this.map = null;
	this.options = {
		success : function() {},
		failed : function() {},
		callback : function() {},
		lat : 51.508742,
		lng : -0.131836,
		zoom : 9,
		serviceUrl : "/ws/dealer/location/",
		mapElement : "map_canvas",
		addressElement : "postcode",
		findElement : "find",
		defaultMarker : null
	};
		
	if (options != null){
		for(var o in options){
			this.options[o] = options[o];
		}
	}

	this.initialize = function() {
		scope = this;

		if ( this.map == null ) {
			var latlng = new google.maps.LatLng(this.options.lat, this.options.lng);
			var myOptions = {
				zoom: this.options.zoom,
				center: latlng,
				mapTypeId: google.maps.MapTypeId.ROADMAP
			};
			this.map = new google.maps.Map(document.getElementById(scope.options.mapElement), myOptions);

			// TODO Add initial marker
			if ( this.options.defaultMarker != null )
				this.addDefaultMarker();

			$("#" + this.options.findElement).click(function(event) {
				event.preventDefault();

				// Get the lat/lng
				var postcode =  $("#"+scope.options.addressElement).val();
				if ( postcode.length == 0 )
					return;

				scope.locate(postcode, scope.map);
			//				google.maps.event.addListener(scope.map, 'bounds_changed', function() {
			//					scope.locate(postcode, scope.map);
			//				});
			});
		}
	};

	this.addDefaultMarker = function() {
		var point = new google.maps.LatLng(this.options.defaultMarker.lat, this.options.defaultMarker.lng );
		var marker = new google.maps.Marker({
			map: this.map,
			position: point,
			title: this.options.defaultMarker.title
		});
	};
		
	this.locate = function(address) {
		var geocoder = new google.maps.Geocoder();
		geocoder.geocode({
			'address' : address + ", /UK",
			'region' : 'GB'
		},
		function(results, status) {
			if ( status == google.maps.GeocoderStatus.OK && results.length > 0 ) {
				var location = results[0].geometry.location;
				scope.map.setCenter(results[0].geometry.location);
				var bounds = scope.map.getBounds();
				var sw = bounds.getSouthWest();
				var ne = bounds.getNorthEast();

				//get the dealers near this location
				for(i in scope.markers){
					scope.markers[i].setMap(null);
				}
				delete(scope.markers);
				scope.markers = [];

				$.getJSON(scope.options.serviceUrl + address+"?sw="+sw.lat()+","+sw.lng()+"&ne="+ne.lat()+","+ne.lng(), function(data){
					if (data != null){
						if ( scope.options.success != null )
							scope.options.success( data );
							
						var dealers = data.dealers;

						for(i =0; i< dealers.length; i++){
							var dealer = dealers[i];
							var point = new google.maps.LatLng(dealer.location.lat, dealer.location.long);
							var marker = new google.maps.Marker({
								map: scope.map,
								position: point,
								title: dealer.name,
								dealerData : dealer
							});
							scope.markers.push(marker);

							google.maps.event.addListener(marker, 'click', function() {
								if (scope.options.callback != null)
									scope.options.callback(this.dealerData);
							});
						}
					} else {
				// No dealer data
				}
				});
			}
			else {
		// No results
		}
		}
		);
	};

	this.initialize();
};
	
