var strWidth;
var objAutoComplete;
function acIncCodeLoadComplete(){
	
	if($('dateSyncFieldAvailCheckIn') != undefined && $('dateSyncFieldAvailCheckOut') != undefined){
		updateDay('b1_checkin_avail_day','b1_checkin_avail_month','idCheckinAvailDay','dateSyncFieldAvailCheckIn');
		updateDay('b1_checkout_avail_day','b1_checkout_avail_month','idCheckoutAvailDay','dateSyncFieldAvailCheckOut');
		picker1 = new DatePicker('dateSyncFieldAvailCheckIn', {
			pickerClass: 'clsDatepicker',
			allowEmpty: true,
			days: arrWeekDaysKalendar,
			months: arrShortMonths,
			startDate: { date: currentDateCheckIn, format: 'd-m-Y' },
			minDate :  { date: startDateCheckIn, format: 'd-m-Y' },
			maxDate : { date: endDateCheckIn, format: 'd-m-Y' },
			updateGlobalDateFields: function(dateElement){
				var dayParts = dateElement.split('-');
				setYearMonthPart('b1_checkin_avail_month',dayParts[2].toInt()+'-'+dayParts[1].toInt(),'b1_checkin_avail_day');
				setDayPart('b1_checkin_avail_day',dayParts[0].toInt());
				updateDay('b1_checkin_avail_day','b1_checkin_avail_month','idCheckinAvailDay','dateSyncFieldAvailCheckIn');
			},
			onShow : function(){
				if(IE6){
					clearIE6SelectBoxSearch(true);
				}
			},
			onClose : function(){
				if(IE6){
					$$('select').setStyle('visibility','visible'); 
				}
			},
			positionOffset : { x: -160, y: 0 },
			toggleElement: 'clsCalendarAvailCheckIn'
		});
	
		picker2 = new DatePicker('dateSyncFieldAvailCheckOut', {
			pickerClass: 'clsDatepicker',
			allowEmpty: true,
			days: arrWeekDaysKalendar,
			months: arrShortMonths,
			startDate: { date: currentDateCheckOut, format: 'd-m-Y' },
			minDate :  { date: startDateCheckOut, format: 'd-m-Y' },
			maxDate : { date: endDateCheckOut, format: 'd-m-Y' },
			updateGlobalDateFields: function(dateElement){
				var dayParts = dateElement.split('-');
				setYearMonthPart('b1_checkout_avail_month',dayParts[2].toInt()+'-'+dayParts[1].toInt(),'b1_checkout_avail_day');
				setDayPart('b1_checkout_avail_day',dayParts[0].toInt());
				updateDay('b1_checkout_avail_day','b1_checkout_avail_month','idCheckoutAvailDay','dateSyncFieldAvailCheckOut');
			},
			onShow : function(){
				if(IE6){
					clearIE6SelectBoxSearch(true);
				}
			},
			onClose : function(){
				if(IE6){
					$$('select').setStyle('visibility','visible'); 
				}
			},
			positionOffset : { x: -160, y: 0 },
			toggleElement: 'clsCalendarAvailCheckOut'
		});
	}
	
	if($('dateSyncFieldCheckIn') != undefined && $('dateSyncFieldCheckOut') != undefined){
		updateDay('b1_checkin_day','b1_checkin_month','idCheckinDay','dateSyncFieldCheckIn');
		updateDay('b1_checkout_day','b1_checkout_month','idCheckoutDay','dateSyncFieldCheckOut');
		picker1 = new DatePicker('dateSyncFieldCheckIn', {
			pickerClass: 'clsDatepicker',
			allowEmpty: true,
			days: arrWeekDaysKalendar,
			months: arrShortMonths,
			startDate: { date: currentDateCheckIn, format: 'd-m-Y' },
			minDate :  { date: startDateCheckIn, format: 'd-m-Y' },
			maxDate : { date: endDateCheckIn, format: 'd-m-Y' },
			onShow : function(){
				if(IE6){
					clearIE6SelectBoxSearch(false);
				}
			},
			onClose : function(){
				if(IE6){
					$$('select').setStyle('visibility','visible'); 
				}
			},
			positionOffset : { 	x: -300,
								y: 0 },
			updateGlobalDateFields: function(dateElement){
				var dayParts = dateElement.split('-');
				setAvailabilityOn();
				setYearMonthPart('b1_checkin_month',dayParts[2].toInt()+'-'+dayParts[1].toInt(),'b1_checkin_day');
				setDayPart('b1_checkin_day',dayParts[0].toInt());
				updateDay('b1_checkin_day','b1_checkin_month','idCheckinDay','dateSyncFieldCheckIn');
			},
			toggleElement: 'clsCalendarCheckIn'
		});
	
		picker2 = new DatePicker('dateSyncFieldCheckOut', {
			pickerClass: 'clsDatepicker',
			allowEmpty: true,
			days: arrWeekDaysKalendar,
			months: arrShortMonths,
			startDate: { date: currentDateCheckOut, format: 'd-m-Y' },
			minDate :  { date: startDateCheckOut, format: 'd-m-Y' },
			maxDate : { date: endDateCheckOut, format: 'd-m-Y' },
			onShow : function(){
				if(IE6){
					clearIE6SelectBoxSearch(false);
				}
			},
			onClose : function(){
				if(IE6){
					$$('select').setStyle('visibility','visible'); 
				}
			},
			positionOffset : { 	x: -300,
								y: 0 },
			updateGlobalDateFields: function(dateElement){
				var dayParts = dateElement.split('-');
				setAvailabilityOn();
				setYearMonthPart('b1_checkout_month',dayParts[2].toInt()+'-'+dayParts[1].toInt(),'b1_checkout_day');
				setDayPart('b1_checkout_day',dayParts[0].toInt());
				updateDay('b1_checkout_day','b1_checkout_month','idCheckoutDay','dateSyncFieldCheckOut');
			},
			toggleElement: 'clsCalendarCheckOut'
		});
	}
	
	
	if($('destSearch') && $('destSearch').value == '' && $('idAltTextSearch')){
		$('destSearch').value = $('idAltTextSearch').title;
		setGreyInputBox($('destSearch'));
		$('destSearch').addEvent('focus', function(e){
			undoGreyInputBox($('destSearch'));
		});
		$('destSearch').addEvent('click', function(e){
			undoGreyInputBox($('destSearch'));
		});
	}
}

