// global variables
var nReviewsmini = 0;
var aReviews = new Array();

var nCurrentRating = 0, nPreviousRating = 0;
var nHighestTotal = 0, nCurrentTotal = 0;
var nAverageRating = 0;

// functions
function sortReviewsBy(sCriteria) {
	switch(sCriteria) {
		case 'recent':
			aReviews.sort(function(a,b){ return b['idmini'] - a['idmini']; });
		break;
		
		case 'highest':			
		case 'lowest':
			if (sCriteria == 'lowest') {
				aReviews.sort(function(a,b){ return a['rating'] - b['rating']; });
			} else {
				aReviews.sort(function(a,b){ return b['rating'] - a['rating']; });
			}
		break;
	}
	
	$.each(aReviews, function(item, review) {
		eReview = '#productreview-' + item;

		$(eReview + ' p.productreviews-item-info span.reviews-body-rate img.productreviews-item-rating').attr('src', sImagesPath + 'rating' + review['rating'] + '.gif');
		$(eReview + ' p.productreviews-item-info span.reviews-body-rate img.productreviews-item-rating').attr('alt', 'Rated ' + review['rating'] + ' out of 5');
		$(eReview + ' p.productreviews-item-info span.reviews-body-author').html(review['author']);
		$(eReview + ' p.reviews-body-message').html(review['content']);
	});
	
	/*
	// uncomment to test the order
	$.each(aReviews, function(item, value) {
		alert(value['idmini'] + "-" + value['rating']);
	});
	*/
}

$(document).ready(function() {
	// iterate through reviews
	$("li.productreviews-item").each(function(index) {
		// grok the rating of this review
		nRating = $(this).children('p.productreviews-item-info').children('span.reviews-body-rate').children('img:first').attr('alt').substr(6,1);
		sAuthor = $(this).children('p.productreviews-item-info').children('span.reviews-body-author').html();
		sContent = $(this).children('p.reviews-body-message').html();
		
		// set 'idmini' of each review
		$(this).attr('idmini', function() {
			return 'productreview-' + index;
		});

		oReview = new Object();
		
		// add rating to review array
		oReview['idmini'] = nReviewsmini;
		oReview['rating'] = nRating;
		oReview['author'] = sAuthor;
		oReview['content'] = sContent;
		
		aReviews[nReviewsmini] = oReview;
		
		// increase the number of reviews
		nReviewsmini += 1;
	});

	aSortedRatings = aReviews.sort(function(a,b) { product = a - b; return product; });

	$.each(aSortedRatings, function(index,oReview) {
		nCurrentRating = oReview['rating'];
		if (nCurrentRating == nPreviousRating) {
			nCurrentTotal++;
		} else {
			// new rating in sequence
			nCurrentTotal = 1;
		}
		
		if (nCurrentTotal > nHighestTotal) {
			nHighestTotal = nCurrentTotal;
			nAverageRating = nCurrentRating;
		}
		nPreviousRating = nCurrentRating;
	});

	if (nReviewsmini == 1 ) {
		$('#productreviews-sort').css('display', 'inline');
			$('#productreviews-averagemini').html('<img width="60px"  src="' + sImagesPath + '/rating' + nAverageRating + '.gif" /> <span style="position:relative;top:-3px;width:100px; color: #666;">(' + nReviewsmini + ' review)</span>');
	}
	
		if (nReviewsmini > 1 ) {
		$('#productreviews-sort').css('display', 'inline');
			$('#productreviews-averagemini').html('<img width="60px" src="' + sImagesPath + '/rating' + nAverageRating + '.gif" /> <span style="position:relative;top:-3px;width:100px; color: #666;">(' + nReviewsmini + ' reviews)</span>');
	}
});
