function browserIsCompatibleWithMapView() {
	if(/msie/i.test(navigator.userAgent) && !/opera/i.test(navigator.userAgent))
	   return false;
	else
		return GBrowserIsCompatible()
}

switch(document.domain.split('.').last()) {
	default:
	case 'de':
	case 'at':
	case 'ch':
		var sitemap_location = (language_token == 'fr' ? '/logement' : '/wohnen');
		break;	

	case 'pl':
		var sitemap_location = '/stancje';
		break;
}

/**
 * Google Maps Interface for Habitation Offers
 *
 * Needs prototype to work
 */
var Map = {
	map: null,
	markers: new Hash(),
	clusterer: null,
	loaded: false,
	
	init: function(tld, container, legend_strings) {
		this.map = new GMap2($(container));
 		this.map.setUIToDefault();
		this.map.disableScrollWheelZoom();
		this.map.removeMapType(G_PHYSICAL_MAP);
		if(legend_strings) {
			legend_content = '<img src="/res/img/habitation/apartment.gif" align="top" /> ' + legend_strings.apartment + ' <img src="/res/img/habitation/flatshare.gif" align="top" /> '  + legend_strings.flatshare;
			if(legend_content) {
				this.map.addControl(new LegendControl(legend_content));
			}
		}

		// set center of map
		this.setCenter(tld);
		
		this.clusterer = new Clusterer(this.map);
		// change the cluster icon
		var ico = new GIcon(G_DEFAULT_ICON);
		ico.shadow = '/res/img/habitation/marker_clusterer_shadow.png';
		ico.shadowSize = new GSize(62.0, 48.0);
		ico.image = '/res/img/habitation/marker_clusterer.png';
		ico.iconSize = new GSize(37, 48);
		ico.iconAnchor = new GPoint(19, 48);
		ico.imageMap = [0,0, 35,0, 35,46, 0,46];
		this.clusterer.SetIcon(ico);
		
		// overwrite click method of clusters. let's zoom-in instead of displaying this ugly popup
		Clusterer.PopUp = function (cluster) {
			Map.map.zoomIn(cluster.marker.getLatLng());
		};
		
		Event.observe(window, 'beforeunload', function() { GUnload(); });
		
		this.loaded = true;
	},
	
	setCenter: function(tld) {
		switch(tld) {
			case 'pl':
				this.map.setCenter(new GLatLng(52.0, 19.6), 6);
				break;
			
			case 'de':
				this.map.setCenter(new GLatLng(51.15, 10.0), 6);
				break;
			
			case 'at':
				this.map.setCenter(new GLatLng(47.75, 13.75), 7);
				break;
			
			default:
			case 'ch':
				this.map.setCenter(new GLatLng(46.85, 8.3), 8);
				break;

		}
	},
	
	localize: function(address) {
		var geocoder = new GClientGeocoder();
		geocoder.getLatLng(address, function(response) { this.map.setCenter(response, 12); }.bind(this) );
	},
	
	addMarker: function(lat, lng, id, properties) {
		id = id > 0 ? id : 0;
		this.markers.set(id, new MapMarker(lat, lng, properties));
		if(id > 0) {
			this.markers.get(id).addListener('click', function() {
				if(this.last_highlighted_id) {
					this.markers.get(this.last_highlighted_id).visit();
				}
				open_ajaxpage('h-details', sitemap_location + '/details/' + id);
				this.markers.get(id).highlight();
				this.centerMarker(this.markers.get(id));
				this.last_highlighted_id = id;
				
				// scroll if detail view is probably out of viewport
				if(document.viewport.getHeight() < 740 && document.viewport.getScrollOffsets().last() < 170) {
					new Effect.ScrollTo('h-details');
				}
			}.bind(this));
		}
		this.markers.get(id).show();
	},
	
	removeMarker: function(id) {
		this.clusterer.RemoveMarker(this.markers.get(id).marker);
	},
	
	centerMarker: function(mapmarker, force, zoomlevel) {
		var marker_coords = mapmarker.marker.getLatLng();
		if(!this.map.getBounds().contains(marker_coords) || force) {
			var bounds = new GLatLngBounds(marker_coords);
			if(zoomlevel) {
				this.map.setZoom(zoomlevel);
			}
			this.map.panTo(bounds.getCenter());
		}
		if(!zoomlevel) {
			// zoom in if marker is displayed as one in a cluster
			while(mapmarker.marker.inCluster) {
				this.map.zoomIn(mapmarker.marker.getLatLng(), true);
			}
		}
	},
	
	isLoaded: function() {
		return this.loaded;
	}
};

/**
 * Map Marker Wrapper
 */
