﻿// Some globals
var type_icons = [
		"", 
		"../img/icon/icon_building_a2.png",
		"../img/icon/icon_place_a2.png",
		"../img/icon/icon_street_a2.png",
		"../img/icon/icon_area_a2.png",
		"../img/map/rightbar/unort.png",
		"../img/map/rightbar/ort.png"
	];

var type_icons_inactive = [
		"",
		"../img/icon/icon_building_p2.png",
		"../img/icon/icon_place_p2.png",
		"../img/icon/icon_street_p2.png",
		"../img/icon/icon_area_p2.png",
		"../img/map/rightbar/unort_p.png",
		"../img/map/rightbar/ort_p.png"
	];
var unort_types = [
		"Andere",
		"Bauwerk, Objekt",
		"Platz, Brennpunkt",
		"Verkehr, Stra&szlig;en und Wege",
		"Bereich",
		"Unorte",
		"Ex Unorte"
	];

// Global definition of the icons for the unort types

var unort_types_img = [ 
		"", 
		"img/icon/icon_building_a.gif", 
		"img/icon/icon_street_a.gif", 
		"img/icon/icon_place_a.gif", 
		"img/icon/icon_area_a.gif"
	];

// var allowedBounds = new GLatLngBounds( new GLatLng( 50.8, 6.7 ), new GLatLng( 51.05, 7.2 ) );
// var allowedZoom = 12;


var globalTopMenu = 
		'<a class="newslink" href="../news">Aktuell</a>'+
		'<a class="maplink" title="Alle Unorte auf der Karte" href="../">Unort Karte</a>'+
		'<a class="listlink" title="Alle Unorte als Liste" href="list_all_unorte.php">Unort Liste</a>'+
		'<a class="aboutlink" title="Und was soll das alles?" href="../news/unortkataster-koln">Unortkataster?</a>'+
		'<a class="userlink" title="Alle Teilnehmer" href="list_all_user.php">Teilnehmer</a>'+
		'<a class="partnerlink" title="Unsere Partner" href="../news/partner/">Partner</a>'+
		//'<a class="surveylink" title="Helfen Sie und nehmen sie kurz an der Umfrage teil! Es gibt Ruhm und Ehre zu gewinnen." href="../news/umfrage/">Umfrage</a>'+
		'<a class="statlink" title="Statistiken" href="statistics.php">Statistik</a>'+
		'<a class="helplink" title="Erläuterung und Erklärung" href="../news/hilfe/">Hilfe</a>'+
		'<a class="contactlink" title="Kontakt & Impressum" href="../news/kontakt/">Kontakt</a>'+
		'<a class="last"  title="RSS 2.0" href="./rss.php">RSS</a>';


var active_page = [];
function setActivePage( name, page ) {
	if (!active_page[''+name])
		active_page[''+name] = 1;
	if (document.getElementById( name + "_pager1_item_" + active_page[''+name] )) {
		var ap = active_page[''+name];
		document.getElementById( name + "_pager1_item_" + ap ).style.fontSize = "9px";
		document.getElementById( name + "_pager1_item_" + ap ).style.fontWeight = "normal";
		document.getElementById( name + "_pager2_item_" + ap ).style.fontSize = "9px";
		document.getElementById( name + "_pager2_item_" + ap ).style.fontWeight = "normal";
		active_page[''+name] = page;
		document.getElementById( name + "_pager1_item_" + page ).style.fontSize = "12px";
		document.getElementById( name + "_pager1_item_" + page ).style.fontWeight = "bold";
		document.getElementById( name + "_pager2_item_" + page ).style.fontSize = "12px";
		document.getElementById( name + "_pager2_item_" + page ).style.fontWeight = "bold";
	}
}


// function that displays an error message
function displayError(message) {
	// ignore errors if showErrors is false
	if (window.location.hostname.search('staging') > -1 || window.location.hostname.search('localhost') > -1) {		
		displayMessage("Error encountered: \n" + $message);	
	}
}