function clearIE6SelectBoxSearch(retainAvail){
	$$('select').setStyle('visibility','hidden');
	$$('.clsCalendarSelectMonth').setStyle('visibility','visible');
	$('b1_checkin_day').setStyle('visibility','visible');
	$('b1_checkout_day').setStyle('visibility','visible');
	$('b1_checkin_month').setStyle('visibility','visible');
	$('b1_checkout_month').setStyle('visibility','visible');
	if(retainAvail ===  true){
		if($('b1_checkin_avail_day') != undefined){
			$('b1_checkin_avail_day').setStyle('visibility','visible');
			$('b1_checkout_avail_day').setStyle('visibility','visible');
			$('b1_checkin_avail_month').setStyle('visibility','visible');
			$('b1_checkout_avail_month').setStyle('visibility','visible');
		}
	}
}

function setAvailabilityOn(){
	$('showAvail').setProperty('checked','checked');
}

function updateDay(strDay,strYearMonth,strElement,syncInputElement){
	var objYearMonth = $(strYearMonth);
	var objDay = $(strDay);
	var objElement = $(strElement);
	if(objElement != null && objDay != null && objYearMonth != null){
		var arrParts = objYearMonth.options[objYearMonth.selectedIndex].value.split('-');
		var yearPart =  arrParts[0].toInt();
	    var monthPart = arrParts[1].toInt();
	    var dayPart = objDay.options[objDay.selectedIndex].value.toInt();
	    var date = new Date();
	    
		date.setYear(yearPart);
		date.setMonth((monthPart-1));
		date.setDate(dayPart);
		
		var strDay = arrWeekDays[date.getDay()];
		objElement.set('html','<strong>'+strDay+'</strong>');
		
		if((''+dayPart).length == 1)
			dayPart = '0'+dayPart;
		if((''+(arrParts[1])).length == 1)
			monthPart = '0'+arrParts[1];
		$(syncInputElement).value = ''+dayPart+'-'+monthPart+'-'+yearPart;
		
		/* in the case of the search box */
		if($('dateSyncFieldCheckOut') != undefined && $('dateSyncFieldCheckOut').value != '' && $('dateSyncFieldCheckOut') != undefined && $('dateSyncFieldCheckIn').value != ''){
			var checkOutDateParts =  $('dateSyncFieldCheckOut').value.split('-');
			var checkInDateParts = $('dateSyncFieldCheckIn').value.split('-');
			var checkOutDate = new Date(checkOutDateParts[2].toInt(),(checkOutDateParts[1].toInt()-1),checkOutDateParts[0].toInt());
			var checkInDate = new Date(checkInDateParts[2].toInt(),(checkInDateParts[1].toInt()-1),checkInDateParts[0].toInt());
			if(objDay.getProperty('id') == 'b1_checkout_day'){
				checkInDate.setDate(checkInDate.getDate()+1);
				if(checkOutDate.getTime() < checkInDate.getTime()){
					/*
					 * at least one day in between
					 * update checkInDate?
					 */
					checkOutDate.setDate(checkOutDate.getDate()-1);
					setYearMonthPart('b1_checkin_month',checkOutDate.getFullYear().toInt()+'-'+(checkOutDate.getMonth().toInt()+1),'b1_checkin_day');
					setDayPart('b1_checkin_day',checkOutDate.getDate().toInt());
					updateDay('b1_checkin_day','b1_checkin_month','idCheckinDay','dateSyncFieldCheckIn');
				}
			} else if(objDay.getProperty('id') == 'b1_checkin_day'){
				checkInDate.setDate(checkInDate.getDate()+1);
				if(checkOutDate.getTime() < checkInDate.getTime()){
					/*
					 * at least one day in between
					 * update checkOutDate?
					 */				
					 setYearMonthPart('b1_checkout_month',checkInDate.getFullYear().toInt()+'-'+(checkInDate.getMonth().toInt()+1),'b1_checkout_day');
					 setDayPart('b1_checkout_day',checkInDate.getDate().toInt());
					 updateDay('b1_checkout_day','b1_checkout_month','idCheckoutDay','dateSyncFieldCheckOut');
				}
			}
		}
		
		
		/* in the case of the availability box */
		if($('dateSyncFieldAvailCheckOut') != undefined && $('dateSyncFieldAvailCheckOut').value != '' && $('dateSyncFieldAvailCheckOut') != undefined && $('dateSyncFieldAvailCheckIn').value != ''){
			var checkOutDateParts =  $('dateSyncFieldAvailCheckOut').value.split('-');
			var checkInDateParts = $('dateSyncFieldAvailCheckIn').value.split('-');
			var checkOutDate = new Date(checkOutDateParts[2].toInt(),(checkOutDateParts[1].toInt()-1),checkOutDateParts[0].toInt());
			var checkInDate = new Date(checkInDateParts[2].toInt(),(checkInDateParts[1].toInt()-1),checkInDateParts[0].toInt());
			if(objDay.getProperty('id') == 'b1_checkout_avail_day'){
				checkInDate.setDate(checkInDate.getDate()+1);
				if(checkOutDate.getTime() < checkInDate.getTime()){
					/*
					 * at least one day in between
					 * update checkInDate?
					 */
					checkOutDate.setDate(checkOutDate.getDate()-1);
					setYearMonthPart('b1_checkin_avail_month',checkOutDate.getFullYear().toInt()+'-'+(checkOutDate.getMonth().toInt()+1),'b1_checkin_avail_day');
					setDayPart('b1_checkin_avail_day',checkOutDate.getDate().toInt());
					updateDay('b1_checkin_avail_day','b1_checkin_avail_month','idCheckinAvailDay','dateSyncFieldAvailCheckIn');
				}
			} else if(objDay.getProperty('id') == 'b1_checkin_avail_day'){
				checkInDate.setDate(checkInDate.getDate()+1);
				if(checkOutDate.getTime() < checkInDate.getTime()){
					/*
					 * at least one day in between
					 * update checkOutDate?
					 */				
					 setYearMonthPart('b1_checkout_avail_month',checkInDate.getFullYear().toInt()+'-'+(checkInDate.getMonth().toInt()+1),'b1_checkout_avail_day');
					 setDayPart('b1_checkout_avail_day',checkInDate.getDate().toInt());
					 updateDay('b1_checkout_avail_day','b1_checkout_avail_month','idCheckoutAvailDay','dateSyncFieldAvailCheckOut');
				}
			}
		}
	}
}

