/******************************************************************************
 Copyright 2009 - 2011 Olaf Hannemann
 
 This file is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.
 
 This file is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.
 
 You should have received a copy of the GNU General Public License
 along with this file.  If not, see <http://www.gnu.org/licenses/>.
 ******************************************************************************

 ******************************************************************************
 This file implements the client-side of the TidalScale display.
 Version 0.1.1  03.10.2011
 ******************************************************************************/
 
/*
angepasst von Tim Reinartz im Rahmen der Bachelor-Thesis
*/

// List of downloaded scales:
var arrayTidalScales = new Array();

// Current state of the user interface. This is used
// to keep track which popups are displayed.
var TidalScaleState = 0;
var TidalScaleCurrentFeature = null;
var TidalScalePopupTime = 0;


// AJAX functions--------------------------------------------------------------------------------------------

// Request tidal scales from the server.
function makeTidalScaleRequest(params) {
	var url = "";
	for (var name in params) {
		url += (url.indexOf("?") > -1) ? "&" : "?";
		url += encodeURIComponent(name) + "=" + encodeURIComponent(params[name]);
	}
	var TidalScaleUrl="http://osm.chaosdwarfs.de/web/getTidalTest.php"+url;
	var script = document.createElement("script");
	script.src = TidalScaleUrl;
	script.type = "text/javascript";
	document.body.appendChild(script);
}

function putTidalScaleMarker(id, lon, lat, tidal_name, name, namegebiet, messwert, tendenz, pnp, datum, uhrzeit, daten_fehler) {
	if (!tidal_scale_exist(id)) {
		var popupText = "<div style=\"position:absolute; top:4.5px; right:5px; cursor:pointer;\">";
		popupText += "<img src=\"./resources/action/info.png\"  width=\"17\" height=\"17\" onClick=\"showMapKey('help-tidal-scale');\"/></div>";
		popupText += "<table><tr><td colspan='3'><b>" + tidal_name +"</b></td></tr>";
		popupText += "<tr><td colspan='3' nowrap>" + name + ", " + namegebiet + "</td></tr>";
		popupText += "<tr><td colspan='3'></td></tr>";
		popupText += "<tr><td>" + linkTextMeasuringValue + "</td><td>"+ linkTextTendency + "</td><td>PnP</td></tr><tr><td>" + messwert + "</td><td>" + tendenz + "</td><td>" + pnp + "</td></tr>";
		popupText += "<tr><td colspan='3'></td></tr>";
		popupText += "<tr><td colspan='3'>" + datum + " - " + uhrzeit + "</td></tr>";
		popupText += "<tr><td colspan='3'></td></tr>";	
		popupText += "<tr><td colspan='3'>" + daten_fehler + "</td></tr>";
		popupText += "<tr><td colspan='3'></td></tr>";
		popupText += "<tr><td colspan='3'><a href='http://www.pegelonline.wsv.de/gast/stammdaten?pegelnr=" + id + "' target='blank'>" + linkTextHydrographCurve + "</a></td></tr>";
		popupText += "<tr><td colspan='3'></td></tr>";
		popupText += "<tr><td colspan='3'></td></tr>";
		popupText += "<tr><td colspan='3'><a href='http://openportguide.org/cgi-bin/weather/weather.pl/weather.png?var=meteogram&nx=614&ny=750&lat=" + lat + "&lon=" + lon + "&lang=de&unit=metric&label=" + tidal_name + "' target='blank'>" + linkTextWeatherHarbour + "</a></td></tr></table>";
		createTidalScaleMarker(lon2x(lon), lat2y(lat), popupText);
	
		var TidalScale = {id: id, name: tidal_name, lat: lat, lon: lon, feature: null};
		arrayTidalScales.push(TidalScale);
	}
}

// TidalScale management----------------------------------------------------------------------------------------

// Downloads new TidalScales from the server.
function refreshTidalScales() {

	if (refreshTidalScales.call_count == undefined) {
		refreshTidalScales.call_count = 0;
	} else {
		++refreshTidalScales.call_count;
	}
	bounds = map.getExtent().toArray();
	b = y2lat(bounds[1]).toFixed(5);
	t = y2lat(bounds[3]).toFixed(5);
	l = x2lon(bounds[0]).toFixed(5);
	r = x2lon(bounds[2]).toFixed(5);

	var params = { "b": b, "t": t, "l": l, "r": r};
	makeTidalScaleRequest(params);
	
}

// Check if a tidal_scale has been downloaded already.
function tidal_scale_exist(id) {
	for (var i in arrayTidalScales) {
		if (arrayTidalScales[i].id == id) {
			return true;
		}
	}

	return false;
}

function createTidalScaleMarker(x, y, popupText) {
	var layer_poi_icon_style = OpenLayers.Util.extend({});
	var TidalScale_marker = new OpenLayers.Geometry.Point(x, y);
	
	layer_poi_icon_style.externalGraphic = './resources/places/tidal_scale_24.png';
	layer_poi_icon_style.graphicWidth = 24;
	layer_poi_icon_style.graphicHeight = 24;
	
	if(zoom >= 5 ||refreshTidalScales.call_count > 0) {
		var pointFeature = new OpenLayers.Feature.Vector(TidalScale_marker, null, layer_poi_icon_style);
		pointFeature.popupClass = OpenLayers.Class(OpenLayers.Popup.FramedCloud);
		pointFeature.data.popupContentHTML = popupText;
		layer_pois.addFeatures([pointFeature]);
	}
}


