function DarkVeil()
{
	this.max_opacity = 0.5;
	this.step = 0.1;
	this.round = 1;
	this.time = 10;
	this.color = '#000000';
	this.z_index = 1;
	
	this.id = 'darkness';
	this.content_object;
	this.darkness;
	
	this.opacity = 0;
	this.interval_id;
	this.display = false;
}

DarkVeil.prototype.detectIE6 = function(content_id)
{
	var browser = navigator.appName;
	if (browser == "Microsoft Internet Explorer")
	{
		var b_version = navigator.appVersion;
		var re = /\MSIE\s+(\d\.\d\b)/;
		var res = b_version.match(re);
		if (res[1] <= 6)
			return true;
	}
	
	return false;
}

DarkVeil.prototype.show = function(content_id)
{
	if(this.display)
		return;
	
	this.content_object = document.getElementById(content_id);
	if(!this.content_object)
		return;
	
	this.darkness = document.getElementById(this.id);
	
	if(!this.darkness)
	{
		this.darkness = document.createElement('div');
		
		this.darkness.id = this.id;
		this.darkness.style.display = 'none';
		this.darkness.style.position = 'absolute';
		this.darkness.style.zIndex = this.z_index;
		this.darkness.style.top = '0';
		this.darkness.style.left = '0';
		this.darkness.style.backgroundColor = this.color;
		this.darkness.style.width = '100%';
		this.darkness.style.height = document.documentElement.scrollHeight+'px';
		
		document.body.appendChild(this.darkness);
	}
	
	this.content_object.style.display = 'none';
	this.content_object.style.zIndex = this.z_index+1;
	
	if(this.detectIE6())
	{
		this.content_object.style.position = 'absolute';
		
		this.content_object.style.left = Math.round(document.documentElement.clientWidth/2-parseInt(this.content_object.style.width)/2)+(document.documentElement.scrollLeft)+'px';
		this.content_object.style.top = Math.round(document.documentElement.clientHeight/2-parseInt(this.content_object.style.height)/2)+(document.documentElement.scrollTop)+'px';
	}
	else
	{
		this.content_object.style.position = 'fixed';
		
		this.content_object.style.left = Math.round(document.documentElement.clientWidth/2-parseInt(this.content_object.style.width)/2)+'px';
		this.content_object.style.top = Math.round(document.documentElement.clientHeight/2-parseInt(this.content_object.style.height)/2)+'px';
	}
	
	var object = this;
	
	this.interval_id = setInterval(function(){object.cutOpacity();}, this.time);
}

DarkVeil.prototype.cutOpacity = function()
{
	this.opacity += this.step;
	
	this.setOpacity(this.darkness, this.opacity*this.max_opacity);
	this.setOpacity(this.content_object, this.opacity);
	
	if(!this.display)
	{
		this.darkness.style.display = 'block';
		this.content_object.style.display = 'block';
		
		this.display = true;
	}
	
	if(this.opacity>=1)
	{
		clearInterval(this.interval_id);
		
		var object = this;
		
		this.darkness.onclick = function(){object.hidden();}
		
		document.onkeydown = function(event)
		{
			var kCode = window.event ? window.event.keyCode : (event.keyCode ? event.keyCode : (event.which ? event.which : null));
			
			if(kCode==27)
				object.hidden();
		};
		
		return;
	}
	
	return;
}

DarkVeil.prototype.hidden = function()
{
	if(!this.display)
		return;
	
	this.darkness.onclick = null;
	document.onkeydown = null;
	
	var object = this;
	
	this.interval_id = setInterval(function(){object.addOpacity();}, this.time);
}

DarkVeil.prototype.addOpacity = function()
{
	if(this.opacity<=0)
	{
		clearInterval(this.interval_id);
		
		this.darkness.style.display = 'none';
		this.content_object.style.display = 'none';
		
		this.display = false;
		
		return;
	}
	
	this.opacity -= this.step;
	
	this.setOpacity(this.darkness, this.opacity*this.max_opacity);
	this.setOpacity(this.content_object, this.opacity);
	
	return;
}

DarkVeil.prototype.setOpacity = function(object, opacity)
{
	if(opacity<0)
		opacity = 0;
	
	if(opacity>1)
		opacity = 1;
	
	var round = Math.pow(10, this.round);
	opacity = Math.round(opacity*round)/round;
	
	object.style.opacity = opacity;
	object.style.filter = 'alpha(opacity='+opacity*100+')';
	object.style['-moz-opacity'] = opacity;
	object.style['-khtml-opacity'] = opacity;
}

var dark_veil = new DarkVeil;
dark_veil.color = '#381834';
dark_veil.z_index = 500;
dark_veil.step = 1;

function questionForm()
{
	dark_veil.show('question_form');
}

function questionConfirm()
{
	dark_veil.show('question_confirm');
}

