function handleSelect(type, args, obj) {
	var dates = args[0];
	var date  = dates[0];
	var year  = date[0], month = date[1], day = date[2];
	
	if(year > new Date().getFullYear()){
		month = month + 12;
	}

	if(this.id.match(/start/)){
		$("search_start_month").value = month;
		$("search_start_day").value   = day;
	}
	else{
		$("search_end_month").value = month;
		$("search_end_day").value   = day;
	}
	
	// Update end month if the new start month is greater than the end month: 
	// console.log("handleSelect: id", this.id);
	if(this.id.match(/start/)){
		// console.log("comparing "+$F("search_start_month")+" > "+$F("search_end_month"));
		if($F("search_start_month") > $F("search_end_month"))
		{
			// console.log("updating end month to ", $F("search_start_month"));
			$("search_end_month").value = $F("search_start_month");
		}
		
		// Close the calendar:
		$('start_calendar_wrapper').hide();
	}
	else{
		$('end_calendar_wrapper').hide();
	}
}

function updateCal(that) {
	if(!that.id) var that = this;
	if(that.id.match(/start/)){
		var month    = $F("search_start_month");
		var day      = $F("search_start_day");
		var calendar = YAHOO.houseboat.start_calendar;
	}
	else{
		var month    = $F("search_end_month");
		var day      = $F("search_end_day");
		var calendar = YAHOO.houseboat.end_calendar;
	}
	
	// console.log("that", that.id);
	
	if(month > 12){
		month = month - 12;
		year  = new Date().getFullYear() + 1;
	}
	else {
		year  = new Date().getFullYear()
	}
	
	if (! isNaN(month) && ! isNaN(day) && ! isNaN(year)) {
		var date = month + "/" + day + "/" + year;

		calendar.select(date);
		calendar.cfg.setProperty("pagedate", month + "/" + year);
		calendar.render();
	}
	
	// Update end month if the new start month is greater than the end month: 
	// if(that.id.match(/start/)){
	// 	console.log("Update end month if the new start month is greater than the end month: ");
	// 	// console.log("comparing "+$F("search_start_month")+" > "+$F("search_end_month"));
	// 	if($F("search_start_month") > $F("search_end_month"))
	// 	{
	// 		// console.log("updating end month to ", $F("search_start_month"));
	// 		$("search_end_month").value = $F("search_start_month");
	// 		console.log("search_end_month.updateCal()", updateCal($("search_end_month")));
	// 	}
	// 	else console.log("end month didn't need to be updated", [$F("search_start_month"), $F("search_end_month"), $F("search_start_month") > $F("search_end_month")]);
	// 	// console.log("search_end_month.updateCal()", updateCal($("search_end_month")));
	// }
}

function parse_iso_date(string)
{
	if(typeof(string) == "string"){
		elements = string.split(/\D/);
		if(elements[0] == 1970) return false;
		return new Date(elements[0], elements[1]-1, elements[2]);
	}
	else return string;
}

function init_calendars(language, current_start_date, current_end_date, minimum_date, maximum_date)
{
	var current_start_date = parse_iso_date(current_start_date);
	var current_end_date   = parse_iso_date(current_end_date);
	var minimum_date       = parse_iso_date(minimum_date);
	var maximum_date       = parse_iso_date(maximum_date);
	
	var mindate = minimum_date.getUTCMonth()+1+"/"+minimum_date.getUTCDate()+"/"+minimum_date.getUTCFullYear();
	var maxdate = maximum_date.getUTCMonth()+1+"/"+maximum_date.getUTCDate()+"/"+maximum_date.getUTCFullYear();
	
	if(current_start_date){
		start_options = {
			pagedate: current_start_date.getUTCMonth()+1+"/"+current_start_date.getUTCFullYear(),
			mindate: mindate,
			maxdate: maxdate,
			start_weekday: 1
		};
	}
	else{
		start_options = {
			mindate: mindate,
			maxdate: maxdate,
			start_weekday: 1
		};
	}
	
	if(current_end_date){
		end_options = {
			pagedate: current_end_date.getUTCMonth()+1+"/"+current_end_date.getUTCFullYear(),
			mindate: mindate,
			maxdate: maxdate,
			end_weekday: 1
		};
	}
	else{
		end_options = {
			mindate: mindate,
			maxdate: maxdate,
			end_weekday: 1
		};
	}
	
	YAHOO.namespace("houseboat");

	YAHOO.houseboat.init = function() {
		YAHOO.houseboat.start_calendar = new YAHOO.widget.Calendar("start_calendar","start_calendar_container", start_options );
		YAHOO.houseboat.start_calendar.selectEvent.subscribe(handleSelect, YAHOO.houseboat.start_calendar, true);
		YAHOO.util.Event.addListener(["search_start_month","search_start_day"], "change", updateCal); 
	
		YAHOO.houseboat.end_calendar = new YAHOO.widget.Calendar("end_calendar","end_calendar_container", end_options );
		YAHOO.houseboat.end_calendar.selectEvent.subscribe(handleSelect, YAHOO.houseboat.end_calendar, true);
		YAHOO.util.Event.addListener(["search_end_month","search_end_day"], "change", updateCal);
		
		if(language == 'nl'){
			months_long    = ["januari", "februari", "maart", "april", "mei", "juni", "juli", "augustus", "september", "oktober", "november", "december"];
			weekdays_short = ["zo", "ma", "di", "wo", "do", "vr", "za"];
		}
		else if(language == 'fr'){
			months_long    = ["janvier", "février", "mars", "avril", "mai", "juin", "juillet", "août", "septembre", "octobre", "novembre", "décembre"];
			weekdays_short = ["di", "lu", "ma", "me", "je", "ve", "sa"];
		}
		else if(language == 'de'){
			months_long    = ["Januar", "Februar", "März", "April", "Mai", "Juni", "Juli", "August", "September", "Oktober", "November", "Dezember"];
			weekdays_short = ["So", "Mo", "Di", "Mi", "Do", "Fr", "Sa"];
		}
		else if(language == 'it'){
			months_long    = ["Januar", "Februar", "März", "April", "Mai", "Juni", "Juli", "August", "September", "Oktober", "November", "Dezember"];
			weekdays_short = ["So", "Mo", "Di", "Mi", "Do", "Fr", "Sa"];
		}
		else if(language == 'es'){
			months_long    = ["enero", "febrero", "marzo", "abril", "mayo", "junio", "julio", "agosto", "septiembre", "octubre", "noviembre", "diciembre"];
			weekdays_short = ["do", "lu", "ma", "mi", "ju", "vi", "sá"];
		}
		else {
			months_long    = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
			weekdays_short = ["Su", "Mo", "Tu", "We", "Th", "Fr", "Sa"];
		}

		
		if(months_long.length > 0 && weekdays_short.length > 0){
			YAHOO.houseboat.start_calendar.cfg.setProperty("MONTHS_LONG",    months_long); 
			YAHOO.houseboat.start_calendar.cfg.setProperty("WEEKDAYS_SHORT", weekdays_short); 
			
			YAHOO.houseboat.end_calendar.cfg.setProperty("MONTHS_LONG",    months_long); 
			YAHOO.houseboat.end_calendar.cfg.setProperty("WEEKDAYS_SHORT", weekdays_short); 
		}
		
		YAHOO.houseboat.start_calendar.render();
		YAHOO.houseboat.end_calendar.render();
	}
	
	YAHOO.util.Event.onDOMReady(YAHOO.houseboat.init);
}
