var flatID=1;
var fl_count=0;
var position=0;
var url=location.host;


function flats_search()
{
	var price = document.getElementById("select_price").value;
	var class2 = document.getElementById("select_class").value;
	var metro = document.getElementById("select_metro").value;
	var district = document.getElementById("select_district").value;
	var rooms_count = document.getElementById("select_rooms_count").value;
	jQuery.getJSON('http://ajax.flatlux.info/index.php?page=search&select_class='+class2+'&select_metro='+metro+'&select_district='+district+'&select_price='+price+'&rooms_count='+rooms_count+'&lng='+lng+'&callback=?', {}, function(searching)
	{
		fl_count=searching.search_count;
		$("#leftmore").hide();
		if(fl_count<4)
		{
			$("#rightmore").hide();
		}else
		{
			$("#rightmore").show();
		}
		position=0;
		
		$("#widthbox").animate({left: position}, '1500' );
		$('#searched_flats').html(searching.search_from_flats);
		$("#lenta_kvartir").show();
		
	});

}

function booking_tobook()
{
	var sId = flatID;
	if(document.getElementById('contact_info'))
	{
		var ur2 ="";
		ur2="&from="+document.getElementById("from").value;
		ur2=ur2+"&first_name="+document.getElementById("first_name").value;
		ur2=ur2+"&last_name="+document.getElementById("last_name").value;
		ur2=ur2+"&email="+document.getElementById("email").value;
		ur2=ur2+"&country="+document.getElementById("country").value;
		ur2=ur2+"&city="+document.getElementById("city").value;
		ur2=ur2+"&tel1="+document.getElementById("tel1").value;
		ur2=ur2+"&tel2="+document.getElementById("tel2").value;
		ur2=ur2+"&security_code="+document.getElementById("security_code").value;
		jQuery.getJSON('http://ajax.flatlux.info/index.php?page=to_check&flatID='+sId+ur2+'&lng='+lng+'&callback=?', {}, function(booking)
		{
			$('#error').html('');
			if(booking.error!='')
			{
				$('#error').append(booking.error);
			}else
			{
				var rdate = document.getElementById("f_date_c").value;
				var r2date = document.getElementById("f_date_c2").value;
				if(document.getElementById('contact_info'))
				{
					var ur='';
					for (i = 1; i <= 10; i++)
					{
						if(document.getElementById('s'+i))
						{
         							if (document.getElementById('s'+i).checked)
							{
								ur=ur+'&'+document.getElementById('s'+i).value+'=1';
								if(document.getElementById('s'+i).value=="internet")
								{
									ur=ur+'&internet_days_counts='+document.getElementById('internet_days_counts').value;
								}
							}
						}
					}
					var ur2 ="";
					ur2="&from="+document.getElementById("from").value;
					ur2=ur2+"&first_name="+document.getElementById("first_name").value;
					ur2=ur2+"&last_name="+document.getElementById("last_name").value;
					ur2=ur2+"&email="+document.getElementById("email").value;
					ur2=ur2+"&country="+document.getElementById("country").value;
					ur2=ur2+"&city="+document.getElementById("city").value;
					ur2=ur2+"&tel1="+document.getElementById("tel1").value;
					ur2=ur2+"&tel2="+document.getElementById("tel2").value;
					ur2=ur2+"&security_code="+document.getElementById("security_code").value;
					jQuery.getJSON('http://ajax.flatlux.info/index.php?page=booking&submit=to_book&flatID='+sId+ur2+'&r_date='+rdate+'&r2_date='+r2date+ur+'&url='+url+'&lng='+lng+'&callback=?', {}, function(booking)
					{
						$("#flat_booking").hide();
						$("#booking_end").show('');
						scrollBy(0,-400);
					});
				}
			}
		});
	}
}


