function load_deals(target) {
	if (target.href) {
		// it's a link inside a span
		q = target.href;
		$ajaxload = $('.ajaxload', $(target).parents('span'));
	} else {
		// it's a form
		q = 'deals-ajax/?' + $(target).serialize();
		$ajaxload = $('.ajaxload', target);
	}
  $ajaxload.show();
  $('#deals-wrap').load(q + ' #deals', function() { 
    $ajaxload.hide();
    deals_attach();
  });
}

function deals_attach() {
	$('#deals tbody td.deals-hover').hover(function(e) {
		var html = $('span', this).html();
		if (html == null) html = 'Sorry, this date is not available';
	  $('body').append('<div id="deals-tip"><div>' + html + '</div></div>');
	  $('#deals-tip').css('top', (e.pageY + 2) + 'px').css('left', (e.pageX + 8) + 'px').show();
	}, function() {
	  $('#deals-tip').remove();
	});
  $('#deals tbody th span.deals-room a').click(function() {
		this.blur();
    var $cell = $(this).parents('th');
    var $row = $cell.parents('tr').next('tr.room-desc');
    if ($row[0].style.display != 'block' && $row[0].style.display != 'table-row') {
      if ($.browser.msie) {
				$row.css('display', 'block');
			} else {
				$row.css('display', 'table-row');
			}
      $cell.addClass('room-desc-active');
      $('div.room-slide', $row).slideDown('normal');
    } else {
      $('div.room-slide', $row).slideUp('normal', function() {
        $cell.removeClass('room-desc-active');
        $row.css('display', 'none');
      });
    }
  });
  $('#deals thead th.dateform input').datepicker({
    minDate: 0,
    dateFormat: 'dd/mm/yy',
    duration: "",
    showOn: 'both',
		buttonImage: '/media/img/btn-dates.gif',
		buttonImageOnly: true,
    onSelect: function() {
			$.cookie('checkin', this.value);
	    load_deals(this.form);
    }    
  });
  $('#deals thead th.dateform button').click(function() {
    load_deals(this.form);
    return false;
  });
  $('#deals tbody td').click(function() {
    var $tr = $(this).parents('tr');
		var $golink = $('.golink', $tr);
		var url = 'enquiry/?msg=unavailable';
		if ($golink.length > 0) {
			var url = $('.golink', $tr)[0].href;
		} 
    window.location = url;
  });
  $('#deals tfoot .next a, #deals tfoot .prev a').click(function() {
		load_deals(this);
    return false;
  });
}

$(function() { 
	deals_attach();
});


