// SCMarker

function SCMarker(latlng, opt_opts)
{
	
	this.firstTime = true;
	this.latlng_ = latlng;
	this.icon_ = opt_opts.icon;
	this.number = null;
	this.numberLabel;
	this.n = opt_opts.number || "";
	var me = this;
	opt_opts.zIndexProcess = function()
	{
		return me.getZOrder();
	};

	GMarker.apply(this, arguments);
}
SCMarker.prototype = new GMarker(new GLatLng(0, 0));

SCMarker.prototype.initialize = function(map)
{
	GMarker.prototype.initialize.apply(this, arguments);
	//var h = this.De();
	
	//
	this.map_ = map;
	
	//
	
	
	var numberLabel = document.createElement("div");
		numberLabel.style.position = "absolute";
		numberLabel.className = "sclabel";
		numberLabel.style.left = "0px";
		numberLabel.style.top = "0px";
		
		var number = document.createElement("div");
			number.style.position = "absolute";
			number.className = "sclabel";
			number.style.left = "0px";
			number.style.top = "0px";
			number.style.width = "20px";
			number.style.textAlign = "center";
			number.style.verticalAlign = "top";
			number.style.fontWeight = "bold";
			number.style.fontFamily = "arial,verdana";
			number.style.fontSize = "14px";
			number.style.color = "white";
			
			this.numberLabel = numberLabel;
			this.number = number;
			this.setNumber(this.n);
			
		numberLabel.appendChild(number);
		
	map.getPane(G_MAP_MARKER_PANE).appendChild(numberLabel);
	this.numberLabel = numberLabel;
};

SCMarker.prototype.getZOrder = function()
{
	if(this.number)return 0 - (this.number.innerHTML);
	else return 0;
};

SCMarker.prototype.redraw = function(force)
{
	if(this.firstTime)
	{
		this.firstTime = false;
		this.setPoint(this.latlng_);
		this.firstTime = true;
	}
	else
	{
		GMarker.prototype.redraw.apply(this, arguments);
		this.numberLabel.style.zIndex = this.getZOrder(); //IE fix
		this.number.style.zIndex = this.getZOrder(); //GOverlay.getZIndex(this.latlng_.lat());
		
		var p = this.map_.fromLatLngToDivPixel(this.latlng_);
		this.numberLabel.style.left = p.x - this.icon_.iconAnchor.x + "px";
		this.numberLabel.style.top = p.y - this.icon_.iconAnchor.y + "px";
	}
}

SCMarker.prototype.remove = function() {

  if (this.numberLabel.outerHTML) {
    this.numberLabel.outerHTML = ""; //prevent pseudo-leak in IE
    
  }
  if (this.numberLabel.parentNode) {
    this.numberLabel.parentNode.removeChild(this.numberLabel);
  }
  this.numberLabel = null;
  GMarker.prototype.remove.apply(this, arguments);
}

SCMarker.prototype.show = function() {
  GMarker.prototype.show.apply(this, arguments);
    this.numberLabel.style.display = "block";
}

SCMarker.prototype.hide = function() {
  GMarker.prototype.hide.apply(this, arguments);
    this.numberLabel.style.display = "none";
}

SCMarker.prototype.setNumber = function(number)
{
	var l = ( number != null) ? number.toString().length : 0;
	if(l == 1 || l == 2)
	{
		this.number.innerHTML = number;
	}
	else
	{

		this.number.innerHTML = "";
	}
	if(this.numberLabel)this.redraw();
}

SCMarker.prototype.panToPoint = function(x,y)
{
	var center = this.map_.fromLatLngToContainerPixel(this.map_.getCenter());
	var markerXY = this.map_.fromLatLngToContainerPixel(this.latlng_)
	this.map_.setCenter(this.map_.fromContainerPixelToLatLng(new GPoint(center.x - x + markerXY.x, center.y - y + markerXY.y)));//panTo
};

// BusinessMarker

function BusinessMarker(lat, lng, imageName, number, id, enhancedCard, infoWindow, population)
{
	this.id = id;
	this.enhanced = enhancedCard
	this.infoWindow = infoWindow;
	this.population = population;
	
	var markerIcon = new GIcon(G_DEFAULT_ICON);
				markerIcon.image = imageName + ".png";
				markerIcon.printImage = imageName + ".gif";
				markerIcon.mozPrintImage = imageName + ".png";
	markerOptions = { icon:markerIcon };
	

	this.marker = new SCMarker(new GLatLng(lat, lng), markerOptions);
	map.addOverlay(this.marker);
	this.marker.setNumber(number);
	
	var me = this;
	GEvent.addListener(this.marker, "click", function() {
		me.openBusinessInfoWindow();
	});
}

