function roomTypeUi()
{
	this.count = {};
    
	this.init = function(count)
	{
		this.count = {};
		var This = this;

		$('.roomTypeInc').click(function(){
			This.addCount(this.rel, 1);
			This.updateTotalPrice();
			return false;
		});
		$('.roomTypeDec').click(function(){
			This.addCount(this.rel, -1);
			This.updateTotalPrice();
			return false;
		});
		
		$('.book_link').click(function(){
         This.bookRoom(this.rel);
			return false;
		});
      
		$('#addAnotherRoom').click(function(){
			$('.rooms').show();
			$('#divBookedRoomBlock').hide();
			$('.cancel_link').hide();
			$('.book_link').show();
			$(this).hide();
			return false;
		});
        
        for(var id in count)
		{   
			for(var i=0; i < count[id]; i++)
			{
                $('#divRoomType_' + id).find('.book_link').trigger('click');
//				this.bookRoom(id);
			}
            
//			this.setCount(id, count[id]);
		}
      
      
//		if(count)
//		{
//			for(var id in count)
//			{
//				this.setCount(id, count[id]);
//			}
//		}
	};


	this.bookRoom = function(id)
	{
		var This = this;

		$('#divRoomType_'+id).clone().prependTo('#divBookedRoomBlock').show();
		$('#divBookedRoomBlock').children().addClass('booked_room');
		numbered(id);
		$('.cancel_link').click(function(){
			$(this).parent().parent().parent().removeClass('booked_room').hide();
			This.cancelRoom(this.rel);
			var a = $(this);
			a.hide().parents('.special_link').find('.book_link').show();
			return false;
		});
		$('.roomTypeDec').click(function(){
			This.addCount(this.rel, -1);
			This.updateTotalPrice();
			$(this).parent().parent().parent().remove();
			numbered(this.rel);
			return false;
		});


		var sum = 0;
		//          $('.booked_room .occupancy').each(function() {
		//              sum += ($(this).val()*1);
		//          });
		$('#guestTotal').show().find('span').html(sum);


		This.chooseRoom(id);
		$('.cancel_link').show();
		$('.book_link').hide();
		$('#addAnotherRoom').show();
		//            $('#divCalendar').hide().prev().hide();
	}
    
	this.addCount = function(id, add)
	{
		this.setCount(id, this.getCount(id) + add);
	};
    
	this.setCount = function(id, count)
	{
		if(count < 0 || !count)
		{
			count = 0;
		}
  
		var max = parseInt($('#spnTypeAvailableCount_' + id).html());
		if(count > max)
		{
			count = max;
		}
		if(count > 0)
		{
			$('#divPersonalData').show();
			$('#aRoomTypeDec_' + id).show();
		}
		else
		{
			$('#aRoomTypeDec_' + id).hide();
		}
        
		$('#tdRoomTypeCount_' + id).html(count);
		$('.count_' + id).val(count);
//		$('#hdRoomTypeCount_' + id).val(count);
		this.count[id] = count;
	};
    
	this.getCount = function(id)
	{
		return parseInt((this.count[id]) ? this.count[id] : 0);
	};
    
    
	this.getPrice = function(id)
	{
		return parseInt($('#spnRoomTypePrice_'+id).html());
	}
    
    
	this.getTotalPrice = function()
	{
		var total = 0;
		for(var i in this.count)
		{
			total += this.getPrice(i) * this.getCount(i);
		};
		return total;
	};
    
    
	this.updateTotalPrice = function()
	{
		$('#spnTotalPrice').html(this.getTotalPrice());
		$('#tdTotalPrice').show();
	};
    
	this.chooseRoom = function(id)
	{
		$('#hdChosenRoom').val(id);
		$('#divBookedRoomBlock').show();
		$('.rooms').each(function()
		{
			//if($(this).attr('id') != 'divRoomType_'+id)
			//{
			//  $(this).hide();
			//}
			elem = $(this);
			if (!elem.hasClass ("booked_room")) {
				$(this).hide();
			}
		})
		$('#divPersonalData').show();
	}
    
	numbered = function(id)
	{
		var i=0;
		$(".block_number").each(function() {
			$(this).html();
		});
		$('#divBookedRoomBlock').children().each(function() {
			elem = $(this);
			if(elem.is('#divRoomType_'+id) )
			{
				i++;
				$(".block_number", elem).html("#"+i);
			}
		});
       
	}
    
	this.cancelRoom = function()
	{
		$('#hdChosenRoom').val('');
		//$('.rooms').show();
         
		if ($('.booked_room').length == 0 )
		{
			$('#divPersonalData').hide();
			$('#divCalendar').show().prev().show();
		}
         
	}
    
    
    
};