function updateDateSelectBox(objDay,monthPart,yearPart){
	var dayPart = 1;
	var selectedIndex = dayPart = objDay.selectedIndex;
	$(objDay).empty();
	var intMonth = (monthPart-1);
	var intYear = yearPart;
	var dteMonth = new Date(intYear,intMonth,1);
	var blnDateFound = false;
	var nrOfDaysDone = 0;		
	while (!blnDateFound){
		var optionX = new Element('option', { 'value':dteMonth.getDate() }).set('text', dteMonth.getDate()).inject(objDay);
		if(nrOfDaysDone == selectedIndex){
			optionX.setProperty('selected','selected');
			dayPart = nrOfDaysDone;
		}
		dteMonth.setDate(dteMonth.getDate()+1);
		var intNewMonth = dteMonth.getMonth();//new month date
		if (intNewMonth != intMonth){
			maxIndex = nrOfDaysDone;
			blnDateFound = true;
		} else {
			nrOfDaysDone++;
		}
	}
	if(selectedIndex >= maxIndex){
		if(optionX != undefined){
			optionX.setProperty('selected','selected');
			dayPart = nrOfDaysDone;
		}
	}
	return dayPart;
}

function setDayPart(idElement,day){
	for(var i=0; i<$(''+idElement).options.length; i++){
		var item = $($(''+idElement).options[i]);
		if(item.getProperty('value') == day){
			item.setProperty('selected','selected');
		} else {
			item.removeProperty('selected');
		}
	}
}

