//benötigt';
//	<script type="text/javascript" src="http://maps.google.com/maps?file=api&amp;v=2&amp;key={$googleMapsKey}"></script>
//	<script type="text/javascript" src="{uri_root}/scripts/elabel.js"></script>
//	<link rel="stylesheet" href="{uri_root}/styles/jMap.css" type="text/css" media="screen" />


//Constructor von JMap ****************************************************************************
function JMap(containerId, imagePath, strategicPath, worldPath, searchTerm)
{
	//containerId muss gleich mit dem Objectnamen sein
	// z.b. jMap = new JMap("jMap"); 
	
	this.id 			= containerId;
	this.startZoom 		= 2;
	this.minZoom 		= 2;
	this.maxZoom 		= 6;
	this.globalLat 		= -50.0;
	this.globalLng 		= 0.0;
	this.startLatLng 	= new GLatLng(0.0, 37.0);
	
	//Array für die Punkte
	this.location 	= new Array();
	this.filter 	= new Array();
	
	//Array für die Links der offenen/abgeschlossenen Projekte
	this.link 		= new Array();
	
	this.container = $("#" + containerId);
	this.container.addClass("jMap");

	//Controls für das Object erzeugen
	this.container.append('<form action="'+document.location.pathname+'" method="get" id="controlsDiv"></form>');
	this.controlsDiv = this.container.children('#controlsDiv');
	if (searchTerm == '') {
		searchTerm = 'search';
	}
	this.controlsDiv.append('<input type="text" value="'+ searchTerm +'" name="searchTerm" onclick="resetText(this);"/> <input class="button" type="submit" name="search" value="" />');
	
	//Div für Content erzeugen
	this.container.append('<div id="contentDiv"></div>');
	this.contentDiv = this.container.children('#contentDiv');
	
	//Div für Karte erzeugen
	this.contentDiv.append('<div id="mapDiv"></div>');
	this.mapDiv = this.contentDiv.children('#mapDiv');
	this.mapDiv.width('100%');		

	//Div für Liste erzeugen
	this.listDiv = $('#listDiv');
	this.listDiv.append("<ul id='listUL' class='list'></ul>")
	this.listUL = this.listDiv.children('#listUL');

	//Meldung wenn kein Projekt gefunden
	this.listDiv.append('<ul id="emptylist" class="list"></ul>');
	this.list = this.listDiv.children('#emptylist');
	this.list.append('<li class="entry" id="empty" style="display:none">No projects that match your selection criteria.</li>');
	this.emptyEntry = this.list.children('#empty');
	
	//Google Map ins Div laden
	this.map = new GMap2(document.getElementById('mapDiv'));
	this.map.setCenter(this.startLatLng, this.startZoom);
	this.mapDiv.map = this.map;
	
	//Karte konfigurieren
	this.map.enableDragging();
	this.map.enableDoubleClickZoom();
	
	//Controls in die Karte laden
	this.map.addControl(new GSmallMapControl());
	
	//Events für max/min Zoom und moveend erstellen
	GEvent.bind(this.map, 'moveend', this, this.moveend);
		
	this.gIcon = new GIcon(G_DEFAULT_ICON);
	this.gIcon.image = imagePath;
    this.gIcon.shadow = "";
    this.gIcon.iconSize = new GSize(20, 20);
    this.gIcon.shadowSize = new GSize(0, 0);
    this.gIcon.iconAnchor = new GPoint(10, 10);
    this.gIcon.infoWindowAnchor = new GPoint(10, 10);
    this.gIcon.infoShadowAnchor = new GPoint(0, 0);

	this.gStrategic = new GIcon(G_DEFAULT_ICON);
	this.gStrategic.image = strategicPath;
    this.gStrategic.shadow = "";
    this.gStrategic.iconSize = new GSize(20, 20);
    this.gStrategic.shadowSize = new GSize(0, 0);
    this.gStrategic.iconAnchor = new GPoint(10, 10);
    this.gStrategic.infoWindowAnchor = new GPoint(10, 10);
    this.gStrategic.infoShadowAnchor = new GPoint(0, 0);

	this.gGlobal = new GIcon(G_DEFAULT_ICON);
	this.gGlobal.image = worldPath;
    this.gGlobal.shadow = "";
    this.gGlobal.iconSize = new GSize(30, 30);
    this.gGlobal.shadowSize = new GSize(0, 0);
    this.gGlobal.iconAnchor = new GPoint(15, 15);
    this.gGlobal.infoWindowAnchor = new GPoint(15, 15);
    this.gGlobal.infoShadowAnchor = new GPoint(0, 0);
}

