/*
 * Fluster2 0.1.1
 * Copyright (C) 2009 Fusonic GmbH
 *
 * This file is part of Fluster2.
 *
 * Fluster2 is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * Fluster2 is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
 */

/**
 * Cluster which holds one or more markers of the map.
 *
 * @constructor
 * @private
 * @param {Fluster2} the Fluster2 itself
 * @param {google.maps.Marker} the first marker
 */
function Fluster2Cluster(_fluster, _marker)
{	
	// Get properties from marker
	var markerPosition = _marker.getPosition();
	
	// Properties
	this.fluster = _fluster;
	this.markers = [];
	this.bounds = null;
	this.marker = null;
	this.lngSum = 0;
	this.latSum = 0;
	this.center = markerPosition;
	this.map = this.fluster.getMap();
	
	this.bookNums = new Array ();	// PNJ - to store number of books' markers added to cluster - must be cleared on init

	
	
	this.bookNums[0]=0;
	this.bookNums[1]=0;
	this.bookNums[2]=0;
	this.bookNums[3]=0;
		
	
	var me = this;
	
	// Get properties from fluster
	var projection = _fluster.getProjection();
	var gridSize = _fluster.gridSize;
	
	// Calculate bounds
	var position = projection.fromLatLngToDivPixel(markerPosition);
	var positionSW = new google.maps.Point(
		position.x - gridSize,
		position.y + gridSize
	);
	var positionNE = new google.maps.Point(
		position.x + gridSize,
		position.y - gridSize
	);
	this.bounds = new google.maps.LatLngBounds(
		projection.fromDivPixelToLatLng(positionSW),
		projection.fromDivPixelToLatLng(positionNE)
	);
	
	/**
	 * Adds a marker to the cluster.
	 */
	this.addMarker = function(_marker)
	{
		if((globalPoiGenreVisibility[_marker.getTitle().split('#')[0]] == true) && (globalPoiBookVisibility[_marker.getTitle().split('#')[0]] == true)|| (globalPoiGenreVisibility[_marker.getTitle().split('#')[0]] == undefined))	// lookup poiid in global visibility array	- PNJ 31/3/10
			// 24/6/11 even though opacity now used, this section uses visibility in counting sum of visible locations (therefore left in situ)
		{
			this.markers.push(_marker);
			bookId = _marker.getTitle().split('#')[1];	// get marker's book number	- NB currently positioned to only number visible PoIs
			// USE MAPBOOKID!!!!!
						
						if (_fluster.bookIds[0] == 0 && _fluster.bookIds[0] != bookId)
						{
							_fluster.bookIds[0] = bookId;
						}
						else if (_fluster.bookIds[1] == 0 && _fluster.bookIds[0] != bookId && _fluster.bookIds[1] != bookId)
						{
							_fluster.bookIds[1] = bookId;
						} 
						else if (_fluster.bookIds[2] == 0 && _fluster.bookIds[0] != bookId && _fluster.bookIds[1] != bookId && _fluster.bookIds[2] != bookId)
						{
							_fluster.bookIds[2] = bookId;
						}
						else if (_fluster.bookIds[3] == 0 && _fluster.bookIds[0] != bookId && _fluster.bookIds[1] != bookId && _fluster.bookIds[2] != bookId && _fluster.bookIds[3] != bookId)
						{
							_fluster.bookIds[3] = bookId;
						}
						
						
						if (bookId == _fluster.bookIds[0])
						{
							this.bookNums[0]++;
						}
						
						if (bookId == _fluster.bookIds[1])
						{
							this.bookNums[1]++;
						}
						
						if (bookId == _fluster.bookIds[2])
						{
							this.bookNums[2]++;
						}

						
						if (bookId == _fluster.bookIds[3])
						{
							this.bookNums[3]++;
						}


		}
	};

	/**
	 * Shows either the only marker or a cluster marker instead.
	 */
	this.show = function()
	{
		
		
		// Show marker if there is only 1
		if(this.markers.length == 1 ||  this.map.getZoom() >= _fluster.maxClusterScale || globalBoolClusterVisible == false)		
		{
			for (var i=0;i<this.markers.length;i++)
			{				

				if (this.markers[i])
				{
					if((globalPoiGenreVisibility[this.markers[i].getTitle().split('#')[0]] == true) && (globalPoiBookVisibility[this.markers[i].getTitle().split('#')[0]] == true))	// lookup poiid in global visibility array	- PNJ 31/3/10 REMmed 24/6/11 if opacity now in effect for genre & book
					{
					this.markers[i].setMap(me.map);
					this.markers[i].setVisible(true);	// added by PNJ to trigger event, to set label's visibility
					}	// end of PNJ 31/3/10 'if'
				}
			}
		}
		else if(this.markers.length > 1)
		{
			// Hide all markers
			for(var i = 0; i < this.markers.length; i++)
			{
				this.markers[i].setVisible(false);	// added by PNJ to trigger event, to set labels' visibility
				this.markers[i].setMap(null);
			}
			
			// Create marker
			if(this.marker == null)
			{
				this.marker = new Fluster2ClusterMarker(this.fluster, this);
				
				if(this.fluster.debugEnabled)
				{
					google.maps.event.addListener(this.marker, 'mouseover', me.debugShowMarkers);
					google.maps.event.addListener(this.marker, 'mouseout', me.debugHideMarkers);
				}
			}
			
			// Show marker
			this.marker.show();
		}
	};
	
	/**
	 * Hides the cluster
	 */
	this.hide = function()
	{
		if(this.marker != null)
		{
			this.marker.hide();
		}
	};
	
	/**
	 * Shows all markers included by this cluster (debugging only).
	 */
	this.debugShowMarkers = function()
	{
		for(var i = 0; i < me.markers.length; i++)
		{
			me.markers[i].setVisible(true);
		}
	};
	
	/**
	 * Hides all markers included by this cluster (debugging only).
	 */
	this.debugHideMarkers = function()
	{
		for(var i = 0; i < me.markers.length; i++)
		{
			me.markers[i].setVisible(false);
		}
	};
	
	/**
	 * Returns the number of markers in this cluster.
	 */
	this.getMarkerCount = function()
	{
		return this.markers.length;
	};
	
	/**
	 * Checks if the cluster bounds contains the given position.
	 */
	this.contains = function(_position)
	{
		return me.bounds.contains(_position);
	};
	
	/**
	 * Returns the central point of this cluster's bounds.
	 */
	this.getPosition = function()
	{
		return this.center;
	};

	/**
	 * Returns this cluster's bounds.
	 */
	this.getBounds = function()
	{
		return this.bounds;
	};

	/**
	 * Return the bounds calculated on the markers in this cluster.
	 */
	this.getMarkerBounds = function()
	{
		var bounds = new google.maps.LatLngBounds(
			me.markers[0].getPosition(),
			me.markers[0].getPosition()
		);
		for(var i = 1; i < me.markers.length; i++)
		{
			bounds.extend(me.markers[i].getPosition());
		}
		return bounds;
	};
	
	// Add the first marker
	this.addMarker(_marker);
}
