/* GLOBALE FUNKTIONEN */
function Cp() 
{
	this.overlayStatus = true;
	this.hints = [];
	this.advice_marker = [];
}

Cp.prototype.changeOverlayStatus = function() 
{	
	if (this.overlayStatus)
	{
		this.overlayStatus = false;
	}
	else
	{
		this.overlayStatus = true;		
	}
}

Cp.prototype.hideOverlays = function() 
{	
	//GLog.write("verstecken");
	var laenge = overlays.length;	
	for (var i = 0; i < laenge; i++)
	{
		overlays[i].hide();
	}
	
	this.clearMarker();	
}

Cp.prototype.showHint = function(number) 
{	
	this.clearMarker();
	var markerTest;
	
	if (number == 1)
	{
		for (i = 1; i <= standorte.anzahl; i++)
		{
			var bound = new GLatLngBounds(new GLatLng(standorte[i][2], standorte[i][3]), new GLatLng(standorte[i][4], standorte[i][5]));
			var icon = new GIcon();	
			//erstelleIcon(icon, icon_breite, icon_hoehe, shadow_breite, shadow_hoehe, iconAnchor_breite, iconAnchor_hoehe, iwAnchor_breite, iwAnchor_hoehe, bild_name, shadow_name)		
			erstelleIcon(icon, 150, 150, 75, 75, 75, 75, 75, 75, "campus.png", "trans.gif");
			markerTest = new GMarker(bound.getCenter(), {icon:icon});
			this.advice_marker.push(markerTest);
			map.addOverlay(markerTest);
		}	
	}

	if (number >= 2)
	{
		var lat = 0;
		var lng = 0;
		
		for (i = 1; i <= standorte.anzahl; i++)
		{
			var bound = new GLatLngBounds(new GLatLng(standorte[i][2], standorte[i][3]), new GLatLng(standorte[i][4], standorte[i][5]));
			lat += bound.getCenter().lat();
			lng += bound.getCenter().lng();			
		}	
		
		var icon = new GIcon();	
		//erstelleIcon(icon, icon_breite, icon_hoehe, shadow_breite, shadow_hoehe, iconAnchor_breite, iconAnchor_hoehe, iwAnchor_breite, iwAnchor_hoehe, bild_name, shadow_name)		
		erstelleIcon(icon, 150, 150, 75, 75, 75, 75, 75, 75, "campus.png", "trans.gif");
		markerTest = new GMarker(new GLatLng((lat/number), (lng/number)), {icon:icon});
		this.advice_marker.push(markerTest);
		map.addOverlay(markerTest);
	}	
}

Cp.prototype.clearMarker = function() 
{	
	var laenge = this.advice_marker.length;	
	for (var i = 0; i < laenge; i++)
	{
		map.removeOverlay(this.advice_marker[i]);
	}
	
	this.advice_marker = [];
}

Cp.prototype.showOverlays = function() 
{
	this.clearMarker();
	
	var noe = this.hints.length;
	for (i = 0; i < noe; i++)
	{
		map.removeOverlay(this.hints[i]);
	}
	
	//GLog.write("anzeigen");
	var laenge = overlays.length;	
	for (var i = 0; i < laenge; i++)
	{
		overlays[i].show();
	}
}


function mail_abschicken()
{
	var titel_node = document.getElementById("cp_titel");
	var text_node = document.getElementById("cp_text");
	var mail_node = document.getElementById("cp_mail");
	
	var titel = titel_node.value;
	var text = text_node.value;
	var mail = mail_node.value;
	
	// Test ob in einem Formularfeld nur Leerzeichen stehen	
	var pattern = /[^\s]/;
	if(!pattern.test(titel)) { titel = ""; }
	if(!pattern.test(text)) { text = ""; }
	if(!pattern.test(mail)) { mail = ""; }	

	if (titel == "" || text == "" || mail == "")
	{
		if (titel == "") { titel_node.style.borderColor = 'red'; }
		else { titel_node.style.borderColor = '#303030'; }
		
		if (text == "") { text_node.style.borderColor = 'red'; }
		else { text_node.style.borderColor = '#303030'; }
		
		if (mail == "") { mail_node.style.borderColor = 'red'; }
		else { mail_node.style.borderColor = '#303030'; }
		
		return;
	}	
	
	titel_node.style.borderColor = '#303030';
	text_node.style.borderColor = '#303030';
	mail_node.style.borderColor = '#303030';
	
	//GLog.write("alles ok, es müssen nur noch die entsprechenden Einstellungen vorgenommen werden, damit die Mail versandt werden kann!");
	//return;
	
	var vars = "";
	vars = "titel=" + suchenUndErsetzen(titel) + "&text=" + suchenUndErsetzen(text) + "&mail=" + suchenUndErsetzen(mail);		
	
	var request = GXmlHttp.create();	
	//GLog.writeUrl(basisurl + ordnerAPI + '/php/mail.php?' + vars);
	//return;
	
	request.open('GET', basisurl + ordnerAPI + '/php/mail.php?' + vars, true);
	request.onreadystatechange = function() {
		//Anforderung abgeschlossen
		if (request.readyState == 4) {
			var jsonstring = request.responseText;
			var mail;
			eval(jsonstring);			
			
			if (mail.nr == -1)
			{
				var sugg = document.getElementById("cp_right_content_3");
				sugg.innerHTML = "<div>Das Formular konnte nicht abgeschickt werden!</div>";
			}
			else
			{
				var sugg = document.getElementById("cp_right_content_3");
				sugg.innerHTML = "<div>Vielen Dank für Ihre Nachricht!</div>";
			}
		}
	}
	request.send(null);	
}

