/**
 * MeteoMapRequestInfo
 *  
 * @return
 */
function MeteoMapRequestInfo()
{
	this.cdCommand = null;
	this.cdApplication = null;
	this.dhDate = null;
	this.cdCountry = null;
	this.idState = null;
}

/**
 * MeteoMap
 */
function MeteoMap()
{
	// Set the hilight defaults
	$.fn.maphilight.defaults =
	{
		fill: true,
		fillColor: "000000",
		fillOpacity: 0.2,
		stroke: true,
		strokeColor: "ffffff",
		strokeOpacity: 1,
		strokeWidth: 1
	};

	this.cdMode = "ICO";

	this.ICO_WIDTH = 35;
	this.ICO_HEIGHT = 35;
	this.ICO_CX = 17;
	this.ICO_CY = 17;
	
	this.cdCountry = "it";
	this.dhDate = null;
	this.idState = null; 
	
	// Settings
	this.meteoMapUrl = window.location.protocol + "//" + window.location.host + "/meteoMapHandler.json";
	this.autocompleteUrl = window.location.protocol + "//" + window.location.host + "/jqaac.json";

	this.mmUrl = window.location.protocol + "//" + window.location.host + "/mm";
	this.cdApplication = getById("cdApplication").value;
	
	this.meteoMapResponseInfo = null;
	this.dumpParameter = false;
	
	this.searchMeteoMap = $("#searchMeteoMap").autocomplete({ 
		serviceUrl: this.autocompleteUrl,
		selectFirst: true,
		minChars: 2, 
		delimiter: /(,|;)\s*/, // regex or character
	    maxHeight: 400,
	    width: 300,
	    zIndex: 9999,
	    deferRequestBy: 200, // Milliseconds
	    params: {cdCmd: "CITY"}, // Additional parameters
	    noCache: false, //default is false, set to true to disable caching
	    onSelect: function(value, data) {submitMeteoCity(value, data);},
	    onChange: function(value, data) {alert("value = " + value);}
	});
}

/**
 * submitMeteoCity
 */
function submitMeteoCity(value, data)
{
	
	 var values = data.split("-")
	 var idCity = values[0];
	 var idMeteoPoint = values[1];
	 
	 var url = "previsioni-del-tempo-per-" + value + "_msc_" + idCity + "_" + idMeteoPoint + ".html";
	 document.location = url;
}

/**
 * load
 */
MeteoMap.prototype.load = function()
{
	// Build the request
	var meteoMapRequestInfo = new MeteoMapRequestInfo();
	meteoMapRequestInfo.cdCommand = "LOAD";
	meteoMapRequestInfo.cdApplication = this.cdApplication;
	meteoMapRequestInfo.dhDate = this.dhDate;
	meteoMapRequestInfo.cdCountry = this.cdCountry;
	meteoMapRequestInfo.idState = this.idState;

	// Complete the url with the request
	var cmd = "?meteoMapRequestInfo=" + JSON.stringify(meteoMapRequestInfo);

	// Block the user interface
	//$.blockUI({message: '<h1>Loading map, please wait...</h1>'});
	$('#cntMap').block({ message: '<h1>Caricamento mappa...</h1>'});
	
	// Execute the command
	$.ajax(
	{
		  url: this.meteoMapUrl + cmd,
		  async: true,
		  success: loadCompleted
	});
}

/**
 * loadCompleted
 */
function loadCompleted(data)
{
	// Release the interface
	$("#cntMap").unblock();
	var json = $.parseJSON(data);
	meteoMap.init(json);
}

/**
 * init
 */
MeteoMap.prototype.init = function(json)
{
	// Parse back the response
//	this.meteoMapResponseInfo = $.parseJSON(data);
	this.meteoMapResponseInfo = json;
	this.cdCountry = this.meteoMapResponseInfo.cdCountry;
	this.idState = this.meteoMapResponseInfo.idState;
	this.render();
}	

/**
 * render
 */