//Karte erstmals initialisieren
JMap.prototype.init = function()
{
	var setMarkers	= true;
	var aFilter 	= this.filter;
	var filterCount	= aFilter.length;

	for(var i=0; i<filterCount; i++) { //alle filter durchgehen
		if (this.filter[i].selected != null) {
			this.filterLinks();
			if (i == 0) {
				setMarkers = false;
				j = this.filter[i].selected;
				if(j >= 1) {
					lat = this.filter[i].options[j].lat;
					lng = this.filter[i].options[j].lng;
					zoom = this.filter[i].options[j].zoom * 1;
					this.map.setCenter(new GLatLng(lat,lng),zoom);
				} else {
					this.map.setCenter(this.startLatLng, this.startZoom);
				}
			}
			break;
		}
	}
	if (setMarkers) {
		this.setMarkers();
	}
}

//Filter setzen
JMap.prototype.addFilter = function(typ, name, title, aOption, selectedValue) {
	var filter = new Object();
	filter.parent = this;
	filter.name = name;
	filter.id = this.filter.length;
	filter.options = eval(aOption);
		
	this.controlsDiv.append('<select class="filter" id="filter' + name + '" name="' + name + '"><option value="">' + title + '</option></select>');
	filter.dom = this.controlsDiv.children('#filter' + name);
	count = filter.options.length;
	var selectedIndex = -1;
	if(typ == 'geo') {
		for(i=0;i<count;i++) {
			filter.dom.append('<option value="' + filter.options[i].title + '">' + filter.options[i].title + '</option>');
			if (selectedValue == filter.options[i].title) {
				selectedIndex = i;
			}
		}
	} else {
		for(i=0;i<count;i++) {
			filter.dom.append('<option value="' + filter.options[i].id + '">' + filter.options[i].title + '</option>');
			if (selectedValue == filter.options[i].id) {
				selectedIndex = i;
			}
		}
	}
	
	dom = document.getElementById(filter.dom.attr('id'));
	dom.selectedIndex = selectedIndex+1;
	
	if(typ == 'geo') {
		GEvent.bindDom(dom,'change',filter,this.mapFilterClick);
	} else {
		GEvent.bindDom(dom,'change',filter,this.filterClick);
	}

	var filterObj = new Object();
	filterObj.name = name;
	filterObj.options = eval(aOption);

	if(typ == 'geo') {
		filterObj.selected = (selectedIndex >= 0) ? selectedIndex : null;
	} else {
		filterObj.selected = (selectedIndex >= 0) ? filter.options[selectedIndex].id : null;
	}
	this.filter.push(filterObj);			
}

//Locations hinzufuegen
JMap.prototype.addLocations = function(jsonLocation)
{
	aLocation = eval(jsonLocation);
	for(var i=0;i<aLocation.length;i++)
	{
		if(aLocation[i]["lat"]!=0 && aLocation[i]["lng"]!=0) {
			locationId = aLocation[i]["id"];
			oLocation = new Location(locationId,aLocation[i]["title"],aLocation[i]["lat"],aLocation[i]["lng"],this.map,this.gIcon,this.gStrategic,this,false);
			this.location[locationId] = oLocation;
		} else {
			// Globale Projekte
			locationId = aLocation[i]["id"];
			oLocation = new Location(locationId,aLocation[i]["title"],this.globalLat,this.globalLng,this.map,this.gGlobal,this.gGlobal,this,true);
			this.location[locationId] = oLocation;
		}
	}
}

