var date_picker = new Class({
	cal_a: null,
	cal_b: null,
	ciel: null,
	coel: null,
	initialize: function()
	{
		this.ciel = $('cid');
		this.coel = $('cod');
	},
	get_avail: function()
	{
		var cidel = $('cid');
		var codel = $('cod');
		var la = $('obm_adults');
		var lc = $('obm_childs');
		var lcval = 0;
		if (lc != null) lcval = lc.options[lc.options.selectedIndex].value;
		if (cidel.value == '' || codel.value == '')
		{
			alert('Please choose dates first.');
			return;
		}
		var res = book.get_availability(hid, dp.cal_a.selectedDate.format('%Y-%m-%d'), dp.cal_b.selectedDate.format('%Y-%m-%d'), la.options[la.options.selectedIndex].value, lcval);
	},
	check_avail: function()
	{
		var la = $('adults');
		var lc = $('childs');
		var cr = $('crange');
		var loc = $('loc');
		var type = $('type');
		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') + '&adults=' + la.options[la.options.selectedIndex].value + '&childs=' + lc.options[lc.options.selectedIndex].value + '&crange=' + cr.options[cr.options.selectedIndex].value + '&loc=' + loc.options[loc.options.selectedIndex].value + '&type=' + type.options[type.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);
		}
	},
	show_ad: function()
	{
		var image = new Asset.images(['/images/offers/pietra-e-mare.jpg'], {onComplete: function(){
			//document.body.adopt(this);
			// create bg layer
			var bg = new Element('div');
			// create img layer
			var div = new Element('div', {'class': 'popimg'});
			var span = new Element('span');
			var a_a = new Element('a', {'href': '#close', 'text': 'Continue to site'});
			a_a.addEvent('click', function(){div.dispose();bg.dispose();});
			var a_b = new Element('a', {'href': '/hotels-in-mykonos-greece/unique-mykonos-hotels/Pietra-e-Mare.asp'});
			a_b.addEvent('click', function(){div.dispose();bg.dispose();});
			//
			span.grab(a_a);
			div.grab(span);
			a_b.grab(image[0]);
			div.grab(a_b);
			// add to DOM
			bg.inject($('container'));
			div.inject($('container'));
			// position, style and display
			var s = window.getScrollSize();
			bg.setStyles({'position': 'absolute', 'width': s.x + 'px', 'height': s.y + 'px', 'top': '0', 'left': '0', 'opacity': .8, 'background-color': '#000'});
			div.position({'position': 'center'});
		}});
	}
});
var dp;
var cid = null;
var cod = null;
var adults = 2;
var childs = 0;
var hid = null;
var _exec = false;
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 (cid != null && cod != null)
	{
		var nd_a = new Date(cid * 1000);
		var nd_b = new Date(cod * 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 (_exec) book.get_availability(hid, cid, cod, adults, childs);
	//
	if (show_ad)
	{
		dp.show_ad();
	}
});