/***************************************************************
*  Copyright notice
*
*  (c) 2009 Stephan Lucas <sl@not-only-pixel.de>
*  All rights reserved
*
***************************************************************/

var map; // OpenLayers.Map
var nopttc; // CafeKarte
var pid; // calling page id

/**
 * Initialise the 'map' object
 * 
 * @param bool use_cache	Use the official Server or a cache server. Default use official
 */



CafeKarte = function (use_cache, zoomResolution){

		//@todo koordinaten.transform per php machen. algorythmus: http://proj4js.org/

		/** preload markers **/
		var preloaded_images = new Array();
		preloaded_images[0] = new Image();
		preloaded_images[0].src = 'typo3conf/ext/nopttc/pi1/OpenLayers-2.8/img/marker.png';
		preloaded_images[1] = new Image();
		preloaded_images[1].src = 'typo3conf/ext/nopttc/pi1/OpenLayers-2.8/img/marker-blue.png';		

		if (use_cache != true) {
			use_cache = false;
		}

		this.addMap();
		this.addBaseLayer(use_cache, zoomResolution);
	
	
	
	/** prepare markers **/
		this.layerMarkers = null;
		this.addMarkerLayer();
	/** Add Regions **/
		this.layerRegions = null;
		this.addRegionsLayer();
		
		
		//this.addRegions();


		
		
};
CafeKarte.default_center_coord = {};
CafeKarte.Layer = {};
CafeKarte.Layer.Region = OpenLayers.Layer.Vector;

/**
 * Center the map to the given coords
 * @param {Object} lon
 * @param {Object} lat
 * @param {Object} zoom
 */
CafeKarte.prototype.setCenter = function (lon, lat, zoom)
{
//		var centerLonLat = new OpenLayers.LonLat(9.7, 52.6).transform(new OpenLayers.Projection("EPSG:4326"), map.getProjectionObject());
	var centerLonLat = new OpenLayers.LonLat(lon, lat).transform(new OpenLayers.Projection("EPSG:4326"), map.getProjectionObject());
	map.setCenter (centerLonLat, zoom);
}

/**
 * Set the default coords to center an zoom to
 * added 2009/07/29 - sl
 * 
 * @required by centerToDefault, addRegions
 */
CafeKarte.prototype.setDefaultCenterCoord = function(lon, lat, zoom)
{
	this.default_center_coord = new Object();
	this.default_center_coord.lon = lon;
	this.default_center_coord.lat = lat;
	this.default_center_coord.zoom = zoom;
	map.defaultZoomLevel = zoom; // required by addRegions
}
/**
 * Zoom and center the map to default coords (region germany)
 * changed 2009/07/27 - ks
 * changed 2009/07/29 - sl
 */
CafeKarte.prototype.centerToDefault = function()
{	
	this.setCenter( this.default_center_coord.lon,
					this.default_center_coord.lat,
					this.default_center_coord.zoom
				   );
}
/**
 * Zoom and Center to region
 * 
 * @param {String} region
 */
CafeKarte.prototype.centerToRegion = function(region)
{
	for (var i in this.layerRegions.regionList)
	{
		if (this.layerRegions.regionList[i].region == region)
		{

		  	//OpenLayers.Console.dir(this.layerRegions.regionList[i]);
			this.layerMarkers.clearMarkers();
			this.addMarkers(region);
			this.layerRegions.setVisibility(false);
			map.zoomToExtent(this.layerRegions.regionList[i].bound, true);
			//@todo: besser per while
			break;
		}
	}
}

/**
 * Adds the base - the main layer with Open Streetmaps (OSM) data
 * 
 * @param {Object} use_cache
 */
CafeKarte.prototype.addBaseLayer = function (use_cache, zoomResolution)
{
	
	var layer_title = "Mapnik";
	var layerMapnik;
	var osm_init_obj = new Object();
	osm_init_obj.buffer = 0;	// this specifies the number of extra rows and colums of tiles on each side which will surround the minimum grid tiles to cover the map.
	
	if (zoomResolution == "std")
	{
		osm_init_obj.resolutions = [156543.0339, 78271.51695, 39135.758475, 19567.8792375, 9783.93961875, 4891.969809375, 2445.9849046875, 1222.99245234375, 611.496226171875, 305.7481130859375];
	}
	else if(zoomResolution == "adv")
	{
		//osm_init_obj.resolutions = [156543.0339, 78271.51695, 39135.758475, 19567.8792375, 9783.93961875, 4891.969809375, 2445.9849046875, 1222.99245234375, 611.496226171875, 305.7481130859375, 152.87405654296876, 76.43702827148438, 38.21851413574219];
	}
	
	layerMapnik = new OpenLayers.Layer.OSM.Mapnik(layer_title, osm_init_obj);
	
	if (use_cache == true) {
		layerMapnik.setUrl("http://www.tolletorten.de/osm-cache/osm-cache.php?${z}/${x}/${y}.png");
	}
	
	map.addLayer(layerMapnik);
/*
	layerTilesAtHome = new OpenLayers.Layer.OSM.Osmarender("Osmarender");
	map.addLayer(layerTilesAtHome);
	layerCycleMap = new OpenLayers.Layer.OSM.CycleMap("CycleMap");
	map.addLayer(layerCycleMap);
*/
}
/**
 * 
 */
