var markersArray = [];

$(document).ready(function(){

	$("#hotel_tabs").tabs({selected:0});

	slideshow();
	
	$("#reservierung").validate({
		errorLabelContainer: $("#fehlermeldungen"),
		rules: {
			kunde_vorname: "required",
			kunde_name: "required",
			kunde_strasse: "required",
			kunde_plz: "required",
			kunde_ort: "required",
			kunde_telefon: "required",
			kunde_email: {
				required: true,
				email: true
			}
		},
		messages: {
			kunde_vorname: "Bitte geben Sie Ihren Vornamen an. ",
			kunde_name: "Bitte geben Sie Ihren Nachnamen an. ",
			kunde_strasse: "Bitte geben Sie Ihre komplette Adresse an. ",
			kunde_plz: "Bitte geben Sie Ihre komplette Adresse an. ",
			kunde_ort: "Bitte geben Sie Ihre komplette Adresse an. ",
			kunde_telefon: "Bitte geben Sie Ihre Telefonnummer an. ",
			kunde_email: "Bitte geben Sie Ihre E-Mail-Adresse an. "
		}
	});

	$(".button").addClass("ui-state-default");
	$(".buttonSmall").addClass("ui-state-default");
	
	$("#navi li").addClass("ui-state-default");
	$("#navi li").hover(
		function(){
			$(this).addClass("ui-state-hover");
			$("> ul", this).show();
		},
		function(){
			$(this).removeClass("ui-state-hover");
			$("> ul", this).hide();
		}
	);
	
	$("#navi li ul li").removeClass("ui-state-default");
	
	
	$(".aktiv").addClass("ui-state-hover");
	
	aufmacher();
	
	$("#infofenster").dialog({
		autoOpen:false
	});
	
	$("#weiterempfehlen").dialog({
		autoOpen:false
	});
	
	$("#feedback").dialog({
		autoOpen:false,
		modal:true
	});
	
	$("#gaestebriefe").dialog({width:700,height:550,autoOpen:false,modal:true,resizable:false});
	
	
	$("#kategorien").accordion({
		collapsible:true,
		active:false,
		header:"h3",
		clearStyle: true
	});
	
	$(".kategorienblock").accordion({
		collapsible:true,
		active:false,
		header:"h4",
		clearStyle: true
	});
	
	$(".recent .recentbox:nth-child(even)").addClass("even");
	$(".recent .recentbox:nth-child(odd)").addClass("odd");
	
	
	$(".recentbox").hover(
		function(){
			$(".removeFromRecent",this).toggle();
		},
		function(){
			$(".removeFromRecent",this).toggle();
		}
	);
	
	$.datepicker.setDefaults({
		dateFormat:'dd.mm.yy',
		dayNames:['Sonntag','Montag','Dienstag','Mittwoch','Donnerstag','Freitag','Samstag'],
		dayNamesMin:['So','Mo','Di','Mi','Do','Fr','Sa'],
		firstDay:1,
		monthNames:['Januar','Februar','März','April','Mai','Juni','Juli','August','September','Oktober','November','Dezember'],
		monthNamesShort:['Jan','Feb','Mar','Apr','Mai','Jun','Jul','Aug','Sep','Okt','Nov','Dez']
	});

	var dates = $( "#start_datum, #ende_datum" ).datepicker({
		defaultDate: "+1w",
		changeMonth: true,
		numberOfMonths: 2,
		onSelect: function( selectedDate ) {
			var option = this.id == "start_datum" ? "minDate" : "maxDate",
				instance = $( this ).data( "datepicker" ),
				date = $.datepicker.parseDate(
					instance.settings.dateFormat ||
					$.datepicker._defaults.dateFormat,
					selectedDate, instance.settings );
			dates.not( this ).datepicker( "option", option, date );
			
			if($("#start_datum").val()=='' || $("#ende_datum").val()==''){
				$("#zimmer").attr("disabled","disabled");
			}
			else{
				$("#zimmer").removeAttr("disabled");
				$("#zimmer option:first").html('W&auml;hlen Sie hier das Zimmer:');
				callZimmerangebot($("#id_hotel").val(),$("#start_datum").val(),$("#ende_datum").val());
			}
			
		}
	});

	$(".artselector").change(function(){
		loadHotels();
	});
	
});


function callZimmerangebot(id_hotel,start,ende){
	$.get("_ajax/callZimmerangebotAdditional.php",{id_hotel:id_hotel,start:start,ende:ende},function(data){
		$("#zimmerauswahlen").html(data);
		$("#zimmerauswahl").show();
	});
}

