$(document).ready(function(){
	//Submit with enter.
	phoneChecking();

	var userStatus = $('div.user-status');
	if(userStatus.length){
		function ajaxForm(){
			$('form', userStatus).attr('action', $('form', userStatus).attr('action'));
			$('h4 > a', userStatus).bind('click', function(event){
				event.preventDefault();
				userStatus.toggleClass('user-status_view').toggleClass('user-status_edit');
				$('form input[type=text]', userStatus).focus();
			});
			$('form', userStatus).bind('submit', function(event){
				event.preventDefault();

				var form = $(this),
					submit = $('input[type=submit]', form),
					data = form.serialize() + '&save=true';

				submit.attr('disabled', 'disabled');

				$.ajax({
					type: 'get',
					url: form.attr('action'),
					data: data,
					dataType: 'jsonp',
					success: function(response){
						if (response.hasOwnProperty('content')) {
							userStatus.parent().html(response.content);
							userStatus = $('div.user-status');
							ajaxForm();
						}
					}
				})
			});
		}

		ajaxForm();
	}

	if($('.registration')[0]){

		$('#live-id-already,#live-id-later').click(function(){
			$('.small-box')[0].style.display = 'none';
			$('.small-box + .bottom-corners')[0].style.display = 'none';
		});

		$('#live-id-yes').click(function(){
			$('.small-box')[0].style.display = 'block';
			$('.small-box + .bottom-corners')[0].style.display = 'block';
		});

		$('#password')[0].onblur = function(){
			if(this.value.length < 6){
				if(!$('.password:has(.err)')[0]){
					$('.password:has(#password)').append("<p class=\"desc err\">Поле заполнено неправильно!</dd>");
				}
				if(!$('.line:has(#password)').hasClass('error')){
					$('.line:has(#password)').addClass('error');
				}
			}
			else {
				if($('.line:has(#password)').hasClass('error')){
					$('.line:has(#password)').removeClass('error');
				}
				if($('.password:has(.err)')[0] && $('#password_again')[0].value.length > 5){
					$('.password .err').remove();
				}
			}
		};

		$('#password_again')[0].onblur = function(){
			if(this.value.length < 6){
				if(!$('.password:has(.err)')[0]){
					$('.password:has(#password)').append("<p class=\"desc err\">Поле заполнено неправильно!</dd>");
				}
				if(!$('.line:has(#password_again)').hasClass('error')){
					$('.line:has(#password_again)').addClass('error');
				}
			}
			else {
				if($('.line:has(#password_again)').hasClass('error')){
					$('.line:has(#password_again)').removeClass('error');
				}
				if($('.password:has(.err)')[0] && $('#password')[0].value.length > 5){
					$('.password:has(#password) .err').remove();
				}
			}
		};

		/*
		 $(':submit')[0].onclick=function(){
		 $('.submit-options .error').remove();
		 if($('.err')[0]&&$('.err')[0].innerHTML!='Тест не пройден!') return false;
		 }
		 */

	}

	if($('#lm-login')[0]){
		$('#lm-login')[0].onfocus = function(){
			if($('#lm-login')[0].style.color != 'rgb(0, 0, 0)'){
				$('#lm-login')[0].value = "";
				$('#lm-login').css('color', '#000');
				$('#lm-password')[0].value = "";
				$('#lm-password').css('color', '#000');
			}
		}
	}

	var hideUserLayer = function(layer){
		layer.slideUp('fast', function(){
			layer.remove()
		});
	};

	$('ul.friends-dropdown > li:not(.all-users)').mouseenter(function(){
		var el = $(this),
			div = el.parent().parent(),
			cont = $('<div class="b-friend-hover b-friend-hover-left"></div>'),
			username = el.find('a:first').attr('href').match(/http:\/\/([^\.]+)/)[1],
			isMutual = el.parents('ul.non-mutual').length;

		cont.append(el.children().clone()).appendTo(div)
			.append($('<p class="remove"><a href="http://' + window.project_domain + '/friends/remove/' + username + '"><i></i>' + (isMutual ? 'Перестать наблюдать' : 'Удалить из друзей') + '</a></p>'))
			.css({
			top: el.offset().top - div.offset().top - 10,
			left: el.offset().left - div.offset().left - 5
		})
			.mouseenter(function(){
			if(el.data('timer')){
				clearTimeout(el.data('timer'));
			}
		})
			.mouseleave(function(){
			hideUserLayer($(this))
		})
			.slideDown('fast')
			.find('p.add > a, p.remove > a').click(getPopup);

		el.data('layer', cont);
	}).mouseleave(function(){
		var el = $(this),
			layer = $(this).data('layer');
		if(layer){
			el.data('timer', setTimeout(function(){
				hideUserLayer(layer);
			}, 10));
		}
	});

	cancelPopup($('#alerts-popup'));

	var paging = $('div.paging > ul.paging-inner');
	if(paging.length){
		var from = 38,
			to = 197,
			both = to + from,
			lis = paging.find('> li'),
			original = paging.find('li.current'),
			current = $(original),
			timer = null;

		paging.removeClass('no-script');
		lis.mouseenter(function(){
			if(timer){
				clearTimeout(timer);
			}

			var li = $(this),
				index = lis.index(li),
				step = function(width){
					current.css('width', both - width);
					//console.debug(width);
				},
				action = function(){
					lis.stop().removeClass('current').width(from);
					current.width(to);

					li.animate({
						width: to
					}, {
						duration: 'fast',
						step: step,
						complete: function(){
							current = li;
							current.addClass('current');
							step(from);
							lis.removeAttr('style');
						}
					});
				};

			if(!li.hasClass('current')){
				timer = setTimeout(action, 200);
			}

			//console.debug('mouseenter', li);
		});
		paging.mouseleave(function(){
			original.trigger('mouseenter');
			//console.debug('------------------');
		});
	}

	var countdownContainer = $('#broadcastStartCountdown');

	if(countdownContainer.length){
		startCountdown(countdownContainer, broadcastStartTime);
	}

	residenceSelectManager.init($('#residenceSelect'));

	// Счётчики
	if( window.store_section !== undefined && store_section != '' && location.hostname != store_section + '.' + project_domain ) {
		var countersBlock = document.createElement('div');
			countersBlockInnerHTML = '';

		/* Rambler TOP100 */
		countersBlockInnerHTML += '<img src="http://counter.rambler.ru/top100.cnt?648840" alt="" width="1" height="1"/>';

		setTimeout(function(){
			var d=document,r=d.referrer,s=screen,rnd=Math.random();

			/* Rating@Mail.ru */
			new Image().src = 'http://dc.c7.bc.a0.top.mail.ru/counter?id=818211;js=13;r='+escape(r)+';j='+navigator.javaEnabled()+';s='+s.width+'*'+s.height+';d='+(s.colorDepth?s.colorDepth:s.pixelDepth)+';rand='+rnd;

			/* LiveInternet */
			new Image().src = "http://counter.yadro.ru/hit?r"+escape(r)+((typeof s=="undefined")?"":";s"+s.width+"*"+s.height+"*"+(s.colorDepth?s.colorDepth:s.pixelDepth))+";u"+escape(d.URL)+";h"+escape(d.title.substring(0,80))+";"+rnd;

			/* tns-counter.ru */
			new Image().src = 'http://www.tns-counter.ru/V13a***'+r.replace(/\*/g,'%2a')+'*sup_ru/ru/CP1251/tmsec=championat_total/';
		}, 0);

		/* SpyLOG */
		countersBlockInnerHTML += '<img src="http://u11731.77.spylog.com/cnt?cid=1173177&p=0" alt="SpyLOG" border="0" width="1" height="1">';

		countersBlock.style.display = 'none';
		countersBlock.innerHTML = countersBlockInnerHTML;

		document.body.appendChild(countersBlock);
	}
});

