/* *** MOOTOOLS *** */

// Footer Positionierung //
function setFooter(windowHeight, windowWidth) {
	var footerElement = $('footer');

	if (windowWidth > 0) {
		if (windowWidth <= 996) {
			footerElement.setStyles({
					right: 15,
					bottom: 15
			});
		}
		else {
			footerElement.setStyles({
					right: 0,
					bottom: 0
			});
		}
	}
}
/*****************************************************/

// Startseiten-Teaser Positionierung
function setTeaser(windowHeight, windowWidth) {
	var sT = $('startTeaser');
	var sBG = $('tHead08');
	var tsPos = (windowHeight-380).round();

	if (windowHeight >= 620) {
		sT.setStyles({
				top: tsPos
		});
		sBG.setStyles({
				marginTop: tsPos + 45
		});
	}
	else {
		sT.setStyles({
				top: 273
		});
		sBG.setStyles({
				marginTop: 318
		});
	}
}
/*****************************************************/

// Fenstergröße Abfragen
//function getSize() {
//	var myWidth = 0, myHeight = 0;
//
//	if( typeof( window.innerWidth ) == 'number' ) {
//		//Non-IE
//		myWidth = window.innerWidth;
//		myHeight = window.innerHeight;
//	} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
//		//IE 6+ in 'standards compliant mode'
//		myWidth = document.documentElement.clientWidth;
//		myHeight = document.documentElement.clientHeight;
//	} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
//		//IE 4 compatible
//		myWidth = document.body.clientWidth;
//		myHeight = document.body.clientHeight;
//	}
//	return [ myWidth, myHeight ];
//}

/*****************************************************/

// Resize-Funktion
window.addEvent('resize', function(){
	var wSize = getSize();
	var wWidth = wSize[0];
	var wHeight = wSize[1];

//	setFooter(wHeight, wWidth);
//	setTeaser(wHeight, wWidth);
});
/*****************************************************/


window.addEvent('domready', function(){
	var wSize = getSize();
	var wWidth = wSize[0];
	var wHeight = wSize[1];

//	setFooter(wHeight, wWidth);
//	setTeaser(wHeight, wWidth);

	// Reservierungsmenu initialisieren
	//init();

	// Start Sliding Menus
	Site.start();

	// Tooltips
	var Tips2 = new Tips($$('.Tips2'), {
		initialize:function(){
			this.fx = new Fx.Style(this.toolTip, 'opacity', {duration: 500, wait: false}).set(0);
		},
		onShow: function(toolTip) {
			this.fx.start(1);
		},
		onHide: function(toolTip) {
			this.fx.start(0);
		}
	});
});


// Sliding Menus
var Site = {

	start: function(){
		if($('accordion')) Site.accordion();
	},

	accordion: function(){
		var list = $$('#accordion li div.collapse');
		var headings = $$('#accordion li h3');
		var collapsibles = new Array();
		var spans = new Array();

		headings.each( function(heading, i) {

			var collapsible = new Fx.Slide(list[i]);

			collapsibles[i] = collapsible;
			spans[i] = $E('span', heading);

			heading.onclick = function(){
				var span = $E('span', heading);

				if(span){
					var newHTML = span.innerHTML == '+' ? '-' : '+';
					span.setHTML(newHTML);
				}

				for(var j = 0; j < collapsibles.length; j++){
					if(j!=i) {
						collapsibles[j].slideOut();
						if(spans[j]) spans[j].setHTML('+');
					}
				}

				collapsible.toggle();

				return false;
			}

			collapsible.hide();

		});
	}
};

// Reservierungsmenu
var isExtended = 0;
var height = 200;
var width = 300;
var slideDuration = 1000;
var opacityDuration = 1500;