function mail_zuruecksetzen()
{
	document.getElementById("cp_titel").value = "";
	document.getElementById("cp_text").value = "";
	document.getElementById("cp_mail").value = "";	
}

function erstelleIcon(icon, icon_breite, icon_hoehe, shadow_breite, shadow_hoehe, iconAnchor_breite, iconAnchor_hoehe, iwAnchor_breite, iwAnchor_hoehe, bild_name, shadow_name) 
{
	icon.image = basisurl + ordnerAPI + ordnerIcons + bild_name;
	icon.shadow = basisurl + ordnerAPI + ordnerIcons + shadow_name;
	icon.iconSize = new GSize(icon_breite, icon_hoehe);
	icon.shadowSize = new GSize(shadow_breite, shadow_hoehe);
	icon.iconAnchor = new GPoint(iconAnchor_breite, iconAnchor_hoehe);
	icon.infoWindowAnchor = new GPoint(iwAnchor_breite, iwAnchor_hoehe);
}// function erstelleIcon()

function entferneOverlays()
{
	map.clearOverlays();
}

function karte_ausrichten(latlng, korrektur_breite, korrektur_hoehe, art)
{
	var breite = map.getSize().width;
	var hoehe = map.getSize().height;
	
	if (art == "center")
	{
		// die Karte zentrieren
		map.setCenter(latlng);
		
		// das Zentrum der Karte (Pixelwerte) speichern
		breite /= 2;
		hoehe /= 2;	
		
		// die neuen Koordinaten berechnen
		var new_latlng = map.fromContainerPixelToLatLng(new GPoint(breite + korrektur_breite, hoehe + korrektur_hoehe));		
		
		// Karte erneut zentrieren
		map.setCenter(new_latlng);		
	}
	else
	{		
		breite = (breite / 2) + (korrektur_breite)
		hoehe += korrektur_hoehe;
		
		var x_click = map.fromLatLngToContainerPixel(latlng).x;
		var y_click = map.fromLatLngToContainerPixel(latlng).y;	
		
		map.panBy(new GSize(breite - x_click, hoehe - y_click));
	}
}// function karte_ausrichten()

function zerlegeUrl()
{
	var argumente = {};
	
	var url = String(location);
	
	var reg = new RegExp(basisurl);
	var pathname = url.replace(reg, "");	// cut basisurl
	
	if (pathname.substring(0, 1) == "/")
	{
		pathname = pathname.substring(1); // cut first slash
	}
	
	var argument = pathname.split("/");
	var noe = argument.length;
	// if basisurl
	if (noe == 0 || (noe == 1 && argument[0] == ""))
	{
		
	}
	// if basisurl + e.g. /gebaeude or /gebaeude/
	else if ((noe == 1 && argument[0] != "") || noe == 2 && argument[1] == "")
	{
		argumente.error = true;
	}
	// only .../id or .../id/ is allowed
	else if (noe == 2 || (noe == 3 && argument[2] == "") )
	{
		if (argument[0] == "gebaeude") { argumente.gebaeude_id = argument[1]; }
		if (argument[0] == "raum") { argumente.raum_id = argument[1]; }
		if (argument[0] == "einrichtung") { argumente.error = true; }
	}
	// only .../id/geb_id or .../id/geb_id/ is allowed
	else if (noe == 3 || (noe == 4 && argument[2] != "" && argument[3] == "") )
	{
		if (argument[0] == "gebaeude") 
		{ 
			//if (argument[2] == 0 || argument[2] == 1)			
			if (argument[2] == 0)
			{
				argumente.gebaeude_id = argument[1]; 
				argumente.iw_art = argument[2]; 
			}			
			else
			{
				argumente.error = true;
			}
		}
		if (argument[0] == "raum") { argumente.error = true; }
		if (argument[0] == "einrichtung") 
		{ 
			argumente.nutzer_id = argument[1]; 
			argumente.geb_nutzer_id = argument[2];
		}		
	}
	else
	{
		argumente.error = true;
	}
	
	return argumente;	
}//zerlegeUrl()