/**
 * Выбор места жительства (с AJAX-подгрузкой списков регионов/городов по ходу выбора)
 */
var residenceSelectManager = (function (){
	var CONFIG = {
		url : {
			region : '/geo/find_regions?country_id={id}',
			city : '/geo/find_cities?region_id={id}'
		},
		tmpl : '<option value="{id}">{name}</option>',
		errorBlockSelector : '.err'
	},
		_container, _selects, _submitButton;

	/**
	 * Находит селекты внутри контейнера и вешает на "onchange" подгрузку данных в след. селект
	 */
	function bindEvents(){
		_selects = _container.find('select');

		_selects
			.bind('change', function (){
			var currentControl = $(this),
				currentValue = currentControl.val(),
				valueIsNotDefault = (parseInt(currentValue, 10) !== 0),
				nextControl = currentControl.next('select');

			if(nextControl.length && valueIsNotDefault){
				loadData(nextControl, currentValue);
			}

			_container
				.nextAll(CONFIG.errorBlockSelector)
				.remove(); // если были ошибки валидации формы, при изменении селекта они по-любому исчезнут
		})
			// мы должны задизейблить первую строку в первом селекте, данные в который вставляются сервером при загрузке страницы
			// (мы не можем сразу поставить "disabled" в шаблоне, потому что тогда первая строка селекта не будет выбрана и пользователь не
			// увидит побудительного сообщения "Выберите страну")
			.first()
			.find('option:first-child')
			.attr('disabled', 'disabled');
	}

	/**
	 * Загружает данные по данной стране/региону (соответственно, регионы/города) в данный селект
	 *
	 * @param {Object} control контрол (селект), куда будем загружать данные
	 * @param {Object} requestId значение (value) селекта, в котором была выбрана страна/регион, для которых будем грузить данные - т.е. id страны/региона
	 */
	function loadData(control, requestId){
		var defaultRow = control.find('option:first-child'), // запоминаем строчку "Выберите..."
			nextControl = control.next('select'),
			controlType = control.attr('id').replace('_id', ''), // определяем тип контрола ("region" или "city")
			url = CONFIG.url[controlType].supplant({ id : requestId }),
			newHtml,
			dataContent;

		// Данный контрол должен работать и на поддоменах
		url = window.project_domain !== undefined ? 'http://' + window.project_domain + url + '&callback=?' : url;

		_selects.attr('disabled', 'disabled');
		_submitButton.attr('disabled', 'disabled');

		$.getJSON(url, function (data){
			dataContent = (data.hasOwnProperty('content') && $.trim(data.content).length) ? $.parseJSON(data.content) : data;

			newHtml = parseData(dataContent);

			control
				.html(newHtml)
				.prepend(defaultRow)// вставляем строчку "Выберите..."
				// .removeAttr('disabled')
				.val(0); // делаем первую строчку ("Выберите...") - выделенной

			_selects.removeAttr('disabled');
			_submitButton.removeAttr('disabled');

			control
				.nextAll('select')
				.val(0)
				.attr('disabled', 'disabled'); // если еще правее есть контролы, задизейбливаем их
		});
	}

	function parseData(data){
		var currentData,
			tmpl = CONFIG.tmpl,
			htmlCache = [ ];

		for(var i = 0, l = data.length; i < l; i++){
			currentData = data[i];
			htmlCache.push(tmpl.supplant(currentData));
		}

		return htmlCache.join(' ');
	}

	return {
		/**
		 * @param {jQuery} container
		 */
		init : function (container){
			if(!container.length){
				return false;
			}

			_container = container;
			_submitButton = container.closest('form').find('input:submit');

			bindEvents();
		}
	};
})();

/**
 * countdown timer for broadcast (translations)
 *
 * @param {jQuery} elem
 * @param {ISO} finalTime yyyy-mm-dd hh:mm:ss
 */
function startCountdown(elem, broadcastTime){
	var currentTime = Date.parse(new Date()),
		finalTime = parseToUnixTime(broadcastTime),
		messageContainer = elem.find('span');

	if(currentTime < finalTime){
		elem.show();
	} else {
		return;
	}

	setInterval(refresh, 1000);

	function parseToUnixTime(time){
		var parsedTime = time.match(/([0-9]{4})\-([0-9]{2})\-([0-9]{2})\s([0-9]{2})\:([0-9]{2})\:([0-9]{2})/),
			year = parsedTime[1],
			month = parsedTime[2] - 1,
			day = parsedTime[3],
			hour = parsedTime[4],
			min = parsedTime[5],
			sec = parsedTime[6],

			unixTime = Date.parse(new Date(year, month, day, hour, min, sec));

		return unixTime;
	}

	/**
	 *
	 * @param {Unix time} timeRemaining
	 * @return {String} timeRemainingStr
	 */
	function parseToString(timeRemaining){
		var days, hours, min;

		// to seconds
		timeRemaining = timeRemaining / 1000;

		days = Math.floor(timeRemaining / (60 * 60 * 24));
		hours = Math.floor(timeRemaining / (60 * 60));
		min = Math.floor(timeRemaining / 60);

		hours = hours - (24 * days);
		min = min - (24 * 60 * days) - (60 * hours);

		function pasteAffixes(x, one, twoThreeFour, other){
			var result, y;

			if(x === 0){
				return '';
			}

			if(x > 10){
				x = parseInt((x + '').substr(-2, 2), 10);
				if(x < 20){
					result = other;
					return x + ' ' + result;
				}
			}

			y = x % 10;

			if(y == 2 || y == 3 || y == 4){
				result = twoThreeFour;
			} else {
				if(y == 1){
					result = one;
				} else {
					result = other;
				}
			}

			return x + ' ' + result;
		}

		return pasteAffixes(days, 'день', 'дня', 'дней') + ' ' + pasteAffixes(hours, 'час', 'часа', 'часов') + ' ' + pasteAffixes(min, 'минута', 'минуты', 'минут');
	}

	function refresh(){
		var currentTime = Date.parse(new Date()),
			remainingTime = finalTime - currentTime,
			resultMessage = '0 дней 0 часов 0 минут';

		if(remainingTime < 0){
			elem.hide();
			return;
		}

		resultMessage = parseToString(remainingTime);
		messageContainer.html(resultMessage);
	}
}

// Popup functions -----------------------------

function getPopup(event){
	var url, data = [], method = 'get';

	if(this.nodeName.toLowerCase() == 'button' || this.nodeName.toLowerCase() == 'input'){
		if($(this).attr('ajax') !== 'true'){
			return;
		}
		if(this.form){
			url = $(this.form).attr('action');
			data = $(this.form).serializeArray();
			method = $(this.form).attr('method');
		}
		if(this.name.length){
			data.push({
				name: this.name,
				value: $(this).val()
			})
		}
	} else {
		url = $(this).attr('href');
	}

	hidePopup();
	showSpinner();

	loadPopup({
		url: url,
		data: data,
		method: method,
		event: event
	});

	return false;
}

function getJSONP(data){
	if(data.hasOwnProperty('next')){
		loadPopup({
			url: data.next
		});
	} else if(data.hasOwnProperty('done')){
		hideSpinner();
		document.location.href = data.done;
	} else if(data.hasOwnProperty('callback_error')){
		var popup = $('body > div.popup');
		if(popup.length){
			showPopupAnimate(popup);
		}
	} else if(data.hasOwnProperty('content') && $.trim(data.content).length){
		showPopup(data.content);
	} else {
		hideSpinner();
	}
}