CafeKarte.prototype.addMap = function()
{
	var restrictedExtent = new OpenLayers.Bounds(0, 47, 22, 58 );
	var restrictedExtent = restrictedExtent.transform(new OpenLayers.Projection("EPSG:4326"), new OpenLayers.Projection("EPSG:900913"));
	map = new OpenLayers.Map ("map", {
		controls:[
			new OpenLayers.Control.Navigation(),
			//new OpenLayers.Control.PanZoomBar(),
			//new OpenLayers.Control.LayerSwitcher(),
			new OpenLayers.Control.Attribution()
			//new OpenLayers.Control.MousePosition()
		],
		restrictedExtent: restrictedExtent,
//		numZoomLevels: 1,
		units: 'm',
		projection: new OpenLayers.Projection("EPSG:900913"),
		// enable this if you whish to see the corrent coords.. osm is working with 9000913
		displayProjection: new OpenLayers.Projection("EPSG:4326"),
		theme: "typo3conf/ext/nopttc/pi1/OpenLayers-2.8/theme/ttc/style.css"
	} );
	
	

}

/**
 * 
 */
CafeKarte.prototype.addRegionsLayer = function()
{

//        layerRegions  = new OpenLayers.Layer.Vector("Bundesländer", {styleMap: myStyles});

	// Create a select feature control and add it to the map.

		// Create a style
		var regionStyles = new OpenLayers.StyleMap({
			"default": new OpenLayers.Style({
				fillColor: "#ffae00",
				fillOpacity: 0.4,
				strokeColor: "#5e3100",
				strokeWidth: 1
			}),
			"select": new OpenLayers.Style({
				fillColor: "#f5dba3",
				fillOpacity: 0.3
			})
		});
		// add a new Layer and apply the style
		this.layerRegions  = new CafeKarte.Layer.Region("Bundesländer", {styleMap: regionStyles});	
}
/**
 * Add the Regions like nrw, nds, ...
 */
 /**
 * Anpassung Kjeld am 27.07.09 -> Koordniaten aus dem T3 BE holen und verwenden
 
 CafeKarte.prototype.addRegions = function()
{

		var points = new Array(); 
		// Niedersachsen 
		points['nds'] = [
			[10.6512451171875, 53.37158203125], [10.72265625, 53.3770751953125], [10.755615234375, 53.3056640625], [11.0906982421875, 53.140869140625], [11.151123046875, 53.1793212890625], [11.2554931640625, 53.1353759765625], [11.282958984375, 53.0914306640625], [11.35986328125, 53.0584716796875], [11.44775390625, 53.0804443359375], [11.6015625, 53.0364990234375], [11.5081787109375, 53.009033203125], [11.5081787109375, 52.9595947265625], [11.2664794921875, 52.8826904296875], [11.1126708984375, 52.899169921875], [11.019287109375, 52.9156494140625], [10.9478759765625, 52.8662109375], [10.7720947265625, 52.84423828125], [10.799560546875, 52.734375], [10.9478759765625, 52.6025390625], [10.997314453125, 52.5146484375], [10.9478759765625, 52.459716796875], [11.0687255859375, 52.3663330078125], [11.041259765625, 52.196044921875], [11.0577392578125, 52.14111328125], [10.9698486328125, 52.1026611328125], [10.997314453125, 52.0477294921875], [10.667724609375, 52.0477294921875], [10.5853271484375, 52.020263671875], [10.6402587890625, 51.954345703125], [10.5743408203125, 51.8115234375], [10.6842041015625, 51.646728515625], [10.6182861328125, 51.5753173828125], [10.5029296875, 51.5643310546875], [10.404052734375, 51.591796875], [10.2667236328125, 51.4764404296875], [10.2008056640625, 51.48193359375], [10.162353515625, 51.448974609375], [10.0579833984375, 51.43798828125], [9.942626953125, 51.383056640625], [9.898681640625, 51.4324951171875], [9.744873046875, 51.3665771484375], [9.7723388671875, 51.3116455078125], [9.722900390625, 51.3006591796875], [9.569091796875, 51.3665771484375], [9.6624755859375, 51.416015625], [9.613037109375, 51.5093994140625], [9.6844482421875, 51.5863037109375], [9.6240234375, 51.646728515625], [9.415283203125, 51.6522216796875], [9.3768310546875, 51.65771484375], [9.3988037109375, 51.756591796875], [9.47021484375, 51.866455078125], [9.3328857421875, 51.85546875], [9.129638671875, 52.1136474609375], [9.0087890625, 52.130126953125], [9.063720703125, 52.2344970703125], [8.98681640625, 52.261962890625], [9.019775390625, 52.3443603515625], [9.1351318359375, 52.393798828125], [9.1241455078125, 52.481689453125], [9.03076171875, 52.4871826171875], [8.96484375, 52.40478515625], [8.72314453125, 52.393798828125], [8.6517333984375, 52.5421142578125], [8.470458984375, 52.5091552734375], [8.4375, 52.459716796875], [8.316650390625, 52.44873046875], [8.448486328125, 52.349853515625], [8.4979248046875, 52.1795654296875], [8.3990478515625, 52.1026611328125], [8.2672119140625, 52.1356201171875], [8.19580078125, 52.0806884765625], [7.9541015625, 52.03125], [7.9156494140625, 52.108154296875], [8.0035400390625, 52.1575927734375], [7.91015625, 52.2015380859375], [7.9376220703125, 52.3114013671875], [7.8826904296875, 52.3992919921875], [7.833251953125, 52.3663330078125], [7.62451171875, 52.4652099609375], [7.5531005859375, 52.3773193359375], [7.283935546875, 52.261962890625], [7.0697021484375, 52.2235107421875], [7.0257568359375, 52.3114013671875], [7.0697021484375, 52.3828125], [6.9818115234375, 52.481689453125], [6.9378662109375, 52.44873046875], [6.7236328125, 52.4761962890625], [6.690673828125, 52.547607421875], [6.767578125, 52.5860595703125], [6.7181396484375, 52.6409912109375], [7.0477294921875, 52.6409912109375], [7.0697021484375, 52.8497314453125], [7.2015380859375, 52.987060546875], [7.2125244140625, 53.2452392578125], [7.09716796875, 53.32763671875], [6.976318359375, 53.3551025390625], [6.9873046875, 53.5198974609375], [7.1136474609375, 53.6846923828125], [7.261962890625, 53.7725830078125], [7.5970458984375, 53.800048828125], [7.789306640625, 53.822021484375], [8.0474853515625, 53.8055419921875], [8.2781982421875, 53.7066650390625], [8.349609375, 53.6517333984375], [8.448486328125, 53.6297607421875], [8.5748291015625, 53.909912109375], [8.7506103515625, 53.909912109375], [8.9373779296875, 53.85498046875], [9.11865234375, 53.865966796875], [9.261474609375, 53.8714599609375], [9.4097900390625, 53.76708984375], [9.5306396484375, 53.6737060546875], [9.5526123046875, 53.6187744140625], [9.73388671875, 53.558349609375], [9.8382568359375, 53.4375], [10.01953125, 53.41552734375], [10.08544921875, 53.45947265625], [10.1898193359375, 53.3990478515625], [10.2557373046875, 53.3990478515625], [10.3326416015625, 53.4429931640625], [10.5413818359375, 53.3660888671875], [10.6512451171875, 53.37158203125]
		];
		// Nordrhein-Westfalen 
		points['nrw'] = [
			//[6.04248046875, 51.8609619140625], [6.822509765625, 51.9708251953125], [7.086181640625, 52.2674560546875], [7.646484375, 52.4761962890625], [7.998046875, 52.0697021484375], [8.470458984375, 52.1795654296875], [8.349609375, 52.4212646484375], [8.6572265625, 52.5201416015625], [8.72314453125, 52.3992919921875], [9.07470703125, 52.4871826171875], [9.107666015625, 52.3773193359375], [8.997802734375, 52.2564697265625], [9.437255859375, 51.8499755859375], [9.349365234375, 51.6522216796875], [9.20654296875, 51.4215087890625], [8.96484375, 51.4544677734375], [8.63525390625, 51.2896728515625], [8.712158203125, 51.1907958984375], [8.54736328125, 51.0150146484375], [8.0859375, 50.7513427734375], [7.811279296875, 50.9271240234375], [6.932373046875, 50.4986572265625], [6.767578125, 50.3448486328125], [6.405029296875, 50.3668212890625], [6.21826171875, 50.6085205078125], [6.009521484375, 50.8062744140625], [6.08642578125, 50.9161376953125], [5.9326171875, 51.0589599609375], [6.17431640625, 51.1907958984375], [6.119384765625, 51.2457275390625], [6.1962890625, 51.5203857421875], [5.965576171875, 51.7840576171875], [6.0205078125, 51.8389892578125], [6.04248046875, 51.8609619140625]
			[7.070517539978027, 52.226600646972656], [7.290244102478027, 52.265052795410156], [7.548422813415527, 52.369422912597656], [7.625327110290527, 52.462806701660156], [7.834067344665527, 52.363929748535156], [7.878012657165527, 52.385902404785156], [7.932944297790527, 52.319984436035156], [7.905478477478027, 52.204627990722656], [7.993369102478027, 52.166175842285156], [7.921957969665527, 52.127723693847656], [7.949423789978027, 52.039833068847656], [8.196616172790527, 52.083778381347656], [8.268027305603027, 52.138710021972656], [8.394370079040527, 52.111244201660156], [8.487753868103027, 52.171669006347656], [8.449301719665527, 52.363929748535156], [8.344931602478027, 52.435340881347656], [8.443808555603027, 52.473793029785156], [8.482260704040527, 52.512245178222656], [8.647055625915527, 52.539710998535156], [8.723959922790527, 52.391395568847656], [8.971152305603027, 52.413368225097656], [9.037070274353027, 52.479286193847656], [9.119467735290527, 52.484779357910156], [9.130454063415527, 52.396888732910156], [9.031577110290527, 52.347450256347656], [8.987631797790527, 52.270545959472656], [9.053549766540527, 52.243080139160156], [9.020590782165527, 52.133216857910156], [9.130454063415527, 52.111244201660156], [9.328207969665527, 51.864051818847656], [9.460043907165527, 51.869544982910156], [9.405112266540527, 51.754188537597656], [9.383139610290527, 51.660804748535156], [9.427084922790527, 51.633338928222656], [9.240317344665527, 51.474037170410156], [9.157919883728027, 51.435585021972656], [9.108481407165527, 51.463050842285156], [9.026083946228027, 51.506996154785156], [8.905234336853027, 51.474037170410156], [8.949179649353027, 51.391639709472656], [8.855795860290527, 51.380653381347656], [8.707480430603027, 51.358680725097656], [8.564658164978027, 51.276283264160156], [8.614096641540527, 51.243324279785156], [8.723959922790527, 51.259803771972656], [8.745932579040527, 51.188392639160156], [8.696494102478027, 51.089515686035156], [8.531699180603027, 51.089515686035156], [8.548178672790527, 51.023597717285156], [8.377890586853027, 50.853309631347656], [8.306479454040527, 50.864295959472656], [8.141684532165527, 50.781898498535156], [8.158164024353027, 50.699501037597656], [8.064780235290527, 50.688514709472656], [7.982382774353027, 50.781898498535156], [7.987875938415527, 50.836830139160156], [7.861533164978027, 50.875282287597656], [7.856040000915527, 50.924720764160156], [7.795615196228027, 50.913734436035156], [7.702231407165527, 50.787391662597656], [7.482504844665527, 50.688514709472656], [7.394614219665527, 50.704994201660156], [7.372641563415527, 50.633583068847656], [7.240805625915527, 50.611610412597656], [7.163901329040527, 50.606117248535156], [6.911215782165527, 50.529212951660156], [6.911215782165527, 50.485267639160156], [6.872763633728027, 50.452308654785156], [6.795859336853027, 50.490760803222656], [6.762900352478027, 50.452308654785156], [6.801352500915527, 50.358924865722656], [6.664023399353027, 50.364418029785156], [6.477255821228027, 50.336952209472656], [6.367392539978027, 50.369911193847656], [6.345419883728027, 50.496253967285156], [6.208090782165527, 50.512733459472656], [6.197104454040527, 50.562171936035156], [6.279501914978027, 50.628089904785156], [6.048789024353027, 50.732460021972656], [5.999350547790527, 50.798377990722656], [6.092734336853027, 50.864295959472656], [6.070761680603027, 50.930213928222656], [6.004843711853027, 50.974159240722656], [5.905966758728027, 50.979652404785156], [5.867514610290527, 51.051063537597656], [6.169638633728027, 51.160926818847656], [6.081748008728027, 51.182899475097656], [6.070761680603027, 51.254310607910156], [6.224570274353027, 51.369667053222656], [6.219077110290527, 51.534461975097656], [6.103720664978027, 51.622352600097656], [6.032309532165527, 51.666297912597656], [6.026816368103027, 51.737709045410156], [5.955405235290527, 51.743202209472656], [5.966391563415527, 51.847572326660156], [6.158652305603027, 51.847572326660156], [6.114706993103027, 51.902503967285156], [6.405844688415527, 51.836585998535156], [6.405844688415527, 51.880531311035156], [6.680502891540527, 51.913490295410156], [6.735434532165527, 51.891517639160156], [6.834311485290527, 51.984901428222656], [6.713461875915527, 52.045326232910156], [6.779379844665527, 52.133216857910156], [6.856284141540527, 52.133216857910156], [7.004599571228027, 52.232093811035156], [7.010092735290527, 52.232093811035156], [7.070517539978027, 52.226600646972656]
		];
		// MV //
		points['mv'] = [
			[14.2108154296875, 53.9208984375], [14.28497314453125, 53.76983642578125], [14.3975830078125, 53.33587646484375], [14.09820556640625, 53.25897216796875], [14.073486328125, 53.37158203125], [13.9306640625, 53.41278076171875], [13.798828125, 53.3990478515625], [13.54339599609375, 53.4100341796875], [13.41705322265625, 53.27545166015625], [13.238525390625, 53.2781982421875], [12.98858642578125, 53.17108154296875], [12.75238037109375, 53.19854736328125], [12.52166748046875, 53.2672119140625], [12.43377685546875, 53.26446533203125], [12.381591796875, 53.3001708984375], [12.227783203125, 53.3331298828125], [12.20306396484375, 53.37158203125], [12.06298828125, 53.3660888671875], [12.0355224609375, 53.36883544921875], [11.90643310546875, 53.2672119140625], [11.69219970703125, 53.250732421875], [11.61529541015625, 53.23699951171875], [11.59881591796875, 53.1793212890625], [11.67572021484375, 53.1243896484375], [11.65374755859375, 53.04473876953125], [11.55487060546875, 53.0474853515625], [11.4532470703125, 53.07769775390625], [11.348876953125, 53.052978515625], [11.25274658203125, 53.13812255859375], [11.2115478515625, 53.14361572265625], [11.151123046875, 53.173828125], [11.0906982421875, 53.1463623046875], [10.74188232421875, 53.31390380859375], [10.75286865234375, 53.349609375], [10.7061767578125, 53.37432861328125], [10.623779296875, 53.37982177734375], [10.64849853515625, 53.46221923828125], [10.82977294921875, 53.51715087890625], [10.82977294921875, 53.57208251953125], [10.93963623046875, 53.59130859375], [10.9368896484375, 53.68743896484375], [10.78582763671875, 53.7451171875], [10.755615234375, 53.85498046875], [10.887451171875, 53.96209716796875], [11.0797119140625, 54.0142822265625], [11.18133544921875, 54.01702880859375], [11.260986328125, 53.9813232421875], [11.3433837890625, 53.95111083984375], [11.3873291015625, 53.9923095703125], [11.56036376953125, 54.0966796875], [11.69769287109375, 54.1571044921875], [11.84051513671875, 54.1461181640625], [12.1124267578125, 54.1900634765625], [12.20855712890625, 54.25872802734375], [12.337646484375, 54.29718017578125], [12.51068115234375, 54.48394775390625], [12.535400390625, 54.4866943359375], [12.55462646484375, 54.459228515625], [13.03253173828125, 54.4537353515625], [13.10943603515625, 54.60479736328125], [13.3099365234375, 54.68719482421875], [13.45550537109375, 54.68719482421875], [13.4088134765625, 54.61029052734375], [13.45001220703125, 54.58831787109375], [13.64227294921875, 54.59930419921875], [13.70269775390625, 54.569091796875], [13.6834716796875, 54.5196533203125], [13.5955810546875, 54.47296142578125], [13.62030029296875, 54.4207763671875], [13.677978515625, 54.4097900390625], [13.7933349609375, 54.34112548828125], [13.75213623046875, 54.2669677734375], [13.809814453125, 54.18182373046875], [14.0240478515625, 54.06646728515625], [14.2108154296875, 53.9208984375]
		];
		// SH //
		points['sh'] = [
			[10.3326416015625, 53.4375], [10.53314208984375, 53.37158203125], [10.6292724609375, 53.37158203125], [10.63751220703125, 53.45672607421875], [10.6842041015625, 53.45672607421875], [10.84075927734375, 53.5198974609375], [10.83251953125, 53.5693359375], [10.92041015625, 53.57757568359375], [10.953369140625, 53.67645263671875], [10.91217041015625, 53.701171875], [10.77484130859375, 53.74786376953125], [10.755615234375, 53.85498046875], [10.8929443359375, 53.95660400390625], [10.8819580078125, 53.9923095703125], [10.799560546875, 53.99505615234375], [10.7611083984375, 54.05548095703125], [10.821533203125, 54.085693359375], [10.88470458984375, 54.07745361328125], [11.10992431640625, 54.195556640625], [11.09344482421875, 54.33013916015625], [11.1895751953125, 54.38507080078125], [11.326904296875, 54.39056396484375], [11.30218505859375, 54.459228515625], [11.22528076171875, 54.52239990234375], [11.06048583984375, 54.54437255859375], [11.00006103515625, 54.48394775390625], [11.00555419921875, 54.393310546875], [10.953369140625, 54.3878173828125], [10.81329345703125, 54.33013916015625], [10.70892333984375, 54.31365966796875], [10.58807373046875, 54.37957763671875], [10.32989501953125, 54.4482421875], [10.228271484375, 54.41802978515625], [10.20355224609375, 54.43450927734375], [10.20904541015625, 54.4647216796875], [10.13214111328125, 54.4976806640625], [9.93988037109375, 54.47845458984375], [10.0469970703125, 54.54437255859375], [10.05523681640625, 54.69818115234375], [10.0030517578125, 54.73663330078125], [9.9755859375, 54.79705810546875], [9.91241455078125, 54.8162841796875], [9.8822021484375, 54.788818359375], [9.8602294921875, 54.77508544921875], [9.810791015625, 54.810791015625], [9.63775634765625, 54.84375], [9.6295166015625, 54.876708984375], [9.6075439453125, 54.8931884765625], [9.547119140625, 54.86297607421875], [9.45098876953125, 54.82452392578125], [9.39056396484375, 54.84649658203125], [9.349365234375, 54.810791015625], [9.29718017578125, 54.810791015625], [9.1680908203125, 54.8712158203125], [9.01153564453125, 54.87945556640625], [8.8934326171875, 54.90966796875], [8.84674072265625, 54.8931884765625], [8.778076171875, 54.91241455078125], [8.74237060546875, 54.89593505859375], [8.6572265625, 54.9151611328125], [8.624267578125, 54.90142822265625], [8.4210205078125, 54.89593505859375], [8.37982177734375, 54.92340087890625], [8.3990478515625, 54.9810791015625], [8.4979248046875, 55.030517578125], [8.44024658203125, 55.074462890625], [8.35235595703125, 55.04425048828125], [8.27545166015625, 54.92340087890625], [8.26446533203125, 54.73114013671875], [8.2781982421875, 54.64599609375], [8.426513671875, 54.415283203125], [8.57757568359375, 54.26971435546875], [8.8275146484375, 54.12139892578125], [8.843994140625, 53.99505615234375], [8.98406982421875, 53.865966796875], [9.2449951171875, 53.876953125], [9.36309814453125, 53.822021484375], [9.44000244140625, 53.73138427734375], [9.525146484375, 53.6737060546875], [9.5526123046875, 53.624267578125], [9.6514892578125, 53.5748291015625], [9.74212646484375, 53.558349609375], [9.7833251953125, 53.63800048828125], [9.84649658203125, 53.602294921875], [9.94537353515625, 53.65447998046875], [10.00579833984375, 53.64898681640625], [10.01678466796875, 53.67645263671875], [10.07171630859375, 53.68194580078125], [10.08819580078125, 53.72589111328125], [10.15960693359375, 53.71490478515625], [10.1898193359375, 53.734130859375], [10.20355224609375, 53.72039794921875], [10.17608642578125, 53.6737060546875], [10.2337646484375, 53.6297607421875], [10.18707275390625, 53.56658935546875], [10.18707275390625, 53.525390625], [10.2667236328125, 53.47320556640625], [10.3326416015625, 53.4375]
		];
		
		this.layerRegions.regionList = new Array(points.length);
		j = 0;
		for (var i in points)
		{
			this.layerRegions.regionList[j] = this.layerRegions.addRegion(points[i], i);
		  	//OpenLayers.Console.dir (this.layerRegions.regionList[j]);
			j++;
		}

 **/
 
