// DEPENDENT on prototype.js

function rateArticle(elementid, article_rater, rater_pos){
	this.elementid = elementid;
	// detects which link has been clicked on rater
	Event.observe($(this.elementid), 'click', function(event) {
		
		var element = Event.findElement(event, 'a');
		var urlParts = element.href.split('/').slice(3);	// split the url and drop the domain from the start
		var url = '/' + urlParts.join('/');				// rebuild the url to post to
		var rating = parseFloat(urlParts.pop());			// get rating value from the end of the array
		
		if (article_rater){
			var currentRating = parseInt($$('span.rating')[0].innerHTML);
			var currentVotes  = parseInt($$('span.total-votes')[0].innerHTML);
			var previousVote  = parseInt($$('span.previous-vote')[0].innerHTML);
		} else {
			var currentRating = parseInt($$('span.rating')[rater_pos].innerHTML);
			var currentVotes  = parseInt($$('span.total-votes')[rater_pos].innerHTML);
			var previousVote  = parseInt($$('span.previous-vote')[rater_pos].innerHTML);
		}
		
		var newRating, newVotes, previousVote;
		
		if (previousVote == 0){
			newVotes = ++currentVotes;
			newRating = currentRating + rating;
		} else {
			if ((previousVote == 1) && (rating != 1)) {
				newRating = (currentRating - 1) + rating;
			} else if ((previousVote == -1) && (rating != -1)) {
				newRating = (currentRating + 1) + rating;
			} else {
				newRating = currentRating;
			}
			newVotes = currentVotes;
		}
		
		if(newRating > 0) newRating = "+" + newRating;
		
		//if (newRating > 0) newRating = '+' + newRating;
		var voteTxt = ' vote';
		if (newVotes != 1) voteTxt += 's';
		
		new Ajax.Request(url, {
			method:'get',
			onSuccess: function(transport){
				
				
				if (article_rater){
					$$('span.rating')[0].update(newRating);
					$$('span.total-votes')[0].update(newVotes + voteTxt);
					$$('span.previous-vote')[0].update(rating);
				} else {			
					$$('span.rating')[rater_pos].update(newRating);
					$$('span.total-votes')[rater_pos].update(newVotes + voteTxt);
					$$('span.previous-vote')[rater_pos].update(rating);
					
					if($$('p.rating')[rater_pos] != null){
						$$('p.rating')[rater_pos].update('<span class="rating-count"></span>');	
						
						newRating = newRating.toString();
						
						var status;
						if(newRating == "+1" || newRating == "-1") { status = ' person found this review'; }
						else { status = ' people found this review'; }
						if(newRating < 0) {
							status += ' not useful'; 
						}
						else { status += ' useful' }
						
						if(newRating != "0") newRating = newRating.substr(1);
						$$('span.rating-count')[rater_pos].update(newRating + status);
					}
				}
				
				if (rating == 1){
					$$('a.thumb_up')[rater_pos].setStyle({ backgroundPosition: '0 0' });
					$$('a.thumb_dn')[rater_pos].setStyle({ backgroundPosition: '0 -13px' });
				} else if (rating == -1){
					$$('a.thumb_up')[rater_pos].setStyle({ backgroundPosition: '0 -13px' });
					$$('a.thumb_dn')[rater_pos].setStyle({ backgroundPosition: '0 0' });
				}
			},
			onFailure: function(){
				alert('Sorry, something has gone wrong.\nYour rating has not been recorded, please try again later');
			}
		});
	});
}

function thumbsUpDown(){
	if ($('thumbsupdown')){
		var myRater = new rateArticle('thumbsupdown', true, 0);
	}
	if ($$('div.thumbsupdown')) {
		thumbsCount = new Array();
		thumbsCount = $$('div.thumbsupdown');
		
		for (var i = 1; i <= thumbsCount.length; i++){
			if (!$('thumbsupdown')){
				rater_pos = i - 1;
			} else {
				rater_pos = i;
			}
		
			thumbsCount[i - 1].down().setAttribute('id', 'thumber_' + i, rater_pos);
			thumbsCount[i - 1] = new rateArticle('thumber_' + i, false, rater_pos);
		}
	}
}

Event.observe(window, 'load', thumbsUpDown, false);
