/*
 * 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/>.
 */

/**
 * Creates a new Fluster to manage masses of markers in a Google Maps v3.
 *
 * @constructor
 * @param {google.maps.Map} the Google Map v3
 * @param {bool} run in debug mode or not
 */
function Fluster2(_map, _debug)
{
	// Private variables
	var map = _map;
	var projection = new Fluster2ProjectionOverlay(map);
	var me = this;
	var clusters = new Object();
	var markersLeft = new Object();
	var bookId;
	// Properties
	this.debugEnabled = _debug;
	this.gridSize = 100;		// NB - used in calculation of bounds - units:pixels - originally 60
	this.markers = new Array();
	this.currentZoomLevel = -1;
	this.bookNums = new Array ();	// PNJ - to store number of books' markers added to cluster - must be cleared on init
	this.bookIds = new Array ();	// PNJ - to store number of books' markers added to cluster - must be cleared on init
	this.maxClusterScale = 9;		// PNJ - any zooming in closer than this (higher number) results in clustering being not used
	
	
	me.bookNums[0]=0;
	me.bookNums[1]=0;
	me.bookNums[2]=0;
	me.bookNums[3]=0;
	me.bookIds[0]=0;
	me.bookIds[1]=0;
	me.bookIds[2]=0;
	me.bookIds[3]=0;	// set up book counting variables	0?null?
	
	this.styles = {
	0: {
		image: 'images/BookClusterWhiteScaledWithShadow2.png',
		textColor: '#000000',
		shadow: 'images/BookClusterWhiteScaledShadow2.png',
		width: 130,
		height: 80
	},
	10: {
		image: 'images/BookClusterWhiteScaledWithShadow2.png',
		textColor: '#000000',
		shadow: 'images/BookClusterWhiteScaledShadow2.png',
		width: 130,
		height: 80
	},
	20: {
		image: 'images/BookClusterWhiteScaledWithShadow2.png',
		textColor: '#000000',
		shadow: 'images/BookClusterWhiteScaledShadow2.png',
		width: 130,
		height: 80
	}

	};
	
	// Timeouts
	var zoomChangedTimeout = null;
	
	/**
	 * Create clusters for the current zoom level and assign markers.
	 */
	function createClusters()
	{
		

		var zoom = map.getZoom();
		
		if(clusters[zoom])
		{
			me.debug('Clusters for zoom level ' + zoom + ' already initialized.');//if(clusters[zoom].markers != null) alert(clusters[zoom].markers.length);
		}
		else
		{
			// Create clusters array
			var clustersThisZoomLevel = new Array();
			
			// Set cluster count
			var clusterCount = 0;
			
			// Get marker count
			var markerCount = me.markers.length;
			// Walk all markers
			for(var i = 0; i < markerCount; i++)
			{
				var marker = me.markers[i];
				var markerPosition = marker.getPosition();
				var done = false;
				
				// Find a cluster which contains the marker
				for(var j = clusterCount - 1; j >= 0; j--)
				{
					var cluster = clustersThisZoomLevel[j];
					if(cluster.contains(markerPosition))
					{
						cluster.addMarker(marker);
						done = true;
						break;
					}
				}
				
				if(!done)
				{
					// No cluster found, create a new one
					var cluster = new Fluster2Cluster(me, marker);
					clustersThisZoomLevel.push(cluster);
					
					// Increase cluster count
					clusterCount++;
				}
			}
			
			clusters[zoom] = clustersThisZoomLevel;
			
			me.debug('Initialized ' + clusters[zoom].length + ' clusters for zoom level ' + zoom + '.');
		}
		
		// Hide markers of previous zoom level
		if(clusters[me.currentZoomLevel])
		{
			for(var i = 0; i < clusters[me.currentZoomLevel].length; i++)
			{
				clusters[me.currentZoomLevel][i].hide();
			}
		}
		
		// Set current zoom level
		me.currentZoomLevel = zoom;
		
		// Show clusters
		showClustersInBounds();
	}
	
	/**
	 * Displays all clusters inside the current map bounds.
	 */
	function showClustersInBounds()
	{
		var mapBounds = map.getBounds();
		
		for(var i = 0; i < clusters[me.currentZoomLevel].length; i++)
		{
			var cluster = clusters[me.currentZoomLevel][i];
			if(mapBounds.contains(cluster.getPosition()))
			{
				cluster.show();
			}
		}
	}
	
	/**
	 * Callback which is executed 500ms after the map's zoom level has changed.
	 */
	this.zoomChanged = function()
	{
		window.clearInterval(zoomChangedTimeout);
		zoomChangedTimeout = window.setTimeout(createClusters, 500);
	};
	
	/**
	 * Returns the map assigned to this Fluster.
	 */
	this.getMap = function()
	{
		return map;
	};
	
	/**
	 * Returns the map projection.
	 */
	this.getProjection = function()
	{
		return projection.getP();
	};
	
	/**
	 * Prints debug messages to console if debugging is enabled.
	 */
	this.debug = function(message)
	{
		if(me.debugEnabled)
		{
			console.log('Fluster2: ' + message);
		}
	};
	
	/**
	 * Adds a marker to the Fluster.
	 */
	this.addMarker = function(_marker)		// called as fluster.addMarker
	{

		//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  REMmed 24/6/11: opacity should now be doing this by genre & book
		{
			me.markers.push(_marker);

			bookId = _marker.getTitle().split('#')[1];	// get marker's book number	- NB currently positioned to only number visible PoIs
						
			// this seems to work (because markers loaded in order?), but using existing mapbookid would ensure proper colour sequence			
			if (me.bookIds[0] == 0 && me.bookIds[0] != bookId)
			{
				me.bookIds[0] = bookId;
			}
			else if (me.bookIds[1] == 0 && me.bookIds[0] != bookId && me.bookIds[1] != bookId)
			{
				me.bookIds[1] = bookId;
			} 
			else if (me.bookIds[2] == 0 && me.bookIds[0] != bookId && me.bookIds[1] != bookId && me.bookIds[2] != bookId)
			{
				me.bookIds[2] = bookId;
			}
			else if (me.bookIds[3] == 0 && me.bookIds[0] != bookId && me.bookIds[1] != bookId && me.bookIds[2] != bookId && me.bookIds[3] != bookId)
			{
				me.bookIds[3] = bookId;
			}
			
			if (bookId == me.bookIds[0])
			{
				me.bookNums[0]++;
			}
			
			if (bookId == me.bookIds[1])
			{
				me.bookNums[1]++;
			}
			
			if (bookId == me.bookIds[2])
			{
				me.bookNums[2]++;
			}

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


			
		}
		// undefined is value, at first load, of marker's title
		
		
	};
	
	/**
	 * Returns the currently assigned styles.
	 */
	this.getStyles = function()
	{
		return me.styles;
	};
	
	/**
	 * Sets map event handlers and setup's the markers for the current
	 * map state.
	 */
	this.initialize = function()
	{		
		// Add event listeners
		google.maps.event.addListener(map, 'zoom_changed', this.zoomChanged);
		google.maps.event.addListener(map, 'dragend', showClustersInBounds);

		// Setup markers for the current state

	};
	
	this.clearClusterMarkers = function ()	// PNJ
	{
		
		for (var i=0;i<this.markers.length;i++)
		{
			this.markers[i].setMap(null);	// set marker's map to nothing
			alert(i);
		}
		
		//alert('my OWN !!! markers cleared');
	};
	
	this.clearClusters = function()	// PNJ
	{ //added by PNJ 30/3/10
		
		for (var zoom=0;zoom < 18;zoom++)	// for every zoom scale

		{
			if (clusters[zoom])
			{
				alert(zoom);
				alert(clusters[zoom].length);
				for (var i=0;i<clusters[zoom].length;i++)
				{
					clusters[zoom].pop();	// just pop each cluster out from each zoom level
					alert('cluster popped');
				}
			}
		}
		
		map.setZoom(map.getZoom());map.panTo(map.getCenter());	// reset map to start showing recalculated clusters
	};
	
	this.hideClusters = function()	// PNJ
	{	// added by PNJ 30/3/10
		
		for (var i=0;i < 18;i++)	// for every zoom scale
			if(clusters[i])		// set the cluster element of that array to null
			{
			for(var j=0;j<clusters[i].length;j++)
				clusters[i][j].hide();		// hide existing cluster marker
			}
		// get rid of cluster marker on current zoom level
	};
	
	this.showClusters = function ()	// PNJ - adapatation of showClustersInBounds as public
	{
		var mapBounds = map.getBounds();
		
		if (this.markers.length > 0)
		{
			for(var i = 0; i < clusters[this.currentZoomLevel].length; i++)
			{
				var cluster = clusters[this.currentZoomLevel][i];
				if(mapBounds.contains(cluster.getPosition()))
				{
					cluster.show();
				}
			}
		}
	};		
}
