function showForm(form) {
	var formCont = formCont || $('#formsCont'),
		hExp = form.innerHeight();
	if (!formCont.data('opened') || formCont.data('opened') === 'undefined') {
		form.show();
		formCont.data('opened', true);
	} else {
		$('div.sentNotice', formCont).remove();
		$('form.opened', formCont).fadeOut(200,
			function() {
				$(this).removeClass('opened').removeAttr('style').data('opener').removeClass('active');
				form.fadeIn(200);
			}
		);
	}
	formCont.animate({'height': hExp +'px'}, 300, function() { form.addClass('opened');} );
}
function hideForm() {
	var formCont = formCont || $('#formsCont');
	if (formCont.data('opened')) {
		formCont.animate({'height': 0}, 300, function() {
			$('form.opened', formCont).removeClass('opened').removeAttr('style').data('opener').removeClass('active');
			$('form.hasErrors', formCont).removeClass('hasErrors');
			$('div.sentNotice, span.errNotice', formCont).remove();
		});
		clearForm($('form.dataForm', formCont));
		formCont.data('opened', false);
	}
}
function clearForm(form) {
	$('input.txtFld, textarea.txtFld', form).val('');
}

$.fn.showErrNotice = function(txt) {
	return this.each(function() {
		var c = $('<span class="errNotice"/>').text(txt).append('<i class="c ct"/><i class="c cb"/><i class="arw"/>');
		$(this).after(c);
	});
};
$.fn.hideErrNotice = function(txt) {
	return this.each(function() {
		$('span.errNotice', $(this).parent()).remove();
	});
};


$(function() {

	$.support.borderRadius = false;
	$.each(['BorderRadius','MozBorderRadius','WebkitBorderRadius','OBorderRadius','KhtmlBorderRadius'], function(i) {
		if (document.body.style[this] !== undefined) $.support.borderRadius = true;
		return !$.support.borderRadius;
	});
	
	$('#container').prepend('<i class="cc ctm"><i class="c l"/><i class="c r"/></i>').append('<i class="cc cbm"><i class="c l"/><i class="c r"/></i>');
	$('a.btn, button.btn span').append('<i class="c l"/><i class="c r"/>')
	$('#siteTitle').append('<i class="spr"/>');
	
	if (!$.support.borderRadius) {
		$('#header, #formsCont, dl.tabs dt.tab, div.b-rnd').append('<i class="c4 ctl"/><i class="c4 ctr"/><i class="c4 cbl"/><i class="c4 cbr"/>');
		// add rounded corners
	}
	
	$('#mdlCol dt.tab a').click(function() {
		var tabsCont = $(this).parents('dl.tabs');
			prnt = $(this).parent();
		this.desc = this.desc || prnt.next('dd.tabDescr');
		if (!prnt.hasClass('active')) {
			$('dt.active, dd.active', tabsCont).removeClass('active');
			prnt.addClass('active');
			this.desc.addClass('active');
		}
		return !1;
	});
	
	$('#callbackLink').click(function() {
		if (!$(this).hasClass('active')) {
			$(this).addClass('active');
			this.form = this.form || $('#callbackForm');
			this.form.data('opener', $(this));
			showForm(this.form);
		}
		return !1;
	});
	
	$('#askLink').click(function() {
		if (!$(this).hasClass('active')) {
			$(this).addClass('active');
			this.form = this.form || $('#askForm');
			this.form.data('opener', $(this));
			showForm(this.form);
		}
		return !1;
	});
	$('#formsCont a.close').click(function() {
		hideForm();
		return !1;
	});
	
	$('#callbackForm').submit(function() {
		var form = $(this),
			errors = 0;
		$('input.txtFld, textarea', form).each(function() {
			switch ($(this).attr('name')) {
				case 'f_phone':
					if ($.trim($(this).val()) === '') {
						form.addClass('hasErrors');
						$(this).showErrNotice('Напишите, пожалуйста, номер телефона');
						errors++;
					} else {
						$(this).hideErrNotice();
					}
					break;
				case 'f_name':
					if ($.trim($(this).val()) === '') {
						form.addClass('hasErrors');
						$(this).showErrNotice('Напишите, пожалуйста, своё имя');
						errors++;
					} else {
						$(this).hideErrNotice();
					}
					break;
			}
		});
		if (errors === 0) {
			$.ajax({
				type: 'POST',
				url: form.attr('action'),
				data: form.serialize(),
				dataType: 'html',
				success: function(msg) {
					form.fadeOut(300, function() {
						var resp = $(msg);
						form.after(resp);
						resp.css({'top': '50%', 'marginTop': '-'+ resp.innerHeight() +'px'});
					});
				},
				error: function() {
					form.css('opacity', .5);
				}
			});
		}
		return !1;
	});
	
	
	$('#askForm').submit(function() {
		var form = $(this),
			errors = 0,
			filter = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
		$('input.txtFld, textarea', form).each(function() {
			switch ($(this).attr('name')) {
				case 'f_question':
					if ($.trim($(this).val()) === '') {
						form.addClass('hasErrors');
						$(this).showErrNotice('Задайте, пожалуйста, свой вопрос');
						errors++;
					} else {
						$(this).hideErrNotice();
					}
					break;
				case 'f_name':
					if ($.trim($(this).val()) === '') {
						form.addClass('hasErrors');
						$(this).showErrNotice('Напишите, пожалуйста, своё имя');
						errors++;
					} else {
						$(this).hideErrNotice();
					}
					break;
				case 'f_phone':
					if ($.trim($(this).val()) === '' && !filter.test($('input[name="f_embox"]', form).val())) {
						form.addClass('hasErrors');
						$(this).showErrNotice('Напишите, пожалуйста, номер телефона');
						errors++;
					} else {
						$(this).hideErrNotice();
					}
					break;
				case 'f_embox':
					if (!filter.test($(this).val()) && $.trim($('input[name="f_phone"]', form).val()) === '') {
						form.addClass('hasErrors');
						$(this).showErrNotice('Напишите, пожалуйста, адрес эл. почты');
						errors++;
					} else {
						$(this).hideErrNotice();
					}
					break;
			}
		});
		if (errors === 0) {
			$.ajax({
				type: 'POST',
				url: form.attr('action'),
				data: form.serialize(),
				dataType: 'html',
				success: function(msg) {
					form.fadeOut(300, function() {
						var resp = $(msg);
						form.after(resp);
						resp.css({'top': '50%', 'marginTop': '-'+ resp.innerHeight() +'px'});
					});
				},
				error: function() {
					form.css('opacity', .5);
				}
			});
		}
		return !1;
	});
});