function getText(data, status, request){
	var json;
	try{
		json = $.parseJSON(data);
	} catch(e){}

	var next = request.getResponseHeader('Next');
	var done = request.getResponseHeader('Done');

	if(next){
		loadPopup({
			url: next
		});
	} else if(done){
		document.location.href = done;
	} else if(json && formPopup.callback){
		formPopup.callback(json);
		delete formPopup.callback;
		hideSpinner();
	} else if(json && !formPopup.callback){
		// костыль
		var favoritesType = $('.favorites-results').attr('id');
		formPopup.callback = function(data){
			favorites.add(favoritesType, data);
		}
		formPopup.callback(json);
		delete formPopup.callback;
		hideSpinner();
	} else if($.trim(data).length) {
		showPopup(data);

	} else {
		hideSpinner();
	}
}

function loadPopup(opt){
	if(!opt.method){
		opt.method = 'get';
	}
	var isCrossDomain = false;
	var matches = /^(\/)?(?:([^:]+:)\/\/)?(?:www\.)?([\w\.-]+)/i.exec(opt.url);
	if(!matches[1]){
		if(!matches[2]){
			opt.url = location.protocol + '//' + opt.url;
		}
		isCrossDomain = matches[2] != location.protocol || matches[3] != location.hostname;
	}

	if(isCrossDomain){
		if(opt.method == 'post'){
			// formXSS is crossdomain post form, that we want to send
			if(opt.formXSS.length){
				var numRundom = Math.round(Math.random() * 100000);
				var inputs = $('<input type="hidden" name="callback_set" value="' + numRundom + '" /><input type="hidden" name="callback" value="getJSONP" />');

				opt.formXSS.attr({
					target: 'jsonPost'
				}).append(inputs);

				// If iframe already exist, then use it
				var iframe = $('#jsonPostIframe');

				if (!iframe.length) {
					iframe = $('<iframe id="jsonPostIframe" name="jsonPost" width="0" height="0"></iframe>');
					$(document.body).append(iframe);
				}
				else {
					iframe.unbind('load');
				}

				iframe.bind('load', function(){
					inputs.remove();
					$.ajax({
						url: opt.url,
						data: [
							{
								name: 'callback_get',
								value: numRundom
							}
						],
						cache: false,
						jsonpCallback: 'getJSONP',
						dataType: 'jsonp',
						error: hideSpinner
					});
				});
			}
		} else {
			if(opt.event){
				opt.event.preventDefault();
			}
			$.ajax({
				type: opt.method,
				url: opt.url,
				data: opt.data,
				cache: false,
				dataType: 'jsonp',
				jsonpCallback: 'getJSONP',
				error: hideSpinner
			});
		}
	} else {
		if(opt.event){
			opt.event.preventDefault();
		}
		$.ajax({
			type: opt.method,
			url: opt.url,
			data: opt.data,
			cache: false,
			dataType: 'html',
			success: getText,
			error: hideSpinner
		})
	}
}

function showPopup(data){
	var popup = $('<div class="popup"></div>').css({
		top: $(window).height() * -1
	}).append(data).appendTo(document.body);


	if(popup.find('form[name=pay]').attr('ajax', 'false').trigger('submit').length){
		return true;
	}

	popup.find('a[ajax=true], input[ajax=true], button[ajax=true]').bind('click', getPopup);
	popup.find('form[ajax=true]').bind('submit', formPopup);

	//		Костыль
	$('.favorites-add-form a').bind('click', function(){
		var favoritesType = $(this).closest('.favorites-results').attr('id');
		formPopup.callback = function(data){
			favorites.add(favoritesType, data);
		};
	});


	cancelPopup(popup);
	showPopupAnimate(popup);

	$(document).trigger('showPopup', popup);

	return popup;
}
function cancelPopup(popup){
	var els;
	if(popup.length){
		els = popup.find('input[name=cancel], input[type=reset], .close-popup').show();
	}

	if(els && els.length){
		var close = function(){
			hidePopup(popup);
			hideSpinner();
		};
		$(document).bind('keyup', function(event){
			if(event.keyCode == 27){
				close();
			}
		});
		els.bind('click', function(event){
			event.preventDefault();
			$(this).parents('form').data('canceled', true);
			close();
		});
	}
}
function centerPopup(){
	var popup = $('body > div.popup'),
		winHeight = $(window).height(),
		popHeight = popup.height();
	if(popHeight < winHeight - 20){
		popup.stop().animate({
			top: $(window).scrollTop() + ((winHeight - popHeight) / 2)
		}, 'fast');
	}
}

var wasScroll = null;
$(window).scroll(function(){
	if(wasScroll){
		clearTimeout(wasScroll);
	}
	wasScroll = setTimeout(centerPopup, 500);
});

function showPopupAnimate(popup){
	popup = popup || $('body > div.popup');
	var winHeight = $(window).height(),
		popHeight = popup.css({ top: winHeight * -1, display: 'block' }).height();
	popup.css({
		top: $(window).scrollTop() + (popHeight * -1),
		marginLeft: popup.width() / -2
	}).animate({
		top: $(window).scrollTop() + ((winHeight - popHeight) / 2)
	}, {
		duration: 'normal',
		complete: function(){
			if(window.reCaptchaOpt){
				Recaptcha.create(window.reCaptchaOpt.key, window.reCaptchaOpt.id, {
					theme: window.reCaptchaOpt.theme,
					callback: window.reCaptchaOpt.callback
				});
				window.reCaptchaOpt = null;
			}
		}
	});

	$('body > div.spinner').css({
		background: '#000000'
	});
	// popup actions
	if(popup.find('#userpics-popup').length){
		upicsControl();
	}
	if(!popup.find('#vob-personal-popup').length && !popup.find('#userpics-popup').length){
		validateForms();
	}
	if(popup.find('form.favorites-add-form').length){
		favoritesAdd();
	}
	residenceSelectManager.init($('#residenceSelect', popup));
}

function hidePopup(popup){
	popup = popup || $('body > div.popup');
	if(popup.length){
		hidePopupAnimate(popup, true);
	}

	$(document).trigger('hidePopup');
}

function hidePopupAnimate(popup, remove){
	popup = popup || $('body > div.popup');
	$('body > div.spinner').css({
		background: 'url("/i/popup.gif") no-repeat scroll center center #000000'
	});
	popup.animate({
		top: $(window).scrollTop() + (popup.height() * -1)
	}, 'fast', function(){
		if(remove){
			popup.remove();
		} else {
			popup.css({ display: 'none' });
		}
	});
}

//noinspection FunctionWithInconsistentReturnsJS
function formPopup(event){
	var form = $(this);

	if(form.data('canceled')){
		return false;
	}

	if(form.attr('ajax') == 'false'){
		return true;
	}

	var data = form.serializeArray();

	var submit = form.find(':submit:first');
	if(submit.length){
		data.push({
			name: submit.attr('name'),
			value: encodeURIComponent(submit.val())
		});
	}

	showSpinner();
	hidePopupAnimate(null, true);

	loadPopup({
		url: form.attr('action'),
		method: form.attr('method'),
		data: data,
		event: event,
		formXSS: form
	});
}

//noinspection FunctionWithInconsistentReturnsJS
function showSpinner(){
	if(!$('body > div.spinner').length){
		return $('<div class="spinner"></div><div class="spinner2"></div>').css({
			opacity: 0
		}).appendTo('body').fadeTo('fast', 0.7);
	}
}
function hideSpinner(){
	return $('body > div.spinner').fadeOut('fast', function(){
	    $('.spinner2').remove();
		$(this).remove();
	});
}

$(document).ready(function(){
	$('a[ajax=true], input[ajax=true], button[ajax=true]').bind('click', getPopup);
	$('form[ajax=true]').bind('submit', formPopup);
});

