// map class for displaying the map.
function map(city, project, view) {
	var city = city;
	var project = project;
	var view = view;
	
	// Städtekoordinaten auslesen
	// Essen
	if (city == 'es') {
		kordinate1 = '51.4580686';
		kordinate2 = '7.0147614';
		zoomlevel = 12;
	// Duisburg
	} else if (city == 'du') {
		kordinate1 = '51.4327884';
		kordinate2 = '6.7680565';
		zoomlevel = 12;
	// Oberhausen
	} else if (city == 'ob') {
		kordinate1 = '51.469612';
		kordinate2 = '6.8658829';
		zoomlevel = 13;
	// Bochum
	} else if (city == 'bo') {
		kordinate1 = '51.482901';
		kordinate2 = '7.21181';
		zoomlevel = 12;
	// Dortmund
	} else if (city == 'do') {
		kordinate1 = '51.5120542';
		kordinate2 = '7.4635729';
		zoomlevel = 12;
	// Ruhrgebiet
	} else
	{
		kordinate1 = '51.482901';
		kordinate2 = '7.21181';	
		zoomlevel = 9;
	}

	// Karte erstellen
	var map = new GMap2(document.getElementById("gmap"));
	map.addControl(new GSmallMapControl());
	map.addControl(new GMapTypeControl());
	map.addMapType(G_PHYSICAL_MAP);

	if (view != '1') {
		if (city == '0' && project == '0') {
			map.setCenter(new GLatLng(kordinate1,kordinate2), zoomlevel, G_SATELLITE_MAP);
		} else
		{
			map.setCenter(new GLatLng(kordinate1,kordinate2), zoomlevel, G_PHYSICAL_MAP);
		}
	} else
	{
		// Project Detailansicht
		// Situation Kunst
		if (project == 'sk') {
			map.setCenter(new GLatLng(51.44867149,7.1896264), 19, G_SATELLITE_MAP);
			
			// Movie Pins
			map.addOverlay(createMovie(new GLatLng(51.44825025,7.18946815),'upload/map/projects/situationkunst/maximdahl.jpg','Kunstsammlung f&uuml;r Max Imdahl und seine Studenten','essentials/bochum/situation-kunst/filme/kunstsammlung-fuer-max-imdahl-und-seine-studenten.html'));
			map.addOverlay(createMovie(new GLatLng(51.44900581,7.18984097),'upload/map/projects/situationkunst/esgibthierkeinezufaelle.jpg','&quot;Es gibt hier keine Zuf&auml;lle&quot;','essentials/bochum/situation-kunst/filme/es-gibt-hier-keine-zufaelle.html'));
			map.addOverlay(createMovie(new GLatLng(51.44838231,7.18975246),'upload/map/projects/situationkunst/circuit.jpg','Richard Serra &quot;Circuit&quot;','essentials/bochum/situation-kunst/filme/richard-serra-circuit.html'));
			map.addOverlay(createMovie(new GLatLng(51.44844081,7.18908191),'upload/map/projects/situationkunst/tyndalesculpture.jpg','David Rabinowitch &quot;Tyndale-Sculpture&quot;','essentials/bochum/situation-kunst/filme/david-rabinowitch-tyndale-sculpture.html'));
		}
		// Kunstmuseum Bochum
		if (project == 'kmb') {
			map.setCenter(new GLatLng(51.48732828,7.22132877), 19, G_SATELLITE_MAP);
						
			// Movie Pins
			map.addOverlay(createMovie(new GLatLng(51.4875095,7.22128719),'upload/map/projects/kunstmuseumbochum/raeumetraeume.jpg','R&aumlume Tr&aumlume - Eine Ausstellung von Peter Pabst','essentials/bochum/kunstmuseum-bochum/filme/raeume-traeume-eine-ausstellung-von-peter-pabst.html'));
		}
		// Christus Kirche
		if (project == 'ck') {
			map.setCenter(new GLatLng(51.48218384,7.21367776), 19, G_SATELLITE_MAP);
			
			// Movie Pins
			map.addOverlay(createMovie(new GLatLng(51.48208279,7.21357048),'upload/map/projects/christuskirche/kirchederkulturen.jpg','Christus Kirche - Kirche der Kulturen','essentials/bochum/christus-kirche/filme/christus-kirche-kirche-der-kulturen.html'));
			map.addOverlay(createMovie(new GLatLng(51.48189738,7.21360534),'upload/map/projects/christuskirche/pev2010.jpg','PEV 2010 - Platz des Europäischen Versprechens','essentials/bochum/christus-kirche/filme/pev-2010-platz-des-europaeischen-versprechens.html'));
			map.addOverlay(createMovie(new GLatLng(51.48226653,7.21375152),'upload/map/projects/christuskirche/celinerudolph.jpg','Céline Rudolph und Michael Wollny','essentials/bochum/christus-kirche/filme/celine-rudolph-und-michael-wollny.html'));
		}
		// Urbanatix
		if (project == 'ux') {
			map.setCenter(new GLatLng(51.47746064,7.2149384), 19, G_SATELLITE_MAP);
			
			// Movie Pins
			map.addOverlay(createMovie(new GLatLng(51.477474,7.21465945),'upload/map/projects/urbanatix/koerpersport.jpg','Stilrichtung: K&ouml;rpersport','essentials/bochum/urbanatix/filme/stilrichtung-koerpersport.html'));
		}
		// Kubus
		if (project == 'ku') {
			map.setCenter(new GLatLng(51.44798948,7.18945742), 19, G_SATELLITE_MAP);
			
			// Movie Pins
			map.addOverlay(createMovie(new GLatLng(51.4477822,7.18919992),'upload/map/projects/kubus/kubusdieerweiterung.jpg','KUBUS - die Erweiterung der Ruhruniversit&auml;t in Haus Weitmar','essentials/bochum/kubus/filme/kubus-die-erweiterung-der-ruhruniversitaet-in-haus-weitmar.html'));
			map.addOverlay(createMovie(new GLatLng(51.44764178,7.1892643),'upload/map/projects/kubus/weltsichten.jpg','&quot;Weltsichten&quot; - Landschaften in der Kunst vom 17 Jh. bis heute','essentials/bochum/kubus/filme/weltsichten-landschaften-in-der-kunst-vom-17-jh-bis-heute.html'));
			map.addOverlay(createMovie(new GLatLng(51.44820679,7.18968809),'upload/map/projects/kubus/overground.jpg','&quot;Overground&quot; - eine einzigartige Audio-Video-Installation am KUBUS','essentials/bochum/kubus/filme/overground-eine-einzigartige-audio-video-installation-am-kubus.html'));
		}
		// Skulpturenpfad
		if (project == 'srs') {
			map.setCenter(new GLatLng(51.51173391,7.01683044), 17, G_SATELLITE_MAP);
			
			// Movie Pins
			map.addOverlay(createMovie(new GLatLng(51.48001649,7.22388089),'upload/map/projects/skulpturenpfad/terminal.jpg','Richard Serras &quot;Terminal&quot; - sein erstes gro&szlig;es Projekt in Europa','essentials/bochum/skulpturenpfad/filme/richard-serras-terminal-sein-erstes-grosses-projekt-in-europa.html'));
			map.addOverlay(createMovie(new GLatLng(51.44838398,7.18975246),'upload/map/projects/skulpturenpfad/rauminstallation.jpg','Die umbaute Rauminstallation &quot;Circuit&quot;','essentials/bochum/skulpturenpfad/filme/die-umbaute-rauminstallation-circuit.html'));
			map.addOverlay(createMovie(new GLatLng(51.51285065,7.01912642),'upload/map/projects/skulpturenpfad/bramme.jpg','Die &quot;Bramme f&uuml;r das Ruhrgebiet&quot; ist ein &uuml;berragendes Zeichen','essentials/bochum/skulpturenpfad/filme/die-bramme-fr-das-ruhrgebiet-ist-ein-berragendes-zeichen.html'));
			map.addOverlay(createMovie(new GLatLng(51.44900289,7.18971625),'upload/map/projects/skulpturenpfad/tod.jpg','&quot;Tod&quot; - das vielleicht existentiellste Werk von Richard Serra','essentials/bochum/skulpturenpfad/filme/tod-das-vielleicht-existentiellste-werk-von-richard-serra.html'));
		}
		// Ruhrtriennale
		if (project == 'rt') {
			map.setCenter(new GLatLng(51.48062787,7.19805121), 17, G_SATELLITE_MAP);
			
			// Movie Pins
			map.addOverlay(createMovie(new GLatLng(51.48136285,7.19872713),'upload/map/projects/ruhrtriennale/willydecker.jpg','Was macht die Ruhrtriennale so ruhr-essentiell?','essentials/bochum/ruhrtriennale/filme/was-macht-die-ruhrtriennale-so-ruhr-essentiell.html'));
			map.addOverlay(createMovie(new GLatLng(51.48114236,7.19838381),'upload/map/projects/ruhrtriennale/neinhaus.jpg','Wo steht die Ruhrtriennale?','essentials/bochum/ruhrtriennale/filme/wo-steht-die-ruhrtriennale.html'));
		}
		// Schauspielhaus
		if (project == 'sh') {
			map.setCenter(new GLatLng(51.47220147,7.21737385), 18, G_SATELLITE_MAP);
			
			// Movie Pins
			map.addOverlay(createMovie(new GLatLng(51.47259576,7.21699834),'upload/map/projects/schauspielhaus/bochumerschauspielhaus.jpg','Unser Bochumer Schauspielhaus','essentials/bochum/schauspielhaus/filme/unser-bochumer-schauspielhaus.html'));
			map.addOverlay(createMovie(new GLatLng(51.47240864,7.21696615),'upload/map/projects/schauspielhaus/publikum.jpg','Das au&szlig;ergew&ouml;hnliche Publikum','essentials/bochum/schauspielhaus/filme/das-aussergewoehnliche-publikum.html'));
			map.addOverlay(createMovie(new GLatLng(51.47215469,7.21758842),'upload/map/projects/schauspielhaus/kulissen.jpg','Hinter den Kulissen','essentials/bochum/schauspielhaus/filme/hinter-den-kulissen.html'));
			map.addOverlay(createMovie(new GLatLng(51.47229503,7.21705198),'upload/map/projects/schauspielhaus/anselmweber.jpg','Anselm Weber &uml;ber seine Intendanz','essentials/bochum/schauspielhaus/filme/anselm-weber-ueber-seine-intendanz.html'));
		}
		// Fidena
		if (project == 'fd') {
			map.setCenter(new GLatLng(51.44221,7.19221), 20, G_SATELLITE_MAP);
			
			// Movie Pins
			map.addOverlay(createMovie(new GLatLng(51.44222036,7.19204575),'upload/map/projects/fidena/fidena_figurentheater.jpg','Fidena ist das Figurentheater der Nationen','essentials/bochum/fidena/filme/fidena-ist-das-figurentheater-der-nationen.html'));
		}
	}

	// Overlay Areale 
	if (city == '0') {
		var pointSW = new GLatLng(51.158677,6.262207);
		var pointNE = new GLatLng(51.835778,8.201294);
		var groundOverlay = new GGroundOverlay('images/map/areale.png', new GLatLngBounds(pointSW, pointNE));		
		map.addOverlay(groundOverlay);

		GEvent.addListener(map, "maptypechanged", function() {
			if (map.getCurrentMapType().getName() == 'Satellit' && map.getZoom() <= '9') {
				map.addOverlay(groundOverlay);
			} else
			{
				if (map.getZoom() >= '9' && map.getCurrentMapType().getName() == 'Satellit') {
					map.addOverlay(groundOverlay);
				} else
				{
					map.removeOverlay(groundOverlay);
				}
			}
		});
	
		GEvent.addListener(map, "zoomend", function() {
			if (map.getZoom() <= '9' && map.getCurrentMapType().getName() == 'Satellit') {
				map.addOverlay(groundOverlay);
			} else
			{
				if (map.getZoom() >= '9' && map.getCurrentMapType().getName() == 'Satellit') {
					map.addOverlay(groundOverlay);
				} else
				{
					map.removeOverlay(groundOverlay);
				}
			}
		});
	}

	// Project Pins
	// Situation Kunst
	if (project == 'sk' || city == 'bo' || city == '0') {
		map.addOverlay(createMarker(new GLatLng(51.44867149,7.1896264),'upload/map/projects/situationkunst/situationkunst.jpg','Situation Kunst Bochum','essentials/bochum/situation-kunst','map(\'bo\',\'sk\',\'1\')'));
	}	
	// Kunstmuseum Bochum
	if (project == 'kmb' || city == 'bo' || city == '0') {
		map.addOverlay(createMarker(new GLatLng(51.48732828,7.22132877),'upload/map/projects/kunstmuseumbochum/kunstmuseumbochum.jpg','Kunstmuseum Bochum','essentials/bochum/kunstmuseum-bochum','map(\'bo\',\'kmb\',\'1\')'));
	}
	// Christus Kirche
	if (project == 'ck' || city == 'bo' || city == '0') {
		map.addOverlay(createMarker(new GLatLng(51.48239347,7.213718),'upload/map/projects/christuskirche/christuskirche.jpg','Christus Kirche Bochum','essentials/bochum/christus-kirche','map(\'bo\',\'ck\',\'1\')'));
	}
	// Urbanatix
	if (project == 'ux' || city == 'bo' || city == '0') {
		map.addOverlay(createMarker(new GLatLng(51.47746064,7.2149384),'upload/map/projects/urbanatix/urbanatix.jpg','Urbanatix Bochum','essentials/bochum/urbanatix','map(\'bo\',\'ux\',\'1\')'));
	}
	// Kubus
	if (project == 'ku' || city == 'bo' || city == '0') {
		map.addOverlay(createMarker(new GLatLng(51.44770029,7.1892643),'upload/map/projects/kubus/kubus.jpg','KUBUS Bochum','essentials/bochum/kubus','map(\'bo\',\'ku\',\'1\')'));
	}
	// Skulpturenpfad
	if (project == 'srs' || city == 'bo' || city == 'es' || city == '0') {
		map.addOverlay(createMarker(new GLatLng(51.51173391,7.01683044),'upload/map/projects/skulpturenpfad/skulpturenpfad.jpg','Skulpturenpfad Bochum und Essen','essentials/bochum/skulpturenpfad','map(\'bo\',\'srs\',\'1\')'));
	}
	// Ruhrtriennale
	if (project == 'rt' || city == 'bo' || city == '0') {
		map.addOverlay(createMarker(new GLatLng(51.48062787,7.19805121),'upload/map/projects/ruhrtriennale/ruhrtriennale.jpg','Ruhrtriennale Bochum','essentials/bochum/ruhrtriennale','map(\'bo\',\'rt\',\'1\')'));
	}
	// Schauspielhaus
	if (project == 'sh' || city == 'bo' || city == '0') {
		map.addOverlay(createMarker(new GLatLng(51.47220147,7.21737385),'upload/map/projects/schauspielhaus/schauspielhaus.jpg','Schauspielhaus Bochum','essentials/bochum/schauspielhaus','map(\'bo\',\'sh\',\'1\')'));
	}
	// Fidena
	if (project == 'fd' || city == 'bo' || city == '0') {
		map.addOverlay(createMarker(new GLatLng(51.44221,7.19221),'upload/map/projects/fidena/fidena.jpg','Fidena Bochum','essentials/bochum/fidena','map(\'bo\',\'fd\',\'1\')'));
	}

	function createMarker(point,image,headline,projectlink,maplink) {
    	var customIcon = new GIcon(G_DEFAULT_ICON);
        customIcon.image = 'images/map/pin.png';
      	markerOptions = { icon:customIcon };
      	var marker = new GMarker(point, markerOptions);

      	GEvent.addListener(marker, "click", function() {
			if(isDefined("info")) {
				map.removeOverlay(info);
			}

			info = new Infowin(point, '<div id="gmap_project"><a href="javascript://" onClick="closeWindow()"><img src="' + image + '" width="217" height="180" border="0" alt="' + headline + '" title="' + headline + '" /></a></div><div id="gmap_close"><a href="javascript://" onClick="closeWindow()"><img src="images/map/close.png" width="15" height="11" border="0" alt="Schlie&szlig;en" title="Schlie&szlig;en" /></a></div><div id="gmap_allmovies"><a href="' + projectlink + '"><img src="images/slide/allmovies.png" width="73" height="11" border="0" alt="Alle Filme" title="Alle Filme" /></a></div><div id="gmap_zoom"><a href="javascript://" onClick="' + maplink + '"><img src="images/map/zoom.png" width="46" height="11" border="0" alt="Zoom" title="Zoom" /></a></div>');
			map.panTo(point);
			map.addOverlay(info);
      	});
      	return marker;
    }
	
	function createMovie(point,image,headline,projectlink) {
    	var customIcon = new GIcon(G_DEFAULT_ICON);
        customIcon.image = 'images/map/movie.png';
      	markerOptions = { icon:customIcon };
      	var marker = new GMarker(point, markerOptions);

      	GEvent.addListener(marker, "click", function() {
			if(isDefined("info")) {
				map.removeOverlay(info);
			}

			info = new Infowin(point, '<div id="gmap_project"><a href="javascript://" onClick="closeWindow()"><img src="' + image + '" width="217" height="180" border="0" alt="' + headline + '" title="' + headline + '" /></a></div><div id="gmap_close"><a href="javascript://" onClick="closeWindow()"><img src="images/map/close.png" width="15" height="11" border="0" alt="Schlie&szlig;en" title="Schlie&szlig;en" /></a></div><div id="gmap_watchmovie"><a href="' + projectlink + '"><img src="images/slide/watchmovie.png" width="97" height="11" border="0" align="top" alt="Film abspielen" title="Film abspielen" /></a></div>');
			map.panTo(point);
			map.addOverlay(info);
      	});
      	return marker;
    }
	
	isDefined = function(variable) { 
		return this[variable] === undefined ? false : true;
	};
	
	closeWindow = function() {
		map.removeOverlay(info);
	};
}