function MapMarker(lat, lng, properties) {
	this.properties = properties;
	
	// custom icon
	var icon = new GIcon(G_DEFAULT_ICON);
	icon.iconSize = new GSize(21, 28);
	icon.iconAnchor = new GPoint(11, 28);
	icon.shadow = '/res/img/habitation/marker_shadow.png';
	icon.shadowSize = new GSize(36, 28);
	icon.image = (this.properties.type == 'wg') ? '/res/img/habitation/marker_flatshare.png' : '/res/img/habitation/marker_apartment.png';
	
	this.marker = new GMarker(new GLatLng(lat, lng), {icon:icon});
	this.marker.mapmarker = this;
	
	this.highlighted = false;
	this.visited = false;
	
	this.added_to_map = false;
	
	this.get = function(key) {
		return this.properties[key];
	};

	this.show = function() {
		if(!this.addedToMap()) {
			// we have to use following trick to get entries redisplayed after we removed them (filter functionality)
			this.marker.inCluster = false;			
			Map.clusterer.AddMarker(this.marker);
			this.added_to_map = true;
		}
	};
	
	this.hide = function() {
		if(this.addedToMap()) {
			Map.clusterer.RemoveMarker(this.marker);
			this.added_to_map = false;
		}
	};
	
	this.addedToMap = function() {
		return this.added_to_map;				
	};
	
	this.isOnMap = function() {
		return this.marker.onMap;
	};
	
	this.addListener = function(event, handler) {
		GEvent.addListener(this.marker, event, handler);
	};
	
	this.highlight = function() {
		if(this.isOnMap()) {
			this.marker.setImage((this.properties.type == 'wg') ? '/res/img/habitation/marker_flatshare_selected.png' : '/res/img/habitation/marker_apartment_selected.png');
		}
		this.highlighted = true;
	};
	
	this.visit = function() {
		if(this.isOnMap()) {
			this.marker.setImage((this.properties.type == 'wg') ? '/res/img/habitation/marker_flatshare_visited.png' : '/res/img/habitation/marker_apartment_visited.png');
		}
		this.visited = true;			
		this.highlighted = false;
	};
	
	this.isHighlighted = function() {
		return this.highlighted;
	};
	
	this.isVisited = function() {
		return this.visited;
	};
}

/**
 * Filter Handler
 */