var favorites = (function(){
	var data = {};

	var sportTypes = {
		club: {
			football: 'Футбольный клуб',
			amateur: 'Любители',
			auto: 'Авто-мото спорт',
			basketball: 'Баскетбольный клуб',
			volleyball: 'Волейбольный клуб',
			hockey: 'Хоккейный клуб'
		},
		player: {
			football: 'Футболист',
			amateur: 'Любитель',
			auto: 'Гонщик',
			basketball: 'Баскетболист',
			volleyball: 'Волейболист',
			tennis: 'Теннисист',
			hockey: 'Хоккеист',
			biathlon: 'Биатлонист',
			box: 'Единоборства',
			'null': 'Другие виды спорта'
		}
	};

	$(document).ready(function(){
		$('.item-favorites').each(function(){
			if(data.type = this.id && this.id.slice(1).toLowerCase()){
				var currentFavorites = data[data.type] = {
					parent: $(this),
					emptyMsg: $('.none-favorites', this),
					list: {},
					length: 0,
					checkedItems: $('input[name=favorite_' + data.type + 's_ids]')
				};

				currentFavorites.parent.children('li:visible').each(function(){
					var id = $('input[name=fId]', this).val();

					$('.fRemove', this).bind('click', {
						obj: currentFavorites,
						id: id
					}, favorites.remove);
					currentFavorites.list[id] = this;
					currentFavorites.length++;
				});

				if(!currentFavorites.length){
					currentFavorites.emptyMsg.show();
				}
			}
		});
		delete data.type;
	});

	return {
		add: function(type, itemData){
			if(!data[type] || data[type].list[itemData.id]){
				return;
			}
			var itemTemplate = $('#fTemplate' + type.slice(0, 1).toUpperCase() + type.slice(1)).html().replace(/%7B/g, '{').replace(/%7D/g, '}');
			itemData.sport = sportTypes[type][itemData.sport] || sportTypes[type]['null'];
			if(!itemData.icon){
				itemData.icon = '/i/content/club-icon.png';
			} else {
				itemData.icon = 'http://img.championat.net/team/icon/' + itemData.icon;
			}
			$(itemTemplate.supplant(itemData)).appendTo(data[type].parent).find('.fRemove').bind('click', {
				obj: data[type],
				id: itemData.id
			}, favorites.remove);
			data[type].list[itemData.id] = data[type].parent.last('li')[0];
			data[type].checkedItems[0].value += itemData.id + ' ';
			data[type].emptyMsg.hide();
			data[type].length++;
		},
		remove: function(evt){
			$(this).parent('li').remove();
			evt.data.obj.checkedItems[0].value = evt.data.obj.checkedItems[0].value.replace(evt.data.id + ' ', '');
			delete evt.data.obj.list[evt.data.id];
			evt.data.obj.length--;
			if(!evt.data.obj.length){
				evt.data.obj.emptyMsg.show();
			}
			return false;
		}
	}
})();

$.ajaxSetup({
	error: function(request, textStatus){
		if(request.status == 403){
			hideSpinner();
			document.location.href = '//' + project_domain + '/login';
		}
	}
});

$(document).ready(function(){
	if(document.location.search){
		var parts = document.location.search.replace(/^\?/, '').split('&'),
			result = {};
		for(var i = 0, l = parts.length; i < l; i++){
			var splited = parts[i].split('=');
			if(splited.length > 1){
				result[splited[0]] = splited[1];
			}
		}
		if(result.autoload){
			showSpinner();
			loadPopup({
				url: result.autoload
			});
		}
	}
});

// ---------------------------------------------

function favoritesAdd(){
	var add = function (event){
		event.preventDefault();
		hidePopup();
		loadPopup({
			url: $(this).attr('href')
		});
	};

	$('div.popup form.favorites-add-form').each(function (){
		var form = $(this),
			help = form.find('div.favorites-help'),
			container = form.find('div.favorites-result'),
			result = form.find('div.favorites-resulted'),
			inputs = form.find('input[type=text]:not([readonly])'),
			aval = '';

		/**
		 * Навешивание событий на контролы внутри контейнера
		 */
		function prepare(){
			var contaner = this.selector;
			$('#pagesLink a', result).bind('click', function(evt){
				$(contaner).html('<img src="/i/popup.gif" style="margin: 10px auto; display: block" />').show();
				$.get($(this).attr('href'), append);
				return false;
			});

			result.find('div.favorites-list a').bind('click', add);
		}

		/**
		 * Обработка и добавление данных, поступивших с сервера (приходит готовый html)
		 *
		 * @param {Object} data
		 */
		function append(data){
			var cache = form.data('cache');
			cache[aval] = data; // добавляем данные в кэш
			form.data('cache', cache);
			form.data('lastV', aval);
			result.html(data); // вставляем данные в попам
			prepare(); // навешиваем события (на контролы внутри вновь поступивших данных)
		}

		form.data('cache', {}); // обнуляем кэш

		if(inputs.length){
			inputs.bind('paste keyup chage', function (){
				var val = $.trim($(this).val());

				// если уже был запущен таймер - останавливаем его
				if(form.data('timer')){
					clearTimeout(form.data('timer'));
				}

				// если текущее значение равно предыдущему - уходим
				if(form.data('lastV') == val){
					return true;
				}

				// работаем только со значениями, число символов которых больше 2-ух
				if(val.length > 2){
					help.hide();
					container.addClass('favorites-result-hover');

					// если по данному запросу есть кэшированныве данные - достаем оттуда, иначе - грузим с сервера
					if(form.data('cache')[val]){
						form.data('lastV', val);
						result.html(form.data('cache')[val]).show();
						prepare(); // навешиваем события
					} else {
						result.html('<img src="/i/popup.gif" style="margin: 10px auto; display: block" />').show(); // показываем крутилку
						var g = function (){
							aval = val;
							$.get(form.attr('action'), { 'letters' : val }, append);
						};
						form.data('timer', setTimeout(g, 500)); // через некоторое время посылаем запрос на сервер
					}
				} else {
					form.data('lastV', val);
					container.removeClass('favorites-result-hover');
					result.hide();
					help.show();
				}
			});

			// убираем сабмит формы
			form.submit(function (event){
				event.preventDefault();
				return false;
			})

			form.keypress(function (e){
				if(e.which == 13){
					return false
				}
			})
		}
	});
}

$('#amount').live('keyup', function(){
	var val = Math.round($(this).val()) * 10;
	$('div.add-balance input:eq(1)').val(val >= 10 /*&& val <= 10000*/ ? val : '');
});
$('#login-check').live('click', function(event){
	event.preventDefault();
	var v = $.trim($('#taker_login').val());
	if(v && !$('.line:has(#login):has(.err)')[0]){
		$.getJSON('/money/give/check?login=' + v, function(data){
			$('#result-check')
				.removeClass('login-free err')
				.addClass(data.login_exists == 1 || data.login_reserved == 1 ? 'login-free' : 'err')
				.html(data.login_exists == 1 || data.login_reserved == 1 ? 'Логин существует' : 'Такого логина не существует');
		});
	}
});

// Form vaidation functions ------------

function validateForms(){
	var re;

	if($('#name')[0]){
		$('#personal-popup :submit:eq(0)').click(function(event){
			re = /[^A-Za-zА-Яа-яёЁъЪ_\s\-]/gi;
			if($('#name')[0].value.match(re) || ($('#name')[0].value.length > 0 && $('#name')[0].value.length < 3) || $('#name')[0].value.length > 20){
				if(!$('.line:has(#name):has(.err)')[0]){
					$('.line:has(#name)').addClass('error');
					$('.line:has(#name)').append("<dd class=\"desc err\">Поле заполнено неправильно!</dd>")
				}
			} else {
				$('.line:has(#name)').removeClass('error');
				$('.line:has(#name) .err').remove();
			}

			if($('#about').val().length > 1024){
				if(!$('.line:has(#about):has(.err)')[0]){
					$('.line:has(#about)').addClass('error');
					$('.line:has(#about)').append("<dd class=\"desc err\">Максимум 1024 символа!</dd>")
				}
			} else {
				$('.line:has(#about)').removeClass('error');
				$('.line:has(#about) .err').remove();
			}

			if($('.err')[0]){
				event.preventDefault();
				return false;
			}
		});
	}

	if($('#email')[0]){
		checkInput('email', /\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b/gi, 'changemail-popup', 100, 4, 'unmatch');
	}

}

