var PublicMap = new Class({
	
	options:
	{
		
	},
	
	markerCache:[],
	
	showedApartment: "",
	
	initialize: function(container)
	{
		
		this.canvas = $(container);
		this.setUp();
		this.registerEvents()
	},
	
	setUp: function()
	{
		this.bus = new SearchBus()
	},
	
	registerEvents: function()
	{
		this.bus.registerCallback(this.hideMap.bind(this)					, "showFavorites");
	},
	
	mapShowed : false,
	
	showMap: function()
	{
		this.canvas.setStyle("display","block")	
		this.instanceMap();
		this.mapShowed = true;
	},
	
	hideMap: function()
	{
		this.canvas.setStyle("display", "none");
		this.mapShowed = false;
	},
	
	hideMarkers: function()
	{
		if($defined(this.map))
		{
			this.map.hideMarkers();
		}
	},
	
	instanceMap: function()
	{
		if(!$defined(this.map))
		{
			this.map = new MooMap(this.canvas);
			if(this.markerCache.length > 0)
			{
				this.addMarkersOnCache();
			}
			this.map.addEvent('onMarkClick', this.showApartment.bind(this));
		}
	},
	
	showApartment: function(marker, markerHash)
	{	
		if (this.showedApartment != "") {
			this.showedApartment.setStyle("display", "none");
		}			
		apartment = $(markerHash.id);
		apartment.setStyle("display","block");
		this.showedApartment = apartment;
	},
	
	addMarkersOnCache: function()
	{
		this.markerCache.each(function(marker){
			this.map.addMarker(marker);
		}.bind(this))
		
		this.markerCache = [];
		this.zoomToMarkers();
	},
	
	zoomToMarkers: function()
	{
		if($defined(this.map))
		{
			this.map.zoomToMarkers();
		}
	},
	
	addApartment: function(apartment)
	{
		var markerData = {};
		markerData.id = apartment.apartmentId.CRS+apartment.apartmentId.remoteId;
		
		var geoLocation = apartment.geoLocation;		
		if($type(geoLocation) == "object")
		{
			markerData.gps = {};
			markerData.gps.lat = geoLocation.latitude;
			markerData.gps.lng = geoLocation.longitude;
		}		
		else
		{
			markerData.address = geoLocation;
		}
		
		if ($defined(this.map)) 
		{
			this.map.addMarker(markerData);
		}
		else
		{
			this.markerCache.push(markerData);
		}
	}
});
