/*
 * 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 Fluster3Cluster(_fluster, _marker, categoryId)
{	
	// Get properties from marker
	var markerPosition = _marker.getPosition();
	
	// Properties
	this.fluster = _fluster;
	this.markers = [];
	this.unclusteredMarkers = [];
	this.bounds = null;
	
	//this is the div that is drawn to the screen
	//via the overlay
	//	this.marker.div.style.left = (position.x - parseInt(this.style.width / 2)) + 'px';
	//  this.marker.div.style.top = (position.y - parseInt(this.style.height / 2)) + 'px';
	this.marker = null;
	
	this.lngSum = 0;
	this.latSum = 0;
	this.center = markerPosition;
	this.map = this.fluster.getMap();
	this.categoryId = categoryId;
	this.maximumZoom = 15;//15 is the max zoom allowed by google maps at the time of writing.
	
	var me = this;
	
	// Get properties from fluster
	var projection = _fluster.getProjection();
	var gridSize = _fluster.gridSize;
	
	// Calculate bounds

	var position = projection.fromLatLngToDivPixel(markerPosition);
	//var position = this.map.getProjection().fromLatLngToPoint(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)
		//this.map.getProjection().fromPointToLatLng(positionSW),
	//	this.map.getProjection().fromPointToLatLng(positionNE)
		
	);
	
	/**
	 * Adds a marker to the cluster.
	 */
	this.addMarker = function(_marker)
	{
		this.markers.push(_marker);
	};

	/**
	 * Shows either the only marker or a cluster marker instead.
	 */
	this.show = function()
	{
		// Show markers if there is only 1 or we're at max zoom
		if(this.markers.length == 1 || this.map.getZoom() == this.maximumZoom)
		{
			//at maximum zoom, we need to draw the markers no matter what
			//if we don't, we get a group that can never be ungrouped.
			for(var index = 0; index < this.markers.length; index++)
			{
				if(this.markers[index].getMap() != me.map)
				{
					this.markers[index].setMap(me.map);
				}
				if(this.markers[index].getVisible() == false)
				{
					this.markers[index].setVisible(true);
				}
				//we need to keep track of these for spacing
				this.unclusteredMarkers.push(this.markers[index]);
			}
		}
		else if(this.markers.length > 1)
		{
			// Hide all markers
			for(var i = 0; i < this.markers.length; i++)
			{
				this.markers[i].setVisible(false);
			}
			
			// Create marker
			if(this.marker == null)
			{
				this.marker = new Fluster3ClusterMarker(this.fluster, this, this.categoryId);
				
				if(this.fluster.debugEnabled)
				{
					google.maps.event.addListener(this.marker, 'mouseover', me.debugShowMarkers);
					google.maps.event.addListener(this.marker, 'mouseout', me.debugHideMarkers);
				}
			} else {
				this.marker.show();
			}
			
			// Show marker
			//this.marker.draw();
		}
	};
	
	/**
	 * Hides the cluster
	 */
	this.hide = function()
	{
		if(this.marker != null)
		{
			this.marker.hide();
			this.marker.div.style.display = 'none';
		}
	};
	
	/**
	 * Hides the cluster
	 */
	this.remove = function()
	{
		if(this.marker != null)
		{
			this.marker.remove();
			this.marker.div.style.display = 'none';
		}
	};
	
	/**
	 * 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;
	};
	/**
	*returns the category ID of this cluster
	*/
	this.getCategoryId = function()
	{
		return this.categoryId;
	};
	
	// Add the first marker
	this.addMarker(_marker);
}