function booking_check()
{
	var sId = flatID;
	if(document.getElementById('contact_info'))
	{
		var ur2 ="";
		ur2="&from="+document.getElementById("from").value;
		ur2=ur2+"&first_name="+document.getElementById("first_name").value;
		ur2=ur2+"&last_name="+document.getElementById("last_name").value;
		ur2=ur2+"&email="+document.getElementById("email").value;
		ur2=ur2+"&country="+document.getElementById("country").value;
		ur2=ur2+"&city="+document.getElementById("city").value;
		ur2=ur2+"&tel1="+document.getElementById("tel1").value;
		ur2=ur2+"&tel2="+document.getElementById("tel2").value;
		ur2=ur2+"&security_code="+document.getElementById("security_code").value;
		jQuery.getJSON('http://ajax.flatlux.info/index.php?page=to_check&flatID='+sId+ur2+'&lng='+lng+'&callback=?', {}, function(booking)
		{
			$('#error').html('');
			if(booking.error!='')
			$('#error').append(booking.error);
		});
	}
}


function booking_refresh()
{
	var rdate = document.getElementById("f_date_c").value;
	var r2date = document.getElementById("f_date_c2").value;
	var sId = flatID;
	

		var ur='';
		for (i = 1; i <= 10; i++)
		{
			if(document.getElementById('s'+i))
			{
         				if (document.getElementById('s'+i).checked)
				{
					ur=ur+'&'+document.getElementById('s'+i).value+'=1';
					if(document.getElementById('s'+i).value=="internet")
					{
						ur=ur+'&internet_days_counts='+document.getElementById('internet_days_counts').value;
					}
				}
			}
		}

	jQuery.getJSON('http://ajax.flatlux.info/index.php?page=booking&flatID='+sId+'&r_date='+rdate+'&r2_date='+r2date+''+ur+'&lng='+lng+'&callback=?', {}, function(booking)
	{
		$('#table_flatID').html('');
		$('#table_flatID').append(booking.table_flatID);
		$('#table_flat_name').html('');
		$('#table_flat_name').append(booking.table_flat_name);
		$('input#f_date_c').attr("value",booking.r_date);
		$('input#f_date_c2').attr("value",booking.r2_date);
		$('#day_price').html('');
		$('#day_price').append(booking.new_day_price);
		$('.cur').html('');
		$('.cur').append(booking.cur);
		$('#booking_table').html('');
		$('#booking_table').append(booking.booking_table);
		$('#total_price').html('');
		$('#total_price').append(booking.total_price);
		$('#all_services').html('');
		$('#all_services').append(booking.all_services);
		$('#full_price').html('');
		$('#full_price').append(booking.full_price);
	});
}