/**
 * 
 * @param {Object} BeCoords
 */
CafeKarte.prototype.addRegions = function(BeCoords)
{
		
		var points = new Array();
		
		points = BeCoords;
		
		this.layerRegions.regionList = new Array(points.length);
		j = 0;
		for (var i in points)
		{
			this.layerRegions.regionList[j] = this.layerRegions.addRegion(points[i], i);
		  	//OpenLayers.Console.dir (this.layerRegions.regionList[j]);
			j++;
		}


		map.addLayer(this.layerRegions);
		var regionSelect = new OpenLayers.Control.SelectFeature(this.layerRegions, {hover: true, onSelect: this.layerRegions.polygonSelected, onUnselect: this.layerRegions.polygonUnselected});
		map.addControl(regionSelect);
		regionSelect.activate();

		/** show regions on zoomout **/
		map.events.register("zoomend", map, function (e)
		{
			//console.dir(this);
			//OpenLayers.Console.log("Resolution:" + map.layers[0].getResolution());
			//OpenLayers.Console.log("zoomlevel:" + this.getZoom());
			
			if (this.getZoom() < this.defaultZoomLevel)
			{
				for (var i=0; i < this.layers.length; i++) {
					if (map.layers[i].name == "Bundesländer") {
						layerRegions = map.layers[i];
					}
					if (map.layers[i].name == "Cafes") {
						layerMarkers = map.layers[i];
					}
				}
				if (layerRegions.visibility == false)
				{
					//OpenLayers.Console.dir(layerRegions);
					//addRegions();
					layerRegions.setVisibility(true);
					layerMarkers.clearMarkers();
				}
			}
		});

}