function extendContract(){
	if(isExtended == 0){
		sideBarSlide(0, height, 0, width);
		sideBarOpacity(0, 1);
		isExtended = 1;
		// Bildwechsel beim Ausfahren
		$('sideBarTab').childNodes[0].src = $('sideBarTab').childNodes[0].src.replace(/(\.[^.]+)$/, '-active$1');
	}
	else{
		sideBarSlide(height, 0, width, 0);
		sideBarOpacity(1, 0);
		isExtended = 0;
		// Bildwechsel beim Einfahren
		$('sideBarTab').childNodes[0].src = $('sideBarTab').childNodes[0].src.replace(/-active(\.[^.]+)$/, '$1');
	}
}

function sideBarSlide(fromHeight, toHeight, fromWidth, toWidth){
		var myEffects = new Fx.Styles('sideBar', {duration: slideDuration, transition: Fx.Transitions.linear});
		myEffects.custom({
			 'height': [fromHeight, toHeight],
			 'width': [fromWidth, toWidth]
		});
		var myEffects = new Fx.Styles('sideBarContents', {duration: slideDuration, transition: Fx.Transitions.linear});
		myEffects.custom({
			 'height': [fromHeight, toHeight],
			 'width': [fromWidth, toWidth]
		});
}

function sideBarOpacity(from, to){
		var myEffects = new Fx.Styles('sideBarContents', {duration: opacityDuration, transition: Fx.Transitions.linear});
		myEffects.custom({
			 'opacity': [from, to]
		});
}

function init(){
	if ($('sideBarTab') != null ){
		$('sideBarTab').addEvent('click', function(){extendContract()});
		return false;
	}
}
/*****************************************************/

function calc_departure(){

	var sel_arrival = document.getElementById("arrival");
	var selector = document.getElementById("nights");


	var naechte = Number(document.getElementById("nights").value);

	if(naechte > 0){

		var arrival = document.getElementById("arrival").value;
		if((check_string(arrival) != false)&&(check_int(naechte) != false)){


			var arrival_arr = arrival.split(".");
			var jahr = Number(arrival_arr[2]);
			var monat = Number(arrival_arr[1]);
			var tag = Number(arrival_arr[0]);


			var abreise = new Date();
			abreise.setFullYear(jahr,monat-1,tag + naechte);
			jahr = abreise.getFullYear();
			jahr = jahr.toString();
			monat = abreise.getMonth()+1;
			if (monat <= 9){
				monat = "0"+monat.toString();
			}else{
				monat = monat.toString();
			}
			tag = abreise.getDate();
			if (tag <= 9){
				tag = "0"+tag.toString();
			}else{
				tag = tag.toString();
			}
			abreise = tag+"."+monat+"."+jahr;

			if(document.getElementById("departure")){
				//var start = 0;
				//var laenge = document.getElementById("departure").firstChild.nodeValue.length;
				//document.getElementById("departure").firstChild.replaceData(start, laenge, abreise);
				document.getElementById("departure2").innerHTML = abreise;
				document.getElementById("departure").value = abreise;
			}

		}
	}
// --- update_34 ---
}

function check_string(feld){

	feld = feld.toString();
	if(feld.length > 0)
		return true;
	else
		return false;
}

function check_int(feld){
	feld = Number(feld);
	if(feld > 0)
		return true;
	else
		return false;
}

function checkNights(){
	var naechte = Number(document.getElementById("nights").value);
	if(naechte > 0){
		return true;
	}else{
		//document.getElementById('nights').style.cssText = "background : red;";
		return false;
	}
}

