/* ---------------------------------------------------------------------------
Copyright:
	Davis Instruments, (c) 2010

Code by:
	Author:  Andy Schmidt
	Created: 01/15/2010

Notes:
	Best viewed with TAB-Size = 4

ToDo:
	Todo items are marked with a $$$ comment

Revisions:

--------------------------------------------------------------------------- */

//--------------------------------------
// MapClusterMarker is the basic Marker Div object that is
// color coded by average temperature of the cluster
// and displays the number of stations within the cluster
//--------------------------------------
function MapClusterMarker(iIndex, oCenter, iCount, dAvgTempF, sUrl) {

	this.oMap		= null;
	this.oCenter	= oCenter;
	this.oDivMain	= null;
	this.oDivImage	= null;
	this.oDivText	= null;
	this.oPos		= null;
	this.dOpacity	= 1;
	this.iIndex		= iIndex;
	this.iCount		= iCount;
	this.dAvgTempF	= dAvgTempF;
	this.sUrl		= sUrl;
	this.iWidth		= iIconSize;
	this.iHeight	= iIconSize;
	this.sText		= "" + iCount;
}

//--------------------------------------
// The marker is a GMap Overlay
//--------------------------------------
MapClusterMarker.prototype = new GOverlay();

//--------------------------------------
MapClusterMarker.prototype.calcPos = function() {

	this.oPos	=  this.oMap.fromLatLngToDivPixel(this.oCenter);
	this.oPos.x	-= parseInt(this.iWidth  / 2);
	this.oPos.y	-= parseInt(this.iHeight / 2);
}

//--------------------------------------
// Initialize the Cluster Marker
//--------------------------------------
MapClusterMarker.prototype.initialize = function(map) {
var oLatLng		= this.oCenter;
var sMainStyle	= "";
var sImgStyle	= "";
var sTextStyle	= "";
var sExtra		= "";

	this.oMap = map;
	this.calcPos();

	// Set the title to display the number of Stations
	// and the average Temperature
	if (this.iCount > 1) { sExtra = "s"; }
	sTitle = "" + this.iCount + " Station" + sExtra + ", Avg. Temp: " + formatTemp(this.dAvgTempF,true);

	// Build the Main DIV overlay CSS
	this.oDivMain = document.createElement("div");
	//sMainStyle	+= "border: 1px solid #333366; ";
	sMainStyle	+= "width: " + this.iWidth + "px; ";
	sMainStyle	+= "height: " + this.iHeight + "px; ";
	sMainStyle	+= "top: " + this.oPos.y + "px; ";
	sMainStyle	+= "left: " + this.oPos.x + "px; ";
	sMainStyle	+= "position: absolute; ";
	sMainStyle	+= "padding: 0px; ";
	sMainStyle	+= "margin: 0px; ";
	this.oDivMain.style.cssText = sMainStyle;

	// Build the Image overlay CSS
	this.oDivImage = document.createElement("img");
	this.oDivImage.src = this.sUrl;
	this.oDivImage.title = sTitle;
	sImgStyle	+= "width: " + this.iWidth + "px; ";
	sImgStyle	+= "height: " + this.iHeight + "px; ";
	sImgStyle	+= "opacity: 1; ";
	//sImgStyle	+= "filter: alpha(opacity=100); ";
	sImgStyle	+= "cursor: pointer; ";
	sTextStyle	+= "background-color: transparent; ";
	this.oDivImage.style.cssText = sImgStyle;

	// Build the Label DIV overlay CSS
	this.oDivText = document.createElement("div");
	this.oDivText.title = sTitle;
	sTextStyle	+= "width: " + this.iWidth + "px; ";
	sTextStyle	+= "height: " + this.iHeight + "px; ";
	sTextStyle	+= "line-height: " + this.iHeight + "px; ";
	sTextStyle	+= "text-align: center; ";
	sTextStyle	+= "cursor: pointer; ";
	sTextStyle	+= "color: #000000; ";
	sTextStyle	+= "font-family: Arial, Helvetica, Verdana, Geneva, sans-serif; ";
	sTextStyle	+= "font-size: 8px; ";
	sTextStyle	+= "font-weight: bold; ";
	sTextStyle	+= "padding: 0px; ";
	sTextStyle	+= "margin: 0px; ";
	sTextStyle	+= "position: relative; ";
	sTextStyle	+= "top: -" + this.iHeight + "px; ";
	sTextStyle	+= "left: 0px; ";
	sTextStyle	+= "background-color: transparent; ";
	this.oDivText.style.cssText	= sTextStyle;
	this.oDivText.innerHTML = this.sText;

	// Add the Image and Text to the Main Div
	this.oDivMain.appendChild(this.oDivImage);
	this.oDivMain.appendChild(this.oDivText);

	// Add the Main DIV to the GMap
	oGMap.getPane(G_MAP_MAP_PANE).appendChild(this.oDivMain);

	// Add a Event Listener for the Text Div
	GEvent.addDomListener(this.oDivText, "click", function(e) {

		showStatusText("Creating Station Map ...");
		
		if (iMapZoom < 6) { iMapZoom = 6;
		} else { iMapZoom = 8; }
		
		oGMap.setZoom(iMapZoom);
		oGMap.setCenter(oLatLng, iMapZoom);

		redrawMap();	// Draw the Map
	});
}