function zimmerauswahlObserve(){
	$(".zimmerauswahl").change(function(event){
		if($(".zimmerauswahl:last").val()!=''){
			var newOne = $(".zimmerauswahl:first").clone();
			$(this).parent().append(newOne);
			$(".zimmerauswahl:last option:first").html('Weiteres Zimmer:');
			zimmerauswahlObserve();
		}
	});
}

function zimmerauswahlAdd(id_hotel,start,ende){
	//$.get("_ajax/callZimmerangebotAdditional.php",{id_hotel:$("#id_hotel").val(),start:$("#start_datum").val(),ende:$("#ende_datum").val()},function(data){
	//	$("#zimmerauswahlen").append(data);
	//});
	$("#zimmerauswahl").show();
	$("#zimmerAdd").hide();
}

function createMarker(point,html) {
	var marker = new google.maps.Marker({
		map: map,
		position: point,
		draggable:false
	});
	markersArray.push(marker);
	bindInfoWindow(marker,map,infoWindow,html);
}

function bindInfoWindow(marker, map, infoWindow, html) {
  google.maps.event.addListener(marker, 'click', function() {
  	refreshsperre = 1;
    infoWindow.setContent(html);
    infoWindow.open(map, marker);
  });
  google.maps.event.addListener(infoWindow, 'closeclick', function() {
  	refreshsperre = 0;
  });
}



function loadMap(minX,maxX,minY,maxY) {
	if(minX){
		bounds = new google.maps.LatLngBounds(new google.maps.LatLng(parseFloat(minY),parseFloat(minX)),new google.maps.LatLng(parseFloat(maxY),parseFloat(maxX)));
			var zoomtotake = 11;
		center = bounds.getCenter();
	}
	else{
		var zoomtotake = 6;
		center = new google.maps.LatLng(38.0, 30.0);
	}
	
	var myOptions = {
		zoom:zoomtotake,
		center:center,
		mapTypeId: google.maps.MapTypeId.ROADMAP
	};


	map = new google.maps.Map(document.getElementById("smallmap"), myOptions);
	google.maps.event.trigger(map, 'resize');
	$("#smallmap").dialog({width:700,height:550,autoOpen:false,modal:true});
	map.setCenter(center);
	
	if(minX){
		//map.fitBounds(map.getBounds(bounds));
	}
	
	
	infoWindow = new google.maps.InfoWindow;

}

function showLocation(lat,lng) {
	center = new google.maps.LatLng(lat, lng);
	google.maps.event.trigger(map, 'resize');
	map.setCenter(center);
	map.setZoom(11);
	var marker = new google.maps.Marker({
		map: map,
		position: center,
		draggable:false
	});
	
	
}

function setMarker(lat,lng,html) {
	var point = new google.maps.LatLng(lat, lng);
	var marker = createMarker(point,html);
	//map.addOverlay(marker);
}

function showGaestebriefe(){
	$("#gaestebriefe").dialog('open');
}

function showKarte(){
	$("#smallmap").dialog('open');
	//map.checkResize();
	google.maps.event.trigger(map, 'resize');
	map.setCenter(center);
	map.fitBounds(map.getBounds(bounds));
}

function showBigKarte(){
	$("#smallmap").dialog('open');
}

function slideshow(){
	$("#albumbilder").cycle({
		fx:     'fade', 
		speed:  'slow', 
		timeout: 4000, 
		next:   '#next', 
		prev:   '#previous'
	});
	
	$("#albumpause").click(function(){
		$("#albumbilder").cycle('pause');
		$("#albumresume").show();
		$("#albumpause").hide();
	});
	$("#albumresume").click(function(){
		$("#albumbilder").cycle('resume');
		$("#albumresume").hide();
		$("#albumpause").show();
	});
}

function aufmacher(){
	$("#aufmacherbilder").cycle({
		fx:     'fade', 
		speed:  4000, 
		timeout: 4000 
	});
	
}

function infoFenster(id_angebot){
	$.get("info.php",{id_angebot:id_angebot},function(data){
		$("#infofenster").html(data);
		var title = $("#infofenster h1").html();
		$("#infofenster").dialog('option','title',title);
		$("#infofenster").dialog('option','width',420);
		$("#infofenster").dialog('open');
	});
}
	
function infoFenster2(id_angebot){
	$.get("info2.php",{id_angebot:id_angebot},function(data){
		$("#infofenster").html(data);
		var title = $("#infofenster h1").html();
		$("#infofenster").dialog('option','title',title);
		$("#infofenster").dialog('option','width',420);
		$("#infofenster").dialog('open');
	});
}
	
function infoFensterZusatzangebot(id_angebot){
	$.get("infoZusatzangebot.php",{id_angebot:id_angebot},function(data){
		$("#infofenster").html(data);
		var title = $("#infofenster h1").html();
		$("#infofenster").dialog('option','title',title);
		$("#infofenster").dialog('option','width',400);
		$("#infofenster").dialog('open');
	});
}

