/*
GOOGLE API CLUSTERING v2a
Carl Barnett 2009
This file is self contained and should NOT require any adjustments...All parameters can be passed to this file.
*/	

function Getcluster(markers, distance, zoom) {
	var clustered = new Array();
	var marker = new Array();
	var x=0;
	var j=0;
	var TotalX;
	var TotalY;
	var ClusterCount=0;
	while (markers.length) {
		TotalX=0;
		TotalY=0;
		marker  = markers.pop(); // Firstly i need to get 1 of the items from the marker array to compare against. Once taken the array size reduces by 1.
		TotalX+=parseFloat(marker[0]);
		TotalY+=parseFloat(marker[1]);
		marker[6] = marker[3]+'#';
		for (i=0;i<markers.length;i++){	//Run through all my other markers and compare against the one i popped off.
			if (markers[i][0] && marker[0]){
				var pixels=pixelDistance(marker[0],marker[1],markers[i][0],markers[i][1],zoom);
			}
			if (distance > pixels) {
				TotalX+=parseFloat(markers[i][0]);
				TotalY+=parseFloat(markers[i][1]);
				markers[i][0]='' //Set it to blank so we know its been merged or is the same as another!!!
				ClusterCount++; //keep track of how many targets we've hit!!! We null the ones weve hit so we cant compare them again ;) -Sneaky skills.
				pixels=99999; //prevent next loop using old values - pwnage hack as it'll never be reached!
				//CHECK TO SEE IF WE HAVE MULTIPLE TYPES OF POI - IF WE DO WE NEED TO CHANGE THE VARIABLE FOR THE CLUSTER - Remeber array slot 4 holds the type it is so we check on that.
				if(marker[4]!=markers[i][4]){
					var str=marker[5];
					if (str==''){
						str=marker[4]+'-';
					}
					if (str.indexOf(markers[i][4]+'-')==-1){
						str+=markers[i][4]+'-';
					}
					marker[5]=str;
				}
				//FINAL PATCH TO STORE UP THE IDS OF ITEMS THAT ARE WITHIN THIS CLUSTER.
				marker[6]+=markers[i][3]+'#';
			}
		}
		if(marker[0]){
			cluster = new Array();
			cluster[0]=new Array(7);					
			cluster[0][3] = marker[3];
			cluster[0][4] = marker[4]; //Holds the individual POI type - Not sure why i have this seperate to item 5 ive got a bit lost with it all!!!! - in theory items 4,5 and 6 should be able to be merged i think....
			cluster[0][5] = marker[5]; //Holds the POI type list in the POI calls - Not required in Property calls
			cluster[0][6] = marker[6]; //Holds the clustered ID listings so we can drag the data back out on a click event
			if (ClusterCount > 0) {
				TotalX=(TotalX/(ClusterCount+1));
				TotalY=(TotalY/(ClusterCount+1));
				cluster[0][0] = TotalX;
				cluster[0][1] = TotalY;
				cluster[0][2]=(ClusterCount+1);//Set icon type for multiple grouped results. Returns just an int in the 3rd array cell - pretty simple.
				clustered[j] = cluster;
				ClusterCount=0;
			} else {
				cluster[0][0] = marker[0];
				cluster[0][1] = marker[1];
				cluster[0][2]='1';//Set icon type for none grouped results. Returns just an int in the 3rd array cell - pretty simple. Usually a 1 for single elements.
				clustered[j] = cluster;
			}
		}
		j++;
	}
	return clustered;
}
	
function lonToX(lon) {
	return Math.round(OFFSET + RADIUS * lon * PI / 180);        
}

function latToY(lat) {
	return Math.round(OFFSET - RADIUS * 
		Math.log((1 + Math.sin(lat * PI / 180)) / 
		(1 - Math.sin(lat * PI / 180))) / 2);
}

function pixelDistance(lat1, lon1, lat2, lon2, zoom) {
	x1 = lonToX(lon1);
	y1 = latToY(lat1);
	x2 = lonToX(lon2);
	y2 = latToY(lat2);
	return Math.sqrt(Math.pow((x1-x2),2) + Math.pow((y1-y2),2)) >> (21 - zoom);
}