/**
 * Adds a Polygon and give its bounds and id back
 *
 * @global map
 * @param layer OpenLayers.Layer.Vector
 * @param coords Array() x/y points where the polygon should drive around
 *
 * @param Object An Object which held the .id and the .bound of the polygon
 */
CafeKarte.Layer.Region.prototype.addRegion = function(coords, region)
{
	var pointList = new Array();
	
	for (var i in coords)
	{
		var point = coords[i];
		var point_ll = new OpenLayers.LonLat(point[0], point[1]).transform(new OpenLayers.Projection("EPSG:4326"), map.getProjectionObject());
		pointList[i] = new OpenLayers.Geometry.Point(point_ll.lon, point_ll.lat);
	}
	
	var linearRing = new OpenLayers.Geometry.LinearRing(pointList);
	var polygonFeature = new OpenLayers.Feature.Vector(new OpenLayers.Geometry.Polygon([linearRing]));

/*

	polygonFeature.style = new Object();
	polygonFeature.style.fillColor = '#ee9900';
	polygonFeature.style.fillOpacity = 0.6;
	polygonFeature.style.hoverFillColor = '#ee9900';
	polygonFeature.style.hoverFillOpacity = 0.2;
	//polygonFeature.style
*/	
//OpenLayers.Console.dir(polygonFeature.style);

	this.addFeatures(polygonFeature);
	//OpenLayers.Console.dir(polygonFeature);

	var featureItem = new Object();

	featureItem.id = polygonFeature.id;
	featureItem.bound = polygonFeature.geometry.getBounds();
	featureItem.region = region;
	return featureItem;
}
/**
 * Fires if a feature on the Region Layer is selected
 */