function weiterempfehlenBox(){
	$.get("weiterempfehlen.php",function(data){
		$("#weiterempfehlen").html(data);
		var title = $("#weiterempfehlen h3").html();
		$("#weiterempfehlen").dialog('option','title',title);
		$("#weiterempfehlen").dialog('option','width',400);
		$("#weiterempfehlen").dialog('open');
	});
}

function weiterempfehlenBoxHotel(){
	$.get("weiterempfehlenHotel.php",function(data){
		$("#weiterempfehlen").html(data);
		var title = $("#weiterempfehlen h3").html();
		$("#weiterempfehlen").dialog('option','title',title);
		$("#weiterempfehlen").dialog('option','width',400);
		$("#weiterempfehlen").dialog('open');
	});
}

function weiterempfehlen(){
	var seite = document.location.href;
	var absender = document.Empfehlung.absender.value;
	var empfaenger = document.Empfehlung.empfaenger.value;
	var nachricht = document.Empfehlung.nachricht.value;
	$.post("_process/weiterempfehlen.php5",{seite:seite,absender:absender,empfaenger:empfaenger,nachricht:nachricht},function(data){
		$("#weiterempfehlen").html(data);
	});
}

function feedbackBox(id_hotel){
	$.get("feedback.php",{id_hotel:id_hotel},function(data){
		$("#feedback").html(data);
		var title = $("#feedback h1").html();
		$("#feedback").dialog('option','title',title);
		$("#feedback").dialog('option','width',600);
		$("#feedback").dialog('open');
	});
}

function feedback(){
	$("label").removeClass("error");
	var error = 0;
	if($("#nachricht").val()==''){
		$("label[for='nachricht']").addClass("error");
		var error = 1;
	}
	if($("#email").val()==''){
		$("label[for='email']").addClass("error");
		var error = 1;
	}
	
	if($("#telefon").val()==''){
		$("label[for='telefon']").addClass("error");
		var error = 1;
	}
	if(error==0){
		var daten = $("#feedbackFormular").serialize();
		$.post("feedbackResponse.php",daten,function(data){
			$("#feedback").html(data);
		});
	}
}

function removeFromRecent(id_hotel){
	$.get("/_ajax/removeFromRecent.php",{id_hotel:id_hotel},function(data){
		$("#recent" + id_hotel).remove();
		$(".recent .recentbox").removeClass("even");
		$(".recent .recentbox").removeClass("odd");
		$(".recent .recentbox:nth-child(even)").addClass("even");
		$(".recent .recentbox:nth-child(odd)").addClass("odd");
	});
}

function checkAdditionalZimmer(){
	alert($(this).val());
}

function zimmerAuswaehlen(id_hotel,id_angebot){
	$.get("_ajax/zimmerauswahl.php",{id_hotel:id_hotel,id_angebot:id_angebot,start:$("#start_datum").val(),ende:$("#ende_datum").val()},function(data){
		$("#zusammenstellung").html(data);
		$("#zimmerauswahl").hide();
		$("#zimmerAdd").show();
	});
}

function zimmerRemove(id_station){
	$.get("_ajax/zimmerauswahlEntfernen.php",{id_station:id_station},function(data){
		$("#zusammenstellung").html(data);
	});
}

function loadComment(tabelle,id){
	$.get("/intern/comments/" + tabelle + ".php",{id_hotel:id},function(data){
		$("#kommentar_intern").html(data);
	});
}

function saveComment(tabelle,id){
	var daten = $("#kommentarformular").serialize();
	$.post("/intern/comments/" + tabelle + "Save.php",daten,function(data){
		loadComment(tabelle,id);
	});
}

var currentHotelart;

function loadHotels(id_art){
	$("#gesamtliste").fadeOut('fast');
	$("#alleHotels").fadeOut('fast');
	if(!id_art || id_art==currentHotelart){
		var id_art = '';
		currentHotelart = '';
	}
	$.get("/_ajax/hotels.php",{hotelart:id_art},function(data){
		$("#gesamtliste").html(data);
		$("#gesamtliste").fadeIn('fast');
		if(id_art==''){
			$(".hotelarten").fadeIn('fast');
			$("#alleHotels").fadeIn('fast');
			$("#headline1").show();
			$("#headline2").hide();
			$("#showAll").hide();
		}
		else{
			
			$(".hotelarten").fadeOut('fast');
			$("#headline1").fadeOut('fast');
			
			
			$("#hotelart" + id_art).show('fast');
			$("#headline2").html($("#hotelart" + id_art + " h2").html());

			
			$("#headline2").fadeIn('fast');
			$("#showAll").show();
			currentHotelart = id_art;
		}
	});
}

