var _init = {
	account: function() {

		$('input[name="login"]', '#accountForm').keypress(function(e) {
			  var key = (typeof e.charCode == 'undefined' ? e.keyCode : e.charCode);
			  if (e.ctrlKey || e.altKey || key < 32) return true;
			  key = String.fromCharCode(key);
			  return /\w/.test(key);
		});

		$('#accountForm').submit(function() {
			var err= $('#errorLog');
			err.html('');

			var $v = $('input[name="login"]', this);
			if (!/\w/.test($v.val())){err.html('Некорректный "Логин" (только латиница и цифры)');$v.focus();return false;}
			var $v = $('input[name="email"]', this);
			if (!/^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/.test($v.val())){err.html('Некорректный "E-mail"');$v.focus();return false;}

			var $v = $('input[name="password"]', this);
			if ($v.parent().parent().hasClass('req')) {
				if ('' == $v.val() || $v.val() != $('input[name="repassword"]', this).val()) {
					err.html('Введенные пароли пусты или не совпадают');
					return false;
				}
			}

			var $v = $('input[name="lastname"]', this);
			if ('' ==$v.val()){err.html('Заполните поле "Фамилия"');$v.focus();return false;}
			var $v = $('input[name="firstname"]', this);
			if ('' ==$v.val()){err.html('Заполните поле "Имя"');$v.focus();return false;}

			var $v = $('input[name="org_name"]', this);
			if ('' ==$v.val()){err.html('Заполните поле "Название организации"');$v.focus();return false;}
			var $v = $('input[name="city"]', this);
			if ('' ==$v.val()){err.html('Заполните поле "Город"');$v.focus();return false;}
			var $v = $('input[name="phone"]', this);
			if ('' ==$v.val()){err.html('Заполните поле "Котактный телефон"');$v.focus();return false;}

			$('input.button', this).attr('disabled', 'disabled');
			err.html('Пожалуйста подождите...');

			$(this).ajaxSubmit({
				dataType: 'json',
				success: function(response, status, xhr, $form) {
					if (response.err) {
						$('#errorLog').html(response.err);
					} else {
						$('#errorLog').html(response.status);
					}

					$('input.button', '#accountForm').removeAttr('disabled');
				}
			});

			return false;
		});

		$('#accountRestore').submit(function() {
			var err= $('#errorLog');
			err.html('');

			var $v = $('input[name="email_restore"]', this);
			if (!/^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/.test($v.val())){err.html('Некорректный "E-mail"');$v.focus();return false;}
			var $v = $('input[name="org_name"]', this);
			if ('' ==$v.val()){err.html('Заполните поле "Название организации"');$v.focus();return false;}

			$('input.button', this).attr('disabled', 'disabled');
			err.html('Пожалуйста подождите...');

			$(this).ajaxSubmit({
				dataType: 'json',
				success: function(response, status, xhr, $form) {
					if (response.success) {
						err.html('<br />Запрос на восстановление пароля отправлен.<br />В ближайшее время мы с вами свяжемся.');
					} else {
						err.html('Такого E-mail не обнаружено');
					}

					$('input.button', '#accountRestore').removeAttr('disabled');
				}
			});

			return false;
		});

	}
}

$(document).ready(function() {
	
	$('div.corner', '#text').corner();

	$('a[class*="gallery"], a[class*="gallerys"]').fancybox({
		'transitionIn'	: 'elastic',
		'transitionOut'	: 'elastic',
		'easingIn'      : 'easeOutBack',
		'easingOut'     : 'easeInBack',
		'titlePosition' : 'inside'
	});

	$('div.ico a.ico-fav', '#header').jBrowserBookmark();
	$('div.ico a.ico-first', '#header').click(function() {
		try {
			document.body.style.behavior = 'url(#default#homepage)';
			document.body.setHomePage('http://' + location.hostname);
		} catch(e){}
		return false;
	});

	var options = {
			start: 1,
			display: 1,
			controls: true,
			interval: true,
			intervaltime: 3500,
			rewind: true,
			animation: true,
			duration: 2000
	};
	$('#slider2').tinycarousel(options);

	marked.slider = $('#slider');
	setInterval('if (marked.auto) markhead()', marked.timeout);
	$('div.more a', marked.slider).click(function() {
		clearTimeout(marked.timer);
		marked.timer = setTimeout("marked.auto = true", 3500);
		marked.auto = false;
		markhead();
		return false;
	});
});

function markhead() {
	if (marked.index++ == marked.count - 1) {
		marked.index = 0;
	}

	$('div.img img', marked.slider).attr('src', '/upload/image/production/' + marked.id[marked.index] + '/thumb/' + marked.thumb[marked.index] + '');
	$('div.img a', marked.slider)
		.attr('href',	'/upload/image/production/' + marked.id[marked.index] + '/' + marked.image[marked.index] + '')
		.attr('title',	marked.name[marked.index]);
	$('div.name', marked.slider).html(marked.name[marked.index]);

	return false;
}

(function($) {
	 
	$.fn.equalizeCols = function(){
		var height = 0,
			reset = $.browser.msie && $.browser.version < 7 ? "1%" : "auto";
		var oh = 8;
  
		return this
			.css("height", reset)
			.each(function() {
				height = Math.max(height, this.offsetHeight)-oh;
			})
			.css("height", height)
			.each(function() {
				var h = this.offsetHeight;
				if (h > height) {
					$(this).css("height", height - (h - height-oh));
				};
			});
			
	};
	
})(jQuery);
