﻿$(document).ready(function() {
	var stars = new Array("", "", "", "", "");
	var whiteSrc = "/client/default/images/ratings/white_big_star.png";
	var redSrc = "/client/default/images/ratings/red_big_star.png";
	var whiteSmallSrc = "/client/default/images/ratings/white_small_star.png";
	var redSmallSrc = "/client/default/images/ratings/red_small_star.png";
	var redVoteSrc = "/client/default/images/ratings/voted_red_big_star.png.png";
	var whiteVoteSrc = "/client/default/images/ratings/voted_white_big_star.png";
	var redSmallVoteSrc = "/client/default/images/ratings/voted_red_small_star.png";
	var whiteSmallVoteSrc = "/client/default/images/ratings/voted_white_small_star.png";

	RefreshSmallStars();

	$('#vote-rating').livequery(function(a) {
		var self = this;

		//Blocks voting if needed
		if (!$(self).attr('notnew')) {
			$.ajax({
				type: "POST",
				contentType: "application/json; charset=utf-8",
				url: "/svc/Ratings.asmx/GetRatingsInfo",
				data: "{'ratNames' : ['" + $(this).attr('ratingName') + "'], 'objects' : ['" + $(this).attr('objId') + "']}",
				dataType: "json",
				success: function(r) {
					for (var i = r.d.Object.length; i--; ) {
						//Blocks voting
						if (!r.d.Object[i].ReVote && r.d.Object[i].Vote != null) {
							$(self).unbind('mouseenter').unbind('mouseleave');
							$(self).find('img').unbind('mouseenter').unbind('mouseleave').unbind('click');
							$(self).css('cursor', 'auto');
						}

						var newAvg = Math.round(r.d.Object[i].Avg);
						//Refresh big stars
						$(self).find('img').each(function(index) {
							if (index > newAvg - 1) {
								if (r.d.Object[i].Vote == index + 1) {
									$(this).attr("src", whiteVoteSrc);
									stars[index] = whiteVoteSrc;
								}
								else {
									$(this).attr("src", whiteSrc);
									stars[index] = whiteSrc;
								}
							}
							else {
								if (r.d.Object[i].Vote == index + 1) {
									$(this).attr("src", redVoteSrc);
									stars[index] = redVoteSrc;
								}
								else {
									$(this).attr("src", redSrc);
									stars[index] = redSrc;
								}
							}
						});

						//Refresh small stars
						smallSelf = $('.vote-rating-small[objId = ' + r.d.Object[i].ObjId + ']');
						$(smallSelf).attr("ratingavg", newAvg);
						$(smallSelf).find('img').each(function(index) {
							if (index > newAvg - 1) {
								if (r.d.Object[i].Vote == index + 1)
									$(this).attr("src", whiteSmallVoteSrc);
								else
									$(this).attr("src", whiteSmallSrc);
							}
							else {
								if (r.d.Object[i].Vote == index + 1)
									$(this).attr("src", redSmallVoteSrc);
								else
									$(this).attr("src", redSmallSrc);
							}
						});
					}
				}
			});
			$(self).attr('notnew', 'true');
		}

		$(this).hover(
			function() { },
			StarsUniverseHoverOut
		);
	}, function() {
		$(this)
			.unbind('mouseenter')
			.unbind('mouseleave');
	});

	$('.vote-rating-small').livequery(function(a) {
		$(this).hover(
			function() { },
			StarsUniverseHoverOut
		);
	}, function() {
		$(this)
			.unbind('mouseenter')
			.unbind('mouseleave');
	});

	$('#vote-rating > img').livequery(function() {
		$('#vote-rating > img').unbind('mouseenter');
		$('#vote-rating > img').unbind('mouseleave');

		$('#vote-rating > img').hover(
			StarsHoverIn,
			function() { }
		);

		$('#vote-rating > img').unbind('click');
		$('#vote-rating > img').click(StarsClick);
	}, function() {
		$(this)
			.unbind('click')
			.unbind('mouseenter')
			.unbind('mouseleave');
	});

	$('.vote-rating-small > img').unbind('mouseenter');
	$('.vote-rating-small > img').unbind('mouseleave');

	$('.vote-rating-small > img').hover(
		StarsHoverIn,
		function() { }
	);
	$('.vote-rating-small > img').unbind('click');
	$('.vote-rating-small > img').click(StarsClick);

	function StarsUniverseHoverOut() {
		if (stars[0] != "")
			for (var i = 5; i--; ) {
			$(this).find("img").eq(i).attr("src", stars[i]);
			stars[i] = "";
		}
	};

	function StarsHoverIn() {
		if (stars[0] == "" || !(stars[0].indexOf('big') == $(this).attr('src').indexOf('big') || stars[0].indexOf('small') == $(this).attr('src').indexOf('small')))
			for (var i = 5; i--; )
			stars[i] = $(this).parent().find("img").eq(i).attr("src");

		var self = this;
		$(self).attr("active", "active");
		var white = false;

		if ($(self).parent().attr('id') == 'vote-rating') {
			wSrc = whiteSrc;
			rSrc = redSrc;
			wVSrc = whiteVoteSrc;
			rVSrc = redVoteSrc;
		}
		else {
			wSrc = whiteSmallSrc;
			rSrc = redSmallSrc;
			wVSrc = whiteSmallVoteSrc;
			rVSrc = redSmallVoteSrc;
		}

		$(self).parent().find('img').each(function() {
			if (white) {
				if ($(this).attr("src").valueOf() == wVSrc.valueOf() || $(this).attr("src").valueOf() == rVSrc.valueOf())
					$(this).attr("src", wVSrc);
				else
					$(this).attr("src", wSrc);
			}
			else {
				if ($(this).attr("src").valueOf() == wVSrc.valueOf() || $(this).attr("src").valueOf() == rVSrc.valueOf())
					$(this).attr("src", rVSrc);
				else
					$(this).attr("src", rSrc);

				if ($(this).attr("active") == "active")
					white = true;
			}
		})
		$(self).removeAttr("active");
	};

	function StarsClick(e) {
		//Cancel bubbling
		if (!e) var e = window.event;
		e.cancelBubble = true;
		if (e.stopPropagation) e.stopPropagation();

		var ratingName = $(this).parent().attr("ratingName");
		if (ratingName == "") return;

		var self = $(this).parent();
		$(self).unbind('mouseenter').unbind('mouseleave');
		$(self).find('img').unbind('mouseenter').unbind('mouseleave').unbind('click');
		$(self).css('cursor', 'auto');

		var objIder = parseInt($(this).parent().attr("objId"));
		$.ajax({
			type: "POST",
			contentType: "application/json; charset=utf-8",
			url: "/svc/Ratings.asmx/AddUserVote",
			data: $.toJSON({ ratingName: ratingName, objId: objIder, vote: parseInt($(this).attr("id").substr(3)) }),
			dataType: "json",
			success: function(r) {
				if (!r.d.Object) return;

				//Flashing stars
				if ($(self).attr('id') == 'vote-rating') {
					wSrc = whiteSrc;
					rSrc = redSrc;
				}
				else {
					wSrc = whiteSmallSrc;
					rSrc = redSmallSrc;
				}

				$(self).find('img').attr('src', wSrc);
				var timer = setTimeout(function() {
					$(self).find('img').attr('src', rSrc);
					var timer = setTimeout(function() {
						$(self).find('img').attr('src', wSrc);
						var timer = setTimeout(function() {
							$(self).find('img').attr('src', rSrc);
							var timer = setTimeout(function() {

								var newAvg = Math.round(r.d.Object.Avg);
								//update big stars
								if ($(self).attr('id') == 'vote-rating') {
									$(self).find('img').each(function(index) {
										if (index > newAvg - 1) {
											if (r.d.Object.Vote == index + 1) {
												$(this).attr("src", whiteVoteSrc);
												stars[index] = whiteVoteSrc;
											}
											else {
												$(this).attr("src", whiteSrc);
												stars[index] = whiteSrc;
											}
										}
										else {
											if (r.d.Object.Vote == index + 1) {
												$(this).attr("src", redVoteSrc);
												stars[index] = redVoteSrc;
											}
											else {
												$(this).attr("src", redSrc);
												stars[index] = redSrc;
											}
										}
									});
								}

								//Разлочим голосовалку
								if (r.d.Object.ReVote) {
									$(self).hover(
										function() { },
										StarsUniverseHoverOut
									);
									$(self).find('img').hover(
										StarsHoverIn,
										function() { }
									).click(StarsClick);
									$(self).css('cursor', 'pointer');
								}

								//update small stars
								var smallSelf = $('.vote-rating-small[objId = ' + objIder + ']');
								if (smallSelf) {
									//Block it if necessary
									if (!r.d.Object.ReVote) {
										$(smallSelf).unbind('mouseenter').unbind('mouseleave');
										$(smallSelf).find('img').unbind('mouseenter').unbind('mouseleave').unbind('click');
										$(smallSelf).css('cursor', 'auto');
									}
									// and refresh images
									$(smallSelf).find('img').each(function(index) {
										if (index > newAvg - 1)
											if (r.d.Object.Vote == index + 1) {
											$(this).attr("src", whiteSmallVoteSrc);
											stars[index] = whiteSmallVoteSrc;
										}
										else {
											$(this).attr("src", whiteSmallSrc);
											stars[index] = whiteSmallSrc;
										}
										else
											if (r.d.Object.Vote == index + 1) {
											$(this).attr("src", redSmallVoteSrc);
											stars[index] = redSmallVoteSrc;
										}
										else {
											$(this).attr("src", redSmallSrc);
											stars[index] = redSmallSrc;
										}
									});
								}

							}, 100);
						}, 100);
					}, 100);
				}, 100);
			}
		});
	};

	function RefreshSmallStars() {
		if ($('.vote-rating-small').length == 0) return;

		var ratings = [];
		var objs = [];

		$('.vote-rating-small').each(function(index) {
			var ratingName = $(this).attr('ratingName');
			var objId = $(this).attr('objId');

			var add = true;
			for (var i = ratings.length; i--; )
				if (ratings[i].valueOf() == ratingName.valueOf())
			{ add = false; break; }
			if (add) ratings.push(ratingName);

			objs.push(objId);
		});

		$.ajax({
			type: "POST",
			contentType: "application/json; charset=utf-8",
			url: "/svc/Ratings.asmx/GetRatingsInfo",
			data: $.toJSON({ ratNames: ratings, objects: objs }),
			dataType: "json",
			success: function(r) {
				for (var i = r.d.Object.length; i--; ) {
					var self = $('.vote-rating-small[objId = ' + r.d.Object[i].ObjId + ']');

					if (!r.d.Object[i].ReVote && r.d.Object[i].Vote != null) {
						$(self).unbind('mouseenter').unbind('mouseleave');
						$(self).find('img').unbind('mouseenter').unbind('mouseleave').unbind('click');
						$(self).css('cursor', 'auto');
					}

					var newAvg = Math.round(r.d.Object[i].Avg);
					$(self).find('img').each(function(index) {
						if (index > newAvg - 1) {
							if (r.d.Object[i].Vote == index + 1)
								$(this).attr("src", whiteSmallVoteSrc);
							else
								$(this).attr("src", whiteSmallSrc);
						}
						else {
							if (r.d.Object[i].Vote == index + 1)
								$(this).attr("src", redSmallVoteSrc);
							else
								$(this).attr("src", redSmallSrc);
						}
					});
				}
			}
		});
	};
});
