var $closeTimer;

jQuery(document).ready(function() {
	$("#link-open-ask-question").live("click", function() { $("#div-ask-question").show(); });
	$("#link-close-ask-question").live("click", closeForm );
	$("#div-ask-question").css("height", $("body").height());

	$("#submit").live("click", sendQuestion)
});

function closeForm()
{
	$closeTimer = clearTimeout($closeTimer);
	$("#div-ask-question").hide();
	$("#ask-question").replaceWith('<form id="ask-question" enctype="application/x-www-form-urlencoded" method="post" action="/index/process-question/"><dl class="zend_form"><div class="form-pair"><label for="name" class="required">Ваше Имя:</label><input type="text" name="name" id="name" value="" class="input" maxlength="256"></div><div class="form-pair"><label for="email" class="required">email:</label><input type="text" name="email" id="email" value="" class="input" maxlength="256"></div><div class="form-pair"><label for="phone" class="optional">телефон:</label><input type="text" name="phone" id="phone" value="" class="input" maxlength="30"></div><div class="form-pair"><label for="question" class="required">вопрос:</label><input type="text" name="question" id="question" value="" class="textarea" maxlength="256"></div><div class="form-pair"><input type="image" name="submit" id="submit" src="/img/form-button.jpg"></div></dl></form>');
}

function sendQuestion()
{
	jQuery.post(
		$("#ask-question").attr('action') + '?' + $("#ask-question").serialize(),
		null,
		onSendQuestion
	);
	return false;
}

function onSendQuestion(response)
{
	$("#ask-question").replaceWith(response);
	if ($("#ask-question").get(0).tagName == 'P' || $("#ask-question").get(0).tagName == 'p') {
		$closeTimer = setTimeout(function(){closeForm()}, 2000);
	}
}
