jQuery.fn.outerHTML = function(s) {
	return (s) ? this.before(s).remove()
		: jQuery("<p>").append(this.eq(0).clone()).html();
}

function captchaReload()
{
	$.get('/index/captcha', function(data){
		data = eval('(' + data + ')');
		$('#captcha_image').attr('src', data.src);
		$('#captcha-id').val(data.id);
	});
}

$(function(){
	$(".collColumn165 .blockOneElement a[id]").click(function(){    //fix bug with ajax in every link in bia & line
		$.get("/admin/logger.php",{href:$(this).attr("href")});
	}
	)
})





function pollVote(poll_id)
{
	$.post(
		'/polls/' + poll_id,
		{ 'variant' : $('#poll_' + poll_id + ' input[type=radio]').val() },
		function (data) {
			$('#poll_' + poll_id).html(
				$('#poll_' + poll_id + ' div:first').outerHTML() // store old poll header
				+ data
			);
		}
	);
	$('#poll_' + poll_id + ' p input').val('Загрузка...').attr('disabled', 'disabled');
}

function pollResults(poll_id)
{
	$.get(
		'/polls/' + poll_id,
		{'results': true},
		function (data) {
			$('#poll_' + poll_id).html(
				$('#poll_' + poll_id + ' div:first').outerHTML() // store old poll header
				+ data
			);
		}
	);
	$('#poll_' + poll_id + ' .linkToSection').html('Загрузка...');
}

function pollUnresults(poll_id)
{
	$.get(
		'/polls/' + poll_id,
		function (data) {
			$('#poll_' + poll_id).html(
				$('#poll_' + poll_id + ' div:first').outerHTML() // store old poll header
				+ data
			);
		}
	);
	$('#poll_' + poll_id + ' .linkToSection').html('Загрузка...');
}

function profileToggleChangePassword()
{
	var $div = $('#change_password');
	var $link = $('#change_password_link');
	if ($div.is(':visible')) {
		$div.hide('fast') // Hide inputs
			.find('input').each(function(){ // Empty inputs values
				this.old_value = this.value;
				this.value = '';
			});
		$link.html('Сменить пароль');
	} else {
		$div.show('fast') // Show inputs
			.find('input').each(function(){ // Restore inputs values
				this.value = this.old_value || this.value;
			});
		$link.html('Не менять пароль');
	}
	return false;
}

function profileSexOnChange()
{
	f = document.getElementById('form_profile');
	$label = $('#profile_sex_label');
	if (document.getElementById('profile_sex-').checked) {
		$label.css('padding-top', '36px');
	}
	else if (document.getElementById('profile_sex-f').checked) {
		$label.css('padding-top', '18px');
	} else {
		$label.css('padding-top', '0');
	}
}
function filterMonthDays(f,prefix)
{
	$('#' + prefix + 'year,#' + prefix + 'month,#' + prefix + 'day').bind('change',
		function(event,customCall)
		{
			f = this.form;
			if (!customCall) {
				this.favVal = parseInt(this.value);
			}
			month = parseInt($(f.elements[prefix+'month']).val());
			year = parseInt($(f.elements[prefix+'year']).val());
			inp_day = f.elements[prefix+'day'];

			var days = {1:31,2:29,3:31,4:30,5:31,6:30,7:31,8:31,9:30,10:31,11:30,12:31};
			if (year && ((year%4) || !(year%100) && (year%400))) {
				days[2] = 28;
			}

			if (days[month] < inp_day.options.length-1) {
				inp_day.options.length = 1 + days[month];
			} else if (days[month] > inp_day.options.length-1) {
				for( var i=inp_day.options.length; i<=days[month]; i++ ) {
					var o = document.createElement('option');
					o.text = o.value = i;
					inp_day.options[i] = o;
				}
			}
			if (inp_day.favVal && inp_day.favVal > inp_day.selectedIndex) {
				inp_day.value = Math.min(inp_day.favVal, inp_day.options.length-1);
			}

			if (!customCall) {
				$(inp_day).trigger('change',[true]);
			}
	}).each(function(){
		this.favVal = parseInt(this.value);
	});
}