jQuery(document).ready(function()
{


	$('#leftmore').click(function()
	{
		var wid=fl_count*165-530;
		position=position+325;
		$("#widthbox").animate({left: "+=325"},'1500');
		if(position>-1){$("#leftmore").hide();}else{$("#leftmore").show();}
		if(position<-wid){$("#rightmore").hide();}else{$("#rightmore").show();}
	});
	$('#rightmore').click(function()
	{
		var wid=fl_count*165-530;
		position=position-325;
		$("#widthbox").animate({left: "-=325"}, '1500' );
		if(position<-wid){$("#rightmore").hide();}else{$("#rightmore").show();}
		if(position>-1){$("#leftmore").hide();}else{$("#leftmore").show();}
	});

	jQuery('#search').click(function()
	{
		var sId = document.getElementById("flatID").value;
		flatID=sId;
		$("#flat_booking").hide();
		$("#booking_end").hide();
		$("#show_flat").hide();
		jQuery.getJSON('http://ajax.flatlux.info/index.php?page=show_flat&flatID='+flatID+'&lng='+lng+'&callback=?', {}, function(flat)
		{
			$('#flat_id').html('');
			$('#flat_id').append(flat.id);
			$('#flatID').html('');
			$('#flatID').append(flat.id);
			$('#flat_name').html('');
			$('#flat_name').append(' '+flat.name);
			$('#flat_price').html('');
			$('#flat_price').append(flat.price);
			$('#description').html('flat.description');
			$('#reduction').html('');
			$('#reduction').append(flat.reduction);
			$('#goods').html('');
			$('#goods').append(flat.goods);
			$('#gallery').html('');
			$('#gallery').append(flat.gallery);
			$('img#photo').attr("src",'http://www.flatlux.kiev.ua/'+flat.photo_main);       
			$('a#a_photo').attr("href",'http://www.flatlux.kiev.ua/'+flat.photo_main);
			$('#video_div').hide('');        
			$('img#photo').show('');
			if(flat.video=='0')
			{
				$('#video').hide(''); 
			}else
			{
				$('#video').show(''); 
			}
		});   
		$("#show_flat").show();             
	});

	jQuery('#map').click(function()
	{
		jQuery.getJSON('http://ajax.flatlux.info/index.php?page=show_flat&flatID='+flatID+'&lng='+lng+'&callback=?', {}, function(flat)
		{
			$('img#photo').attr("src",'http://www.flatlux.kiev.ua/'+flat.photo_map+'_'+flat.lng+'.gif');
			$('a#a_photo').attr("href",'http://www.flatlux.kiev.ua/'+flat.photo_map+'_'+flat.lng+'.gif');
			$('#video_div').hide('');   
			$('img#photo').show('');
		});
	});

	$('.gal').live('click', function()
	{
		var phid = $(this).text();
		jQuery.getJSON('http://ajax.flatlux.info/index.php?page=show_photo&flatID='+flatID+'&photoID='+phid+'&lng='+lng+'&callback=?', {}, function(photo)
		{
			$('img#photo').attr("src",'http://www.flatlux.kiev.ua/'+photo.photo_url);
			$('a#a_photo').attr("href",'http://www.flatlux.kiev.ua/'+photo.photo_url);
			$('#video_div').hide('');   
			$('img#photo').show('');
		});
	});

	jQuery('#book_button').click(function()
	{
		$("#show_flat").hide();
		$("#flat_booking").show();   
		$("#booking_end").hide();   
         		booking_refresh();
	});

	$('.service').live('click', function()
	{
		booking_refresh();
	});

	$('.contact').live('change', function()
	{
		booking_check();
	});

	$('#internet_days_counts').live('change', function()
	{
		booking_refresh();
	});

	$('#to_book').click(function()
	{
		booking_tobook();
	});

	$('#show_result').click(function()
	{
         		flats_search();
	});

	$('.search_select').change(function()
	{
         		flats_search();
	});

	$('#video').click(function()
	{
         		jQuery.getJSON('http://ajax.flatlux.info/index.php?page=video&flatID='+flatID+'&lng='+lng+'&callback=?', {}, function(flat)
		{
			$('img#photo').attr("src",'nul');
			$('img#photo').hide('');      
			$('#video_div').html('');
			$('#video_div').append(flat.video);
			$('#video_div').show('');
		});
	});

	$('.searched_flat').live('click', function()
	{
		var sId = this.id;
		$('.searched_flat').each(function()
		{
				this.style.border='0px solid';
		});  
		this.style.border='2px solid';
		flatID=sId;
		$("#flat_booking").hide();
		$("#booking_end").hide();
		$("#show_flat").hide();
		jQuery.getJSON('http://ajax.flatlux.info/index.php?page=show_flat&flatID='+flatID+'&lng='+lng+'&callback=?', {}, function(flat)
		{
			$('#flat_id').html('');
			$('#flat_id').append(flat.id);
			$('#flat_name').html('');
			$('#flat_name').append(' '+flat.name);
			$('#flat_price').html('');
			$('#flat_price').append(flat.price);
			$('#description').html('');
			$('#description').append(flat.description);
			$('#reduction').html('');
			$('#reduction').append(flat.reduction);
			$('#goods').html('');
			$('#goods').append(flat.goods);
			$('#gallery').html('');
			$('#gallery').append(flat.gallery);
			$('img#photo').attr("src",'http://www.flatlux.kiev.ua/'+flat.photo_main);       
			$('a#a_photo').attr("href",'http://www.flatlux.kiev.ua/'+flat.photo_main);
			$('#video_div').hide('');   
			$('img#photo').show('');   
			if(flat.video=='0')
			{
				$('#video').hide(''); 
			}else
			{
				$('#video').show(''); 
			}
		});   
		$("#show_flat").show();             
	});

$('#flatID').click(function()
{
	{
		document.getElementById("flatID").value="";
	}
});
	
	

	flatID=sId;
	$("#booking_end").hide()  
	$("#flat_booking").show();   
	$("#booking_end").hide();   
         	booking_refresh();


});

