

	function calendarRange()
	{
		this.elem;
		this.start;
		this.end;
		this.selectedCells = {};
		
		this.select = function(time, autorun, forceRooms)
		{
			time = parseInt(time);
         
			// 1 click, select one day
			if((!this.start && !this.end) || (this.start && this.end))
			{
				this.clearSelection();
				
				// try to select a cell
				if(this.selectCell(time))
				{
					this.setStart(time);
					
					// load the room types
					if(!autorun || forceRooms)
					{
						try {

                     $('.calendar .info').show();

                     // set dates
                     var startDate = new Date(this.start * 1000);
                     $('#spnDates').html(startDate.getDate() + '.' + (startDate.getMonth()+1) + '.' + startDate.getFullYear());

                     // set nights
                     $('#spnNights').html(1);

							if(!autorun || forceRooms)
							{
								roomTypeFrame.init('divRoomTypes', {start: this.start, end: this.end});
							}
						} catch (e){}
					}
				}
			}
			// 2 click, select a range
			else if(this.start && !this.end && this.start != time)
			{
				if(time < this.start)
				{
					var start = time;
					var end = this.start;
				}
				else
				{
					var start = this.start;
					var end = time;
				}
				
				if(this.selectRange(start, end))
				{
					this.setEnd(end);
					this.setStart(start);

					if(!autorun || forceRooms)
					{
                  $('.calendar .info').show();

                  // set dates
                  var startDate = new Date(this.start * 1000);
                  var endDate = new Date(this.end * 1000);
                  var html = startDate.getDate() + '.' + (startDate.getMonth()+1) + '.' + startDate.getFullYear();
                  html += ' &mdash; ';
                  html += endDate.getDate() + '.' + (endDate.getMonth()+1) + '.' + endDate.getFullYear();
                  $('#spnDates').html(html);

                  // set nights
                  $('#spnNights').html( Math.ceil( (this.end - this.start) / 24 / 3600 ) );

						// load the room types
						try {
							roomTypeFrame.init('divRoomTypes', {start: this.start, end: this.end});
						} catch (e){}
					}
				}
			}

			
		},
		
		this.setStart = function(start)
		{
			this.start = start;
			if(this.elem.attr('id').indexOf('_') != -1)
			{
				var data = this.elem.attr('id').split('_'); 
				var id = '#hdStart_' + data[1];
			}
			else
			{
				var id = '#hdStart';
			}
			$(id).val(this.start).trigger('change');
		},
		
		this.setEnd = function(end)
		{
			this.end = end;
			if(this.elem.attr('id').indexOf('_') != -1)
			{
				var data = this.elem.attr('id').split('_'); 
				var id = '#hdEnd_' + data[1];
			}
			else
			{
				var id = '#hdEnd';
			}
			$(id).val(this.end).trigger('change');			
		}
		
//		this.isSelected = function(time)
//		{
//			return (this.selectedCells[time]);
//		},
		
		this.getCell = function(time)
		{
			return $('#' + this.elem.attr('id') + ' #tdDay_'+time);
		},
		
		this.selectCell = function(time)
		{
			var cell = this.getCell(time);
			if(!cell.hasClass('unavailable'))
			{
				cell.addClass('sel');
				this.selectedCells[time] = cell;
				return true
			}
			return false;
		},
		
		this.clearSelection = function()
		{
			this.setStart('');
			this.setEnd('');
			
			for(var i in this.selectedCells)
			{
				this.selectedCells[i].removeClass('sel');
				delete this.selectedCells[i];
			}
		},
				
		this.selectRange = function(start, end)
		{
			var date = new Date();
			date.setTime(start * 1000);
						
			for(date; date.getTime() <= end*1000; date.setDate(date.getDate() + 1))
			{
				// try to select the cell
				var cell = this.getCell(date.getTime() / 1000);
				if(cell.hasClass('unavailable'))
//				if(!this.selectCell(date.getTime() / 1000))
				{
					this.clearSelection();
					break;
					return false;
				}
			}
			
			this.selectCell(start);
			this.selectCell(end);
			
			return 1;
		}
	};
	

	function calendar()
	{
		this.elem;
		this.range = new calendarRange();

		this.init = function(id)
		{
			this.elem = $('#' + id);
			this.range.elem = this.elem;

			var i = 0;
			var This = this;
			$('#' + this.elem.attr('id') + ' .calendarDay').click(function()
			{
				This.range.select( this.id.replace('tdDay_', ''), 0 );
				return false;
			});
		};
			
	}; 
