

	function calendarRange()
	{
		this.elem;
		this.start;
		this.end;
		this.selectedCells = {};
		
		this.select = function(time, autorun)
		{
			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 || 1)
					{
						try {
							roomTypeFrame.init('divRoomTypes', {start: this.start, end: this.end});
						} catch (e){}
					}
				}
			}
			// 2 click, select a range
			else if(this.start && !this.end)
			{
				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)
					{
						// 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('selected');
				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('selected');
				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;
			});
		};
			
	}; 