﻿BenefulMap.MapCount = Class.extend({
	init: function(spotCountDiv, locationText) {
	    this.spotCountDiv = spotCountDiv;
	    this.locationText = locationText;
	    
	    this.updateListener();
	},
	getSpotCountDiv: function() {
	   return this.spotCountDiv;
	},
	getLocationText: function() {
	   return this.locationText;
	},
	update: function(mapState) {
	    var self = this;
	    WagWorld.Web.AjaxUtility.GetLocationTotals(mapState.toJSON(), function(result) {
    		mapState.setTotalSpots(result.value.TotalCount);
    		mapState.setTotalFriendlyCount(result.value.FriendlyTotalCount);
	    	var countText;
    		if (mapState.isDefaultView()) {
				var currentNum = mapState.getPage() + "0";
				countText = '<span id="default-view-count">' + currentNum + '</span>' + ' of ' + mapState.getTotalFriendlyCount();
			} else {
				countText = mapState.getTotalSpots();
			}
			$(self.getSpotCountDiv()).html(countText);
    		$(self.getLocationText()).show();
    		$(document).trigger('pagination.update', [mapState]);
    	});
	},
	updateListener: function() {
	   var self = this;
	   $(document).bind('count.update', function(event, mapState) {
            console.log("count.update fired"); 
            self.update(mapState); 
	   });
	}
});

