jQuery.fn.outerHTML = function(s) {
	return (s) ? this.before(s).remove()
		: jQuery("<p>").append(this.eq(0).clone()).html();
}
$(document).ready(function(){
	//timeNow=new Date();
	//$.get("/index/timer?time="+(timeNow.getTime()-clockStart));
	//$(".timer").html("Страница загружалась:"+(timeNow.getTime()-clockStart)+'мс');

	$("#DIV_NNN_4391").attr("src","/js/nnn.html");
	$("#DIV_NNN_4347").attr("src","/js/nnn.html");
	$("#MarketGid").attr("src","/js/mg.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 commentsAjaxInit(table_name, object_id)
{
	$('#comments_pager a').click(function(){
		commentsLoad(table_name, object_id, this.href.substring(this.href.lastIndexOf('/')+1));
		return false;
	});
}

function commentsLoad(table_name, object_id, page)
{
	$('#comments_pager > *').html('<img src="/img/loader.gif" alt="Загрузка..." title="Загрузка..." />');
	$.get(
		'/comments/list',
		{
			table_name:table_name,
			object_id:object_id,
			page:page
		},
		function(data){
			$('#comments_list').html(data);
		}
	);
}

comments_edit_current = null;
function commentsEditToggle(comment_id, content)
{
	if ($('#comment_' + comment_id + '_edit_form').is(':visible')) {
		commentsEditHide(comment_id);
		comments_edit_current = null;
	} else {
		commentsEditHide(comments_edit_current);
		commentsEditShow(comment_id, content);
		comments_edit_current = comment_id;
	}
}

function commentsEditShow(comment_id, content)
{
	$('#comment_' + comment_id + ' .comment_content').after(
		'<form method="POST" onsubmit="return commentsEditPost(this);" id="comment_' + comment_id + '_edit_form" style="display: none;">'
		+ '<input type="hidden" name="comments_id" value="' + comment_id + '" />'
		+ '<textarea name="comments_content">' + content + '</textarea>'
		+ '<div class="buttons"><input type="submit" class="submit" name="comment_update" value="Сохранить изменения" /> <input type="reset" value="отмена" onclick="commentsEditHide(' + comment_id + ');" />'
		+ '</form>'
	).hide('fast');
	$('#comment_' + comment_id + '_edit_form').show('fast');
}

function commentsEditHide(comment_id)
{
	$('#comment_' + comment_id + '_edit_form').hide('fast', function(){$(this).remove()});
	$('#comment_' + comment_id + ' .comment_content').show('fast');
}

function commentsEditPost(f)
{
	$('.submit', f).attr('disabled', 'disabled');
	$.post('/comments/edit',
		{
			comments_id: f.comments_id.value,
			comments_content: f.comments_content.value
		},
		function(response){
			try {
				response = eval('(' + response + ')');
				var error = 'Редактирование комментария: ';
				for (var i in response['comments_content']) {
					error += response['comments_content'][i] + ', ';
				}
				alert( error.substring(0, error.length-2));
			} catch (e) {
				// If no error, HTML returned
				$('#comments_list').html(response);
			}
			$('.submit', f).attr('disabled', false);
		}
	);
	return false;
}

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 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);
	});
}


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 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 add_favorite(a) {
   title=document.title;
   url=document.location;
   try {
     // Internet Explorer
     window.external.AddFavorite(url, title);
   }
   catch (e) {
     try {
       // Mozilla
       window.sidebar.addPanel(title, url, "");
     }
     catch (e) {
       // Opera
       if (typeof(opera)=="object") {
         a.rel="sidebar";
         a.title=title;
         a.url=url;
         return true;
       }
       else {
         // Unknown
         alert('������� Ctrl-D ����� �������� �������� � ��������');
       }
     }
   }
   return false;
 } 
