$(function() {
	$('body').addClass('js')
	// пропадающие подписи к форме консультанта
	var inputs = $('form.consultant dd input,form.consultant textarea')
	inputs.focus(function() {
		$(this).parent().prev().find(':first-child').fadeOut('fast')
	}).blur(function() {
		if (/^\s*$/.test($(this).val())) $(this).parent().prev().find(':first-child').fadeIn('fast')
	}).each(function(){
		$(this).parent().attr('title', $(this).parent().prev().find(':first-child').html())
	})
	$('form.consultant label').click(function() {
		$(this).parent().next().find(':first-child').focus()
	})
	$('form.consultant').submit(function() {
		var form = this
		var fieldset = $(this).find('fieldset').hide()
		var message = $('<p class="loading"></p>').text('Отправка вопроса...').insertBefore(fieldset)
		var data = {ajax:'yes'}
		inputs.each(function() { data[$(this).attr('name')] = $(this).val() })
		$.ajax({
			url: $('base').attr('href')+'quest',
			type: 'post',
			data: data,
			dataType: 'json',
			success:function(result) {
				message.remove()
				message = $('<p></p>').text('Ваш вопрос отправлен!').insertBefore(fieldset)
				$('<input type="button" value="Показать форму" />').appendTo(message).click(function() {
					message.remove()
					fieldset.show()
					form.reset()
					$(form).find('label').show()
				})
			},
			error: function() {
				message.remove()
				message = $('<p></p>').text('Не удалось отправить вопрос!').insertBefore(fieldset)
				$('<input type="button" value="Показать форму" />').appendTo(message).click(function() {
					message.remove()
					fieldset.show()
					$(form).find('label').show()
				})
			}
		})
		return false
	})
	$('.focus').focus()
	$('#order').each(function() {
		$('input[name=company]', this).focus()
		$('select', this).each(function() {
			var node = $(this)
			var replace = function() {
				if (node.val() === 'other') {
					node.hide()
					var input = $('input', node.parent()).show().focus()
				}
			}
			node.change(replace)
			replace()
		})
	})
	mailTable = ['eW5rQHluay5ydQ==', 'aWtzaUBpa3NpLmluZm8=', 'a2FsZ3VzaGtpbkB5YW5kZXgucnU=']
	$('div.bm span.txt').each(function(i){
		var mail = Base64.decode(mailTable[i])
		$(this).append('<span><span class="new-line"></span>e-mail: <a href="mailto:'+mail+'">'+mail+'</a></span>')
	})
})
