var availability_manager = new Class({
	ro: null,
	res: null,
	get_availability: function(hid, cid, cod, adults, childs){
		$('avail').innerHTML = '<div id="loader"><p>Please wait, requesting availability...</p><img src="/images/ajax-loader.gif" alt="" /></div>';
		$('avail').style.display = 'block';
		var json = new Request.JSON(
			{
				url: '/availability/check/',
				onSuccess: function(res_obj, res_str)
				{
					if (res_obj.err != 'OK')
					{
						str = '<div id="error">' + res_obj.err + '</div>';
					}
					else
					{
						//alert(res_str);
						if (typeof(res_obj.dat.msg) != 'undefined')
						{
							str = '<div id="error">' + res_obj.dat.msg + '</div>';
						}
						else
						{
							if (res_obj.dat.rooms.length == 0)
							{
								str = '<div id="error">There are no rooms available for the dates you have selected</div>';
							}
							else
							{
								var rids = '';
								var opt = '';
								var str = '<div class="b" style="margin-bottom: 20px;border-bottom: 1px #999 solid;"><form name="form1" id="form1" action="https://www.mykonos-hotels.info/availability/reserve/" method="post">';
								str += '<table cellspacing="4" cellpadding="4" border="0" width="100%" class="tobms"><thead><tr><th>Room type</th><th>Max</th><th>Rate for ' + res_obj.dat.total_days + ' nights</th><th>Nr. rooms</th><th>Reservation</th></tr></thead><tbody>';
								// build html!
								$each(res_obj.dat.rooms, function(room, i){
									var img = '', imgs = '';
									if (room.images.length != 0)
									{
										img = '<img src="' + room.images[0] + '" alt="" style="margin-right: 10px;" />';
										imgs = '<div class="imgs">';
										for (var i = 0; i < room.images.length; i++)
										{
											imgs += '<a href="' + room.images[i].replace(/t_/, 'p_') + '" target="_blank"><img src="' + room.images[i] + '" alt="" /></a>';
										}
										imgs += '</div>';
									}
									str += '<tr>\
									<td class="aroom">\
									' + img + '\
									<div id="rinf">\
										<span class="n">' + room.name + '</span>\
										<span class="rr">Rates are per room</span>\
										<span class="ndesc">' + room.desc + '</span>\
										<a href="#mi" class="mi" onclick="show_tip(\'mi_r_' + room.id + '\');">[More information]</a>\
										<div class="rtip" id="mi_r_' + room.id + '">\
											<a href="#close" class="rtipc" onclick="hide_tip(\'mi_r_' + room.id + '\');">[Close]</a>\
											' + imgs + '\
											<div class="dat">\
												<p class="p"><b>Facilities:</b> ' + room.facilities + '</p>\
												<p class="p"><b>Breakfast:</b> ' + room.breakfast + '</p>\
											</div>\
										</div>\
									</div>\
									<td class="max"><img src="/images/winnie_' + room.availability.max_persons + '.gif" alt="' + room.availability.max_persons + ' persons maximum" /></td>\
									<td class="rt">&euro;' + room.availability.total_nights_rate + '</td>\
									<td class="rm">';
									opt = '';
									for (var i = 1; i <= room.availability.rooms_available; i++)
									{
										opt += '<option value="' + i + '_' + (room.availability.total_nights_rate * i) + '">' + i + ' (&euro;' + (room.availability.total_nights_rate * i) + ')</option>';
									}
									str += '<select name="sr_' + room.id + '" id="sr_' + room.id + '"><option value="0">0</option>' + opt + '</select><span class="rl">' + room.availability.rooms_available + ' room(s) left!</span></td>\
									<td class="rv"><input type="button" name="btn_book_1" value="Book this" onclick="book.do_reservation(' + room.id + ');" class="btn_obm" />';
									rids = '';
									$each(room.availability.ids, function(id, i){
											rids += id + '_';
									})
									if (rids != '') rids = rids.substr(0, rids.length - 1);
									str += '<input type="hidden" name="sr_' + room.id + '_ids" id="sr_' + room.id + '_ids" value="' + rids + '" />';
									str += '</td></tr>';
								})
								str += '</tbody></table>';
								str += '<input type="hidden" name="hid" value="' + res_obj.dat.hotel_id + '" />';
								str += '<input type="hidden" name="cid" value="' + res_obj.dat.cid + '" />';
								str += '<input type="hidden" name="cod" value="' + res_obj.dat.cod + '" />';
								str += '<input type="hidden" name="adults" value="' + res_obj.dat.adults + '" />';
								str += '<input type="hidden" name="childs" value="' + res_obj.dat.childs + '" />';
								str += '<input type="hidden" name="nights" value="' + res_obj.dat.total_days + '" />';
								str += '<input type="hidden" name="nor" id="nor" value="" />';
								str += '<input type="hidden" name="step" value="1" />';
								str += '<input type="hidden" name="rid" id="rid" value="" />';
								str += '<input type="hidden" name="rids" id="rids" value="" />';
								str += '<input type="hidden" name="cost" id="cost" value="" />';
								str += '</form></div>';
							}
						}
					}
					$('avail').innerHTML = str;
					$('avail').style.display = 'block';
				}
			}).send('hid=' + hid + '&cid=' + cid + '&cod=' + cod + '&adults=' + adults + '&childs=' + childs);
	},
	do_reservation: function(rid){
		var el = $('sr_' + rid);
		var dat = el.options[el.options.selectedIndex].value;
		var arr = dat.split('_');
		if (arr.length == 2)
		{
			$('rid').value = rid;
			$('rids').value = $('sr_' + rid + '_ids').value;
			$('nor').value = arr[0];
			$('cost').value = arr[1];
			document.form1.submit();
		}
		else
		{
			alert('Please choose number of rooms.');
		}
	},
	show_cp: function(hid){
		var win = window.open('/availability/children-policy/' + hid + '/', '', 'menubar=no,width=650,height=300,toolbar=no,scrollbars=no');
	}
});
var book = new availability_manager();
// tooltips!!
function show_tip(id){new Fx.Tween($(id)).set('opacity', 0).set('display', 'block').start('opacity', 0, 1);}
function hide_tip(id){$(id).tween('opacity', 0);}

