PublicLocal = function()
{
	this.start();	
}

PublicLocal.prototype = new Local();

// Resize the map viewport based on the height of the screen, 
// minus the nav bar at the top of the screen.
PublicLocal.prototype.resizeMap = function()
{
	var mapHeight = document.viewport.getHeight()-($('headWrapper').getHeight() + $('mapHead').getHeight() + 60);
	
	if ($('msg-message').visible())
	{
		mapHeight = mapHeight - $('msg-message').getHeight();
	}

	$('map').setStyle({height: mapHeight +'px'});
}

PublicLocal.prototype.buildMapPoints = function(opt_opts)
{	
	// Get the bubble html and populate some vars.
	this.getBubbleHtml_();

	var opts = (opt_opts == null) ? {} : opt_opts;

	var showUserPins 	= (opts.showUserPins == null) 	? this.getUserPinStatus()  	: opts.showUserPins;
	var showFriendPins 	= (opts.showFriendPins == null) ? this.getFriendPinStatus()	: opts.showFriendPins;
	var spotType 		= (opts.spotType == null) 		? this.getCategory() 		: opts.spotType;
	
	// Get the map points.
	var bounds = this.map.getBounds();
	
	// Cancel any previous request..
	if (this.ajaxPoints)
	{
		this.ajaxPoints.transport.abort();
	}
	
	this.clusterPoints = [];
	var that = this;
	
	// Fire up the ajax and load the initial set of spots.
	this.ajaxPoints = new Ajax.Request('/ajax/get_spots/type/'+spotType+'/show_user_pins/'+showUserPins+'/show_friend_pins/'+showFriendPins+'/sw_lat/'+bounds.getSouthWest().lat()+'/sw_long/'+bounds.getSouthWest().lng()+'/ne_lat/'+bounds.getNorthEast().lat()+'/ne_long/'+bounds.getNorthEast().lng()+'/', 
	{
		method: 'get',
		onSuccess: function (transport, json) 
		{
			json = eval(transport.responseText);
			
			var jsonLen = json.length;
			
			for (i = 0; i < jsonLen; ++i)
			{
				var bubbleHtml = '';
				
				if (json[i].userid > 0)
				{
					if (json[i].userid == MPORA.globalVars.uid)
					{
						// My spot.	
						json[i].spot_type_url = 'my_spot';
					}
					else
					{
						// Friend spot.	
						json[i].spot_type_url = 'friend_spot';
					}

					bubbleHtml = new Template(that.userBubbleHtml_);

				}
				else
				{
					bubbleHtml = new Template(that.spotBubbleHtml_);	
				}
				
				// Cut down the scription..
				if (json[i].description && json[i].description.length > 100)
				{
					json[i].description = json[i].description.substr(0, 100) + '...';
				}
				
				// Eval the template and get the final html.
				that.addExistingPoint({point:that.getLatLong(json[i].spot_lat, json[i].spot_long), htmlSrc:bubbleHtml.evaluate(json[i]), spot_type: json[i].spot_type_url}, json[i].id);
			}
			
			if (!that.cluster)
			{
				that.cluster=new ClusterMarker(that.map, { fitMapToMarkers:false} );
			}
			
			that.cluster.removeMarkers();
			that.cluster.addMarkers(that.clusterPoints);
			that.cluster.refresh();	
			//that.map.closeExtInfoWindow();
			
			
			if (that.clusterPoints.length >= 200)
			{
				new Effect.Appear('msg-message', {duration:0.5}); //, afterFinish:function(){ that.resizeMap(); }
			}
			else
			{
				new Effect.Fade('msg-message', {duration:0.5}); //afterFinish:function(){ that.resizeMap(); }	
			}	
		}
	});	
}

PublicLocal.prototype.getFriendPinStatus = function()
{
	return ($('my_friends_spots') && $('my_friends_spots').hasClassName('my_friends_pin_selected')) ? true : false;
}

PublicLocal.prototype.getUserPinStatus = function()
{
	return ($('my_spots') && $('my_spots').hasClassName('my_spots_pin_selected')) ? true : false;
}

PublicLocal.prototype.getCategory = function()
{
	return $F('categories');
}

PublicLocal.prototype.getBubbleHtml_ = function()
{
	var that = this;
	
	if (!this.spotBubbleHtml_)
	{
		new Ajax.Request('/ajax/get_bubble_html/spot/', 
		{
			method: 'get',
			onSuccess: function (transport) 
			{
				that.spotBubbleHtml_ = transport.responseText;
			}
		});	
	}

	if (!this.userBubbleHtml_)
	{
		new Ajax.Request('/ajax/get_bubble_html/user/', 
		{
			method: 'get',
			onSuccess: function (transport) 
			{
				that.userBubbleHtml_ = transport.responseText;
			}
		});	
	}
}