$(document).ready(function() 
{
	$("#booking_form").submit(function(){booking();return false;})

	function booking()
	{
		$("#loading").show();
		$("#booking").hide();
		$.ajax({
				   type: "GET",
				   url: 'ajax/booking.php',
				   data: 'data='+$("#booking_form input").serialize(), 
				   success: function(msg)
				   {
						$("#loading").hide();
						$("#booking").show();
						
						$("#booking").html(msg);
					 	$("#booking_form").unbind();
					 	$("#booking_form").submit(function(){booking();return false;})
				   		$("#choose_city").keyup(function(){chooseCity();})
				   		$("#choose_termin input").click(function(){highlightTermin(this);})
				   		$("#choose_termin td").mouseover(function(){highlight(this);})
						
				   		$("#back").click(function(){back();return false;})
				   }
			   });	
	}
	
	function back()
	{
		$("#loading").show();
		$("#booking").hide();
		$.ajax({
				   type: "GET",
				   url: 'ajax/booking.php?move=back',
				   data: 'data='+$("#booking_form input").serialize(), 
				   success: function(msg)
				   {
						$("#loading").hide();
						$("#booking").show();
						
						$("#booking").html(msg);
					 	$("#booking_form").unbind();
					 	$("#booking_form").submit(function(){booking();return false;})
				   		$("#choose_city").keyup(function(){chooseCity();})
						$("#choose_termin input").click(function(){highlightTermin(this);})
				   		$("#choose_termin td").mouseover(function(){highlight(this);})
						
						$("#back").click(function(){back();return false;})
				   }
			   });	
	}
	
	function chooseCity()
	{
		$.ajax({
				   type: "GET",
				   url: 'ajax/chooseCity.php',
				   data: 'city='+encodeURI($("#choose_city").val()), 
				   success: function(msg)
				   {
						$("#choose_city_response").html(msg);
						$("#service_list li").click(function(){$("#city").val($(this).attr('id'))});
						$("#service_list li").click(function(){setSelected(this)});
						
						
						
						$("#service_list li").mouseover(function()
							{
								if($(this).attr('class') != 'selected')
								{
									$(this).attr('class','hover');
								}
							});

						$("#service_list li").mouseout(function()
							{
								if($(this).attr('class') != 'selected')
								{
									$(this).attr('class','');
								}
							});


				   }
			   });	
	}
	
	actual = '';
	function setSelected(elm)
	{
		//$('#'+actual).css('background','white');
		//$('#'+actual).css('color','#007EC1');
		$('#'+actual).attr('class','')
		
		//$(elm).css('background','#F57C1F');
		//$(elm).css('color','white');
		
		$(elm).attr('class','selected');

		actual = $(elm).attr('id');
		booking();
	}
	
	function highlight(td)
	{
		source = $(td);
		

		$('#choose_termin .highlight').attr('class', '');
		
		x_position = source.prevAll().length;
		y_position = source.parent().prevAll().length;
		
		rows = jQuery.makeArray($('#choose_termin tr'));
		
		
		for (i = 0; i < rows.length; i++) {
			cells = $(rows[i]).find('td');
			acells = jQuery.makeArray(cells);
			
			//alert(acells);
			if ('selected' != $(acells[x_position]).attr('class') && 'semiselected' != $(acells[x_position]).attr('class')) 
			{
				$(acells[x_position]).attr('class', 'highlight');
			}
		}
		
		//source.parent().attr('class','highlight');
		if ('selected' != source.parent().attr('class') && 'semiselected' != source.parent().attr('class')) 
		{

			source.parent().attr('class', 'highlight');
		}
		//source.parent().attr('class', 'highlight');

		
	}
	
	function highlightTermin(inp)
	{
		$('#choose_termin .selected').attr('class','');
		$('#choose_termin .semiselected').attr('class','');
		
		source = $(inp);

		x_position = source.parent().prevAll().length;
		y_position = source.parent().parent().prevAll().length;

		rows = jQuery.makeArray($('#choose_termin tr'));

		
		//alert('x='+x_position+'y='+y_position+'rows='+rows);
		
		for(i=0;i<rows.length;i++)
		{
			cells = $(rows[i]).find('td');
			acells = jQuery.makeArray(cells);
			
			//alert(acells);
			$(acells[x_position]).attr('class','semiselected');
		}
		
		source.parent().attr('class','selected');
		source.parent().parent().attr('class','semiselected');
		
	}
});