var date_picker = new Class({
	ciel: null,
	coel: null,
	initialize: function()
	{
		this.ciel = $('cid');
		this.coel = $('cod');
	},
	check_avail: function()
	{
		var loc = $('loc');
		if (this.ciel.value == '' || this.coel.value == '')
		{
			alert('Please choose dates first.');
			return;
		}
		window.location = '/search/?cid=' + dp.cal_a.selectedDate.format('%Y-%m-%d') + '&cod=' + dp.cal_b.selectedDate.format('%Y-%m-%d') + '&loc=' + loc.options[loc.options.selectedIndex].value;
	},
	adjust_dates: function()
	{
		if (dp.cal_a == null || dp.cal_b == null) return;
		var da = dp.cal_a.selectedDate.getTime();
		var db = dp.cal_b.selectedDate.getTime();
		var d = null;
		var five_days_diff = da + ((86400 * 5) * 1000);
		if (da >= db)
		{
			d = dp.cal_a.selectedDate;
			var nd_b = d.clone().increment('day', 1);
			//dp.cal_b.options.minDate = nd_b;
			dp.cal_b.setDate(nd_b);
		}
	}
});
var dp;
var _cidt = 0;
var _codt = 0;
var _city = 0;
window.addEvent('domready', function(){
	if ($('cid') == null && $('cod') == null) return;
	dp = new date_picker();
	dp.cal_a = new CalendarEightysix('cid', {'format': '%a %b %d, %Y', 'disallowUserInput': true, 'toggler': 'cii'});
	dp.cal_b = new CalendarEightysix('cod', {'format': '%a %b %d, %Y', 'disallowUserInput': true, 'toggler': 'coi'});
	if (_cidt != 0 && _codt != 0)
	{
		var nd_a = new Date(_cidt * 1000);
		var nd_b = new Date(_codt * 1000);
		dp.cal_a.options.minDate = new Date();
		dp.cal_a.setDate(nd_a);
		dp.cal_b.options.minDate = new Date().increment('day', 1);
		dp.cal_b.setDate(nd_b);
	}
	else
	{
		var nd_a = new Date().increment('day', 1);
		var nd_b = new Date().increment('day', 2);
		dp.cal_a.options.minDate = new Date();
		dp.cal_a.setDate(nd_a);
		dp.cal_b.options.minDate = nd_b;
		dp.cal_b.setDate(nd_b);
	}
	dp.cal_a.options.pickFunction = function(d){dp.adjust_dates();};
	dp.cal_b.options.pickFunction = function(d){dp.adjust_dates();};
	if (_city != 0)
	{
		var loc = $('loc');
		if (loc != null)
		{
			for (var i = 0; i < loc.options.length; i++)
			{
				if (loc.options[i].value == _city)
				{
					loc.selectedIndex = i;
					break;
				}
			}
		}
	}
});
