function initArticleDiv(id, selector, isTopBar, markupReadAndComments) {
	var $ = jQuery;
	var outer = $(selector);
	outer.css('display', 'block');	
	try {
		outer.find('.current-rating').hover(function() {displayVotingInformation($(this), isTopBar);}, function() {$('.voting-info-popup').remove();});
		if (window.userAuthenticated) {
			var aud = articleUserData[id];
			
			if (markupReadAndComments) {
				if (aud.newComments > 0) {
					outer.find('.comment-count').find('strong').css({color: 'red'});
				}		
				if (aud.read) {
					outer.find('.article-header a').css({color: '#666', 'text-decoration': 'line-through'});
				}
			}
			if (aud.vote == 0) {
				setArticleNotVoted(outer, id);
			} else {
				setArticleVoted(outer, aud.vote, id);
			}
		} else {
			outer.find('.vote-up, .vote-down').click(function() {
				window.location="/login.php?return=" + location.href;
			});
		}
	} catch (e) {
	}
}

function displayVotingInformation(target, isTopBar) {
	var $ = jQuery;
	var offset = target.offset();
	$('<div class="voting-info-popup">Tallet er et udtryk for det samlede antal stemmer artiklen har fået. Artiklerne sorteres dog ikke udelukkende efter antal af stemmer, tiden spiller også en faktor i rangeringen.</div>').appendTo($('body')).css({top: (offset.top | 0) + (isTopBar ? -6 : 0) + 'px', left: (0 | offset.left) + (isTopBar ? 30 : 45) + 'px'});
}

function getClearedUpDown(outer) {
	return {
		u: outer.find('.vote-up').unbind('click').unbind('mouseenter mouseleave').removeClass('vote-up-voted').removeClass('vote-up-hover').removeClass('vote-up-disabled'), 
		d: outer.find('.vote-down').unbind('click').unbind('mouseenter mouseleave').removeClass('vote-down-voted').removeClass('vote-down-hover').removeClass('vote-down-disabled')
	};
}

function sendVote(vote, article, callback) {
	var data = {vote: vote, article: article};
	jQuery.post('/elements/article_vote.php', data, function(result, text, xhr) {
		if (!xhr.status || !result.request_completed)
			return;
		callback(result.votes);
	});
}

function setArticleNotVoted(outer, article) {
	var b = getClearedUpDown(outer);
	b.u.hover(function() {b.u.addClass('vote-up-hover');}, function() {b.u.removeClass('vote-up-hover');}).click(function() {
		//send the vote up
		var votes = sendVote(1, article, function(votes) {
			setArticleVoted(outer, 1, article);
			updateCountDiv(outer, votes);
		});
	});
	b.d.hover(function() {b.d.addClass('vote-down-hover');}, function() {b.d.removeClass('vote-down-hover');}).click(function() {
		//send the vote down
		var votes = sendVote(-1, article, function(votes) {
			setArticleVoted(outer, -1, article);
			updateCountDiv(outer, votes);
		});
	});
}

function setArticleVoted(outer, vote, article) {
	var b = getClearedUpDown(outer);
	var se = vote >= 1 ? b.u : b.d;//self element
	var oe = vote >= 1 ? b.d : b.u;//other element
	var sc = vote >= 1 ? 'up' : 'down';//self class
	var oc = vote >= 1 ? 'down' : 'up';//other class
	
	se.addClass('vote-' + sc + '-voted').hover(function() {se.addClass('vote-' + sc + '-disabled');}, function() {se.removeClass('vote-' + sc + '-disabled');});
	oe.hover(function() {oe.addClass('vote-' + oc + '-hover');}, function() {oe.removeClass('vote-' + oc + '-hover');});
	
	se.click(function() {
		//cancel the vote
		var votes = sendVote(0, article, function(votes) {
			setArticleNotVoted(outer, article);
			updateCountDiv(outer, votes);
		});
	});
	
	oe.click(function() {
		//change the vote to the opposite
		var votes = sendVote(-vote, article, function(votes) {
			setArticleVoted(outer, -vote, article);
			updateCountDiv(outer, votes);
		});
	});
}

function updateCountDiv(outer, votes) {
	var countDiv = outer.find('.current-rating');
	$(countDiv[0]).html(String(votes));
}