CafeKarte.Layer.Region.prototype.polygonSelected = function(polygon)
{
	OpenLayers.Console.log("selected");
	polygon.layer.events.register("click", polygon, function (e)
	{
		//OpenLayers.Console.dir(this);
		// should be same as e.target but ie works only with this
		var layerTarget = OpenLayers.Event.element(e);
		var gotLayer = false;
		var i = 0;
		while (!gotLayer && i < this.layer.regionList.length)
		{
			if (layerTarget._featureId == this.layer.regionList[i].id)
			{
				OpenLayers.Console.log("click");
				//OpenLayers.Console.dir(layerTarget);
				map.zoomToExtent(layerTarget._geometry.bounds, true);
				//var px =  new OpenLayers.Pixel(e.layerX, e.layerY);
				//var ll = map.getLonLatFromViewPortPx(px);
				//map.setCenter(ll, map.getZoom());
				//map.zoomIn(5);
				//map.setCenter(ll, map.getZoom());
				
				//OpenLayers.Console.dir(nopttc.layerMarkers);
				//OpenLayers.Console.dir(this);
				// find the layerMarkers
				for (var j=0; j < this.layer.map.layers.length; j++) {
					if (this.layer.map.layers[j].name == "Cafes") {
						layerMarkers = this.layer.map.layers[j];
					}
				}
				//@todo nopttc ist ein objekt keine klasse!!
				nopttc.addMarkers(this.layer.regionList[i].region);
//				addMarkers(this.layer, this.layer.regionList[i].region);
				//this.layer.events.remove("click");
				//this.layer.destroy();
				this.layer.setVisibility(false);
				gotLayer = true;
			}
			i++;
		}
	});
}
/**
 * Fires if a feature on the Region Layer change its state from selected to unselected
 */