//--------------------------------------
// Is this Marker hidden? True/False
//--------------------------------------
MapClusterMarker.prototype.isHidden = function () {

	if (this.oDivMain.style.display == "none") { return true; }
	return false;
}

//--------------------------------------
// Hide the Marker
//--------------------------------------
MapClusterMarker.prototype.hide = function () {

	this.oDivMain.style.display = "none";
}

//--------------------------------------
// Show the Marker
//--------------------------------------
MapClusterMarker.prototype.show = function () {

	this.oDivMain.style.display = "block";
}

//--------------------------------------
// Copy this Marker
//--------------------------------------
MapClusterMarker.prototype.copy = function () {

	return new MapClusterMarker(
		this.iIndex,
		this.oCenter,
		this.iCount,
		this.dAvgTempF,
		this.sUrl
	);
}

//--------------------------------------
// Remove this Marker
//--------------------------------------
MapClusterMarker.prototype.remove = function () {

	this.oDivMain.parentNode.removeChild(this.oDivMain);
}

//--------------------------------------
// Redraw this Marker
//--------------------------------------
MapClusterMarker.prototype.redraw = function(force) {
var izoom,ics,ict;

	// Calculate the dynamic Icon size depending on the Map zoom level
	izoom	= this.oMap.getZoom();
	if (izoom < iZoomShowSegments) {
		ics = izoom * dIconSizeMod * iIconSize;
	} else {
		ics = izoom * (dIconSizeMod - (izoom * 0.05)) * iIconSize;
	}
	if (ics < iIconSize) { ics = iIconSize; }

	// Calculate the dynamic Text size depending on the Map zoom level
	ict = izoom * dIconSizeMod;
	if (ict < 1) { ict = 1; }
	if (izoom < iZoomShowSegments) {
		ict *= 6;
	} else {
		ict *= 5;
	}
	if (ict < 8) { ict = 8; }

	// Only display label text at larger zoom levels
	this.sText = "&nbsp;";
	if (izoom >= iZoomLabelDisplay) { this.sText = "" + this.iCount; }

	// Calculate the dynamic Image opacity depending on the Map zoom level
	this.dOpacity = 1 - (izoom * 0.05);

	// Position the Main Div
	this.iWidth		= ics;
	this.iHeight	= ics;
	this.calcPos();

	// Main Div
	this.oDivMain.style.top			= this.oPos.y + "px";
	this.oDivMain.style.left		= this.oPos.x + "px";
	this.oDivMain.style.width		= this.iWidth + "px";
	this.oDivMain.style.height		= this.iHeight + "px";

	// Image Div
	this.oDivImage.style.opacity			= this.dOpacity;
	//this.oDivImage.filters.alpha.opacity	= (this.dOpacity * 100);
	this.oDivImage.style.width				= this.iWidth + "px";
	this.oDivImage.style.height				= this.iHeight + "px";
	
	// Text Label Div
	this.oDivText.innerHTML			= this.sText;
	this.oDivText.style.fontSize	= ict + "px";
	this.oDivText.style.width		= this.iWidth + "px";
	this.oDivText.style.height		= this.iHeight + "px";
	this.oDivText.style.lineHeight	= this.iHeight + "px";
	this.oDivText.style.top			= -this.iHeight + "px";
}
