TpClusterer.regions = [null,null,null,null,null,null,null,null,[],[],[],[],[],[],[],[],[],[]];
TpClusterer.icons = [ null,null,null,null,null,null,null,null /*no such zoom levels in the app*/
, {img:'spot_5.png', size:{w:10, h:11 }, offset:{x:3,y:0} } //zoom level 8
, {img:'spot_5.png', size:{w:10, h:11 }, offset:{x:3,y:0} }
, {img:'spot_5.png', size:{w:10, h:11 }, offset:{x:3,y:0} }
, {img:'spot_5.png', size:{w:10, h:11 }, offset:{x:3,y:0} }
, {img:'spot_4.png', size:{w:18, h:19 }, offset:{x:5,y:0} }
, {img:'spot_4.png', size:{w:18, h:19 }, offset:{x:5,y:0} } //zoom level 13
, {img:'spot_4.png', size:{w:18, h:19 }, offset:{x:5,y:0} }
, {img:'spot_1-2-3.png', size:{w:26, h:27 }, offset:{x:7,y:0} }
, {img:'spot_1-2-3.png', size:{w:26, h:27 }, offset:{x:7,y:0} }
, {img:'spot_1-2-3.png', size:{w:26, h:27 }, offset:{x:7,y:0} }//zoom level 17
];

TpClusterer.defaultVenueLabels =[ null,null,null,null,null,null,null,null /*no such zoom levels in the app*/
,null,null
,null,null
,{suffix:'50percent', width: 67, height:23, offset:{x:33,y:12}}
,{suffix:'60percent', width: 80, height:28, offset:{x:40,y:14}}
,{suffix:'70percent', width: 94, height:32, offset:{x:47,y:16}}
,{suffix:'80percent', width: 108, height:36, offset:{x:54,y:18}},
{suffix:'90percent', width: 120, height:42, offset:{x:60,y:21}},
{suffix:'100percent', width: 134, height:46,offset:{x:67,y:23}}];



function TpClusterer(map,scrollover, mini, detached)
{
	this.minIconZoom=detached?8:9;
	this.visible = true;
	this.currentZoom = null;
	this.markers = [];
	this.managers = [];
	for(var i=0;i<TpClusterer.regions.length;i++)
	{
		this.markers[i] = [];
		this.managers[i] = [];
	}
	this.map = map;
	this.mini = mini;
	this.scrollover = scrollover;
	this.detached = detached;
	if (!detached){
		this.listener = GEvent.bind(map, "moveend", this, this.onMapMoveEnd);
		this.onMapMoveEnd();
	} else {
		this.listener = GEvent.bind(map, "moveend", this, this.onDetachedMapMoveEnd);
		this.onDetachedMapMoveEnd();
	}
}

TpClusterer.instance = null;
TpClusterer.getInstance = function(map, scrollover)
{
	if(TpClusterer.instance == null)
	{
		if(map == null)
		{
			return null;
		}
		TpClusterer.instance = new TpClusterer(map,scrollover);
	}
	return TpClusterer.instance;
}


TpClusterer.getNewInstance = function(map, scrollover,mini, detached){
	if(map == null){
		return null;
	}
	TpClusterer.instance = new TpClusterer(map, scrollover,mini,detached);
	return TpClusterer.instance;
}


TpClusterer.prototype.show = function() {
	this.visible = true;
	for(var z=0;z<this.managers.length;z++)
	{
		for (var i=0;i<this.managers[z].length;i++)
		{
				if (this.managers[z][i]){
					this.managers[z][i].show();
				}
		}
	}

}

TpClusterer.prototype.hide = function() {
	this.visible = false;
	this.map.getInfoWindow().hide();
	for(var z=0;z<this.managers.length;z++)
	{
		for (var i=0;i<this.managers[z].length;i++)
		{
				if (this.managers[z][i]){
					this.managers[z][i].hide();
				}
		}
	}
}

TpClusterer.prototype.onDetachedMapMoveEnd = function(){
	var z = this.map.getZoom();
	var cz = this.currentZoom;
	if (cz!= z ){
		if(cz!=null) {
			this.unloadRegion(cz, 0);
		}
		this.currentZoom = z;
	}
	this.markers[z][0] = this.detached;
	this.loadManager(z,0);
	
}

TpClusterer.prototype.onMapMoveEnd = function()
{
	var z = this.map.getZoom();
	var cz = this.currentZoom;
	if(cz != z)
	{
		if(cz != null)
		{
			if (TpClusterer.regions[cz] != null) {
				for(var i=0;i<TpClusterer.regions[cz].length;i++)
				{
					this.unloadRegion(cz, i);
				}
			}
		}
		this.currentZoom = z;
	}
	
	if (TpClusterer.regions[z] !=null) {
		for(var i=0;i<TpClusterer.regions[z].length;i++)
		{
			var r = TpClusterer.regions[z][i];
			if(new GLatLngBounds(new GLatLng(r[1], r[0]), new GLatLng(r[3], r[2])).intersects(this.map.getBounds()))
			{
				this.loadRegion(z, i);
			}
			else
			{
				this.unloadRegion(z, i);
			}
		}
	}
}