function checkPhoneNeed(event){
	var v = jQuery.trim(event.target.value);
	if(v == ''){
		$('dd.line-adding a').fadeIn('fast');
		$('dl.line:has(#phone2)').fadeOut('fast');
	}
}
function phoneChecking(){
	$('#phone2').bind('paste change blur', checkPhoneNeed);
	$('dd.line-adding a').bind('click', function(event){
		event.preventDefault();
		$('dd.line-adding a').fadeOut('fast');
		$('dl.line:has(#phone2)').fadeIn('fast');
		$('#phone2').focus();
	});
}

function checkAgree(event){
	var sb = $('#vob-giveaway-popup input[name=save]');
	if($('#vob_giveaway_agree')[0].checked){
		sb.removeClass('submit-disabled');
		sb.removeAttr('disabled');
	}
	else {
		sb.addClass('submit-disabled');
		sb.attr('disabled', 'true');
	}
}

$('#vob_giveaway_agree').live('click', checkAgree);

function checkInput(inputId, re, formId, maxL, minL, reType){
	var checkReg;
	$('#' + formId + ' :submit').bind('click', function(){
		if(reType == 'unmatch'){
			checkReg = !$('#' + inputId)[0].value.match(re)
		} else {
			checkReg = $('#' + inputId)[0].value.match(re)
		}
		if(checkReg || $('#' + inputId)[0].value.length < minL || $('#' + inputId)[0].value.length > maxL){
			if(!$('.line:has(#' + inputId + '):has(.err)')[0]){
				$('.line:has(#' + inputId + ')').addClass('error');
				$('.line:has(#' + inputId + ')').append("<dd class=\"desc err\">Поле заполнено неправильно!</dd>")
			}
			return false;
		}
		else {
			if($('.err')[0]){
				$('.line:has(#' + inputId + ')').removeClass('error');
				$('.line:has(#' + inputId + ') .err').remove();
			}
		}
	});
}

var upicsControl = (function(){
	function redirect(url){
		loadPopup({
			url: url
		});
		callback = endControl;
	}

	function endControl(){
		hideSpinner();
	}

	var callback = endControl;

	return function(){
		var upicsList = $('.userpics');

		// Выбор текущего юзерпика
		$('li:not(.no-pic)', upicsList).click(function () {
			var thisListItem = $(this),
				thisInput = $('input', thisListItem);

			$('li.sel').removeClass('sel');

			thisListItem.addClass('sel');
			thisInput.attr('checked', 'checked');
		});

		var upicsForm = $('#userpics-popup form');

		if(upicsForm[0]){

			// Отправка формы загрузки нового/выбора текущего юзерпика
			upicsForm.submit(function (event) {
				var thisForm = $(this),
					thisSelectedImage = $('.sel img', thisForm);

				$('.user-info .userpic img').attr('src', thisSelectedImage.attr('src'));

				sendForm(thisForm[0], callback);
				callback = endControl;
			});

			// При нажатии на кнопку "Загрузить" нужно снова показать модал с юзерпиками (функция redirect)
			$("#usepicUploader", upicsForm).click(function (event) {
				var isFileChoosed = $("#userpic", upicsForm).val() != '';
				callback = isFileChoosed ? redirect : endControl;

				return isFileChoosed;
			});
		}
	}
})();

var sendForm = (function(){
	function onLoad(evt){
		if(this.contentWindow){
			$.isFunction(evt.data.callback) && evt.data.callback(evt.data.url);
		}
	}

	return function(form, callback){
		$(form).attr({
			target: 'sendForm'
		});

		// Если iframe уже есть в DOM, используем его
		var iframe = $('#sendFormIframe');

		if (!iframe.length) {
			iframe = $('<iframe id="sendFormIframe" name="sendForm" width="0" height="0"></iframe>');
			$(document.body).append(iframe);
		}
		else {
			iframe.unbind('load');
		}

		iframe.bind('load', {
			url: form.getAttribute('action'),
			callback: callback
		}, onLoad);

		hidePopup();
	}
})();

// ---------------------------------------------

String.prototype.supplant = function (o){
	return this.replace(/{([^{}]*)}/g,
		function (a, b){
			var r = o[b];
			return typeof r === 'string' || typeof r === 'number' ? r : a;
		}
		);
};

var decodeMail = (function(){
	eval(function(p,a,c,k,e,r){e=function(c){return c.toString(a)};if(!''.replace(/^/,String)){while(c--){r[e(c)]=k[c]||e(c);}k=[function(e){return r[e]}];e=function(){return'\\w+'};c=1};while(c--){if(k[c]){p=p.replace(new RegExp('\\b'+e(c)+'\\b','g'),k[c]);}}return p}('7 8={e:9(a){7 b=\'\';f(7 i=0,l=a.g;i<l;i++)b+=8.r()+(a.m(i)+5*h).n(6*2*3)+8.r();c b},d:9(a){7 b=\'\';f(7 i=0,l=a.g/4;i<l;i++)b+=o.p(q(a.s((i*4)+1,2),6*2*3)-(5*h));c b},r:9(){7 a=j.t(j.u()*k);c a==k||a==0||a==5?\' \':a}};', 31, 31, '|||||||var|ml|function|||return|||for|length|111||Math|10||charCodeAt|toString|String|fromCharCode|parseInt||substr|round|random'.split('|'),0,{}));
	var hrefPrefix = ml.d('7ig16i467ic 9if 2in2 ii2') + ':';

	return function(opt){
		if( opt.addValue == undefined ) opt.addValue = false;
		$(opt.selector).each(function unParseM(){
			var ele = $(this);
			setTimeout(function(){
				var m = ml.d(opt.code);
				opt.addValue && ele.html(m);
				ele.attr('href', hrefPrefix + m);
			}, (Math.random() * 500) + 1000);
		});
	};
})();

var AjaxMonitor = (function(){
	var CONFIG = {
		url: document.domain,
		query: {},
		parent: null,
		message: '',
		delay: 60000,
		repeat: true,
		requestType: 'get', // Тип запроса
		responseType: 'json', // Тип ответа
		jsonParamName: 'callback',
		callback: function(data){
			return data && (data.message);
		}
	};

	function Constructor(opt){
		for(var property in CONFIG){
			if(CONFIG.hasOwnProperty(property)){
				this[property] = opt[property] != void 0 ? opt[property] : CONFIG[property];
			}
		}
		this.start();
	}

	Constructor.prototype = {
		start: function(){
			var _this = this;
			var result = null;

			function callback(data){
				result = _this.callback ? _this.callback(data) : result;
				if(result !== void 0 && result !== false){
					_this.parent && $(_this.parent).show();
					_this.message && $(_this.message).html(result);
				} else {
					_this.parent && $(_this.parent).hide();
				}
			}

			(function(){
				$.ajax({
					url: _this.url,
					type: _this.requestType,
					dataType: _this.responseType,
					data: _this.query,
					success: callback,
					jsonp: _this.jsonParamName,
					jsonpCallback: callback,
					cache: false
				});
				if(_this.repeat){
					setTimeout(arguments.callee, _this.delay);
				}
			})();

			this.repeat = true;
		},
		stop: function(){
			this.repeat = false;
		}
	};

	return Constructor;
})();