MeteoMap.prototype.render = function()
{
	// Set the background image
	$("#imgMeteoMap").attr("src","/mm?id=" + this.meteoMapResponseInfo.idMultimediaMap);

	// Build the AreaMap
	var html = "";
	var meteoMapAreaInfoList = this.meteoMapResponseInfo.meteoMapAreaInfoList;
	for (var i = 0; i < meteoMapAreaInfoList.length; i++)
	{
		html += 
				"<area" +
				" id=\"idState_" + meteoMapAreaInfoList[i].idState + "\"" +
				" shape=\"poly\"" +
				" coords=\"" + meteoMapAreaInfoList[i].cdCoords + "\"" +
				" title=\"" + meteoMapAreaInfoList[i].nmArea + "\"";

		if (this.idState != null && this.idState ==  meteoMapAreaInfoList[i].idState)
			html += " onclick=\"showCityList(" + meteoMapAreaInfoList[i].idState + ");\"/>";
		else
			html += " onclick=\"loadState(" + meteoMapAreaInfoList[i].idState + ");\"/>";
	}
	
	$('#hiMeteoMap').html(html);

	// Place the tooltips
	html = "";
	var meteoPointInfoList = this.meteoMapResponseInfo.meteoPointInfoList;
	for (var i = 0; i < meteoPointInfoList.length; i++)
		html += this.getIcon(meteoPointInfoList[i]);
	$("#icoMeteoMap").html(html);
	
	for (var i = 0; i < meteoPointInfoList.length; i++)
	{
		var id = "#icoMP_" + meteoPointInfoList[i].idMeteoPoint;
		var tico = this.getMeteoPointValue(meteoPointInfoList[i].meteoPointValueList, "TICO");
		var tmp = this.getMeteoPointValue(meteoPointInfoList[i].meteoPointValueList, "TMP");
		var tmin = Math.round(Number(this.getMeteoPointValue(meteoPointInfoList[i].meteoPointValueList, "TMIN")));
		var tmax = Math.round(Number(this.getMeteoPointValue(meteoPointInfoList[i].meteoPointValueList, "TMAX")));
		var wdir = this.getMeteoPointValue(meteoPointInfoList[i].meteoPointValueList, "WDIR");
		var wkts = this.getMeteoPointValue(meteoPointInfoList[i].meteoPointValueList, "WKTS");

		if (("ICO" == this.cdMode && meteoPointInfoList[i].cdMeteoPointType != "WD") || ("SWI" == this.cdMode && meteoPointInfoList[i].cdMeteoPointType == "SE"))
		{
			html = "<div>";
			html += "<div class=\"title\">" + meteoPointInfoList[i].nmMeteoPoint + "</div>";
			html += "<img class=\"img\" src=\"/img/meteo/ico/" + meteoPointInfoList[i].cdIcon + "_B.png\"/>";
			html += "<div class=\"desc\">";
			if (tico == 0)
				html += "Neve";
			else if (tico == 1)
				html += "Temporale";
			else if (tico == 2)
				html += "Pioggia debole";
			else if (tico == 3)
				html += "Pioggia";
			else if (tico == 4)
				html += "Acquazzoni";
			else if (tico == 5)
				html += "Nebbia";
			else if (tico == 6)
				html += "Sereno";
			else if (tico == 7)
				html += "Poco nuvoloso";
			else if (tico == 8)
				html += "Nuvoloso";
			else if (tico == 9)
				html += "Coperto";
			else if (tico == 10)
				html += "Calmo";
			else if (tico == 11)
				html += "Poco mosso";
			else if (tico == 12)
				html += "Mosso";
			else if (tico == 13)
				html += "Molto mosso";
			html += "</div>";

			html += "<div class=\"tmp\">" + Number(format_number(tmp, 0, ",")) + "°</div>";
			html += "<div class=\"tmin\">MIN<br/>" + Number(format_number(tmin, 0, ",")) + "°</div>";
			html += "<div class=\"tmax\">MAX<br/>" + Number(format_number(tmax, 0, ",")) + "°</div>";
			html += "</div>";

			var tipClass = "tooltipMeteo";
			if (meteoPointInfoList[i].cdMeteoPointType == "SE")
				tipClass = "tooltipSea";
			
			$(id).tooltip(
			{ 
				predelay: 700,
				layout: html,
				tipClass: tipClass,
				offset: [0, 85]
			});
		}
		else if ("SWI" == this.cdMode || (meteoPointInfoList[i].cdMeteoPointType == "WD" && "TMP" != this.cdMode))
		{
			html = "<div>";
			html += "<div class=\"title\">" + meteoPointInfoList[i].nmMeteoPoint + "</div>";
			html += "<img class=\"img\" src=\"/meteoImgHandler.do?CMD=WND&WD=" + wdir + "&WK=" + wkts + "\">";
			
			html += "<div class=\"desc\">";
			var pc = ""

			if ((wdir >= (360 - 22.5) && wdir <= 360) || (wdir >= 0 && wdir <= 0 + 22.5))
 			{ 
 				html += "Tramontana";
 				pc = "N";
 			}
 			else if (wdir >= (0 + 22.5) && wdir <= 45 + 22.5)
 			{ 
 				html += "Grecale";
 				pc = "NE";
 			}
 			else if (wdir >= (45 + 22.5) && wdir <= 90 + 22.5) 	 
 			{ 
 				html += "Levante";
 				pc = "E";
 			}
 			else if (wdir >= (90 + 22.5) && wdir <= 135 + 22.5)
 			{ 
 				html += "Scirocco";
 				pc = "SE";
 			}
 			else if (wdir >= (135 + 22.5) && wdir <= 180 + 22.5)
 			{ 
 				html += "Ostro";
 				pc = "S";
 			}
 			else if (wdir >= (180 + 22.5) && wdir <= 225 + 22.5) 
 			{ 
 				html += "Libeccio";
 				pc = "SW";
 			}
 			else if (wdir >= (225 + 22.5) && wdir <= 270 + 22.5) 
 			{
 				html += "Ponente";
 				pc = "W";
 			}
 			else if (wdir >= (270 + 22.5) && wdir <= 315 + 22.5)
 			{ 
 				html += "Maestrale";
 				pc = "NW";
 			}

			html += "</div>";

			html += "<div class=\"vel\">VEL " + format_number(wkts, 1, ",") + "<small> Kts</small></div>";
			html += "<div class=\"dir\">DIR " + format_number(wdir, 0, ",") + "° " + pc + "</div>";
			html += "</div>";

			$(id).tooltip(
			{ 
				predelay: 700,
				layout: html,
				tipClass: "tooltipWind",
				offset: [0, 85]
			});
		}
	}

	$('#imgMeteoMap').maphilight();
}

