function Cluster( bounds, numberOfContainedMarkers, zoomlevel ) {
	this.bounds = bounds;
	this.numMarkers = numberOfContainedMarkers;
	this.zoomlevel = zoomlevel;
	this.clusterData = [];
	this.latSum = 0;
	this.lngSum = 0;
	this.isActive = false;
	this.tourButtonSize = 0;
	this.tourButtonFocusPoint = { x: 12.24, y: 89.79 }; // percentage of x and y of image-focus-point
	//this.tourButtonFocusPoint = null; // set to null for default center mode!

}

Cluster.prototype = new GOverlay();

Cluster.prototype.getIcon = function( name ) {
	if( clusterIcons[name] != undefined && clusterIcons[name] ) {
		return clusterIcons[name];		
	}
	else { return ""; }
}


Cluster.prototype.getControlCenter = function() {
	return new GLatLng( this.latSum / this.numMarkers, 
						this.lngSum / this.numMarkers );
}

Cluster.prototype.getBounds = function() {
	return this.bounds; 
}

Cluster.prototype.getCenter = function() {
	return this.bounds.getCenter(); 
}

Cluster.prototype.getNumberOfMarkers = function() {
	return this.numMarkers;
}


Cluster.prototype.initialize = function( map ) {
	if( this.div != null ) {
		return null;
	}
	
	this.map = map;
	
	this.div = $j( "<div />" );
	this.div.css( "position", "absolute" );
	this.div.css( "cursor", "pointer" );	

	var displayImage = $j( '<img />' );
	var showDetailIcon = false;
	var cssNumMarkers = 0;
	
	var type = this.clusterData[0].type;
	if( this.numMarkers == 1 && !( type == "N" || type == "" || type == null ) ) {		
		if( this.isActive ) { displayImage.attr( "src", this.getIcon( "" + type.toLowerCase() + "_2010_a" ) ); }
		else { displayImage.attr( "src", this.getIcon( "" + type.toLowerCase() + "_2010" ) ); }
		this.tourButtonSize = 49;
		showDetailIcon = true;		
	} else {		
		if( this.numMarkers >= 100 ) {
			this.tourButtonSize = 67;
			cssNumMarkers = 100;
			displayImage.attr( "src", this.getIcon( "ae100_2010" ) );
		} else if( this.numMarkers >= 10 ) {
			this.tourButtonSize = 62;
			cssNumMarkers = 10;
			displayImage.attr( "src", this.getIcon( "ae10_2010" ) );
		} else {
			this.tourButtonSize = 49;
			cssNumMarkers = 1;
			displayImage.attr( "src", this.getIcon( "ae1_2010" ) );
		}		
	}
	
	this.alignDiv();	

	var label = $j( "<div />" );
	if( showDetailIcon == false ) { label.html( this.numMarkers.toString() ); }
		
	label.addClass( "mapClusterTable" );
	
	if( cssNumMarkers > 0 ) { label.addClass( "mapClusterSize" + cssNumMarkers ); }
	this.div.append( label );
	
	this.div.append( displayImage );
	
	var pane = G_MAP_OVERLAY_LAYER_PANE;
	if( this.isActive ) { pane = G_MAP_MARKER_PANE; }
	$j( map.getPane( pane ) ).append( this.div );
}

Cluster.prototype.getDivForAnimation = function() { return this.div; }
Cluster.prototype.getActive = function() { return this.isActive; }
Cluster.prototype.setActive = function( val ) {	this.isActive = (val==true); }

Cluster.prototype.addConditionally = function( dest, point ) {
	if ( point == null ) {
		point = new GLatLng( dest.latitude, dest.longitude );
	}
	if( this.bounds.containsLatLng( point ) ) {
		if( dest.destid == "MULTIMIXED" ) {
			this.numMarkers += dest.rank;
			this.clusterData.push( dest );
			this.latSum += ( dest.latitude * dest.rank );
			this.lngSum += ( dest.longitude * dest.rank );
			return true;
		} else {
			this.numMarkers++;
			this.clusterData.push( dest );
			this.latSum += dest.latitude;
			this.lngSum += dest.longitude;
			return true;
		}
	}
	
	return false;
}

Cluster.prototype.remove = function() {
	if( this.div == null ) { return; }
	this.div.remove();
	this.div = null;
}

Cluster.prototype.copy = function( ) {
	return new Cluster( this.bounds, 
						this.numMarkers, 
						this.zoomlevel );
}

Cluster.prototype.redraw = function( force ) {
	if (!force) return;
	this.alignDiv();	
}