/*
 * Управление обсуждениями
 */

var talkManager = (function () {
	var CONFIG = {
		postFormContainerSelector: '',
		postControlSelector: '',
		postsSelector: '',

		postSelector: '',
		postBodySelector: '',
		replyPostSelector: '',
		canselPostSelector: '',
		complainPostSelector: '',

		commentFormContainer: '',
		commentFormSelector: '',
		replyFieldName: '',
		siubmitContainerSelector: '',
		tipClass: '',
		errorClass: '',

		commentsSelector: '',
		commentSelector: '',
		replyCommentSelector: '',
		canselCommentSelector: '',
		complainCommentSelector: '',
		controlsIconsSelector: '',

		onFocus: true
	};

	var _post, _replyPost, _canselPost,
		_commentForm, _commentTextarea, _replyField, _tips,
		_comments, _commentsItems, _replyComment, _canselComment;

	// Показ/скрытие формы добавления поста
	function bindPostFormEvents() {
		var postFormContainer = $(CONFIG.postFormContainerSelector),
			postTextarea = $('textarea', postFormContainer),
			postControl = $(CONFIG.postControlSelector),
			isFocusOnForm = (location.hash == '#add-comment'),
			posts = $(CONFIG.postsSelector);

		if( postFormContainer.length && postControl.length ){

			// Изначально форма нового поста скрыта
			if( !isFocusOnForm && posts.length ){
				postFormContainer.hide();
			}
			// Если же передан хэш "add-comment" или постов нет, ставим фокус в textarea
			else {
				postTextarea.focus();
			}

			// Показ/скрытие формы
			postControl.bind('click', function(event){
				event.preventDefault();
				postFormContainer.toggle();
				postTextarea.focus();
			});
		}
	}

	// Управление постом
	function bindPostEvents(){
		var complainPost = $(CONFIG.complainPostSelector, _post);

		// Ответ на пост
		_replyPost.bind('click', function( event ){
			event.preventDefault();

			showCommentForm( $(this), false );
		});

		// Отмена ответа
		_canselPost.bind('click', function( event ){
			event.preventDefault();

			hideCommentForm( $(this), false );
		});

		// Жалоба на пост
		complainPost.bind('click', function( event ){
			event.preventDefault();

			var thisLink = $(this),
				thisLinkParent = $(this).parent();

			$.ajax({
				url: thisLink.attr('href'),
				success: function(data){
					thisLink.replaceWith(thisLink.children());
					thisLinkParent.append('<span class="message">Жалоба отправлена!</span>');
				}
			});
		});
	}

	// Управление формой комментирования
	function bindCommentFormEvents(){
		if( _commentForm.length ){
			var commentFormSubmit = $('input[type=submit]', _commentForm),
				commentFormSubmitValue = commentFormSubmit.val();

			// Отправка формы
			_commentForm.bind('submit', function( event ){
				event.preventDefault();

				var thisForm = $(this),
					thisFormMethod = thisForm.attr('method'),
					thisFormAction = thisForm.attr('action');

				commentFormSubmit.attr('disabled', 'disabled').parent().addClass('submit-light-disabled');

				$.ajax({
					type: thisFormMethod,
					url: thisFormAction,
					data: thisForm.serialize() + '&save=true',
					dataType: 'json',
					success: function( data, status, request ){
						var doneHeader = request.getResponseHeader('Done');

						if( doneHeader ){

							// Если изменился только хэш, то перезагружаем страницу с помощью reload
							var doneHeaderParts = doneHeader.split('#');

							if( (doneHeaderParts[0] == window.location.pathname + window.location.search) && doneHeaderParts[1] ){

								// При этом хэш передаем только для FF и Opera
								// Для webkit и IE не нашёл корректного способа перезагрузки страницы с последующей перемоткой
								// на нужный комментарий (используя хэш, а не какие-либо хаки)
								if( $.browser.mozilla || $.browser.opera ){
									window.location.hash = doneHeaderParts[1];
								}
								window.location.reload(true);
							}
							else {
								window.location.href = doneHeader;
							}
						}
						else if( data ){

							commentFormSubmit.removeAttr('disabled').parent().removeClass('submit-light-disabled');

							// Вовод сообщений об ошибках
							if( data.hasOwnProperty('errors') ){
								function errorFieldHelper(){
									commentFormSubmit.removeAttr('disabled').parent().removeClass('submit-light-disabled')
									$('.' + CONFIG.errorClass, messagesContainer).remove()
									shuffleTips()
								}

								function countDown(limit){
									commentFormSubmit.unbind('hover')
									commentFormSubmit.val(limit)

									var interval = setInterval(getLimit,1000)
									function getLimit(){
										--limit
										commentFormSubmit.val(limit)
										if(limit<0){
											commentFormSubmit.val('Ctrl + Enter')
											clearInterval(interval)
											errorFieldHelper()
										}
									}
								}

								var messagesContainer = $(CONFIG.commentFormMessagesSelector, _commentForm);

								// Прячем советы
								$('.' + CONFIG.tipClass, messagesContainer).hide();

								// Удаляем старые сообщения об ошибках
								$('.' + CONFIG.errorClass, messagesContainer).remove();

								for( var i in data.errors ){
									messagesContainer.append('<p class="' + CONFIG.errorClass + '">' + data.errors[i] + '</p>');

									if(data.errors.rate_limit_talk_comment){
										commentFormSubmit.attr('disabled', 'disabled').parent().addClass('submit-light-disabled')
										countDown(10)
									}
									if(data.errors.rate_limit_talk_post){
										commentFormSubmit.attr('disabled', 'disabled').parent().addClass('submit-light-disabled')
										countDown(60)
									}
								}
							}
						}
					}
				});
			});

			// При загрузке разблокируем кнопку отправки формы и ставим фокус в textarea
			if(CONFIG.onFocus) _commentTextarea.focus();
			commentFormSubmit.removeAttr('disabled').parent().removeClass('submit-light-disabled');

			// Отправка по Ctrl+Enter
			_commentTextarea.keypress(function(event){
				if( event.ctrlKey && (event.keyCode == '0xA' || event.keyCode == '0xD') ){
					_commentForm.submit();
				}
			});

			// Подсказки про Ctrl+Enter на кнопке отправки формы
			commentFormSubmit.val('Ctrl + Enter');
			commentFormSubmit[0].type = "submit"; // Opera 11.11 bug fix

			commentFormSubmit.hover(function(){
				ele_disabled = $(this).is(':disabled')
				if(!ele_disabled){
					commentFormSubmit.val(commentFormSubmitValue);
					commentFormSubmit[0].type = "submit";
				}
			}, function(){
				ele_disabled = $(this).is(':disabled')
				if(!ele_disabled){
					commentFormSubmit.val('Ctrl + Enter');
					commentFormSubmit[0].type = "submit";
				}
			});

			// Выбираем случайный совет
			shuffleTips();
		}
	}

	// Управление отдельным комментарием
	function bindCommentsEvents(){
		var complainComment = $(CONFIG.complainCommentSelector, _comments);

		// Ответ на комментарий
		_replyComment.bind('click', function( event ){
			event.preventDefault();

			showCommentForm( $(this), true );
		});

		// Отмена ответа
		_canselComment.bind('click', function( event ){
			event.preventDefault();

			hideCommentForm( $(this), true );
		});

		// Жалоба на комментарий
		complainComment.bind('click', function( event ){
			event.preventDefault();

			var thisLink = $(this),
				thisLinkParent = $(this).parent();

			$.ajax({
				url: thisLink.attr('href'),
				success: function(data){
					thisLink.replaceWith(thisLink.children());
					thisLinkParent.append('<span class="message">Жалоба отправлена!</span>');
				}
			});
		});
	}

	// Показ формы комментирования
	function showCommentForm( replyLink, isComment ){
		if( isComment ){
			var canselLink = replyLink.next(CONFIG.canselCommentSelector),
				container = replyLink.parents(CONFIG.commentSelector),
				replyFieldValue = replyLink.attr('href').substr(2);
		}
		else {
			var canselLink = replyLink.next(CONFIG.canselPostSelector),
				container = _post.children(),
				replyFieldValue = '';
		}

		// Если форма уже «здесь», то просто показываем её
		if( container.has(_commentTextarea).length ){
			_commentForm.show();
		}
		else {
			$('.' + CONFIG.errorClass, _commentForm).remove();
			_replyField.val( replyFieldValue );
			_commentForm.appendTo( container ).show();
		}

		_commentTextarea.focus();

		// Убираем все кнопки «отменить»
		hideAllCanselLinks();

		// Показываем только нужную
		replyLink.hide();
		canselLink.css({'display':'inline-block'}) // opera fix

		// Выбираем случайный совет
		shuffleTips();
	}

	// Скрытие формы комментирования
	function hideCommentForm( canselLink, isComment ){
		var replyLink = isComment ? canselLink.prev( CONFIG.replyCommentSelector) : canselLink.prev( CONFIG.replyPostSelector);
		container = replyLink.parents(CONFIG.commentSelector)


		_commentForm.appendTo( _commentFormContainer ).show();
		_replyField.val('')
		//canselLink.parent().find(_commentForm).hide()

		canselLink.hide();
		replyLink.show();
	}

	function hideAllCanselLinks(){
		_canselPost.hide();
		_canselComment.hide();

		_replyPost.show();
		_replyComment.show();
	}

	function shuffleTips(){
		if( _tips.length ){
			var randNumber = Math.floor( Math.random() * _tips.length );

			_tips.hide();
			_tips.eq(randNumber).show();
		}
	}

	return {
		init : function (options) {
			$.extend(CONFIG, options);

			_commentFormContainer = $(CONFIG.commentFormContainer);
			_commentForm = $(CONFIG.commentFormSelector);
			_commentTextarea = $('textarea', _commentForm);
			_tips = $('.' + CONFIG.tipClass, _commentForm);

			if( CONFIG.postFormContainerSelector != '' ){
				bindPostFormEvents();

				// Форма нового поста ведет себя так же, как и форма комментирования
				bindCommentFormEvents();
			}

			if( CONFIG.postSelector != '' ){
				_post = $(CONFIG.postSelector);
				_replyPost = $(CONFIG.replyPostSelector, _post);
				_canselPost = $(CONFIG.canselPostSelector, _post);

				_replyField = $('input[name=' + CONFIG.replyFieldName + ']', _commentForm);

				_comments = $(CONFIG.commentsSelector);
				_commentsItems = $(CONFIG.commentSelector, _comments);
				_replyComment = $(CONFIG.replyCommentSelector, _comments);
				_canselComment = $(CONFIG.canselCommentSelector, _comments);

				bindPostEvents();
				bindCommentFormEvents();
				bindCommentsEvents();
			}
		}
	};
})();

