function MapFacade(element_id) {
	this.element = element_id;
	this.options = {
		zoom: 13,
		center: new google.maps.LatLng(0, 0),
		mapTypeId: google.maps.MapTypeId.ROADMAP
	};
	this.layer = [];
	this.map = null;
	this.is_loaded = false;
	this._onready_functions = [];
	this.load_listener = false;
	this.zoom;
	
	// Overlay
	this.overlay = $('<div id="mapoverlay"></div>');
	this.overlay.click(function() {
		$(this).fadeOut();
	});
	this.loadInfo = function(url) {
		var that = this;
		that.overlay.load(url, function() {
			that.overlay.fadeIn();
		});
	};
	
	// Grenzen
	this.setBounds = function(n,e,s,w) {
		this.bounds = new google.maps.LatLngBounds(
			new google.maps.LatLng(s, w),
			new google.maps.LatLng(n, e)
		);
	};
	
	// Layer hinzufügen
	this.addLayer = function(layer) {
		this.layer.push(layer);
	};
	
	// laden
	this.load = function() {
		this.map = new google.maps.Map(document.getElementById(this.element), this.options);
		this.map.fitBounds(this.bounds);
		this.load_listener = google.maps.event.addListener(this.map, 'bounds_changed', this._isready);
		
		this.overlay.appendTo(document.getElementById(this.element));
		
		this.ready(function() {
			for(var i=0;i< this.layer.length;i++) {
				this.layer[i].mapReady.apply(this.layer[i], []);	
			}
			
			this.zoom_changed(function() {
				for(var i=0;i< this.layer.length;i++) {
					this.layer[i].zoom_changed.apply(this.layer[i], []);	
				}
			});
			
			this.drag_end(function() {
				for(var i=0;i< this.layer.length;i++) {
					this.layer[i].drag_end.apply(this.layer[i], []);	
				}
			});
		});
	};
	
	// Grenzeenexport	
	this.getActualBounds = function() {
		var bounds = this.map.getBounds();
		
		var ne = bounds.getNorthEast();
		var sw = bounds.getSouthWest();
		
		var n = ne.lat();
		var e = ne.lng();
		var s = sw.lat();
		var w = sw.lng();
		
		return [n,e,s,w].join(';');	
	};
	
	this.getZoom = function() {
		return this.map.getZoom();
	};
	
	this.ready = function(tue) {
		if(this.is_loaded) {
			tue.apply(this, []);	
		} else {
			this._onready_functions.push(tue);
		}
	};
	
	var that = this;
	this._isready = function(event) {
		google.maps.event.removeListener(that.load_listener);
		that.is_loaded = true;
		
		for(var i=0; i < that._onready_functions.length;i++) {
			that._onready_functions[i].apply(that, []);
		}
	};
	
	this.drag_end = function(func) {
		var that = this;
		google.maps.event.addListener(this.map, 'dragend', function() {
			func.apply(that, []);
		});
	};
	
	this.zoom_changed = function(func) {
		var that = this;
		google.maps.event.addListener(this.map, 'zoom_changed', function() {
			func.apply(that, []);
		});
	};
	
	return this;
}

function change_eyecatcher() {
    var $active = $('#eyecatcher_slideshow IMG.active');

    if ( $active.length == 0 ) $active = $('#eyecatcher_slideshow IMG:last');

    // use this to pull the images in the order they appear in the markup
    var $next =  $active.next().length ? $active.next()
        : $('#eyecatcher_slideshow IMG:first');

    $active.addClass('last-active');

    $next.css({opacity: 0.0})
        .addClass('active')
        .animate({opacity: 1.0}, 3000, function() {
            $active.removeClass('active last-active');
        });
}

$(function() {
	// Suchformular initialisieren
    Zo.loadClass('Suchformular');
	var formular = new Suchformular('.suche');
	
	$('.wettertabs').toggle(function() {
		document.getElementById('weatherTabTomorrow').className = 'weather_tab_current';
		document.getElementById('weather_tomorrow').style.display = 'block';
		document.getElementById('data_tomorrow').style.display = 'block';
		
		document.getElementById('weatherTabToday').className = '';
		document.getElementById('weather_today').style.display = 'none';
		document.getElementById('data_today').style.display = 'none';		
	}, function() {
		document.getElementById('weatherTabToday').className = 'weather_tab_current';
		document.getElementById('weather_today').style.display = 'block';
		document.getElementById('data_today').style.display = 'block';
		
		document.getElementById('weatherTabTomorrow').className = '';
		document.getElementById('weather_tomorrow').style.display = 'none';
		document.getElementById('data_tomorrow').style.display = 'none';
	});
	
	$('input[name="nr"]', 'form[action="/submit/ZumObjekt"]').focus(function() {
		$(this).val('');
		$(this).unbind('focus');
	});
	
	
	$('input[name="von_hotel"], input[name="bis_hotel"]').datepicker({
		showOn: 'both',
		buttonImage: 'http://img.travanto.de/img/icon/calendar2.png',
		buttonImageOnly: true
	}); 
	
	//Datepicker Autovermietung
	$('input[name="mietwagen_von"], input[name="mietwagen_bis"]').datepicker({
		showOn: 'both',
		buttonImage: 'http://img.travanto.de/img/icon/calendar2.png',
		buttonImageOnly: true
	});
	
	//Datepicker Fahrradverleih
	$('input[name="fahrrad_von"], input[name="fahrrad_bis"]').datepicker({
		showOn: 'both',
		buttonImage: 'http://img.travanto.de/img/icon/calendar2.png',
		buttonImageOnly: true
	});
	
	// GoogleMaps einbinden, wenn DIV dafuer vorhanden
	if($('#map').length == 1) {
		
		window.init_map = function() {
			Zo.loadScript('/js/layers/objekte.js');
			
			var map = new MapFacade('map');
			map.setBounds(
				$('#map').attr('data-n'),
				$('#map').attr('data-e'),
				$('#map').attr('data-s'),
				$('#map').attr('data-w')
			);
			
			var objekte_layer = new TravantoObjekteLayer(map);
			map.addLayer(objekte_layer);
			map.load();
			
			map.ready(function() {
				$('input, select', '#suchformular').change(function() {
					objekte_layer.clearMap();
					objekte_layer.update();
				});
			});
		}
		$('head').append('<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false&callback=init_map"></script>');
	}
	
	if ($('#eyecatcher_slideshow img').length > 1) {
		setInterval( "change_eyecatcher()", 10000 );
	}
});
