
	

	var ClassLageplan = Class.create({
		mapDiv: null,
		map: null,
		immos: [],
		//geocoder: null,
		initialize: function() {
			
			Event.observe($("gesamtUebersicht"), 'click', this.showWholeMap.bind(this));
			
			if (!GBrowserIsCompatible()) return false;
			// create map
			this.map = new GMap2(document.getElementById("map"));
			
			this.map.enableScrollWheelZoom();
			
			this.immoLinkClick = this.immoLinkClick.bindAsEventListener(this);
			this.immoLinkMouseOver = this.immoLinkMouseOver.bindAsEventListener(this);
			this.initLinks();
			
			// add controlls
			this.map.addControl(new GLargeMapControl()); 
			this.map.addControl(new GMapTypeControl());
			
			this.showWholeMap();
		},
		initLinks: function() {
			$$('a.immoLink').each(function(a){
				Event.observe(a, 'click', this.immoLinkClick);
			}.bind(this));
			$$('a.immoLink').each(function(a){
				Event.observe(a, 'mouseover', this.immoLinkMouseOver);
			}.bind(this));
		},
		showWholeMap: function() {
			this.map.setCenter(new GLatLng(53.5105175, 9.9857756), 10);
			return false;
		},
		immoById: function(id) {
			for (var index = 0; index < this.immos.length; index++) {
				if (this.immos[index].id == id) break;
			}
			return this.immos[index];
		},
		mouseover: function(id) {
			immo = this.immoById(id);
			this.immoPopup(immo);
		},
		click: function(id) {
			immo = this.immoById(id);
			
			this.map.setCenter(new GLatLng(immo.geo_lat, immo.geo_lng), 13);
			this.immoPopup(immo);
		},
		immoLinkClick: function(evt) {
			evt.stop();
			
			if( evt.currentTarget )
				a = evt.currentTarget;
			else a = evt.target;
			if (IE.version > 0 && a.tagName.toLowerCase() != "a") {
				a = a.parentNode;
			}
			
			immo = this.immoById(a.id);
			
			this.immoPopup(immo);
			this.map.setCenter(new GLatLng(immo.geo_lat, immo.geo_lng), 13);
		},
		immoLinkMouseOver: function(evt) {
			evt.stop();
			
			if( evt.currentTarget )
				a = evt.currentTarget;
			else a = evt.target;
			if (IE.version > 0 && a.tagName.toLowerCase() != "a") {
				a = a.parentNode;
			}
			
			immo = this.immoById(a.id);
			
			this.immoPopup(immo);
		},
		immoPopup: function(immo) {
			
			var html = '';
			html += '<table><tr><td valign="top" width="76">';
			html += '<img src="' + immo.image + '" width="66" height="53" />';
			html += '</td><td valign="top">';
			html += immo.name;			
			html += '<br /><a href="' + immo.url + '" >mehr Infos</a>';			
			html += '</td></tr></table>';
			
			immo.marker.openInfoWindowHtml(html);
		},
		populate: function(immosData) {
			
			this.immos = immosData;
		
			/**
			 * prepare markers?!
			 */
			var iconHouse = new GIcon(G_DEFAULT_ICON);
			iconHouse.image = "/img/gm.markers/house.1.png";
			iconHouse.shadow = null;
			iconHouse.iconSize = new GSize(34, 21);
			// tinyIcon.shadowSize = new GSize(34, 21);
			iconHouse.iconAnchor = new GPoint(17, 21);
			iconHouse.infoWindowAnchor = new GPoint(17, 2);
			
			var iconPoint = new GIcon(G_DEFAULT_ICON);
			iconPoint.image = "/img/gm.markers/house.1.png";
			iconPoint.shadow = null;
			iconPoint.iconSize = new GSize(17, 17);
			// tinyIcon.shadowSize = new GSize(34, 21);
			iconPoint.iconAnchor = new GPoint(8, 17);
			iconPoint.infoWindowAnchor = new GPoint(8, 2);
			
		
			/**
			 * for all immos create marker
			 */
			for (var index = 0; index < this.immos.length; index++) {
				
				immo = this.immos[index];
//				{
//					id: 23,
//					type: "gewerbe" || "wohnung",
//					geo_lat: 1.1111111,
//					geo_lng: 1.1111111
//					marker:   WILL BE ADDED IN THIS FUNCTION
//				}
				
				if (this.immos[index].type == "gewerbe") {
					var icon = new GIcon(iconHouse);
					icon.image = "/img/gm.markers/house." + (index+1) + ".png";
				}
				else {
					var icon = new GIcon(iconPoint);
					icon.image = "/img/gm.markers/point." + (index+1) + ".png";					
				}
				var markerOptions = {
					icon: icon
				};
				var point = new GLatLng(this.immos[index].geo_lat, this.immos[index].geo_lng);
				var marker = new GMarker(point, markerOptions); 
				marker.id = this.immos[index].id;
				this.immos[index].marker = marker;
				GEvent.addListener(marker, "mouseover", test);
				GEvent.addListener(marker, "click", test2);
				this.map.addOverlay(marker);
			}
		
		}
	});
	
	function test() {
		Lageplan.mouseover(this.id);
	}
	
	function test2() {
		Lageplan.click(this.id);
	}
	
	var Lageplan;
	
	function onloadLageplan() {
		Lageplan = new ClassLageplan();
		Lageplan.populate(lageplanImmos);
	}
	
	Event.observe(window, 'load', onloadLageplan);
	Event.observe(window, 'unload', GUnload);