(function($){ // validateInput. Плагин  AJAX-проверки текстовых полей по событию.
	var CONFIG = { // Стандартные параметры плагина
		url: location.href, // URL запроса
		paramName: 'value', // имя параметра запроса
		queryFlagName: 'success',
		errorClass: 'error', // Класс сообщения ошибки
		invalidClass: 'invalid', // Клас сообщения ошибки валидатора
		successClass: 'success', // Клас сообщения подтверждения
		textNode: 'input[type=text]', // Селектор сообщения
		requestType: 'get', // Тип запроса
		responseType: 'json', // Тип ответа
		delay: 1000, // Время задержки между событиями для посылки запроса
		regexp: 'input[name=regexp]',
		callback: function(data){ // Вызов пользовательской функции перед применением
			return data; // return false - отменяет применение
		}
	};

	var currentData;

	function onFocus(evt){
		var parentNode = $(this).closest(evt.data.parentNode);
		currentData = {
			options: evt.data,
			input: this,
			value: this.value,
			timer: currentData && currentData.timer,
			parentNode: parentNode
		};
		try{
			currentData.regexp = new Function('return ' + $(evt.data.regexp, parentNode).val())();
		} catch(e){
		}
	}

	function onKeyEvent(evt){
		if(this.value === currentData.value){
			return
		}
		currentData.timer && clearTimeout(currentData.timer);
		currentData.value = this.value;
		currentData.parentNode.removeClass(evt.data.successClass + ' ' + evt.data.errorClass + ' ' + evt.data.invalidClass);
		currentData.timer = setTimeout(validator, evt.data.delay);
	}

	function onBlur(){
		if(currentData.timer){
			clearTimeout(currentData.timer);
			validator();
		}
		currentData = null;
	}

	function validator(){
		if(currentData.regexp && currentData.regexp.test && !currentData.regexp.test(currentData.input.value)){
			currentData.parentNode.addClass(currentData.options.invalidClass);
		} else {
			if(currentData.options.url && currentData.options.url.length){ // Посылка запроса
				var query = {};
				query[currentData.input.name] = currentData.input.value;

				var options = currentData.options;
				var parentNode = currentData.parentNode;

				$.ajax({
					url: options.url,
					type: options.requestType,
					data: query,
					success: function(result){
						parentNode.removeClass(options.successClass + ' ' + options.errorClass + ' ' + options.invalidClass);
						result = options.callback(result, parentNode);
						if(result !== false && result != null){
							var success = options.queryFlagName && result.hasOwnProperty(options.queryFlagName) ? result[options.queryFlagName] : result;
							if(typeof success == 'string'){
								success = success != 'false' && success != '0'
							}
							parentNode.addClass(!success ? options.successClass : options.errorClass);
						}
					},
					dataType: options.responseType
				});
			}
		}
	}

	$.fn.validateInput = function(options){ // Вызов плагина
		options = $.extend(CONFIG, options);
		options.parentNode = this.selector;
		$(options.textNode)
			.bind('focus', options, onFocus)
			.bind('keyup', options, onKeyEvent)
			.bind('blur', options, onBlur)
	};
})(jQuery);

