$(function(){
	$('#iAmOwner').change(function(){
		$('#forOwners').slideToggle(100);
	});
	
	$('#placeStreet').autocomplete('/addplace/getstreets/',{
		cacheLength: 500,
		delay:1,
		minChars:0,
		matchSubset:1,
		matchContains:1,
		cacheLength:10,
		maxItemsToShow:10,
		formatItem: streetLiFormat,
		onItemSelect: onStreetSelect
	});
	
	 $("#addPlaceForm .input_text").each(function (i){
		 if ( $(this).attr('value')=='' )
		 {
			 $(this).prev("em").show();
		 }
	 });
		 
	 $("#addPlaceForm em").click(function(){
		 $(this).hide();
		 $(this).next().focus();
	 });
	 
	 $("#addPlaceForm .input_text").focus(function(){
		 $(this).prev().hide();
	 });
	 
	 $("#addPlaceForm .input_text").blur(function(){
		 if ( $(this).attr('value')=='' )
		 {
			 $(this).prev().show();
		 };
	 }); 
	
});

function changeSelect()
{
	$('select.catSelect').val('').hide();
	$('#cat_' + $('#parentCategory').val()).show();
}

function addCategory()
{
	var category = $('#cat_' + $('#parentCategory').val()).val();

	if(parseInt(category) > 0 && !($('#selected' + category).length))
	{
		var html;
		html = '<span id="selected' + category + '">' + $('#cat_' + $('#parentCategory').val() + ' option:selected').text(); 
		html += ' <img src="/images/smallIcons/delete.png" style="cursor: pointer;" onclick="removeCategory(' + category + ')">';
		html += '<input type="hidden" name="placeSphere[]" value="' + category + '"></span>';
		$('#catResult div').after(html);
	}
}

function removeCategory(idCategory)
{
	$('#selected' + idCategory).remove();
}

// Форматирует элементы списка-подсказки при выборе улицы, выделяя их полужирным
function streetLiFormat(row, i, num)
{
	pattern = new RegExp("(" + $('#placeStreet').val() + ")", "i");
	return row[0].replace(pattern, '<b>$1</b>');
}

// Сохраняет в скрытый инпут id улицы при её выборе
function onStreetSelect(li)
{
	if (li.extra[0] != null) $("#placeStreetVal").val(li.extra[0]);
}

function addPlace()
{
	var categoriesArray = new Array;
	$('#catResult input').each(function (i){
		categoriesArray.push($(this).val());
	});

	var metro;
	if($('#placeMetro').length)
	{
		metro = $('#placeMetro').val();
	}
	else
	{
		metro = '';
	}
	
	showLoader('#addPlaceForm .error', 'append');

	$.post('/addplace/add/', {
		placeName: $('#placeName').val(),
		placeSphere: categoriesArray,
		placeCity: $('#placeCity').val(),
		placeStreet: $('#placeStreetVal').val(),
		placeHouse: $('#placeHouse').val(),
		placeCorps: $('#placeCorps').val(),
		placeMetro: metro,
		placeDistrict: $('#placeDistrict').val(),
		placeParking: $('#placeParking').val(),
		placePhone: $('#placePhone').val(),
		placeEmail: $('#placeEmail').val(),
		placeSite: $('#placeSite').val(),
		placeJure: $('#placeJure').val(),
		placeTrademark: $('#placeTrademark').val(),
		representativeName: $('#representativeName').val(),
		representativePhone: $('#representativePhone').val(),
		captcha: $('#captcha').val(),
		iAmOwner: $('#iAmOwner').attr('checked')
	}, function(json){
		hideLoader();
		$('#addPlaceForm .errors').show();
		tip('#addPlaceForm .errors', json.text, json.type);
		if (json.type == 'error')
		{
			changeCaptcha();
		}
		else
		{
			$('#addPlaceForm select').val('');
			$('#addPlaceForm input').val('');
			$('#addPlaceForm input:checked').attr('checked','');
			$('#forOwners').hide();
			changeSelect();
			$('#catResult span').remove();
		}
	}, 'json')
}