var Filter = {
	uri: { base: null, filter: null },
	default_properties: {},
	properties: {},
	update_timeout: null,
	update_uri_timeout: null,
	
	init: function(default_properties) {
		this.default_properties = default_properties;
		this.properties = Object.clone(default_properties);
		
		if(window.location.href.indexOf('filter:') > 0) {
			this.uri.base = window.location.href.split('#').first();
			this.uri.filter = this.unslashifyProperties(window.location.href.split('filter:').last().split(/[#\/\?]/).first());
			this.uri2properties();
		} else {		
			this.uri.base = window.location.href.split('#').first();
			if(window.location.href.indexOf('#') > 0) {
				this.uri.filter = window.location.href.split('#').last();
				this.uri2properties();
			}
		}
		
		// preserve filter when clicking nav2 elements (left navigation) or hide the link to map on incompatible browsers
		if(browserIsCompatibleWithMapView()) {
			$('nav2').childElements().first().childElements().first().childElements().first().observe('click', function(event) {
				Filter.openLocationPreserveFilter('');
				event.stop();
				return false;
			});
		} else {
			$('nav2').childElements().first().childElements().first().hide();
		}
		$('nav2').childElements().first().childElements()[1].childElements().first().observe('click', function(event) {
			Filter.openLocationPreserveFilter('/list', true);
			event.stop();
			return false;
		});
	},
	
	uri2properties: function() {
		this.properties = this.uri.filter.toQueryParams();
	},
	
	updateUri: function() {
		// clear update timeout if there's a pending one
		if(this.update_uri_timeout) {
			clearTimeout(this.update_uri_timeout);
		}
		// set a timeout to prevent unnecessary map updates 
		this.update_uri_timeout = setTimeout(function() {
			window.location.href = this.uri.base + '#' + Object.toQueryString(this.properties);
			this.update_uri_timeout = null;
		}.bind(this), 100);
	},

	getProperties: function() {
		return this.properties;
	},
	
	set: function(key, value) {
		this.properties[key] = value;
	},
	
	get: function(key, get_default) {
		if(!get_default) {
			return this.properties[key];
		} else {
			return this.default_properties[key];
		}
	},
	
	updateData: function() {
		if(Map.isLoaded()) {
			// clear map update timeout if there's a pending one
			if(this.update_timeout) {
				clearTimeout(this.update_timeout);
			}

			// set a timeout to prevent unnecessary map updates
			this.update_timeout = setTimeout(function() {
				// hide marker if it doesn't match filter properties.
				// wg-items will be displayed even if they doesn't match the number of rooms (doesn't make sense otherwise)
				Map.markers.each(function(pair) {
					var item = pair.value;
					if(this.get('type') != this.get('type', true) && item.get('type') != this.get('type')) {
						item.hide();
					} else if(this.get('price_min') != this.get('price_min', true) && item.get('price') < this.get('price_min')) {
						item.hide();
					} else if(this.get('price_max') != this.get('price_max', true) && item.get('price') > this.get('price_max')) {
						item.hide();
					} else if(item.get('type') != 'wg' && this.get('rooms_min') != this.get('rooms_min', true) && item.get('rooms') < this.get('rooms_min')) {
						item.hide();
					} else if(item.get('type') != 'wg' && this.get('rooms_max') != this.get('rooms_max', true) && item.get('rooms') > this.get('rooms_max')) {
						item.hide();
					} else if(this.get('qm_min') != this.get('qm_min', true) && item.get('qm') < this.get('qm_min')) {
						item.hide();
					} else if(this.get('qm_max') != this.get('qm_max', true) && item.get('qm') > this.get('qm_max')) {
						item.hide();
					} else {
						item.show();
					}
				}.bind(this));
				
			}.bind(this), 100);
		}
	},
	
	updateControls: function(type_apartment, type_wg, price_slider, room_slider, qm_slider) {
		// clone the current properties because every setValue changes the values
		var properties = Object.clone(this.properties);
		
		if(properties.type == 'apartment') {
			type_apartment.removeClassName('unchecked');
			type_wg.addClassName('unchecked');
			
			room_slider.setEnabled();
			$('handle3').removeClassName('disabled');
			$('handle4').removeClassName('disabled');
		} else if(properties.type == 'wg') {
			type_apartment.addClassName('unchecked');
			type_wg.removeClassName('unchecked');

			room_slider.setDisabled();
			$('handle3').addClassName('disabled');
			$('handle4').addClassName('disabled');
		} else {
			type_apartment.removeClassName('unchecked');
			type_wg.removeClassName('unchecked');
			
			room_slider.setEnabled();
			$('handle3').removeClassName('disabled');
			$('handle4').removeClassName('disabled');
		}
		
		price_slider.setValue(properties.price_min, 0);
		price_slider.setValue(properties.price_max, 1);
		
		room_slider.setValue(properties.rooms_min, 0);
		room_slider.setValue(properties.rooms_max, 1);
		
		qm_slider.setValue(properties.qm_min, 0);
		qm_slider.setValue(properties.qm_max, 1);
	},
	
	clickCheckbox: function(type_apartment, type_wg, value) {
		var current_type = this.get('type');
		if(current_type == '__' && value == 'apartment') {
			type_apartment.addClassName('unchecked');
			this.set('type', 'wg');
			
			room_slider.setDisabled();
			$('handle3').addClassName('disabled');
			$('handle4').addClassName('disabled');			
		} else if(current_type == '__' && value == 'wg') {
			type_wg.addClassName('unchecked');
			this.set('type', 'apartment');
			
			room_slider.setEnabled();
			$('handle3').removeClassName('disabled');
			$('handle4').removeClassName('disabled');
		} else if(current_type != value) {
			type_apartment.removeClassName('unchecked');
			type_wg.removeClassName('unchecked');
			this.set('type', '__');
			
			room_slider.setEnabled();
			$('handle3').removeClassName('disabled');
			$('handle4').removeClassName('disabled');
		}
		
		this.updateUri();
		this.updateData();
	},
	
	openLocationPreserveFilter: function(uri, slashify) {
		if(slashify) {
			var city = ($('h-city') && $('h-city').getValue() ? '/' + escape($('h-city').getValue()) : '');
			window.location = sitemap_location + uri + city + '/filter:' + this.slashifyProperties();
		} else {
			window.location = sitemap_location + uri + '#' + Object.toQueryString(this.properties);
		}
	},
	
	openRssLocation: function(city) {
		city = city ? '/' + escape(city) : '';
		window.location = language_token + '/rss' + sitemap_location + city + '/filter:' + this.slashifyProperties();
	},	
		
	openAdminpanelPreserveFilter: function(uri) {
			var city = '';
			var choosecity_in_panel = '';
			
			if($('h-city')) {
				city = ($('h-city').getValue() ? '/' + escape($('h-city').getValue()) : '');
			} else {
				choosecity_in_panel = '?choosecity';
			}			
			adminpanel.open(uri + city + '/filter:' + this.slashifyProperties() + choosecity_in_panel);
	},
		
	slashifyProperties: function() {
		var type = this.get('type') ? this.get('type') : this.get('type', true);
		var price = (this.get('price_min') ? this.get('price_min') : this.get('price_min', true)).toString();
		price += '-' + (this.get('price_max') ? this.get('price_max') : this.get('price_max', true)).toString();
		var rooms = (this.get('rooms_min') ? this.get('rooms_min') : this.get('rooms_min', true)).toString();
		rooms += '-' + (this.get('rooms_max') ? this.get('rooms_max') : this.get('rooms_max', true)).toString();
		var qm = (this.get('qm_min') ? this.get('qm_min') : this.get('qm_min', true)).toString();
		qm += '-' + (this.get('qm_max') ? this.get('qm_max') : this.get('qm_max', true)).toString();
		
		return type + ':' + price + ':' + rooms + ':' + qm;
	},
	
	unslashifyProperties: function(filter) {
		var properties = filter.split(':');
		var price 	= properties[1].split('-');
		var rooms 	= properties[2].split('-');
		var qm 		= properties[3].split('-');
		var prop_obj = {
			type: 		properties[0],
			price_min:	price[0],
			price_max: 	price[1],
			rooms_min:	rooms[0],
			rooms_max:	rooms[1],
			qm_min:		qm[0],
			qm_max:		qm[1]
		};
		return Object.toQueryString(prop_obj);
	}
};

/**
 * Add item to history on-the-fly
 */
function addToHistory(id, title, type, zip, city, street, alphaurl) {
	$('h-history').show();
	
	var history_element = $('h-history').childElements().first();
	var item_in_history = false;
	history_element.childElements().each(function(item) {
		if(item.readAttribute('itemid') == id) {
			item_in_history = true;	
			throw $break;
		}
	});
	if(!item_in_history) {
		var row_class = (history_element.childElements().length > 1 && history_element.childElements()[1].hasClassName('list_row_0')) ? 'list_row_1' : 'list_row_0';
		var icon = type == 'apartment' ? 'apartment.gif' : 'flatshare.gif';
		new Insertion.After(history_element.childElements().first(), '<tr itemid="' + id + '" class="' + row_class + '"><td style="width: 16px"><img src="/res/img/habitation/' + icon + '" alt="icon" /></td><td><a href="javascript:removeFromHistory(' + id + ');" style="float: right; padding-left: 5px">x</a><a href="' + sitemap_location + '/details/' + id + '/' + alphaurl + '" onclick="javascript:showDetails(' + id + ', \'' + alphaurl + '\'); return false;">' + title + '</a><br /><small>' + street + ', ' + (zip != '0' ? zip + ' ' : '') + city + '</small></td></tr>');
	}
}

/**
 * Remove item from history
 */
function removeFromHistory(id) {
	var history_element = $('h-history').childElements().first();
	history_element.childElements().each(function(item) {
		if(item.readAttribute('itemid') == id) {
			item.remove();
			throw $break;
		}
	});
	for(i=1; i < history_element.childElements().length; i++) {
		var row_class = (i % 2) ? 'list_row_0' : 'list_row_1';
		history_element.childElements()[i].className = row_class;
	}
	new Ajax.Request(language_token + '/ajax' + sitemap_location + '/removefromhistory/' + id);
	if(history_element.childElements().length < 2) {
		$('h-history').hide();
	}
}

/**
 * Open offer by id
 *
 * Check if there is a marker on the map and use the click handler of it
 * Otherwise open the static detail view
 */
function showDetails(id, alphaurl) {
	if(Map.markers.get(id)) {
		if(!Map.markers.get(id).addedToMap()) {
			Map.markers.get(id).show();
		}
		GEvent.trigger(Map.markers.get(id).marker, 'click');
	} else {
		Filter.openLocationPreserveFilter('/details/' + id + '/' + alphaurl);
	}
}


/**
 * Legend Control for GMap
 *
 * extends GControl
 */
function LegendControl(legend_content) {
	this.legend_content = legend_content;
}
LegendControl.prototype = new GControl();
LegendControl.prototype.initialize = function(map) {
	var container = document.createElement("span");
	container.style.border = '1px outset #888';
	container.style.backgroundColor = 'white';
	container.style.padding = '2px 4px';
	container.style.whiteSpace = 'nowrap';
	container.style.color = 'black';
	container.style.width = 'auto';
	container.update(this.legend_content);
	map.getContainer().appendChild(container);
	return container;
}
LegendControl.prototype.getDefaultPosition = function() {
  return new GControlPosition(G_ANCHOR_BOTTOM_RIGHT, new GSize(5, 20));
}