//Projekte hinzufuegen
JMap.prototype.add = function(id, title, strategic, jsonLocation, href, jsonTags)
{
	this.link[id] = new Link(id,title,href,jsonTags,this);
	
	aLocation = eval(jsonLocation);
	var count = aLocation.length;
	for(var i=0;i<count;i++)
	{
		locationId = aLocation[i];
		//ist Projekt global
		if(this.location[locationId].global) {
			this.link[id].setGlobal();
		}
		if(strategic == "0") {
			this.location[locationId].changeToNonStrategic();
		}
		this.location[locationId].addLink(this.link[id]);
	}
	this.listUL.append("<li class='entry' id='listentry" + id + "'>" + this.link[id].link + "</li>");
}

JMap.prototype.filterClick = function()
{
	dom = document.getElementById(this.dom.attr("id"));
	i = dom.selectedIndex;
	this.parent.filter[this.id].selected = (i > 0) ? this.options[i-1].id : null;
	this.parent.filterLinks();
	this.parent.setMarkers();
}

JMap.prototype.mapFilterClick = function()
{
	dom = document.getElementById(this.dom.attr("id"));
	i = dom.selectedIndex;
	this.parent.filter[this.id].selected = (i > 0) ? i-1 : null;
	this.parent.filterLinks();
	if(i > 1) {
		lat = this.options[i-1].lat;
		lng = this.options[i-1].lng;
		zoom = this.options[i-1].zoom * 1;
		this.parent.map.setCenter(new GLatLng(lat,lng),zoom);
	} else {
		this.parent.map.setCenter(this.parent.startLatLng, this.parent.startZoom);
	}
}

JMap.prototype.mapCheckResize = function()
{
	this.map.checkResize();
	setTimeout(this.id + ".setMarkers()",200);
}

//event moveeend
JMap.prototype.moveend = function() {
	this.checkZoom();
}

JMap.prototype.checkZoom = function() {
	
	var actZoom = this.map.getZoom();
	if(actZoom < this.minZoom) {
		this.map.setZoom(this.minZoom);
	} else {
		if(actZoom > this.maxZoom) {
			this.map.setZoom(this.maxZoom);	
		}
	}

	if(actZoom != 2 && this.mapDiv.css("width") != "100%") {
		center = this.map.getCenter();
		this.mapDiv.width("100%");		
		this.map.checkResize();
		this.map.setCenter(center);
	} else {
		this.setMarkers();	
	}
}

JMap.prototype.filterLinks = function() {
	for(var i=0; i<this.link.length; i++) {
		if(this.link[i] != null) { //null objekte aus array filtern
			this.link[i].filter();	
		}
	}	
}

JMap.prototype.setMarkers = function() {
	//überprüfen welche punkte sich im Kartenausschnitt befinden

	bEmpty 			= true;
	regionFilter 	= this.filter[0].selected;
	gBound 			= this.map.getBounds();
	
	this.listUL.children().hide();

	for(var i=0;i<this.location.length;i++)
	{
		if(this.location[i] != null) //null objekte aus array filtern
		{
			latlng = new GLatLng(this.location[i].lat, this.location[i].lng);
			inMap = gBound.containsLatLng(latlng);
			this.location[i].hide();
			
			if (regionFilter < 1 && this.location[i].global && this.location[i].show()) {
				bEmpty = false;
			}
			if (regionFilter != 0 && !this.location[i].global && inMap && this.location[i].show()) {
				bEmpty = false;
			}
		}
	}
	
	if(bEmpty) {
		this.emptyEntry.show();
	} else {
		this.emptyEntry.hide();		
	}
	
}

//LOCATION ************************************************************

function Location(id, title, lat, lng, map, gIcon, gStrategic, parent, global)
{
	this.id = id;
	this.title = (title == 'Worldwide') ? 'Global Projects' : title;
	this.lat = lat;
	this.lng = lng;
	this.link = new Array();
	
	this.gIcon = gIcon;
	this.gStrategic = gStrategic;
	this.strategic = true;
	this.map = map
	
	this.label = null;
	this.marker = null;
	this.parent = parent;
	this.global = global;
	
	this.href = "";
}

