var clickTd = function(ev,ob){
	//Check to see if all days within stay range are open
	var t = $(ob).closest('table');
	var numnights = $('input[NAME=num_nights]',$(ob).closest('form')).val();
	var nn = numnights;
	if((nn=numnights)>0){
		var clicked = ob;
		var start = 0;
		$('td',t).each(function(){
			if (this==clicked) start = 1;
			if(start && nn-->0) if(!$(this).hasClass('selectable')) throw('Oops! - One (or more) of the dates you have selected are not available.\nPlease choose another date range or select another rate');
		});
	}
	// if all multidays stay not fit
	if(nn>0) throw('Oops! - One (or more) of the dates you have selected are not available.\nPlease choose another date range or select another rate');
	//Clear the previous one and select the new one
	$('td.selected',t).removeClass('selected');
	$('td.selected-package',t).removeClass('selected-package');
	
	if((nn=numnights)>0){
		var clicked = ob;
		var start = 0;
		$('td',t).each(function(){
			if (this==clicked) start = 1;
			if(start && nn-->0) $(this).addClass('selected'+((numnights-nn==1)?'':' selected-package'));
		});
	}
	$('input[NAME=InDate]',$(ob).closest('form')).val(ob.id);
};

$('table.booking-engine td.enabled').live('click', function(){
	//Check to see if all days within stay range are open
	try{
		var numnights = $('input[NAME=num_nights]',$(this).closest('form')).val();
		var nn = numnights;
		if((nn=numnights)>0){
			var clicked = this;
			var start = 0;
			$('table.booking-engine td').each(function(){
				if (this==clicked) start = 1;
				if(start && nn-->0) if(!$(this).hasClass('selectable')) throw('Date cannot be selected');
			});
		}
		// if all multidays stay not fit
		if(nn>0) throw('Date cannot be selected');
		
		//Clear the previous one and select the new one
		$('table.booking-engine td.selected').removeClass('selected');
		$('table.booking-engine td.selected-package').removeClass('selected-package');
		
		if((nn=numnights)>0){
			var clicked = this;
			var start = 0;
			$('table.booking-engine td').each(function(){
				if (this==clicked) start = 1;
				if(start && nn-->0) $(this).addClass('selected'+((numnights-nn==1)?'':' selected-package'));
			});
		}
		
		$('input[NAME=InDate]',$(this).closest('form')).val(this.id);
		
	} catch (e){
		alert(e);
	}
});

$('table.booking-engine-fs td.enabled').live('click', function(ev,ob){
	//Check to see if all days within stay range are open
	var t = $(this).closest('table'); 
	try{
		if($(this).hasClass('selected')){
			var l=0;
			var lb=false;
			var s = $('td.selected',t).size();
			var o = this;
			if(s>=1){
				$('td.selected',t).each(function(){
						if(!lb)l++;
						if(this==o) lb=true
					});
				lb=false;
				
				if(l==1 || l==s){
					$(this).removeClass('selected');
				} else if(l<(s/2)) {
					$('td.selected',t).each(function(){
						if(this==o) lb=true
						if(!lb) $(this).removeClass('selected');
					});
				} else {
					$('td.selected',t).each(function(){
						if(lb) $(this).removeClass('selected');
						if(this==o) lb=true
					});
				}
			} else {
				$(this).removeClass('selected');
			}
		} else {
			$(this).addClass('selected');
		}
		$('input[NAME=num_nights]',$(this).closest('form')).val(0);
		var o=$('td.selected:first',t);
		if(o.size()){
			$('input[NAME=num_nights]',$(this).closest('form')).val(1+parseInt($('td.selected:last',t).attr('index'))-parseInt($('td.selected:first',t).attr('index')));
			try{
				clickTd(ev,o.get(0));
			} catch (e){
				$(this).removeClass('selected');
				alert(e);
			}
		}
	} catch (e){
		alert(e);
	}
	$('input[NAME=num_nights]',$(this).closest('form')).val($('td.selected',t).size());
});

$('.arr-prev').live('click',function(){
	$(this).closest('table').parent().load('/ajax.php?acc=calendarweek&prev=1&hotels_id='+$('input[name=HOTELS_ID]',$(this).closest('form')).val()+'&rates_id='+$('input[name=RATES_ID]',$(this).closest('form')).val());
});
$('.arr-next').live('click',function(){
	$(this).closest('table').parent().load('/ajax.php?acc=calendarweek&next=1&hotels_id='+$('input[name=HOTELS_ID]',$(this).closest('form')).val()+'&rates_id='+$('input[name=RATES_ID]',$(this).closest('form')).val());
});

$('.rate-description').live('click',function(e){
	e.stopPropagation();
	$('.rate-description-container').hide();
	$(this).next().show();
})
$('.rate-description-container').live('click',function(e){
	e.stopPropagation();
})

$('.tr-rate').live('click',function(e){
	if(e.isPropagationStopped()) return;
	var key = $('span.key', $(this)).html();
	var hotels_id = $('span.hotels-id', $(this)).html();
	$('.content').load('/ajax.php?acc=rate&id='+key+'&hotels_id='+hotels_id);
})

$('.rate-description-container .close').live('click',function(e){
	$(this.parentNode).hide();
})

$('.room-description').live('click',function(e){
	e.stopPropagation();
	$('.room-popup').hide();
	$('.room-popup .room-description-trigger',$(this).closest('tr')).trigger('click');
	$('.room-popup',$(this).closest('tr')).show();
})


$('.room-image').live('click',function(e){
	e.stopPropagation();
	$('.room-popup').hide();
	$('.room-popup .room-image-trigger',$(this).closest('tr')).trigger('click');
	$('.room-popup',$(this).closest('tr')).show();
})

$('.room-popup .room-image-trigger').live('click',function(e){
	$('.selected',$(this).parent()).removeClass('selected');
	$(this).addClass('selected');
	$('.room-description-container',$(this).parent().next()).hide();
	$('.room-image-container',$(this).parent().next()).show();
})
$('.room-popup .room-description-trigger').live('click',function(e){
	$('.selected',$(this).parent()).removeClass('selected');
	$(this).addClass('selected');
	$('.room-image-container',$(this).parent().next()).hide();
	$('.room-description-container',$(this).parent().next()).show();
})

$('.room-popup .close').live('click',function(e){
	$(this.parentNode).hide();
})


