﻿ZoomItemRateThis = function () {
	var rateItems = [];
	return {
		Initialise: function (articleId, items, spam, offensive, total) {
			rateItems = items;
			for (key in items) {
				var thisItem = items[key];
				var itemLink = document.getElementById(thisItem.link);
				var rating = thisItem.rating;
				if (itemLink) {
					itemLink.onclick = function (rating) { return function () { iCM.ZoomItemRateThis.Rate(articleId, rating, total); return false; } }(rating);
					itemLink.onmouseover = function (rating) { return function () { iCM.ZoomItemRateThis.ShowRate(articleId, rating); } }(rating);
					itemLink.onfocus = itemLink.onmouseover;
					itemLink.onblur = itemLink.onmouseout;
				}
			}

			// Spam
			var spamLnk = document.getElementById(spam);
			if (spamLnk) {
				spamLnk.onclick = function () { iCM.ZoomItemRateThis.ReportAsSpam(articleId); }
			}

			// Offensive
			var offensiveLnk = document.getElementById(offensive);
			if (offensiveLnk) {
				offensiveLnk.onclick = function () { iCM.ZoomItemRateThis.ReportAsOffensive(articleId); }
			}

			// Get current ratings
			if (total) {
				iCM.RPC.GuildfordZoomItemRateThis.GetRate(articleId,
					function (target, response) {
						iCM.ZoomItemRateThis.UpdateTotals(target, response.result);
					},
					function (error) {
						alert(error);
					},
					total
				);
			}
		},

		// Rate the article
		Rate: function (articleId, rating, totalId) {
			iCM.RPC.GuildfordZoomItemRateThis.Rate(articleId, rating,
				function (target, response) {
					if (response.result.msg) {
						iCM.ZoomItemRateThis.UpdateTotals(target, response.result);
						alert(response.result.msg);
					}
				},
				function (error) {
					alert(error);
				},
				totalId
			);
		},

		// Report the article as spam
		ReportAsSpam: function (articleId) {
			iCM.RPC.GuildfordZoomItemRateThis.ReportAsSpam(articleId,
				function (target, response) {
					alert(response.msg);
				},
				function (error) {
					alert(error);
				}
			);
		},

		// Report the article as offensive
		ReportAsOffensive: function (articleId) {
			iCM.RPC.GuildfordZoomItemRateThis.ReportAsOffensive(articleId,
				function (target, response) {
					alert(response.msg);
				},
				function (error) {
					alert(error);
				}
			);
		},

		// Show a rating
		ShowRate: function (articleId, rating) {
			for (var idx = 0; idx < rateItems.length; idx++) {
				var rateItem = rateItems[idx];
				var image = document.getElementById(rateItem.image);
				if (image) {
					if (rateItem.rating < rating) {
						image.src = rateItem.below;
					} else if (rateItem.rating == rating) {
						image.src = rateItem.selected;
					} else {
						image.src = rateItem.above;
					}
				}
			}
		},

		// Update totals
		UpdateTotals: function (totalId, data) {
			var total = document.getElementById(totalId);
			if (total) {
				var voteCount = 0;
				for (var fld = 0; fld < data.scores.length; fld++) {
					voteCount += data.scores[fld];
				}
				total.innerHTML = voteCount > 0 ? voteCount + ' votes so far in total' : ' no votes so far';
			}
		}
	}
} ();

iCM.ZoomItemRateThis = ZoomItemRateThis;