function setYearMonthPart(idElement,yearMonth,updateElement){
	for(var i=0; i<$(''+idElement).options.length; i++){
		var item = $($(''+idElement).options[i]);
		if(item.getProperty('value') == yearMonth){
			item.setProperty('selected','selected');
		} else {
			item.removeProperty('selected');
		}
	}
	var yearMonthParts= yearMonth.split('-');
	updateDateSelectBox($(updateElement),yearMonthParts[1].toInt(),yearMonthParts[0].toInt());
}

function setGreyInputBox(element){
	$(element).setStyle('color','#999999');
}
function undoGreyInputBox(element){
	$(element).setStyle('color','#000000');
}

function fixIE6RoundedCorners(element,index){
		clearTimeout(ie6Timeout);
		var intHeight = Math.max((element.getScrollSize().y.toInt()),(element.getSize().y.toInt()),(element.offsetHeight.toInt()));
		var intWidth = Math.max((element.getScrollSize().x.toInt()),(element.getSize().x.toInt()),(element.offsetWidth.toInt()));
		var divs = element.getChildren('div');
		var intPositionLeft = element.getStyle('border-left-width').toInt();
		var intPositionTop = element.getStyle('border-top-width').toInt();
		
		/*
		 * Odd-even bug is dependant on borders
		 * If border set left and top -> correct for EVEN
		 * If border not set left and top -> correct for ODD
		 * Correction integrated in border size
		 */
		
		var intNoCorrectionBottom = 0-intPositionTop;
		var intNoCorrectionRight = 0-intPositionLeft;
		var intCorrectionBottom = -1+intPositionTop;
		var intCorrectionRight = -1+intPositionLeft;
		if(intWidth % 2 == 1 && divs.length > 4){
			divs[1].setStyle('right',intCorrectionRight+'px');
			divs[3].setStyle('right',intCorrectionRight+'px');
		}
		else if(divs.length > 4){
			divs[1].setStyle('right',intNoCorrectionRight+'px');
			divs[3].setStyle('right',intNoCorrectionRight+'px');
		}
		if(intHeight % 2 == 1 && divs.length > 4){
			divs[2].setStyle('bottom',intCorrectionBottom+'px');
			divs[3].setStyle('bottom',intCorrectionBottom+'px');
		}
		else if(divs.length > 4){
			divs[2].setStyle('bottom',intNoCorrectionBottom+'px');
			divs[3].setStyle('bottom',intNoCorrectionBottom+'px');
		}
}
var IE6 = (navigator.userAgent.toLowerCase().indexOf('msie 6') != -1)
&& (navigator.userAgent.toLowerCase().indexOf('msie 7') == -1);
var IE7 = (navigator.userAgent.toLowerCase().indexOf('msie 7') != -1)
&& (navigator.userAgent.toLowerCase().indexOf('msie 8') == -1);
var ie6FixRoundedCorners;
var ie6Timeout;
window.addEvent('load', function(){
	/*if($('idBookButtonColumn') != null){
	 	$('idBookButtonColumn').setStyle('width',($('bookButtonSize').getSize().x+20)+"px");
	}*/
	

	if(IE6){
		ie6FixRoundedCorners = $$('div.clsDivBox1',
			'div.clsDivBox2',
			'div.clsDivBox4',
			'div.clsDivBox5',
			'div.clsDivBox6',
			'div.clsDivBox7',
			'div.clsDivBox8',
			'div.clsDivBox9',
			'div.clsDivBox10',
			'div.clsDivBox11',
			'div.clsDivBox12',
			'div.clsDivBox13',
			'div.clsDivBox14');
		if(ie6Timeout != undefined){
			clearTimeout(ie6Timeout);
		}
		ie6Timeout = setTimeout(function(){ie6FixRoundedCorners.each(fixIE6RoundedCorners)},500);
	}
});
window.addEvent('resize', function(){
	if(strWidth != null && strWidth != undefined && objAutoComplete != undefined){
		strWidth = $('destSearch').hasClass('clsEbSearchBoxTxtInput')?('500px'):($('destSearch').getSize().x);
		objAutoComplete.options.width = strWidth.toInt()+'px';
		objAutoComplete.hideChoices();
		if(objAutoComplete.visible){
			objAutoComplete.showChoices();
		} 
	}
	if(IE6){
		if(ie6Timeout != undefined){
			clearTimeout(ie6Timeout);
		}
		ie6Timeout = setTimeout(function(){ie6FixRoundedCorners.each(fixIE6RoundedCorners)},500);
	}
});