$(document).ready(function(){
	// Text
	// Массив начальных текстов
	inputStartText = [];
	$("input:text").focus(function(){
		var inputName = this.name;
		if(inputStartText[inputName] === undefined) {
			inputStartText[inputName] = this.value;
		}
		if(this.value == inputStartText[inputName]) {
			this.value = "";
		}
	}).blur(function(){
		if(this.value == '') {
			var inputName = this.name;
			this.value = inputStartText[inputName] === undefined?"":inputStartText[inputName];
		}
	});
        form = false;
	$("#moreparametrs").click(
		function(){
			if($("#dopparametrs").is(":hidden")){
				$("#dopparametrs").show("blind",function(){
					$("#moreparametrs").text("Свернуть дополнительные параметры");
					$("#icon_more").attr("src","/imgs/icon_umore.png");
				});
			}
			else { 
				$("#dopparametrs").hide("blind",function(){
					$("#moreparametrs").text("Раскрыть дополнительные параметры");
					$("#icon_more").attr("src","/imgs/icon_more.png");
				});
				
			}
		}
	);

	$("#search form input:checkbox[name=vklpodm]").click(function(){
		if(this.checked){
			var rad = $("#search form input:radio[name=podm][value=msk]").attr("rad");
			$("#search .radioImg[radimg="+rad+"]").click();
		}
			
	});
	$("#search form input:radio[name=podm]").click(function(){
		if(this.value=='podmsk'){
			$("#choiceCity").fadeIn();
			$(".selraion, #dometro_hidden").hide();
			var chbox = $("#search form input:checkbox:checked[name=vklpodm]").attr("chbox");
			$("#search .checkBoxImg[chimg="+chbox+"]").click();
		} else {
			$("#choiceCity").hide();
			$(".selraion, #dometro_hidden").fadeIn();
		}
	});
	$("#search form input:radio[name=xod]").click(function(){
		if(this.value=='transport'){
			$("#slider_dometro_wrap").fadeIn();
		} else {
			$("#slider_dometro_wrap").fadeOut();
		}
	});

	$("#city").autocomplete({
		source: function(request,response){
			request.add='city';
			$.ajax({
				url: "/",
				dataType: "json",
				data: request,
				success: function(data) {
					response(data);
				}
			});

		},
		minLength: 2,
		select: function(event, ui) {
		    if(ui.item){
		    	addCity(ui.item.id);
		    }
		},
		close: function(event,ui){
			//$(this).val('');
		}

	 }).bind("keypress blur",function(e){
		 if((e.keyCode == 13 || e.originalEvent.type=='blur') && this.value != '') {
			 checkCity(this.value);
		 }
	 });
	function checkCity(name){
		$.ajax({
			url		:"/check/city",
			dataType:"json",
			data	:"check=array&name="+encodeURI(name),
			success	: function(m){
				if(m && m.id){
					$("#city").val(m.name);
					addCity(m.id);
				} else {
					$("#city").val("");
				}
				$("#city").autocomplete("close");
			}
		
		});
	}
	
	function addCity(sValue){
			var val = $("#city").val();
			
			var maxSize = 5;
			if (val != ''){
				var $select = $("#raionselect");
				var optsCount = $select.find("option").length;		
				if(optsCount == 0) {
					$("#cityArr").show();
					optsCount = 1;
				} else {
					$select.find("option[value="+sValue+"]").remove();
				}
				$("<option></option>").attr({
					value:sValue
				}).html(val).appendTo($("#raionselect"));
				$("#city").val('');
				optsCount++;
				if(optsCount <= 5){ 
					$select.attr("size",optsCount);
				}
			};
			
			board.send();
			$("#city").val("");
			$("#city").click();
	};
	
	// Отписаться от рассылки
	$("#tel_off").click(function(){
		var phone = $("#input_tel").val();
		$.ajax({
			url:"/rent",
			dataType: "json",
			data:"action=telOff&phone="+phone,
			success:function(m){
				if(m && m.ok == 1){
					$(".plashka .area").html(m.m);
				} else {
					$(".area .alert").html("<br>"+m.m);
				}
			}
		});
	});
	
	$("#deleteCity").click(function(){
		$("#raionselect").find("option[selected]").remove();
		var optsCount = $("#raionselect").find("option").length; 
		if(optsCount == 0) {
			$("#cityArr").hide();
		} else {
			if(optsCount > 1 && optsCount < 5){
				$("#raionselect").attr("size", optsCount);
			}
		}
		board.send();
	});
	
	
	
	$("#search form").submit(function(){
		return false;
	});
	$("#tel_1_yes").click(function(){
		$("#phone").val($("#save_tel").val());
		$("#SubmitKey").click();
	});
	$("#SubmitKey").bind("click", function(){
		board.send("/index/save");
		return false;
    });
	
});
