$(function(){
	var value = "диван Палермо...";$("#q").val(value);
	$("#q").focus(function(){$(this).removeClass('header_search_d');$(this).addClass('header_search_a');if ($(this).val()== value)$(this).val('');}).blur(function(){$(this).removeClass('header_search_a');$(this).addClass('header_search_d');if ($(this).val()==''){$(this).val(value);}});
	$('#q').keyup(function(){
		var q = $(this).val();
		if (q.match(/[А-Яа-я0-9]+/g)){
			if ($('#complete').length > 0) {
				$('#complete').remove();
				$('document').unbind('click');
			}	
			var link = 'complete/?q='+q+'&r='+Math.floor(Math.random()*10000000);
			$.getJSON('/sea'+'rch/'+link, function(json){
				$('#compl').remove();
				var ul = $('<ul id="compl"/>');
				var li = '';
				ul.attr('id', 'complete');
				$.each(json, function(i, item){
					li +='<li><a href="'+item['link']+'">'+item['key']+'</a></li>';
				});
				ul.append($(li));
				$('body').append(ul);
				$(document).bind('click', removeComplete);
			});
		}
	});
});

function removeComplete(){
	$('document').unbind('click', removeComplete);
	if ($('#complete').length > 0) {
		$('#complete').remove();
	}
}

function loadProperty(href){
	if ($('#properties').length > 0){
		$.getJSON(href, function(json){$('body').data('json', json);});	
	}
}

function propGoods(){
	$('.prop_arrow').toggle(
		function(){
			if ($('#prop_open').length > 0)
				$('#prop_open').remove();			
			var ul = $('<ul id="prop_open"/>');
			var li = '';
			
			var prn = $(this).parent();
			var id = $(prn).attr('id');
			var input = $('#'+id+' input:first');
			var set_val_input = parseInt($('#new_'+input.attr('id')).val());
			var json = $('body').data('json');

			li += '<li id="">-------</li>';
			var cut = $('body').data('cut_json');
			if (cut != undefined){
				cut = cut[input.attr('id')];
			}
			$.each(json[input.attr('id')].items, function(i, item){
				if (i == set_val_input) {
					li += '<li class="selected" id="' + i + '">' + item + '</li>';
				} else {					
					if (cut != undefined){
						var inc = false;
						$.each(cut.items, function(j, it){
							if (i == it){
								li += '<li id="' + i + '">' + item + '</li>';
								inc = true;	
							}
						});
						if (inc == false){
							li += '<li id="' + i + '" class="disable" style="color:#ccc">' + item + '</li>';
						}
					}else{
						li += '<li id="' + i + '">' + item + '</li>';						
					}
				}
			});
			ul.append($(li));
			$(this).parent().append(ul);
			$('#prop_open > li[class!=disable]')
			.mouseover(function(){
				$(this).addClass('over');
			})
			.mouseout(function(){
				$(this).removeClass('over');
			})
			.click(function(){
				var prn = $(this).parent().parent();
				var id = $(prn).attr('id');
				var input = $('#'+id+' input:first');
				$('#new_'+input.attr('id')).val($(this).attr('id'));				
				input.val($(this).html());
				$(this).parent().remove();
				checkProperty();
			});
			$(document).bind('click', removeProperty);
			return false;
		},
		function(){
			removeProperty();
			return false;
		}
	);
}

function checkProperty(){
	var link = $('#js_form').attr('action');
	//alert($('.rowjs[value!=]').length);
	var query = {};
	$.each($('.rowjs[value!=]'), function(i,item){
		query[$(item).attr('name')] = $(item).val();
	});
	$.post(link+'property/', query, function(json){
		$('body').data('cut_json', json);
	},'json');
}

function removeProperty(){
	$('#prop_open').remove();
	$('document').unbind('click');	
}

function changeCity(id){
	if ($(id).val() != ''){
		metroCity($(id).val());
	}
	$(id).change(function(){
		var slug = $(this).val();
		if (slug !=''){
			metroCity(slug);
		}else{
			
		}
	});
}

function metroCity(slug){
	$.getJSON('/address/'+slug+'/metro/', function (json){
		if (json.length > 0) {
			$('#id_metro').parent().show();
			var option = '';
			$.each(json, function(i, item){
				option+='<option value="'+item[0]+'">'+item[1]+'</option>';
			});
			$('#id_metro').append($(option));
		}else{
			$('#id_metro').parent().hide();
		}
	});
}