/**
 * getIcon
 */
MeteoMap.prototype.getIcon = function(meteoPointInfo)
{
	var html = "";
	
	if (("ICO" == this.cdMode && meteoPointInfo.cdMeteoPointType != "WD") || ("SWI" == this.cdMode && meteoPointInfo.cdMeteoPointType == "SE"))
	{
		html =
			"<img" +
			" id=\"icoMP_" + meteoPointInfo.idMeteoPoint + "\"" +
			" style=\"position: absolute; left: " + (meteoPointInfo.cdX - this.ICO_CX) + "px; top: " + (meteoPointInfo.cdY - this.ICO_CY) + "px;\"" +
			" src=\"/img/meteo/ico/" + meteoPointInfo.cdIcon + ".png\"" +
			" alt=\"" + meteoPointInfo.nmMeteoPoint + "\"" +
			" title=\" \"";
		if (this.idState == null)
			html += " onclick=\"loadState(" + meteoPointInfo.idState + ");\"";
		else
			html += " onclick=\"showCityList(" + meteoPointInfo.idState + ");\"";
		
		if (this.dumpParameter)
		{
			html +=
				" onmouseover=\"showParam(" + meteoPointInfo.idMeteoPoint + ");\"" +
				" onmouseout=\"hideParam();\"";
		}
		else
		{
			html +=
				" onmouseover=\"showStateHilight(" + meteoPointInfo.idState + ");\"" +
				" onmouseout=\"hideStateHilight(" + meteoPointInfo.idState + ");\"";
		}
		
		html +=
			"/>";
	}
	else if ("TMP" == this.cdMode && meteoPointInfo.cdMeteoPointType == "TR")
	{
		if (this.idState == null)
		{
			var tmp = this.getMeteoPointValue(meteoPointInfo.meteoPointValueList, "TMP");
			html +=
					"<div " +
					" class=\"icoTmp\" " +
					" onmouseover=\"showStateHilight(" + meteoPointInfo.idState + ");\"" +
					" onmouseout=\"hideStateHilight(" + meteoPointInfo.idState + ");\"" +
					" onclick=\"loadState(" + meteoPointInfo.idState + ");\"" +
					" style=\"background: url(/img/meteo/ico/TMPI.png) top left no-repeat; width: 32px; height: 32px; position: absolute; left: " + (meteoPointInfo.cdX - this.ICO_CX) + "px; top: " + (meteoPointInfo.cdY - this.ICO_CY) + "px;\">" +
					"<div class=\"tmpi\">" + Number(format_number(tmp, 0, ",")) + "°</div>" +
					"</div>";
		}
		else
		{
			var tmp = this.getMeteoPointValue(meteoPointInfo.meteoPointValueList, "TMP");
			var tmin = this.getMeteoPointValue(meteoPointInfo.meteoPointValueList, "TMIN");
			var tmax = this.getMeteoPointValue(meteoPointInfo.meteoPointValueList, "TMAX");
			html += 
					"<div " +
					" class=\"icoTmp\" " +
					" onmouseover=\"showStateHilight(" + meteoPointInfo.idState + ");\"" +
					" onmouseout=\"hideStateHilight(" + meteoPointInfo.idState + ");\"" +
					" onclick=\"showCityList(" + meteoPointInfo.idState + ");\"" +
					" style=\"background: url(/img/meteo/ico/TMP.png) top left no-repeat; width: 32px; height: 32px; position: absolute; left: " + (meteoPointInfo.cdX - this.ICO_CX) + "px; top: " + (meteoPointInfo.cdY - this.ICO_CY) + "px;\">" +
					"<div class=\"tmp\">" + Number(format_number(tmp, 0, ",")) + "°</div>" +
					"<div class=\"tmin\">" + Number(format_number(tmin, 0, ",")) + "</div>" +
					"<div class=\"tmax\">" + Number(format_number(tmax, 0, ",")) + "</div>" +
					"</div>";
		}
	}
	else if (("SWI" == this.cdMode && meteoPointInfo.cdMeteoPointType != "SE") || (meteoPointInfo.cdMeteoPointType == "WD" && "TMP" != this.cdMode))
	{
		var wdir = this.getMeteoPointValue(meteoPointInfo.meteoPointValueList, "WDIR");
		var wkts = this.getMeteoPointValue(meteoPointInfo.meteoPointValueList, "WKTS");
		html += 
			"<img" +
			" id=\"icoMP_" + meteoPointInfo.idMeteoPoint + "\"" +
			" style=\"position: absolute; left: " + (meteoPointInfo.cdX - this.ICO_CX) + "px; top: " + (meteoPointInfo.cdY - this.ICO_CY) + "px;\"" +
			" src=\"/meteoImgHandler.do?CMD=WND&WD=" + wdir + "&WK=" + wkts + "\"" +
			" alt=\"" + meteoPointInfo.nmMeteoPoint + "\"" +
			" title=\" \"";

		if (this.idState == null)
			html += " onclick=\"loadState(" + meteoPointInfo.idState + ");\"";
		else
			html += " onclick=\"showCityList(" + meteoPointInfo.idState + ");\"";

		html += 
			" onmouseover=\"showStateHilight(" + meteoPointInfo.idState + ");\"" +
			" onmouseout=\"hideStateHilight(" + meteoPointInfo.idState + ");\"" +
			"/>";
	}

	return html;
}