CafeKarte.Layer.Region.prototype.polygonUnselected = function(polygon)
{
	OpenLayers.Console.log("unselected");
	polygon.layer.events.remove("click");
}


/**
 * 
 */
CafeKarte.prototype.addMarkerLayer = function()
{

		// add the layer to the map
		this.layerMarkers = new OpenLayers.Layer.Markers("Cafes");
		map.addLayer(this.layerMarkers);
	
		// prepare icon files
		var markerIconNormal_size = new OpenLayers.Size(21, 25);
		var markerIconNormal_offset = new OpenLayers.Pixel(-(markerIconNormal_size.w/2), -(markerIconNormal_size.h / 2));
		var markerIconHighlighted_size = new OpenLayers.Size(21, 25);
		var markerIconHighlighted_offset = new OpenLayers.Pixel(-(markerIconHighlighted_size.w / 2), - (markerIconHighlighted_size.h / 2));
		
		this.markerIconNormal = new OpenLayers.Icon('typo3conf/ext/nopttc/pi1/OpenLayers-2.8/img/marker.png', markerIconNormal_size, markerIconNormal_offset);
		this.markerIconHighlighted = new OpenLayers.Icon('typo3conf/ext/nopttc/pi1/OpenLayers-2.8/img/marker-blue.png', markerIconNormal_size, markerIconHighlighted_offset);
}