Cluster.prototype.isPointInsideButton = function( point ) {
	var p = this.map.fromLatLngToDivPixel( point );
	var rect = this.getButtonScreenCoords();
	return ( p.x >= rect.left && 
			 p.x <= rect.left + rect.width &&
			 p.y >= rect.top && 
			 p.y <= rect.top + rect.height );
}

Cluster.prototype.getButtonScreenCoords = function() {
	var centerPoint = this.getControlCenter();
	var c1 = this.map.fromLatLngToDivPixel( centerPoint );
	
	if ( this.tourButtonSize == 0 ) {
		this.tourButtonSize = 60;
	}
	
	var centerleft = 0;
	var centertop = 0;
	if( this.tourButtonFocusPoint != null ) {
		// method for placing icons
		var xxx = ( this.tourButtonFocusPoint.x * this.tourButtonSize / 100 );
		var yyy = ( this.tourButtonFocusPoint.y * this.tourButtonSize / 100 );
		centerleft = Math.round( c1.x - xxx );
		centertop = Math.round( c1.y - yyy );
	}
	else {
		// method for placing buttons at center of clusters
		centerleft = Math.round( c1.x - ( this.tourButtonSize / 2 ) );
		centertop = Math.round( c1.y - ( this.tourButtonSize / 2 ) );
	}
	
	return { left: centerleft,
			 top: centertop, 
			 width: this.tourButtonSize,
			 height: this.tourButtonSize
			 };
}

Cluster.prototype.alignDiv = function() {
	var rect = this.getButtonScreenCoords();	
	this.div.css( "width", rect.width + "px" );  
	this.div.css( "height", rect.height + "px" );  
	this.div.css( "left", rect.left + "px" );
	this.div.css( "top", rect.top + "px" );			
}

Cluster.prototype.getClusterData = function() {
	return this.clusterData;
}


//preload images
var clusterIcons = {
	"1_2010": "http://www.bergleben.de/img/activities/1_2010.png", 
	"2_2010": "http://www.bergleben.de/img/activities/2_2010.png",
	"3_2010": "http://www.bergleben.de/img/activities/3_2010.png",
	"20_2010": "http://www.bergleben.de/img/activities/20_2010.png",
	"1_2010_a": "http://www.bergleben.de/img/activities/1_2010_a.png",
	"2_2010_a": "http://www.bergleben.de/img/activities/2_2010_a.png",
	"3_2010_a": "http://www.bergleben.de/img/activities/3_2010_a.png",
	"20_2010_a": "http://www.bergleben.de/img/activities/20_2010_a.png",	
	"ae100_2010": "http://www.bergleben.de/img/activities/ae100_2010.png",
	"ae10_2010": "http://www.bergleben.de/img/activities/ae10_2010.png",
	"ae1_2010": "http://www.bergleben.de/img/activities/ae1_2010.png",	
	"bikepark_2010": "http://www.bergleben.de/img/activities/bikepark_2010.png",
	"bikepark_2010_a": "http://www.bergleben.de/img/activities/bikepark_2010_a.png",
	"golf_2010": "http://www.bergleben.de/img/activities/golf_2010.png",
	"golf_2010_a": "http://www.bergleben.de/img/activities/golf_2010_a.png",
	"hochseilgarten_2010": "http://www.bergleben.de/img/activities/hochseilgarten_2010.png",
	"hochseilgarten_2010_a": "http://www.bergleben.de/img/activities/hochseilgarten_2010_a.png",
	"kletterhalle_2010": "http://www.bergleben.de/img/activities/kletterhalle_2010.png",
	"kletterhalle_2010_a": "http://www.bergleben.de/img/activities/kletterhalle_2010_a.png",
	"nordicwalking_2010": "http://www.bergleben.de/img/activities/nordicwalking_2010.png",
	"nordicwalking_2010_a": "http://www.bergleben.de/img/activities/nordicwalking_2010_a.png",
	"rodelbahn_2010": "http://www.bergleben.de/img/activities/rodelbahn_2010.png",
	"rodelbahn_2010_a": "http://www.bergleben.de/img/activities/rodelbahn_2010_a.png",
	"therme_2010": "http://www.bergleben.de/img/activities/therme_2010.png",
	"therme_2010_a": "http://www.bergleben.de/img/activities/therme_2010_a.png"			
};

var iconObjects = {};
for( i in clusterIcons ) {
	iconObjects[i] = new Image(); 
	iconObjects[i].src = clusterIcons[i];
} 