jQuery(document).ready(function($){
	$("#reserve").fancybox({
		'autoDimensions'	: false,
		'width'			: 350,
		'height'			: 420,
		'autoScale'			: false,
		'transitionIn'		: 'none',
		'transitionOut'		: 'none',
		'type'				: 'iframe'
	});

	$('#slider').anythingSlider({
		startStopped    : false, // If autoPlay is on, this can force it to start stopped
		width           : 566,   // Override the default CSS width
		autoPlayLocked  : true,  // If true, user changing slides will not stop the slideshow
		resumeDelay     : 5000, // Resume slideshow after user interaction, only if autoplayLocked is true (in milliseconds).
		onSlideComplete : function(slider){
			// alert('Welcome to Slide #' + slider.currentPage);
		}
	});

	var landingTeaserMaxHeight = 0;
	var landingTeaserCurrentRowOffsetTop = null;
	$('.landingTeaser').each(function() {
		if (landingTeaserCurrentRowOffsetTop != $(this).offset().top) {
			$('.equalHeightsProcessing').height(landingTeaserMaxHeight);
			$('.equalHeightsProcessing').removeClass('equalHeightsProcessing');
			landingTeaserCurrentRowOffsetTop = $(this).offset().top;
			landingTeaserMaxHeight = 0;
		}

		$(this).addClass('equalHeightsProcessing');
		if ($(this).height() > landingTeaserMaxHeight) {
			landingTeaserMaxHeight = $(this).height();
		}
	});
	$('.equalHeightsProcessing').height(landingTeaserMaxHeight);
	$('.equalHeightsProcessing').removeClass('equalHeightsProcessing');
});

//checkin & checkout element ID
var checkinElementId = 'dateBook';
var checkoutElementId = 'date2';
var nnightsId = 'nnights';
var nchildsId = 'nchilds';
var nadultsId = 'nadults';
var checkoutDateChanged = false;
var checkoutDate = new Date();
checkoutDate.setHours(0);
checkoutDate.setMinutes(0);
checkoutDate.setSeconds(0);
var checkinDate = checkoutDate;

function calc_departureNew(){

	var sel_arrival = document.getElementById("dateBook");
	var selector = document.getElementById("nnights");

	var naechte = Number(document.getElementById("nnights").value);
	if(naechte > 0){

		var arrival = document.getElementById("dateBook").value;
		if((check_string(arrival) != false)&&(check_int(naechte) != false)){

			var arrival_arr = arrival.split("-");
			var year = Number(arrival_arr[0]);
			var month = Number(arrival_arr[1]);
			var day = Number(arrival_arr[2]);

			var newMonth = month-1;
			var newDay = day+naechte;

			var abreise = new Date(year,newMonth,newDay);

			jahr = abreise.getFullYear();
			jahr = jahr.toString();
			monat = abreise.getMonth()+1;

			if (monat <= 9){
				monat = "0"+monat.toString();
			}else{
				monat = monat.toString();
			}
			tag = abreise.getDate();


			if (tag <= 9){
				tag = "0"+tag.toString();
			}else{
				tag = tag.toString();
			}
			abreise = jahr+"-"+monat+"-"+tag;

			if(document.getElementById("date2")){
				document.getElementById("date2").value = abreise;
			}
		}
	}
}

function ds_format_date(d, m, y) {
	// 2 digits month.
	m2 = '00' + m;
	m2 = m2.substr(m2.length - 2);
	// 2 digits day.
	d2 = '00' + d;
	d2 = d2.substr(d2.length - 2);
	// YYYY-MM-DD
	return y + '-' + m2 + '-' + d2;
}

function setmonth() {
	var checkinDate = new Date()
	var month = checkinDate.getMonth() + 1
	if (month <10)
		month = "0" + month
	var day = checkinDate.getDate()
	var year = checkinDate.getFullYear()
	document.bookerform.dateBook.value=ds_format_date(day, month, year);
	year = checkoutDate.getFullYear();
	month = checkoutDate.getMonth() + 1;
	day = checkoutDate.getDate();
	 document.bookerform.date2.value = ds_format_date(day, month, year);
}

function sendform(){
	calc_departureNew();

	document.bookerform.calArrivalDateField.value=document.bookerform.dateBook.value
	document.bookerform.calDepartureDateField.value=document.bookerform.date2.value
	//alert('TEST und '+document.bookerform.calDepartureDateField.value);
	document.bookerform.submit();
}