/**
 * getValue
 */
MeteoMap.prototype.getMeteoPointValue = function(meteoPointValueList, key)
{
	for (var j = 0; j < meteoPointValueList.length; j++)
	{
		if (meteoPointValueList[j].key == key)
			return meteoPointValueList[j].value;
	}
	return null;
}

/**
 * showStateHilight
 */
function showStateHilight(idState)
{
	var key = "#idState_" + idState;
	var data = $(key).mouseout().data('maphilight') || {};
	data.alwaysOn = true;
	$(key).data("maphilight", data).trigger("alwaysOn.maphilight");
}

/**
 * hideStateHilight
 */
function hideStateHilight(idState)
{
	var key = "#idState_" + idState;
	var data = $(key).mouseout().data('maphilight') || {};
	data.alwaysOn = false;
	$(key).data("maphilight", data).trigger("alwaysOn.maphilight");
}

/**
 * loadState
 */
function loadState(idState)
{
	$("#idState").val(idState);
	document.meteoMap.submit();
}

/**
 * showCityList
 */
function showCityList(idState)
{
	$("#idState").val(idState);
	$("#meteoMap").attr("action", "lista-citta-per-regione_sc_31.html");
	document.meteoMap.submit();
}

/**
 * setCdMode
 */
MeteoMap.prototype.setCdMode = function(cdMode)
{
	this.cdMode = cdMode;
}

/**
 * showParam
 */
function showParam(idMeteoPoint)
{
	meteoMap.showParam(idMeteoPoint);
}

/**
 * showParam
 */
MeteoMap.prototype.showParam = function(idMeteoPoint)
{
	var html = "";
	var meteoPointInfoList = this.meteoMapResponseInfo.meteoPointInfoList;
	for (var i = 0; i < meteoPointInfoList.length; i++)
	{
		var meteoPointInfo = meteoPointInfoList[i];
		if (meteoPointInfo.idMeteoPoint == idMeteoPoint)
		{
			for (var v = 0; v < meteoPointInfo.meteoPointValueList.length; v++)
				html += meteoPointInfo.meteoPointValueList[v].key + " = " + meteoPointInfo.meteoPointValueList[v].value + "<br/>";
			break;
		}
	}
	
	$("#meteoMapPar").html(html);
}

/**
 * hideParam
 */
function hideParam()
{
	meteoMap.hideParam();
}

/**
 * hideParam
 */
MeteoMap.prototype.hideParam = function()
{
	$("#meteoMapPar").html("");
}

/**
 * doAddAccountCityMeteoPoint
 */
function doAddAccountCityMeteoPoint(command, idCityMeteoPoint, formName)
{
	var objCommand = getById("command");
	var objCityMeteoPoint = getById("idCityMeteoPoint");
	var objForm = getById(formName);
	
	objCommand.value = command;
	objCityMeteoPoint.value = idCityMeteoPoint;
	objForm.submit();
}