BusinessMarker.prototype.openBusinessInfoWindow = function()
{
	closeEnhancedInfoCard();
	if(this.enhanced)
	{
		this.marker.panToPoint(313,425); //(306,425)
		
		var focusMarker = this.marker;
		ZoomListener = GEvent.addListener(map, "zoomend", function() {
			zooming == true;
			focusMarker.panToPoint(313,425);
			zooming == false;
		});
		
		
		openEnhancedInfoCard(this.id);
	}
	else this.marker.openInfoWindow(this.infoWindow);
};

// BusinessMarkerManager

function BusinessMarkerManager()
{
	this.markers = new Array();
	this.maxpopulation = 0;
	
}

BusinessMarkerManager.prototype.addBussiness = function(lat, lng, imageName, number, id, enhancedCard, infoWindow, population)
{
	
	if(population > this.maxpopulation)
	{
		this.maxpopulation = population;
	}
	this.markers.push(new BusinessMarker(lat, lng, imageName, number, id, enhancedCard, infoWindow, population||0));
}

BusinessMarkerManager.prototype.click = function(id)
{
	var l = this.markers.length;
	for(var i = 0; i <  l; i++)
	{
		if(this.markers[i].id == id)
		{
			this.markers[i].openBusinessInfoWindow();
			break;
		}
	}
};

BusinessMarkerManager.prototype.centerOnMarkers = function(map)
{
	var maxlat = null;
	var maxlng = null;
	
	var minlat = null;
	var minlng = null;
	
	var lng = new Array();
	
	var l = this.markers.length;
	if(l > 0)
	{
		for(var i = 0; i <  l; i++)
		{
			var ll = this.markers[i].marker.latlng_;
			if(minlat == null || ll.lat() < minlat)
			{
				minlat = ll.lat();
			}
			
			if(maxlat == null || ll.lat() > maxlat)
			{
				maxlat = ll.lat();
			}
			
			lng.push(ll.lng());
		}
		
		lng.sort(function(a,b){return a - b});
		var minlng_i = 0;
		var minlng_d = lng[lng.length - 1] - lng[0];
		
		for(var i = 1; i < lng.length; i++)
		{
			var d = 360 + lng[i-1] - lng[i];
			if(d < minlng_d)
			{
				minlng_i = i;
				minlng_d = d;
			}
		}
		
		minlng = lng[minlng_i];
		maxlng = minlng + minlng_d;
		
		var markerbounds = new GLatLngBounds(new GLatLng(minlat,minlng),new GLatLng(maxlat,maxlng));
		
		if(map.getBoundsZoomLevel(markerbounds) < 10)
		{
			map.setCenter(markerbounds.getCenter(),map.getBoundsZoomLevel(markerbounds));
		}
		else
		{
			map.setCenter(markerbounds.getCenter(),10);
		}
		
		//this.SortForPopulation();
		//this.ZoomFilter(0,map.getZoom());
		
		
	}
	
};


BusinessMarkerManager.prototype.clear = function()
{
	map.clearOverlays()
	while(this.markers.length)
	{
		var bm = this.markers.pop();
		//delete bm;
	}
	this.maxpopulation = 0;
};

BusinessMarkerManager.prototype.ZoomFilter2 = function(oldLevel, newLevel)
{
	var allLevel = 10;
	
	var p = this.maxpopulation - this.maxpopulation*(newLevel+1)/allLevel;
	
	var l = this.markers.length;
	for(var i = 0; i <  l; i++)
	{		
		if(this.markers[i].population >= p)
		{
			this.markers[i].marker.show();
		}
		else
		{
			this.markers[i].marker.hide();
		}
	}
};

BusinessMarkerManager.prototype.SortForPopulation = function()
{
	this.markers.sort(function(a,b){ return b.population - a.population});
}

BusinessMarkerManager.prototype.ZoomFilter = function(oldLevel, newLevel)
{
	/*
	var allLevel = 13;
	
	var l = this.markers.length;
	var p = l*((newLevel*newLevel)/(allLevel*allLevel));
	var i = 0;

	for(i = 0; ((i < 10) || (i <  p)) && (i < l); i++)
	{		
		this.markers[i].marker.show();
	}
	for(; i < l; i++)
	{
		this.markers[i].marker.hide();
	} */
};
