var booking_calendar_arrival = '';
var booking_calendar_departure = '';

var calendar_errors = {
	'da': {},
	'en': {},
	'de': {}
};

$(document).ready(function(){
	new booking();
	new slideshow(7);
	
	booking_calendar_arrival = new calendar('arrival');
	booking_calendar_departure = new calendar('departure');
	
	// the frontpage booking button
	$(".booking-button").click(function(){
		var arrival = booking_calendar_arrival.getSelectedDate();
		var departure = booking_calendar_departure.getSelectedDate();
		if (arrival && departure) {
			// check to see if the selected dates are equal to or greater then today
			var today = new Date();
			
			if ( (arrival < today) && (departure < today) ) {
				alert("Ankomst og afgang m� ikke v�re f�r dags dato"); return false;
			}
			
			// check if the departure is equal to or greater then arrival
			if ( !(departure >= arrival) ) {
				alert("Afgangs dato'en skal v�rre senere end ankomst"); return false;
			}
			
			// go to booking
			arrival_url = get_date(arrival.getDate()) + '-' + get_date(arrival.getMonth()+1) + '-' + arrival.getFullYear();
			departure_url = get_date(departure.getDate()) + '-' + get_date(departure.getMonth()+1) + '-' + departure.getFullYear();
			
			var url = 'http://danhhern.onlinebooking.dk/booking/?datefrom=' + arrival_url + '&dateto=' + departure_url + '&profileid=0&adultcount=0&childcount=0&flexdays=0';
			var win = window.open(url);
			setTimeout(function(){ win.location.reload() },250);			
		} else {
			if ( arrival && !departure ) {
				alert('Du skal v�lge en afgangs dato'); return false;
			} else if ( departure && !arrival ) {
				alert('Du skal v�lge en ankomst dato'); return false;
			} else {
				alert('Du skal v�lge en dato for ankomst og afgang'); return false;
			}
		}
	});
	
	// load the lightbox
	$("[rel*='lightbox']").lightbox();
	
	// disable ie seleciton
	if ( $.browser.msie && $(document.body).hasClass('frontpage') ) {
		document.onselectstart = new Function ("return false");
	}
});

var booking = function() {
	
	var year = new Date().getYear();
	
	var arrival = {
		day: $("#room [name='arrival_day']"),
		month: $("#room [name='arrival_month']"),
		year: $("#room [name='arrival_year']")
	}
	
	var departure = {
		day: $("#room [name='departure_day']"),
		month: $("#room [name='departure_month']"),
		year: $("#room [name='departure_year']")
	}
	
	// set the current day
	this.setToday = function() {
		var today = new Date();
		arrival.day[0].selectedIndex = (today.getDate()-1);
		arrival.month[0].selectedIndex = (today.getMonth());
		departure.day[0].selectedIndex = (today.getDate()-1);
		departure.month[0].selectedIndex = (today.getMonth());
	}
	
	// set the current amount of days
	this.setAvailableDays = function(type) {
		var month_days = [31,28,31,30,31,30,31,31,30,31,30,31];
		var leep_years = [2012,2016,2020,2024,2028];
		var isLeap = new Date(year,1,29).getDate() == 29;
		
		
		
		var arrival_month = arrival.month.val()-1;
		var arrival_days = month_days[arrival_month];
		var departure_month = departure.month.val()-1;
		var departure_days = month_days[departure_month];
		
		if ( isLeap && arrival_month == 1 ) {			
			arrival_days += 1;
		}
		
		
		switch(type) {
			case 'arrival':
				arrival.day.html('');		
				for ( i = 1; i <= arrival_days; i++) {		
					var opt = $('<option value="' + i + '">'+ get_date(''+i) +'</option>');
					arrival.day.append(opt);
				}
				break;
			case 'departure':
				departure.day.html('');		
				for ( i = 1; i <= departure_days; i++) {		
					var opt = $('<option value="' + i + '">'+ get_date(''+i) +'</option>');
					departure.day.append(opt);
				}
				break;
			default:
				arrival.day.html('');		
				for ( i = 1; i <= arrival_days; i++) {		
					var opt = $('<option value="' + i + '">'+ get_date(''+i) +'</option>');
					arrival.day.append(opt);
				}
				
				departure.day.html('');		
				for ( i = 1; i <= departure_days; i++) {		
					var opt = $('<option value="' + i + '">'+ get_date(''+i) +'</option>');
					departure.day.append(opt);
				}
				break;
		}		
	}
	
	// bind events
	this.bindEvents = function() {
		$("#book_room").click(function(){
			var find_room = true;
			
			// get the dates
			var arrival_url = get_date(arrival.day.val()) + '-' + get_date(arrival.month.val()) + '-' + arrival.year.val();
			var departure_url = get_date(departure.day.val()) + '-' + get_date(departure.month.val()) + '-' + departure.year.val();
			
			var arrival_date = new Date(arrival.year.val(), arrival.month.val()-1, arrival.day.val());
			var departure_date = new Date(departure.year.val(), departure.month.val()-1, departure.day.val());
			
			// check the dates
			if ( departure_date <= arrival_date ) {
				alert('Afgangs dato\'en skal v\u0153re st\u00F8rre end ankomst datoen');
				return false;
			}
			
			// redirect to the new page
			var url = 'http://danhhern.onlinebooking.dk/booking/?datefrom=' + arrival_url + '&dateto=' + departure_url + '&profileid=0&adultcount=0&childcount=0&flexdays=0';
			//window.location = url;
			window.open(url);
		});
	}
	
	// init the calendar
	this.init = function() {
		var booking = this;
		booking.setAvailableDays();
		booking.setToday();		
		
		// on month change		
		arrival.month.change(function(){
			booking.setAvailableDays('arrival');
		});
		
		departure.month.change(function(){
			booking.setAvailableDays('departure');
		});
		
		// adjust years
		var d = new Date();
		var current_year = d.getFullYear();
		var end_year = current_year+4;
		
		// clear years
		$(arrival.year).html('');
		$(departure.year).html('');
		for( current_year; current_year < end_year; current_year++ ) {
			$(arrival.year).append('<option value="' + current_year + '">' + current_year + '</option>');
			$(departure.year).append('<option value="' + current_year + '">' + current_year + '</option>');
		}
		
		
		// bind events
		this.bindEvents();		
	}
	
	// fire it up
	if ( $("#book_room").length ) {
		this.init();
	}
	
}