// sets redio button of tim efilter and jumps into the funtion that opens the calendar
function useCalendar( calendar_type, form_obj, value ) {
	for (var i=0; i<form_obj.length; i++){
			if(form_obj[i].value == value){
				form_obj[i].checked = true;
			}
	}
	calendarShow( calendar_type );
}

// Global variables for markers and windows
var xmlHttp = createXmlHttpRequestObject();
var GL_page_name;

// Global function

// creates an XMLHttpRequest instance
function createXmlHttpRequestObject() {
	// will store the reference to the XMLHttpRequest object
	var xmlHttp;
	// this should work for all browsers except IE6 and older
	try {
		// try to create XMLHttpRequest object
		xmlHttp = new XMLHttpRequest();
	} catch(e) {
		// assume IE6 or older
		var XmlHttpVersions = new Array("MSXML2.XMLHTTP.6.0",
																		"MSXML2.XMLHTTP.5.0",
																		"MSXML2.XMLHTTP.4.0",
																		"MSXML2.XMLHTTP.3.0",
																		"MSXML2.XMLHTTP",
																		"Microsoft.XMLHTTP");
		// try every prog id until one works
		for (var i=0; i<XmlHttpVersions.length && !xmlHttp; i++) {
			try {
				// try to create XMLHttpRequest object
				xmlHttp = new ActiveXObject(XmlHttpVersions[i]);
			} catch (e) {}
		}
	}
	// return the created object or display an error message
	if (!xmlHttp)
		displayError("Error creating the XMLHttpRequest object.");
	else 
		return xmlHttp;
}

// call server asynchronously
function getPager(id,total_count,start,name,itemsPerPage,allowedPageLinks) {
	// only continue if xmlHttp isn't void
	if (xmlHttp) {
		// try to connect to the server
		try {
			GL_page_name = name; 
			var query = "list_global_processAjax.php?action=getPager&id="+id+"&total_count=" + total_count + "&start=" + start + "&name=" + name + "&itemsPerPage=" + itemsPerPage + "&allowedPageLinks=" + allowedPageLinks;
			xmlHttp.open("GET", query , true);
			xmlHttp.onreadystatechange = handleGetPager;
			xmlHttp.send(null);
		} catch(e) {
			displayError(e.toString());
		}
	}
}

// function called when the state of the HTTP request changes
function handleGetPager() {
	// when readyState is 4, we are ready to read the server response
	if (xmlHttp.readyState == 4) {
		// continue only if HTTP globalStatus is "OK"
		if (xmlHttp.status == 200) {
			try {
				response = xmlHttp.responseText;
				// FIXME - make error proof
				if (response != "") {
					switch (GL_page_name) {
						case "comment":
							document.getElementById("commentPager1").innerHTML = response;
							document.getElementById("commentPager2").innerHTML = response;
							break;
						case "media":
							document.getElementById("mediaPager1").innerHTML = response;
							document.getElementById("mediaPager2").innerHTML = response;
							break;
						case "unort":
							document.getElementById("unortPager1").innerHTML = response;
							document.getElementById("unortPager2").innerHTML = response;
							break;
						case "users":
							document.getElementById("usersPager1").innerHTML = response;
							document.getElementById("usersPager2").innerHTML = response;
							break;
						case "unorte":
							document.getElementById("unortePager1").innerHTML = response;
							document.getElementById("unortePager2").innerHTML = response;
							break;
						default:
							displayError("Error while processing AJAX call.");
					}
				}
			} catch(e) {
				// display error message
				displayError(e.toString());
			}
		} else {
			// display error message
			displayError(xmlHttp.statusText);   
		}
	}
}

var uploadWin;
function popupAddMediaUploadWindow(unort_id) {
	uploadWin =  window.open( 
			"../sys/fileupload.php?place_id="+unort_id, 
			"popup_id", 
			"z-lock=yes,resizable=no,personalbar=no,location=no,menubar=no,depent=no,alwaysRaised=yes,width=380,height=320,screenX=400,screenY=300"
		);
	uploadWin.focus();
}


