function refresh() { 
	document.location.reload();
};


var phcount=1;
var d = document;
var offsetfromcursorY=30 // y offset of tooltip
var ie=d.all && !window.opera;
var ns6=d.getElementById && !d.all;
var tipobj,op;

function tooltip(el, txt) {
	tipobj = d.getElementById('mess');
	tipobj.innerHTML = txt;
	op = 1;	
	tipobj.style.opacity = op; 
	tipobj.style.visibility = "visible";
	el.onmousemove = positiontip;
	el.onmouseout = hide_info;
}

function hide_info(el) {
	d.getElementById('mess').style.visibility='hidden';
	el.onmousemove='';
}

function ietruebody(){
return (d.compatMode && d.compatMode!="BackCompat")? d.documentElement : d.body
}
function set_top(tp,set){
  document.getElementById(tp).style.display=set?'':'none';
}

function positiontip(e) {
	var curX=(ns6)?e.pageX : event.clientX+ietruebody().scrollLeft;
	var curY=(ns6)?e.pageY : event.clientY+ietruebody().scrollTop;
	var winwidth=ie? ietruebody().clientWidth : window.innerWidth-20
	var winheight=ie? ietruebody().clientHeight : window.innerHeight-20
	
	var rightedge=ie? winwidth-event.clientX : winwidth-e.clientX;
	var bottomedge=ie? winheight-event.clientY-offsetfromcursorY : winheight-e.clientY-offsetfromcursorY;

	if (rightedge < tipobj.offsetWidth)	tipobj.style.left=curX-tipobj.offsetWidth+"px";
	else tipobj.style.left=curX+"px";

	if (bottomedge < tipobj.offsetHeight) tipobj.style.top=curY-tipobj.offsetHeight-offsetfromcursorY+"px"
	else tipobj.style.top=curY+offsetfromcursorY+"px";
}


function signOut(){
	$.post('ajax.php?method=logout',
		function(data){
			$('#loginres').html('<img src="img/accept.gif" width="16px" height="16px"> Вышли!');
			setTimeout("refresh()", 1000);
		});
	$('#loginres').html('<img src="img/i2.gif" width="13px" height="13px"> Выход ...');
}
function go(url){
	document.location.href = SITE_URL + url;
}
function myXOR(a,b) {
  return ( a || b ) && !( a && b );
}
function isFilled(id){
	return $('#' + id).val() && $('#' + id).val() != $('#' + id).attr('title');
}



function tryToLogin(){
	$.post('ajax.php?method=login',
		{username: $('#login').val(),
		password:	$('#password').val()},
		function(data){
			if (data['res'] == 'ok'){
				$.cookie('user_id', data['user_id'], {expires: 365});
				$('#loginres').html('<img src="img/accept.gif" width="16px" height="16px"> Вошли на сайт!');
				setTimeout("refresh()", 1000); 
			} else {
				$('#loginres').html('<img src="img/error.gif" width="15px" height="15px"> Ошибка! Такой логин не зарегистрировал или пароль неверный! Если забыли пароль, нажмите <a href="/forgotpassword">тут</a>.');
			}
		}, 'json');
	$('#loginres').html('<img src="img/i2.gif" width="13px" height="13px"> Вход на сайт ...');
}
function tryToRegister(sess_id){
	if (!isFilled('password_r') || $('#password_r').val() != $('#password2_r').val()) {
		$('#loginres').html('<img src="img/error.gif" width="15px" height="15px"> Не введен пароль или пароли не совпадают!');
		return;
	}
	if (!isFilled('login_r')) {
		$('#loginres').html('<img src="img/error.gif" width="15px" height="15px"> Не введен логин!');
		return;
	}
	if (!isFilled('email_r')) {
		$('#loginres').html('<img src="img/error.gif" width="15px" height="15px"> Не введен e-mail!');
		return;
	}
	if (!isFilled('code_r')) {
		$('#loginres').html('<img src="img/error.gif" width="15px" height="15px"> Не введен код с картинки!');
		return;
	}
	$.post('ajax.php?method=register&PHPSESSID=' + sess_id,
		{username: $('#login_r').val(),
		password:	$('#password_r').val(),
		email: $('#email_r').val(),
		code: $('#code_r').val()},
		function(data){
			if (data['res'] == 'ok'){
				$('#loginres').html('<img src="img/accept.gif" width="16px" height="16px"> Регистрация завершена ' + data['login']);
				setTimeout("refresh()", 1000); 
			} else {
				$('#loginres').html('<img src="img/error.gif" width="15px" height="15px"> Ошибка! ' + data['info']);
			}
		}, 'json');
	$('#loginres').html('<img src="img/i2.gif" width="13px" height="13px"> Регистрация ...');
}

function tryToDeleteFile(_id){
	if (!confirm('Вы уверены?')) return;
	$.post('ajax.php?method=deletefile&id=' + _id,
		{user_id: $.cookie('user_id')},
		function(data){
			if (data['res'] == 'ok'){
				alert('Файл успешно удалён!');
			} else {
				alert(data['info']);
			}
		}, 
		'json');
}

function tryToDeletePhoto(_id){
	if (!confirm('Вы уверены?')) return;
	$.post('ajax.php?method=deletephoto&id=' + _id,
		{user_id: $.cookie('user_id')},
		function(data){
			if (data['res'] == 'ok'){
				alert('Изображение успешно удалёно!');
			} else {
				alert(data['info']);
			}
		}, 
		'json');
}

function eachAll(){
	$('.inpt').each(function(){
	  if ($(this).attr('title')) 
		$(this).val($(this).attr('title')).addClass('gray').focus(function(){
			if ($(this).val() == $(this).attr('title')) {
				$(this).val('');
				$(this).removeClass('gray');
			}
		}).blur(function(){
			if ($(this).val() == '') {
				$(this).val($(this).attr('title'));
				$(this).addClass('gray');
			}
		});
	});
}

function upd_pages(count, selected){
	$('.pages_data').html('Страницы: ');
  var b = false;
	selected = parseInt(selected);
	if (count > 10) {
		var k = selected - 5;
		if (k < 1) k = 1;
		for (var i = k; i < selected; i++) {
			if (i == selected){
				$('.pages_data').append((b ? ', ' : '') + '<b>' + i + '</b>');
			} else {
				$('.pages_data').append((b ? ', ' : '') + '<a href="#" onclick="req_page(' + i + '); return false;">' + i + '</a>');
			}
			b = true;
		}
		k = selected + 5;
		if (k > count) k = count; 
		for (i = selected ; i <= k; i++) {
			if (i == selected){
				$('.pages_data').append((b ? ', ' : '') + '<b>' + i + '</b>');
			} else {
				$('.pages_data').append((b ? ', ' : '') + '<a href="#" onclick="req_page(' + i + '); return false;">' + i + '</a>');
			}
			b = true;
		}
	} else {
		for (var i = 1; i <= count; i++) {
			if (i == selected){
				$('.pages_data').append((b ? ', ' : '') + '<b>' + i + '</b>');
			} else {
				$('.pages_data').append((b ? ', ' : '') + '<a href="#" onclick="req_page(' + i + '); return false;">' + i + '</a>');
			}
			b = true;
		}
	}
}

$(function(){
	eachAll();
});