var slideshow = function(seconds) {
	
	var images = $(".slideshow li");
	var length = (images.length-1);
	var index = 0;
	
	this.next = function() {
		var slideshow = this;
		
		// wait 5 seconds before changeing
		setTimeout(function(){			
			var visible = index;
			
			if ( index == length ) {
				visible = length;
			}
			
			// fade out the current image
			$(images[visible]).fadeOut('slow', function(){
				// update the index if needed
				if ( index == length ) {
					index = 0;
				} else {
					index++;
				}
				
				// show the next image
				$(images[index]).fadeIn('normal');				
				
				// play the next
				slideshow.next();
			});
			
		}, (seconds*1000));
	}
	
	this.init = function() {
		// play the slideshow
		this.next();
	}
	
	// load the slideshow
	if ( $(".slideshow").length ) {
		this.init();
	}
}

var calendar = function(id) {	
	var calendar = this;
	var obj = $("#"+id);
	var today = new Date();
	var month_days = [31,28,31,30,31,30,31,31,30,31,30,31];
	
	// Get the current language
	var lng = $("html").attr("lang");
	
	
	var month_names = { 
		'da': ['Januar','Februar','Marts','April','Maj','Juni','Juli','August','September','Oktober','November','December'],
		'en': ['January','February','March','April','May','June','July','August','September','October','November','December'],
		'de': ['Januar','Februar','M&auml;rz','April','Mai','Juni','Juli','August','September','Oktober','November','Dezember']
	}
	
	var months = ['Januar','Februar','Marts','April','Maj','Juni','Juli','August','September','Oktober','November','December'];
	var currentDate = '';	
	
	var weekday_names = {
		'da': ['Man','Tir', 'Ons', 'Tor', 'Fre', 'L&oslash;r', 'S&oslash;n'],
		'en': ['Mon','Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'],
		'de': ['Mon','Die', 'Mit', 'Don', 'Fre', 'Sam', 'Son']
	}
	
	// Set the weekdays
	$("#" + id + " .weekdays li").each(function(i,el){
		$(el).html( weekday_names[lng][i] );
	});
	
	// get the selected date
	this.getSelectedDate = function() {
		var year = currentDate.getFullYear();
		var month = currentDate.getMonth();
		var day = $("#"+id+" .days li.selected").html();
		
		if (day) {
			return new Date(year, month, day);
		}
		
		return false;
	}
	
	// set the current date
	this.changeDate = function(d) {
		//get the date info
		var month = d.getMonth();
		var year = d.getFullYear();
		var day = d.getDate();
		var days = month_days[month];
		
		// Get leap year
		var isLeap = new Date(year,1,29).getDate() == 29;
		if ( month === 1 && isLeap === true ) {
			days++;
		}
			
		// get the first day of the month
		var firstDay = new Date(year, month, 1);
		var weekday = firstDay.getDay()-1;
		currentDate = firstDay; // update the current date
		
		// set the current month
		$("#"+id+" .month-nav .month").html('<b>'+ month_names[lng][month] +'</b> ' + year);
				
		// see if the weekday is a sunday
		if ( weekday == -1 ) { weekday = 6; }
		
		// fade out days before changine
		$("#"+id+" .days").fadeOut('fast', function(){
			var debug = true;
			
			// reset the days
			$(this).html('');
			
			// loop through the days
			for( i=1; i <= (days+weekday); i++ ) {
				// create the new day
				var itm = $('<li>'+ (i-weekday) +'</li>').bind('click', function(){
					$("#"+id+" .days li.selected").removeClass('selected');
					$(this).addClass('selected');
				});
				
				// if the new day is from a prev month, we clear it
				if ( (i-weekday) < 1 ) {
					itm.unbind('click',null).html('&nbsp;');
				}
				
				// see if today is the current day
				var thisDay = new Date(year, month, (i-weekday));
				if ( (thisDay.toDateString() == today.toDateString()) ) {
					itm.addClass('today');
				}
				
				// add the new day
				$("#"+id+" .days").append(itm);
				
				if (debug == true && i > 6) {
					//alert($("#"+id+" .days li").length);
					debug = false;
				}
			}
			
			
			// fade in when done
			$(this).fadeIn('fast');
			
		});
	}
	
	// bind the month navigation
	this.monthNavigation = function() {
		$("#"+id+" .month-nav .prev").bind('click', function(){
			calendar.changeMonth( (currentDate.getMonth()-1) );
		});
		
		$("#"+id+" .month-nav .next").bind('click', function(){
			calendar.changeMonth( (currentDate.getMonth()+1) );
		});
	}
	
	// change to a new month
	this.changeMonth = function(m) {
		var d = new Date(currentDate.getFullYear(), m, 1);
		calendar.changeDate(d);
	}
	
	// initiate the calednar
	this.init = function() {
		this.changeDate(today);
		this.monthNavigation();
	}
	
	this.init();
}

function get_date(date) {
	if ( date.toString().length == 1 ) {
		date = '0' + date;
	}
	
	return date;
}