function commentForm(parent_id)
{
	parent_id = parent_id || 0;
	
	$('#comment_parent_id').val(parent_id);
	
	dark_veil.show('comment_form');
}

function checkAnchor()
{
	if(location.hash=='#question_confirm')
		questionConfirm();
}

function authForm()
{
	dark_veil.show('auth_form');
}

function showMessForm()
{
	$('div.message_form').slideDown();
	
	return false;
}

var mess_checked = false;

function selectAllMess()
{
	mess_checked = !mess_checked;
	
	$('#del_mess input').attr('checked', mess_checked);
	
	return false;
}

function delSelectMess()
{
	if(!confirm('Точно удалить?'))
		return false;
	
	$('#del_mess').submit();
	
	return false;
}

function searchMess(a)
{
	$(a).parent().submit()
	
	return false;
}

function delMess()
{
	if(!confirm('Точно удалить?'))
		return false;
	
	document.forms.del_messages.submit();
	
	return false;
}

function importantMess(obj)
{
	var a = $(obj);
	var color = a.css('color');
	
	a.css('color', '#cccccc');
	
	function callback(data)
	{
		a.css('color', color);

		if(parseInt(data))
			a.text('Убрать из важных');
		else
			a.text('Пометить как важное');
	}
	
	$.post(document.location, {important:1}, callback);
	
	return false;
}

function setRating(obj)
{
	var a = $(obj);
	var div = a.parent();
	var id = div.attr('rel');
	var rating = a.attr('rel');
	var color = a.css('color');
	
	a.css('color', '#cccccc');

	function callback(data)
	{
		a.css('color', color);
		
		if(data=='error')
			alert('Ошибка');
		else if(data=='error_auth')
			alert('Чтобы голосовать за фото, Вам нужно авторизоваться или зарегистрироваться');
		else if(data=='error_exists')
			alert('Вы уже голосовали за это фото');
		else
			$('b.rating'+id).text(data);
	}
	
	$.get('/user', {'mode':'setRating', 'id':id, 'rating':rating}, callback);
}

function buildRating()
{
	var div = $(this);
	
	for(var i=1; i<=5; i++)
	{
		var a = $('<a />');
		a.text('+'+i);
		a.attr('rel', i);
		a.attr('title', 'Ваша оценка');
		a.attr('href', '#'+i);
		a.attr('onclick', 'setRating(this)');
		// a.click(setRating);
		
		div.append(a);
		div.append(' ');
	}
}

function initRating()
{
	$('div.rating').each(buildRating);
}

$(initRating);

function addFriend(obj, friend_id)
{
	var a = $(obj);
	var color = a.css('color');
	
	a.css('color', '#cccccc');
	
	function callback(data)
	{
		a.css('color', color);
		
		if(data == 'error')
			a.text('Ошибка');
		else
			a.replaceWith('<span>Предложение отправлено</span>');
	}
	
	$.post('/private?mode=addFriend', {'friend_id':friend_id}, callback);
	
	return false;
}

function deleteFriend(obj, friend_id)
{
	var a = $(obj);
	var color = a.css('color');
	
	a.css('color', '#cccccc');
	
	function callback(data)
	{
		a.css('color', color);
		
		if(data == 'error')
			a.text('Ошибка');
		else
			a.replaceWith('<span>Убран</span>');
	}
	
	$.post('/private?mode=deleteFriend', {'friend_id':friend_id}, callback);
	
	return false;
}

function acceptFriend(obj, friend_id)
{
	var a = $(obj);
	var color = a.css('color');
	
	a.css('color', '#cccccc');
	
	function callback(data)
	{
		a.css('color', color);
		
		if(data == 'error')
			a.text('Ошибка');
		else
			a.replaceWith('<span>Принято</span>');
	}
	
	$.post('/private?mode=acceptFriend', {'friend_id':friend_id}, callback);
	
	return false;
}

function rejectFriend(obj, friend_id)
{
	var a = $(obj);
	var color = a.css('color');
	
	a.css('color', '#cccccc');
	
	function callback(data)
	{
		a.css('color', color);
		
		if(data == 'error')
			a.text('Ошибка');
		else
		{
			a.parent().siblings().find('a').replaceWith('<span>Принять предложение</span>');
			a.replaceWith('<span>Отклонено</span>');
		}
	}
	
	$.post('/private?mode=rejectFriend', {'friend_id':friend_id}, callback);
	
	return false;
}

function recallFriend(obj, friend_id)
{
	var a = $(obj);
	var color = a.css('color');
	
	a.css('color', '#cccccc');
	
	function callback(data)
	{
		a.css('color', color);
		
		if(data == 'error')
			a.text('Ошибка');
		else
			a.replaceWith('<span>Отозвано</span>');
	}
	
	$.post('/private?mode=recallFriend', {'friend_id':friend_id}, callback);
	
	return false;
}