/**
 * Adds a Marker with a framed cloud
 *
 * @param ol_map - deprecated
 * @global map
 * @prarm layer
 * @param lon
 * @param lat
 * @param content_id
 */
CafeKarte.prototype.addMarker = function (lon, lat, hightlight, content_id)
{
	var ll = new OpenLayers.LonLat(lon, lat).transform(new OpenLayers.Projection("EPSG:4326"), map.getProjectionObject());

	if (content_id == null || content_id == '') {
		content_id = false;
	}


	/*** Prepare the icon ***/
	var icon;
	if (hightlight) {
		icon = this.markerIconNormal.clone();
	} else {
		icon = this.markerIconHighlighted.clone();
	}
	/*** Build an the Marker and add it to the Layer ***/
	var marker = new OpenLayers.Marker(ll, icon);
	marker.content_id = content_id;	// to find him later
	this.layerMarkers.addMarker(marker);
	
	
	/*** Only add a Feature / Popup to the Marker if we have a text to display. required for singleview ***/
	if (content_id != false)
	{
		/*** Add a feature / Popup to the Marker ***/
		var feature = new OpenLayers.Feature(this.layerMarkers, ll);
		feature.closeBox = true;
		feature.popupClass = OpenLayers.Class(OpenLayers.Popup.FramedCloud, {
			minSize: new OpenLayers.Size(200, 120),
			maxSize: new OpenLayers.Size(250, 800)
		});
		//feature.popupClass = OpenLayers.Class(OpenLayers.Popup.AnchoredBubble, {} );
		//feature.data.popupContentHTML = popupContentHTML;

		//feature.popup.setContentHTML("loading");
		
		feature.data.overflow = "hidden";
		marker.feature = feature;

		
		var markerOver = function(evt){
			// get content, if not loaded
			/*
			var loading_text = "Lade Daten..";
			if (this.data.popupContentHTML == false || this.data.popupContentHTML == loading_text)
			{
				this.data.popupContentHTML = loading_text;
				var url = "index.php?eID=nopttc_pi1_ajax&action=marker_text&id=" + content_id;
				var myAjax = new Ajax.Request(url, {
					method: 'get',
					onComplete: update_popup_content
				});
			}
			*/

		}
		marker.events.register("mouseover", feature, markerOver);
		/*** Event Click ***/
		var markerClick = function(evt)
		{
			var loading_text = "Inhalt wird geladen..";
			// hide previous clicked popup
			if (this.layer.currentMarkerPopup != null) {
				this.layer.currentMarkerPopup.hide();
			}
			// display current popoup		
			if (this.popup == null) {
				this.popup = this.createPopup(this.closeBox);
				//contentHTML
				this.popup.setContentHTML(loading_text);
				map.addPopup(this.popup);
				this.popup.show();
			}
			else {
				this.popup.toggle();
			}
			this.layer.currentMarkerPopup = this.popup;
			
			// now init the content request
			if (this.popup.contentHTML == loading_text) {
				var url = "index.php?eID=nopttc_pi1_ajax&action=marker_text&id=" + content_id + "&pid=" + pid;
				var url_get_vars = get_http_get_str();
				if (url_get_vars != false) {
					url = url + "&" + url_get_vars;
				}
				var myAjax = new Ajax.Request(url, {
					method: 'get',
					onComplete: update_popup_content
				});
			}
		};
		marker.events.register("mousedown", feature, markerClick);
		this.layerMarkers.currentMarkerPopup = null;
		
		/** Hover Effekt **/
		var markerHoverIn = function(evt){
			this.inflate(1.2);			
		}
		var markerHoverOut = function(evt){
			this.inflate(0.8333);
		}
		marker.events.register("mouseover", marker, markerHoverIn);
		marker.events.register("mouseout", marker, markerHoverOut);
	}
}

