var debug = false;
if (debug) alert("reading geo_function js");

if (debug) alert("Path = " + window.location.pathname);
var pathItem = window.location.pathname.split("/");
var contentPath = "";
if(pathItem[1] == 'portal')
        contentPath = "/" + pathItem[1];

if (debug) alert("contentPath = " + contentPath);
if (debug) alert("reading geo_function js done");

function resizeMap(val, mapId) {
	//alert("Change map " + val);
	
	var obj = document.getElementById(mapId);
	
	if(val == 1) {
		document.getElementById('geoLeft').style.display = 'none';	
		obj.style.width = '873px';
		obj.style.height = '500px';
		obj.style.marginLeft = '5px';
	} else {
		document.getElementById('geoLeft').style.display = 'block';	
		obj.style.width = '694px';
		obj.style.height = '444px';
		obj.style.marginLeft = '0px';
	}
}

function resizeMapIfNeccessary(mapId) {
	//alert('resize...');
	var obj = document.getElementById(mapId);
	
	var objLeft = document.getElementById('geoLeft');
	if(objLeft != null)
		objLeft.style.display = 'none';	
		
	if(obj != null) {
		obj.style.width = '873px';
		obj.style.height = '500px';
		obj.style.marginLeft = '5px';
	}
}

function changeTimeMap(id, val, mapId) {
	//alert("Change time period " + val);	
	if(val == null)
		return;
		
	var myIds = val.split(",");
	if(myIds.length < 5)
		return;
	
	valT = myIds[0];
	x = myIds[1];
	y = myIds[2];
	zoom = myIds[3];
	valid = myIds[4];
	//alert("D= " + valT + ',' + x + ',' + y + ',' + zoom + ',' + valid);	
	
	if(valid != '1') {
		alert('In diesem Zeitraum sind keine Inhalte verfügbar.');
		return;
	}
	
	//show/hide points
	//alert('show/hide points');
	for(var i in myTimestamps)
	{
		if(typeof(myTimestamps[i]) != 'function') {
		    //alert( i + ' => ' + myTimestamps[i] + ', ' + valT );
		    obj = myPoints[i];
		    var type = (myTimestamps[i] <= valT || valT == 7) ? true : false;
			if(obj != null) {
				if(type)
					obj.show();
				else
					obj.hide();
			}
		}
	}	
	
	//center map
	var map = document.getElementById(mapId);
	//alert(map + ',' + mapId);
	var t = mapId + '.centerAndZoom(new GPoint(' + x + ', ' + y + '), ' + zoom + ')';
	//alert(t);
	eval(t);
	
	//show/hide articles AJAX
	//alert('show/hide articles AJAX');
	var url = contentPath + '/portal/services/geo/leftPart.jsp?id=' + id + '&period=' + valT;
	//alert('Url=' + url); 
	xmlhttp = new XMLHttpRequest();
	xmlhttp.open('GET', url, true); 
	xmlhttp.onreadystatechange = function() {
		if (xmlhttp.readyState == 4 ) {
			if (xmlhttp.status == 200) {
				var resp = xmlhttp.responseText;
				resp = resp.replace("<div class=\"geoLeft\" id=\"geoLeft\">", "");
				var pos = resp.lastIndexOf("</div>");
				if(pos != -1)
					resp = resp.substr(0, pos);
				//alert('R=' + resp);
		  		document.getElementById('geoLeft').innerHTML = resp;
		  		//alert('Done');
		   	}
			else if (xmlhttp.status == 404) {
		   		alert("URL doesn't exist!");
			} else {
				alert("Status is " + xmlhttp.status);
			}
		}
		else {
			document.getElementById('geoLeft').innerHTML = "Suche läuft...";
		}
	}
	xmlhttp.send(null);
}

function showpin(id, flexId) {
	//alert('Call point ' + id + ' flex id=' + flexId);
	
	var obj = myPoints[id];
	if(obj != null) {
		var text = myTexts[id];
		obj.openInfoWindowHtml(text);
	}
//	else
//		alert('Object not found!');
}

function showPinMouseOn(id, flexId) {
	//alert('Call showPinOn for ' + id + ' flex id=' + flexId);
	
	var obj = myPoints[id];
	if(obj != null) {
		var icon = obj.getIcon();
		if(icon != null) {
			//alert('Pic2' + icon.image);
			var image = icon.image;
			var pos = image.lastIndexOf("/");
			imageNew = image.substring(0, pos) + '/over' + image.substring(pos, image.length);
			//alert(imageNew);
			//icon.image = imageNew;
			obj.setImage(imageNew);
		}
	}
}

function showPinMouseOff(id, flexId) {
	//alert('Call showPinOff for ' + id + ' flex id=' + flexId);
	
	var obj = myPoints[id];
	if(obj != null) {
		var icon = obj.getIcon();
		if(icon != null) {
			//alert('Pic2' + icon.image);
			//icon.image = imageNew;
			obj.setImage(icon.image);
		}
	}
}

function showGroup(map, posX, posY, zoom, id) {
	//alert('Show points for id=' + id + '(Center=' + posX + '/' + posY + '/' + zoom + ')');
	
	var d = eval('myGroups.g' + id);
	//alert('D=' + d);
	var myIds = d.split(",");
	for(i=0; i<myIds.length; i++) {
		idItem = myIds[i];
		//alert('I=' + idItem);
		obj = myPoints[idItem];
		//alert('Obj=' + obj);
		if(obj != null) {
			//alert("V=" + obj.isHidden());
			obj.show();
		}
//		else
//			alert('Obj not found...');
	}
	
	var obj = myPoints[id];
	if(obj != null)
		obj.hide();
		
	map.centerAndZoom(new GPoint(posY, posX), 17 - zoom);
}