Location.prototype.show = function(map)
{
	var visible = 0;
	var link = new Array();
	for(var i=0; i<this.link.length; i++) {
		if(this.link[i].visible) {
			visible++;
			link.push(this.link[i].link);
			href = this.link[i].href;
			
			//in Listview anzeigen
			this.parent.listUL.children("#listentry" + this.link[i].id).show();
		}
	}
	if(visible > 0) {
		if(this.marker != null) {
			this.marker.show();
			this.label.show();
			this.label.setContents("<p>" + visible + "</p>");					
		} else {
			if (this.global) {
				this.label 				= new ELabel(new GLatLng(this.lat,this.lng), "<p>" + visible + "</p>", 'labelStyleGlobal');
				this.label.pixelOffset 	= new GSize(-15,15);
				var title 				= visible > 1 ? 'Global Projects' : 'Global Project';
			} else {
				this.label 				= new ELabel(new GLatLng(this.lat,this.lng), "<p>" + visible + "</p>", 'labelStyle');
				this.label.pixelOffset 	= new GSize(-10,10);
				if (this.strategic) {
					var title			= visible > 1 ? 'Strategic Projects' : 'Strategic Project';
				} else {
					var title			= '';
				}
			}
			this.map.addOverlay(this.label);
			if(this.strategic) {
				var opts = {icon:this.gStrategic, title:title};
			} else {
				var opts = {icon:this.gIcon, title:title};
			}
			this.marker = new GMarker(new GLatLng(this.lat,this.lng), opts);				
			GEvent.bind(this.marker, "infowindowbeforeclose", this, this.infowindowclose);	
			GEvent.bind(this.marker, "click", this, this.click);	
			this.map.addOverlay(this.marker);
		}

		if(visible == 1) {
			this.href = href;
		} else {
			this.href = "";
			//infowindow
			this.html = "<div class=window><strong>" + this.title + "</strong><ul class=list>";
			for(i=0; i<link.length; i++) {
				this.html = this.html + "<li class='entry'>" + link[i] + "</li>";
			}
			this.html = this.html + "</ul></div>";		}
		return true;
	}
	return false;
}

Location.prototype.hide = function()
{
	if(this.marker != null) {
		this.marker.hide();
		this.label.hide();
	}
}

Location.prototype.addLink = function(link)
{
	this.link.push(link);
}

Location.prototype.infowindowclose = function()
{
	if(this.parent.state == "splitView") {
		this.parent.splitView(0);
	}
}

Location.prototype.click = function()
{
	if(this.href != "") {
		document.location = this.href;
	} else {
		this.marker.openInfoWindowHtml(this.html);	
	}
}

Location.prototype.changeToNonStrategic = function()
{
	if(this.strategic == true) {
		this.strategic = false;
	}
}

//LINK *****************************************************

function Link(id,title,href,jsonTags,parent)
{
	this.id = id;
	this.title = title;
	this.href = href;
	this.aTags = eval(jsonTags);
	this.visible = true;
	this.link = "<a href=" + href + ">" + title + "</a>";
	this.parent = parent;
	this.global = false;
}

Link.prototype.filter = function()
{
	aFilter = this.parent.filter;
	regionFilter = this.parent.filter[0].selected;

	filterCount = aFilter.length;
	this.visible = true;
	var found;
	for(var i=1; i<filterCount; i++) //alle filter durchgehen
	{
		oFilter = aFilter[i];
		if(oFilter.selected != null) //filter gesetzt
		{
			var found = false;
			for(var j in this.aTags[oFilter.name])
			{
				if(this.aTags[oFilter.name][j] == oFilter.selected) //filter stimmt überein
				{
					found = true;
				}
			}
			this.visible = this.visible && found;
		}
	}
	if (regionFilter > 1 && this.global) {
		this.visible = false;
	}
}

Link.prototype.setGlobal = function()
{
	this.global = true;
}
