var count = 0;
$(document).ready(function(){
	$('table.bg tr td div').css({'height':$(window).height()});
	$(window).resize(function(){$('table.bg tr td div').css({'height':$(window).height()});});
	$.ajaxSetup({
		timeout: 5000,
		type: "POST",
		cache: false,
		async: true,
		contentType: "application/x-www-form-urlencoded"
	});
	
/*	var fade_tm;
	$('#logo').live('mouseover', function(){$('#logo').fadeOut('fast');});
	$('#google-map').live('mouseover', function(){clearTimeout(fade_tm);});
	$('#google-map').live('mouseout', function(){fade_tm = setTimeout(function(){$('#logo').fadeIn('fast')}, 800);});
*/
	$("input.in:first").focus();
	$('input.in').keyup(function(event){ if (event.keyCode == 13) login(); });
	$("#trylog").click(function(){login();});

	$(".logout").click(function(){
		$.ajax({
			url:"/ajax/ajax/auth/",
			data:"event=logout",
			success: function(XMLHttpRequest){ if (XMLHttpRequest == 'ok') top.location.href = '/'; }
		});
	});
	$(".reg").live('click', function(){
		var ev = $(this).attr('name');
		var data = '';
		$('table.edit-data tr td input, table.edit-data tr td select, #write-message textarea').each(function(){
			data +='&'+$(this).attr('name')+'='+$(this).val();
		});
		$.ajax({
			url:"/ajax/ajax/reg/",
			data:"event="+ev+data,
			success: function(answer)
			{
				var top = $(window).height() / 2 + $(document).scrollTop();
				switch (ev)
				{
					case 'reg':
					case 'restore':
					case 'getact':
					case 'help':
						$('#popup').html(answer.replace('@@ok', '')).show().css({'top':(top - $('#popup').height() / 2)+'px'});
						break;
					case 'getcode':
					case 'newuser':
						answer = answer.split('@@');
						if (answer[1] == 'ok') $('#popup').html(answer[0]).show().css({'top':(top - $('#popup').height() / 2)+'px'});
						else $('#popup > div > p').html(answer[0]);
						break;
					case 'setcode':
					case 'activation':
						answer = answer.split('@@');
						$('#popup > div > p').html(answer[0]);
						if (answer[1] == 'ok')
						{
							$('#popup > div > a').hide();
							setTimeout("$('#popup').fadeOut(1000, function(){$('#popup').html('')});", 1000);
						}
						break;
				}
			}
		});
	});
	$('#close-popup').live('click', function(){$('#popup').html('');});
	if ($('div.center > div.bn').html() == "") $('div.center > div.bn').remove();
	if ($('div.center').height() < $(window).height() - 40) $('div.center').css({'margin':(($(window).height() - $('div.center').height()) / 2)+'px 0 0 0'});
	else $('div.center').css({'margin':'-10px 0 0 0'});
});

function login()
{
	var login = $('#login').val();
	var pass = $('#password').val();
	var remember = $('#remember').is(':checked')?1:0;
	if (login.length == 0 || pass.length == 0)
	{
		$('#message').html('Ââåäèòå äàííûå').show('fast');
	 	return;
	}
	$.ajax({
		url:"/ajax/ajax/auth/",
		data:"event=login&login="+login+"&pass="+pass+"&rlp="+remember,
		success:function(answer, textStatus){
			if (answer == 'ok') document.location = '/';
			else $('#message').html(answer).show('fast');
		}
	});
}

function imgChange(speed)
{
	if (speed == 'undefined') speed = 300;
	var rnd, im1, im2, src;
	for (i = 0; i < 7; i++)
	{
		rnd = Math.floor(Math.random() * count);
		im1 = $('table.bg tr td div img:eq('+rnd+')');
		rnd = Math.floor(Math.random() * count);
		im2 = $('table.bg tr td div img:eq('+rnd+')');
		src = im1.attr('src');
		im1.attr('src', im2.attr('src'));
		im2.attr('src', src);
	}
	setTimeout("imgChange("+speed+")", speed);
}