TpClusterer.prototype.loadRegion = function(zoom, ind)
{
	if(this.markers[zoom][ind] == null)
	{
		var noCache = Math.random();
		var e = document.createElement("script");
		e.src = '/WiFi/GMapMarkers/markers.js?z='+zoom+'&i='+ind+'&noCache='+noCache;
		e.type="text/javascript";
		document.getElementsByTagName("head")[0].appendChild(e);
	}
}

TpClusterer.prototype.createVenueMarker = function(point, icon, dim, zoom) {
	
	var defaultLabel = TpClusterer.defaultVenueLabels[zoom];
	if (defaultLabel){
		width = defaultLabel.width;
		height = defaultLabel.height;
		if (dim) {
			var splittedDim = dim.split("x");
			width = splittedDim[0];
			height = splittedDim[1];
		}
	} else {
		return null;
	}
	
	
	
	var iconNameparts = icon.split(".");
	var marker = new MarkerLight(point , 
		{image:'/images/WiFi/venue_logos/'+ iconNameparts[0] +'_'+defaultLabel.suffix+'.'+ iconNameparts[1], 
		width: width, 
		height: height, 
		offset:{x:width/2,
		y:height/2}});
  	return marker;
}


// Creates a marker at the given point
// Attaches listener to 'click'
TpClusterer.prototype.createMarker = function(point,zoom) {
  var iconDef = TpClusterer.icons[zoom];
  if (iconDef == null || zoom < this.minIconZoom) {
  	return null;
  }
  var marker;
  var event;
  //if (this.scrollover){
  //	icon = new GIcon();
  //	icon.image = '/images/WiFi/'+iconDef.img;
  //	icon.iconSize = new GSize(iconDef.size.w, iconDef.size.h);
  //	icon.iconAnchor = new GPoint(iconDef.offset.x, iconDef.size.h /*iconDef.offset.y*/);
  //	marker = new GMarker(point,{"icon":icon});
  //	event = 'click';
  //} else {
  	marker = new MarkerLight(point , {image:'/images/WiFi/'+iconDef.img, width:iconDef.size.w, height:iconDef.size.h, offset:iconDef.offset});
  	event = 'click';
 // }
  
  
  
  GEvent.addListener(marker, event, function() {
  	jQuery(function($){
  				fixDragging = TpClusterer.instance.mini;
  				if (fixDragging){
  					TpClusterer.instance.map.enableDragging();
  				}
  				
	    		$.get("/WiFi/GMapMarkers/markers", { "markData": marker.id, "noCache": (new Date()).getTime(), "miniCallout":TpClusterer.instance.mini },
					 function(data){
					 TpClusterer.instance.map.openInfoWindowHtml(point,data);
					 if (!TpClusterer.instance.map.getInfoWindow().isHidden()) {
					 	TpClusterer.instance.map.updateCurrentTab(
	    					function(currentTab){
//	    						currentTab.contentElem.innerHTML = data; 
	    						if (fixDragging){
	  								TpClusterer.instance.map.disableDragging();
	  							}
	    					});
	    				}
	  				});
	  			
    	});
  });
  
  return marker;
}

TpClusterer.prototype.loadManager = function(zoom, ind)
{
	var ml = this.markers[zoom][ind];
	if(ml != null)
	{
		if(this.managers[zoom][ind] == null)
		{
			this.managers[zoom][ind] = new MarkerManager(this.map);
		}
		var arr = [];  
		for(var i=0;i<ml.length;i++)
		{
			var point = new GLatLng(ml[i].Y, ml[i].X);
			var marker;
			if (ml[i].venueIcon) {
				marker = this.createVenueMarker(point,ml[i].venueIcon, ml[i].iconDim,zoom);
			} else {
				marker = this.createMarker(point,zoom);
			}
			if (marker!= null){
				marker.id = ml[i].id;
				arr.push(marker);
			}
		}
		var mgr = this.managers[zoom][ind];
		mgr.addMarkers(arr,zoom,zoom);
		if (this.visible == true) {
			mgr.show();
		} else {
			mgr.hide();
		}
		//mgr.refresh();
	}
}






TpClusterer.prototype.unloadRegion = function(zoom, ind)
{
	if(this.markers[zoom][ind] != null)
	{
		this.markers[zoom][ind] = null;
		this.managers[zoom][ind].clearMarkers();
	}
}


TpClusterer.prototype.unload = function(){
	GEvent.removeListener(this.listener);
	var cz = this.currentZoom;
	if(cz != null)
	{
		if (TpClusterer.regions[cz]!=null) {
			for(var i=0;i<TpClusterer.regions[cz].length;i++)
			{
				this.unloadRegion(cz, i);
			}
		}
	}
	TpClusterer.instance = null;
}