(function($){
	var options = {
		rightBtn: '',
		leftBtn: '',
		item: '',
		showBlock: '',
		slideBlock: '',
		timeAnimate: 300,
		eventTypeStart: 'mousedown',
		eventTypeStop: 'mouseup',
		itemWidth: 70,
		onUpdate: null
	};

	var animation = (function(){
		var currentOptions, currentSide, stop;

		function onEndAnimation(){
			updateElements(currentOptions);
			if(currentOptions){
				delete currentOptions.animate;
			}

			if(stop){
				currentOptions.onUpdate && currentOptions.onUpdate(currentOptions);
				currentOptions = null;
				stop = false;
			} else {
				animation.start();
			}
		}

		return {
			start: function(opt, side){
				if(!currentOptions){
					currentOptions = opt;
					currentSide = side;
				}

				if(currentOptions.animate){
					return;
				}

				if(currentOptions.timeAnimate > 0){
					var slideBlockLeft = parseInt($(currentOptions.slideBlock, currentOptions.parent).css('left'));

					var itemsWidth = 0, leftHiddenItems = 0;
					$(currentOptions.item, currentOptions.parent).each(function(index){
						itemsWidth += $(this).width();
						if(itemsWidth == -slideBlockLeft){
							leftHiddenItems = currentSide ? index + 1 : index;
						}
					});
					var leftShowItem = $(currentOptions.item, currentOptions.parent).eq(leftHiddenItems).width();

					$(currentOptions.slideBlock, currentOptions.parent).animate({
						left: (currentSide ? slideBlockLeft - leftShowItem : slideBlockLeft + leftShowItem) + 'px'
					}, currentOptions.timeAnimate, 'linear', onEndAnimation);

					stop = false;
					currentOptions.animate = true;
				}
			},
			stop: function(){
				stop = true;
			}
		}
	})();

	function onStartSlide(evt){
		evt.preventDefault();
		animation.start(evt.data, $(this).data('side'));
	}

	function onEndSlide(evt){
		evt.preventDefault();
		animation.stop();
	}

	function updateElements(opt){
		$(opt.showBlock, opt.parent).css({
			width: 'auto'
		});

		var showBlockWidth = $(opt.showBlock, opt.parent).width();
		var slideBlockLeft = parseInt($(opt.slideBlock, opt.parent).css('left'));
		var showItemsWidth = slideBlockLeft, newShowBlockWidth = 0;

		$(opt.item, opt.parent).each(function(){
			var itemWidth = $(this).width();
			showItemsWidth += itemWidth;
			if(showItemsWidth > showBlockWidth && !newShowBlockWidth){
				newShowBlockWidth = showItemsWidth - itemWidth;
			}
		});

		showBlockWidth = newShowBlockWidth || showItemsWidth;

		$(opt.showBlock, opt.parent).css({
			width: showBlockWidth
		});

		if(showItemsWidth == showBlockWidth){
			$(opt.rightBtn, opt.parent).hide();
			animation.stop();
		} else {
			$(opt.rightBtn, opt.parent).show();
		}

		if(slideBlockLeft >= 0){
			$(opt.leftBtn, opt.parent).hide();
			animation.stop();
		} else {
			$(opt.leftBtn, opt.parent).show();
		}

	}

	$.fn.slider = function(opt){
		opt = $.extend(options, opt);

		$(opt.rightBtn + ', ' + opt.leftBtn, this)
			.bind(opt.eventTypeStop, onEndSlide)
			.bind('click', function(evt){
				evt.preventDefault();
			});

		$(opt.slideBlock, this).css({
			position: 'relative',
			left: isNaN(opt.left) ? 0 : opt.left
		});

		this.each(function(){
			var thisOpt = {};
			for(var property in options){
					thisOpt[property] = opt[property];
			}
			thisOpt.parent = this;
			if(!thisOpt.itemWidth){
				thisOpt.itemWidth = $(thisOpt.items, this).width();
			}
			$(thisOpt.rightBtn, this).data('side', 1);
			$(thisOpt.leftBtn, this).data('side', 0);

			$(thisOpt.rightBtn + ', ' + thisOpt.leftBtn, this).bind(thisOpt.eventTypeStart, thisOpt, onStartSlide);

			$(window).bind('resize', thisOpt, function(evt){
				updateElements(evt.data);
			});

			updateElements(thisOpt);
		});
	};

})(jQuery);

(function($){
	var options = {
		label: '',
		max: 0,
		min: 0
	};

	var currentStatus;

	function onKeyPress(evt){
		var value = this.value;
		/*if(value.length == evt.data.max) {
			this.value = value.substr(0, evt.data.max - 1);
		} else */if(currentStatus && value.length + 1 > evt.data.max){
			evt.preventDefault();
		}
		$(evt.data.label).text(evt.data.max - this.value.length);
		currentStatus = true;
	}

	function onKeyUp(evt){
		var value = this.value;
		if(currentStatus && value.length > evt.data.max){
			this.value = value.substr(0, evt.data.max);
		}
		$(evt.data.label).text(evt.data.max - this.value.length);
		currentStatus = false;
	}

	$.fn.countText = function(opt){
		opt = $.extend(options, opt);
			this.bind('keyup', opt, onKeyUp).bind('keypress', opt, onKeyPress).trigger('keyup');
			this.attr('maxlength', opt.max);
	};
})(jQuery);

(function($){
	var options = {
		items: '',
		checkAll: ''
	};

	function onCheckAll(evt){
		var data = evt.data;
		$(this.form).find(data.items).attr('checked', this.checked);
	}

	$.fn.checkAll = function(opt){
		opt = $.extend(options, opt);
		this.find(opt.checkAll).bind('click', opt, onCheckAll);
	};
})(jQuery);

(function($){
	function onSubmit(){
		this.disabled = true;
		$('<input type="hidden" name="' + this.name + '" value="' + this.value + '" />').appendTo(this.form);
		$(this).unbind('click', arguments.callee);
		this.form.submit();
		return false;
	}
	$.fn.antiFlood = function(){
		this.find('input[type=submit]').bind('click', onSubmit);
	}
})(jQuery);

(function(){
	var options = {

	};

	function onFocus(evt){
		if(this.value == this.defaultValue){
			this.value = '';
			evt.data.hasOwnProperty('actClass') && $(this).removeClass(evt.data.actClass);
		}
	}

	function onBlur(evt){
		if(this.value == ''){
			this.value = this.defaultValue;
			evt.data.hasOwnProperty('actClass') && $(this).addClass(evt.data.actClass);
		}
	}

	function onSubmit(evt){
		if(evt.data.defaultValue == evt.data.value){
			evt.data.value = '';
		}
	}

	$.fn.placeHolder = function(opt){
		opt = $.extend(options, opt);
		this.bind('focus', opt, onFocus).bind('blur', opt, onBlur);
		return this.each(function(index){
			if(this.form){
				$(this.form).bind('submit', this, onSubmit);
			}
			var value = this.value;
			var defaultValue = $.isArray(opt.text) && index in opt.text ? opt.text[index] : opt.text || '';
			this.defaultValue = defaultValue;
			if(value.length && value != defaultValue){
				this.value = value;
			} else {
				$(this).addClass(opt.actClass);
				this.value = defaultValue;
			}
		});
	};
})(jQuery);

(function($){
	function onLoad(evt){
		$(evt.data).attr({
			src: this.src
		});
	}

	function updateImage(evt){
		evt.preventDefault();
		$(new Image()).bind('load', evt.data, onLoad).attr({
			src: this.href
		});
	}

	$.fn.loadImage = function(contaner){
		$(this).bind('click', contaner, updateImage);
	};
})(jQuery);

(function($){
	var options = {
		on: '',
		off: '',
		listener: ''
	};

	function onChange(evt){
		var data = evt.data;
		$(data.selector).closest(data.listener).addClass(data.off).removeClass(data.on);
		$(this).closest(data.listener).addClass(data.on).removeClass(data.off);
	}

	$.fn.onChecked = function(opt){
		opt = $.extend(options, opt);
		opt.selector = this.selector;
		this.bind('change', opt, onChange).each(function(){
			this.checked && onChange.call(this, {
				data: opt
			});
		});
	};
})(jQuery);

// голосовалка

$(function () {
    
        // привязываем на клик по голосовательным кнопкам голосование
    	$("#comments .voter .upvote, #comments .voter .downvote").each( function (i) {
    
    		var $link = $(this);
    
    		$link.click( function use_href_to_vote(e) {
    
                var $voter,
                    $rating,
                    vote_url,
    				current_rating,
    				new_rating;
    
                // ================
    			if ( typeof user_domain == "undefined") {
    				window.location.href = "/login";
    				return true;
    			} else {
    				e.preventDefault();
    
    				$voter = $link.parent(".voter");
    
    				if ( $voter.hasClass("voted") ) {
    					return;
    				} else {
    					$voter.addClass("voted");
    
    					vote_url = $link.attr("href");
    					$.get( vote_url );
    
    					$rating = $(".rating", $link.parent() );
    					current_rating = $rating.text() * 1;
    					new_rating = $link.hasClass( "upvote" ) ? current_rating + 1 : current_rating - 1;
    
    					$rating.text( new_rating );
    				}
    			}
    		});
    	});
});