function update_popup_content(orgRequest)
{
	var xmldoc = orgRequest.responseXML;
    var id = xmldoc.getElementsByTagName('id')[0].firstChild.data;
	var content = xmldoc.getElementsByTagName('text')[0].firstChild.data;
    //document.getElementById('citydiv').innerHTML = result;
	var cafe_layer = false;
	var marker = false;
	for (var i in map.layers)
	{
		if (map.layers[i].name == "Cafes")
		{
			cafe_layer = map.layers[i];
		}
	}
	for (var i in cafe_layer.markers) {
		if (cafe_layer.markers[i].content_id == id) {
			marker = cafe_layer.markers[i];
		}
	}
	if (marker != false) {
		//console.dir(marker);
		marker.feature.popup.setContentHTML(content);
		//marker.feature.data.popupContentHTML = content;
		/*
		marker.feature.popup.hide();
		marker.feature.popup.show();
		*/
	}
	 
}
 
/**
 * Extracts the get part from the current url
 * @return false or String
 */
function get_http_get_str()
{
	var get_begin = document.location.href.search(/\b\?\b/) + 1;
	var get_str = false;
	if (get_begin > 0) {
		get_str = document.location.href.substr(get_begin, document.location.href.length - get_begin);
	}
	return get_str;
}
 

