var Profile = {};

function change_avatar() {
	$('#upic').css('display', 'none');
	$('#upload_new').css('display', 'block');
	return false;
}

function change_avatar_cancel() {
	$('#upic').css('display', 'block');
	$('#upload_new').css('display', 'none');
	return false;
}

var Karma = {
	key: 0,
	rateslist_opened: 0,

	mouseover: function() {
		$("#ProfileHead td.k").removeClass("inactive");
	},
	mouseout: function() {
		$("#ProfileHead td.k").addClass("inactive");
	},
	help: function() {
		Karma.close();
		var el = $("#ProfileHead td.k div.help_popup");
		el.toggle();
	},
	help_close: function() {
		$("#ProfileHead td.k div.help_popup").hide();
	},
	vote: function(mark) {
		Karma.close();
		Karma.help_close();
		$.post("/sys/karma.php", {uid: Profile.uid, mark: mark, key: Karma.key}, function(data) {
			$("#ProfileHead td.k big").html(data);
		});
		return false;
	},
	show: function() {
		Karma.help_close();
		if(Karma.rateslist_opened != 0) {
			Karma.close();
			return false;
		}
		Karma.close();
		Karma.rateslist_opened = 1;

		var em = $("#ProfileHead td.k");
		$(document.body).append("<div id='RatesList'><img class='arr' src='/i/rateslist_arrow.gif' alt='' /><div>Минуточку...</div><p><a href='#' onclick='return Karma.close()'>закрыть</a></p></div>");
		var offset = em.offset();
		$("#RatesList").css("left", offset.left - 18 + "px").css("top", offset.top + 50 + "px");

		$.post("/sys/karma.php", {uid: Profile.uid}, function(data) {
			$("#RatesList div").html(data);
			$(document).bind('click', Karma.click_outside);
		});

	},
	close: function() {
		if(Karma.rateslist_opened == 0) return;
		$("#RatesList").remove();
		Karma.rateslist_opened = 0;
		$(document).unbind('click', Karma.click_outside);
		return false;
	},
	click_outside: function(e) {
		var $clicked=$(e.target);
		if(!($clicked.is('#RatesList') || $clicked.parents().is('#RatesList'))) {
			Karma.close();
		}
	}
}

var Upic = {
	show: function(url) {
		var html = "<div id='upic_show'><img src='" + url + "' alt='' /></div>";
		$(document.body).append(html);
		setTimeout(function() {$(document).click(Upic.click_outside)}, 100);
		return false;
	},
	click_outside: function() {
		$("#upic_show").remove();
		$(document).unbind("click", Upic.click_outside);
	}
}


$(function() {
	$("#ProfileHead td.k").hover(Karma.mouseover, Karma.mouseout);
});

