﻿//
// status
//

var username = '';
var user_id = 0;

var globalStatus = {
	showErrors:false,
	user_name: '',
	user_id: 0,
	admin: 0,
	lat: 50.933632048107846,
	lng: 6.960632801055908,
	zoom: 11,
	map_type: 0,
	clustering: 0,
	place_id: 0,
	type_filter:[true, true, true, true ],
	timeline: false,
	timeline_center: 0,
	timeline_from: 0,
	timeline_to: 0,
	page_no: 0,
	open_window: false,
	open_marker: -1,
	init: function() {
		this.placeid = (OPEN_UNORT) ? OPEN_UNORT : 0;
	},
	setUserID: function( uid ) {
		this.user_id = uid;
		user_id = uid;
	},
	setUserName: function( uname ) {
		this.user_name = uname;
		username = uname;
	},
	setCenter: function( lat, lng ) {
		this.lat = lat;
		this.lng = lng;
	}
}

function isdefined( variable)
{
    return (typeof(window[variable]) == "undefined")?  false: true;
}


if(isdefined('GLatLngBounds')) {
	var allowedBounds = new GLatLngBounds( new GLatLng( 50.8, 6.7 ), new GLatLng( 51.05, 7.2 ) );
	var allowedZoom = 12;
}




var timeline_switch = [ new Image(), new Image() ];
timeline_switch[0].src = "img/map/timebar/icon_timeline_off.png";
timeline_switch[1].src = "img/map/timebar/icon_timeline_on.png";


var globalTopMenu = 
	'<a class="newslink" href="news">Aktuell</a>'+
	'<a class="maplink" title="Alle Unorte auf der Karte" href="index.php">Unort Karte</a>'+
	'<a class="listlink" title="Alle Unorte als Liste" href="sys/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="sys/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="sys/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="sys/rss.php">RSS</a>';

function setUserID( uid ) {
	user_id = uid;
	globalStatus.user_id = uid;
	if (uid == -1)
	{
		setAdmin(0);
	}
}

function setAdmin( role )
{
	globalStatus.admin = role & 1;
}

function isAdmin()
{
	if (globalStatus.admin == 1)
		return true;
	else
		return false;
}

function setUserName( un ) {
	username = un;
	globalStatus.user_name = un;
}

function getUserID() {
	return globalStatus.user_id;
}

function checkLoggedIn( open_alert ) {
	if (globalStatus.user_id > -1)
		return true;
		
	if (open_alert)
		showMessage( 'Sie sind zur Zeit nicht angemeldet!' );
	return false;
}

// 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) {		
		alert("Error encountered: \n" + $message);
	}
}

function displayMessage($message){
	alert("Mitteilung: \n" + $message);
}


/* 	For easier recognition of functions, all methods and vars 
	for the global namespace should start with GL_
*/

// Global variables for markers and windows

var xmlHttp = createXmlHttpRequestObject();
var xmlHttp2 = createXmlHttpRequestObject();
var GL_addUnortIsOpen = 0;

var dragmarker = null;
var GL_searchMarker = null;
var overlayWindow = null;

var last_clicked_marker = null;

//
// Months
//

var mon_map = [];
mon_map["01"]  = "JAN";
mon_map["02"]  = "FEB";
mon_map["03"]  = "MAR";
mon_map["04"]  = "APR";
mon_map["05"]  = "MAI";
mon_map["06"]  = "JUN";
mon_map["07"]  = "JUL";
mon_map["08"]  = "AUG";
mon_map["09"]  = "SEP";
mon_map["10"] = "OKT";
mon_map["11"] = "NOV";
mon_map["12"] = "DEZ";

// Global definition for types of unorts

var unort_types = [ "Andere", "Bauwerk, Objekt", "Platz, Brennpunkt", "Verkehr, Stra&szlig;en und Wege", "Bereich" ];

// 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 comment_types = [ "img/win/comment_agree.gif", "img/win/comment_disagree.gif", "img/win/comment_suggestion.gif" ];
var comment_types_title = [ "Ablehnung dieses Unorts", "Unterst&uuml;tzung dieses Unorts", "Verbesserungsvorschlag" ];





// Global functions

function user_list_view_link( user_id, username ) {
	return '<a href="sys/list_single_user.php?user_id=' + user_id + '" title="Übersicht dieses Teilnehmers öffnen" >' + username + '</a>';
}

function reporting_and_unort_list_view_link( unort_id ) {
	var html = '' +
	'<div class="right">' +
	'	<a href="mailto:feedback@unortkataster.de?subject=Bedenklichen Inhalt melden: Unort ' + unort_id + '"><img src="img/btn/report_content.png" title="bedenklichen Inhalt melden" /></a>' +
	'	<a href="sys/list_single_unort.php?unort_id=' + unort_id + '"><img src="img/btn/btn_details.png" tilte="Detailansicht &#246;ffnen" /></a>' +
	'</div>';
	return html;
}

// 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;
}

function trimStr( str, max_length ) {
	if (str.length > max_length) {
		return str.substr( 0, max_length ) + '...';
	} else {
		return str;
	}
}

