(function(){

window.showFullImage = function(src) {
	var img = $('<img src="'+src+'" alt="" class="content-image-fill" />');
	img
	.click(function(){
		$.screen.hide();
		img.remove();
		$.tip().hide();
	})
	.tip('Нажми, чтобы закрыть');
	$('body').append(img);

	var w_w = window.screenWidth();
	var w_h = window.screenHeight();

	img.opacity(0);
	img.show();
	if (img.width() > w_w) {
		img.css('width', (w_w - 40));
	}
	if (img.height() > w_h) {
		img.css('height', (w_h - 40));
		img.css('width', 'auto');
	}
	$.screen.show(img);
	img.opacity(100);

}

window.comments = {
	block: false,

	get: function(i, p) {
		if (!this.block) this.block = $('#comments');
		var block = this.block;
		block.opacity(50);
		block.load('/item_comments', {'i':i, 'cp':p}, function(){
			block.opacity(100);
		});
		return false;
	},

	add: function(i) {
		var form = $('#body .add_comment form');
		var msg = $('#body .add_comment textarea').val();
		if (!msg) {
			alert('Напиши чтонибуть');
			return false;
		}
		form.opacity(30);
		form.children().attr('disabled', 'disabled');
		$.post('/item_add_comment', {'i':i, 'm': msg}, function(){
			form.opacity(100);
			form.children().removeAttr('disabled');
			comments.get(i, 1);
		});
		$('#body .add_comment textarea').val('');
	}
};

window.ctVote = function(i, v) {
	var contest_voter = $('#contest_voter');

	contest_voter.text('...');
	$.tip().hide();
	$.getJSON('/contest_item_vote', {'i': i, 'v':v}, function(r){
		if (r.rating) {
			$('#body .contest .left .rating strong').text(r.rating);
			$('#body .contest .left .counts').text('-'+r.down+', +'+r.up);
		}
		contest_voter.text(r.msg || 'error');
	});
}

var voterTips = {
	1: 'полный отстой!',
	2: 'отстой',
	3: 'ничё так',
	4: 'прикольно',
	5: 'супер!'
}

$(function(){

	var voter = $('#voter');
	if (voter.size() == 1) {
		var voterData = voter.get(0).onclick();

		function vote(v) {
			voter.text('...');
			$.tip().hide();
			$.getJSON('/item_vote', {'i':voterData.i, 'v':v}, function(r){
				$('#body .item-info .ratings .total strong').text(r.total_rating);
				$('#body .item-info .ratings .week strong').text(r.week_rating);
				$('#body .item-info .ratings .day strong').text(r.day_rating);
				voter.text(r.msg || 'error');
			});
		}

		$.each(voterTips, function(n, v){
			$('<img src="/images/0.gif"/>')
			.css({
				'width': '16px',
				'height': '16px',
				'background-image': 'url(/images/rating_stars.gif)',
				'cursor': 'pointer'
			})
			.appendTo(voter)
			.mouseover(function(){
				$(this).css({'background-position': '0px -32px'})
				.prevAll().css({'background-position': '0px -32px'})
			})
			.mouseout(function(){
				$(this).css({'background-position': '0px 0px'})
				.prevAll().css({'background-position': '0px 0px'})
			})
			.click(function(){
				vote(n);
			})
			.tip(v);
		});
	}

	/////////

});

})();