function makeurl(module, action)
{
	var url = '/';

	if (module)
	{
		url += module + '/';
	}
	else
	{
		url += $('#this_module').text() + '/';
	}
	
	if(action.length<1){
		url += 'index' + '/';
	}
	else {
		url += action + '/';
	}

	return url;
}

function ajaxpost(action, data, success, error, my)
{
	var req_module = null;
	var req_action = action;

	var url = action.split('/');
	if (url.length>1)
	{
		req_module = url[0];
		req_action = url[1];
	}

	$.ajax({'url':makeurl(req_module, req_action), 'dataType':'json', 'type':'POST', 'data':data, 'success':success, 'error': error, 'my': my});
}

function ajaxpost_xml(action, data, success, error, my)
{
	var req_module = null;
	var req_action = action;

	var url = action.split('/');
	if (url.length>1)
	{
		req_module = url[0];
		req_action = url[1];
	}

	$.ajax({'url':makeurl(req_module, req_action), 'dataType':'xml', 'type':'POST', 'data':data, 'success':success, 'error': error, 'my': my});
}


function ajax_disable(disable)
{
	if (disable)
	{
		$('.ajax').addClass('disabled');
		$('input.ajax, select.ajax').attr('disabled', 'disabled');
	}
	else
	{
		$('.ajax').removeClass('disabled');
		$('input.ajax, select.ajax').removeAttr('disabled');
	}
}

function toggle_inputs(inputs)
{
	var allchecked = true;
	inputs.each(function(){ allchecked = allchecked & this.checked; });

	if (allchecked)
	{
		inputs.removeAttr('checked');
	}
	else
	{
		inputs.attr('checked', 'checked');
	}
}





/*Голосование за блог*/
function voterBlog(blogId, znak) {
	ajaxpost('ajax/blogVoter', {'blogId': blogId, 'znak': znak}, voterBlog_success, voterBlog_error, {});
}
	
function voterBlog_success(data, textStatus){
	if (data.errno>0)
	{
		alert(data.error);
		return;
	}
	
	if (data.array.blog_rating.length>0)
	{
		$('.profile-blog > .voting > .total').html(data.array.blog_rating);
		$('.profile-blog > .voting > .count').html(data.array.blog_count_vote);
		
		if(data.array.status) {
			message_notice(data.array.message);
			if(data.array.znak == 'plus') $('.profile-blog > .voting').addClass("voted plus");
			if(data.array.znak == 'minus') $('.profile-blog > .voting').addClass("voted minus");
		}
		else{ 
			message_error(data.array.message); 
		}
	}
}

function voterBlog_error(xhr, textStatus) {
	alert(textStatus);
	alert(xhr.responseText);
}

/*Голосование за топик*/
function VoterTopic(topicId, znak) {
	ajaxpost('ajax/topicVoter', {'topicId': topicId, 'znak': znak}, voterTopic_success, voterTopic_error, {});
}
	
function voterTopic_success(data, textStatus){
	if (data.errno>0)
	{
		alert(data.error);
		return;
	}
	
	if (data.array.message.length>0)
	{
		$('#topic-'+ data.array.topicId +'> .infopanel > .total').html(data.array.topic_rating);
		$('#topic-'+ data.array.topicId +'> .infopanel > .count').html(data.array.topic_count_vote);
		if(data.array.topic_rating >= 0)$('#topic-'+ data.array.topicId +'> .infopanel').addClass('positive');
		if(data.array.topic_rating < 0)$('#topic-'+ data.array.topicId +'> .infopanel').addClass('negative');
		
		if(data.array.status) { 
			message_notice(data.array.message);
			if(data.array.znak == 'plus') $('#topic-'+ data.array.topicId +'> .infopanel').addClass("voted plus");
			if(data.array.znak == 'minus') $('#topic-'+ data.array.topicId +'> .infopanel').addClass("voted minus");
		}
		else{ 
			message_error(data.array.message); 
		}
	}
}

function voterTopic_error(xhr, textStatus) {
	alert(textStatus);
	alert(xhr.responseText);
}


//Голосование за пользователя
function voterUser(user, znak)
{
	ajaxpost('ajax/userVoter', {'user': user, 'znak': znak}, voterUser_success, voterUser_error, {});
}
	
function voterUser_success(data, textStatus)
{
	if (data.errno>0)
	{
		alert(data.error);
		return;
	}
	
	if (data.array.user_rating.length>0)
	{
		$('.profile-user > .voting > .total').html(data.array.user_rating);
		$('.profile-user > .voting > .count').html(data.array.user_count_vote);
		if(data.array.user_rating >= 0) {
			$('.profile-user > .voting').removeClass('negative');
			$('.profile-user > .voting').addClass('positive');	
		}
		if(data.array.user_rating < 0) {
			$('.profile-user > .voting').removeClass('positive');
			$('.profile-user > .voting').addClass('negative');
		}
		
		if(data.array.status) {
			message_notice(data.array.message);
			if(data.array.znak == 'plus') $('.profile-user > .voting').addClass("voted plus");
			if(data.array.znak == 'minus') $('.profile-user > .voting').addClass("voted minus");
		}
		else{ 
			message_error(data.array.message); 
		}
	}
}

function voterUser_error(xhr, textStatus)
{
	alert(textStatus);
	alert(xhr.responseText);
}

//Добавляем топик в избранное
function favoritesSend(topicId)
{
	ajaxpost('ajax/favoritesSend', {'topicId': topicId}, favoritesSend_success, favoritesSend_error, {});
}
function favoritesSend_success(data, textStatus)
{
	if (data.errno>0)
	{
		alert(data.error);
		return;
	}
	
	if (data.array.message.length>0) {
		
	
	
		if(data.array.status) {
			$('#topic-'+ data.array.topicId +'> .infopanel > .favorite').addClass("active");
			message_notice(data.array.message);
		}
		else{ 
			$('#topic-'+ data.array.topicId +'> .infopanel > .favorite').removeClass("active");
			message_error(data.array.message); 
			
		}
	}
}

function favoritesSend_error(xhr, textStatus)
{
	alert(textStatus);
	alert(xhr.responseText);
}

function message_error(text)
{
	$.jGrowl(text, { 
		theme: 'error',
		header: 'Ошибка',
		life: 2000 
		});
}

function message_notice(text) {
	$.jGrowl(text, {
		theme: 'success',
		header: '',
		life: 2000 
	});
	
}