// Infowin class for displaying a miniature info window.
function Infowin(latlng, html) {
	this.latlng_ = latlng;
	this.html_ = html;
	this.prototype = new GOverlay();

	// Creates the DIV representing the infowindow
	this.initialize = function(map) {
		var div = $('<div />');
		div.css({
			position : 'absolute',
			width : 318,
			height: 221,
			'background-image' : 'url(images/map/infowindow.png)'
		}).appendTo(map.getPane(G_MAP_FLOAT_PANE))
	
		this.map_ = map;
		this.div_ = div;

		this.update(html);
	}

	this.update = function(html){
		this.html_ = html;
		
		this.div_.empty();

		var content = $('<div/>').addClass('infowin-content').css({
			'position' : 'relative',
			'overflow' : 'hidden'
		}).html(html);
		
		$('<div />').css({
			'padding' : '0 10px 25px 0'
		}).append(content).appendTo(this.div_);
		
		this.redraw(true);
	}
	
	// Remove the main DIV from the map pane
	this.remove = function() {
	  this.div_.remove();
	}

	// Copy our data to a new instance
	this.copy = function() {
	  return new Infowin(this.latlng_, this.html_);
	}

	// Redraw based on the current projection and zoom level
	this.redraw = function(force) {
		if (!force) return;

		var point = this.map_.fromLatLngToDivPixel(this.latlng_);

		// Now position our DIV based on the DIV coordinates of our bounds
		this.div_.css({
			left : point.x - 202,
			top : point.y - this.div_.height() - 22
		});
	}
}
