/**
 * This handles the rating system
 */

/*global cwRating jQuery -- */

(function($) {

 	cwRating = {

		initialize: function(options) {

			var opts = $.extend({}, cwRating.defaults, options);
			cwRating.options = opts; // save configuration options
			
			return this.each(function() {

				var $rating = $(this);
				var rating_id = this.id;

				$rating.children(".star").each( function(i) { // loop over each star

					var $star = $(this);
					//if (i===0) { cwRating.starAverage($star); }
					cwRating.initialRating[rating_id] = cwRating.getDefaultRating($rating); // store the default rating

					$star.bind("mouseover", function(e) {

						cwRating.hoverStars(this);

					});

					$star.bind("mouseout", function(e) {

						cwRating.defaultStars(this);

					});

					$star.bind("click", function(e) {

						cwRating.starClicked($rating, $star);

					});
					
					cwRating.defaultStars(this);

				});
				
			});

		},

		starClicked: function($rating, $star) {

			var rating_id = $rating.attr("id");

			var ratingObj = cwRating.getDefaultRating($rating);
			cwRating.initialRating[rating_id] = ratingObj;

			this.saveRating($rating, ratingObj);

		},

		saveRating: function($rating, ratingObj) {

			var id = this.getIdFromRating($rating);
			var params = { "id": id, "rating": ratingObj.stars }; // type and stars
			
			$.getJSON(this.options.saveRatingUrl, params, this.saveRatingDone);

		},
		
		saveRatingDone: function(json) {
		
			//alert(json.saved);
			
		},

		getDefaultRating: function($rating) {

			var user_stars = this.getStarsOfType($rating, "user");

			var type = (user_stars > 0) ? "user" : "average";
			var stars = (type === "user") ? user_stars : this.getStarsOfType($rating, "average");

			return { "type": type, "stars": stars };

		},

		getStarsOfType: function ($rating, type) {

			var stars = 0;
			$rating.children(".star").each( function(i) {

				var star_type = $(this).attr("type");
				if (star_type) {
			       
					if (star_type === type) { stars++; }

				}

			});

			return stars;

		},

		starUser: function ($star) {

			this.starColor($star, this.userRatingColor, "user");

		},

		starDefault: function ($star) {

			this.starColor($star, this.defaultRatingColor, "");

		},

		starAverage: function($star) {

			this.starColor($star, this.averageRatingColor, "average");	

		},

		starColor: function ($star, color, type) {

			$star.css("background-color", color);
			$star.attr("type", type);

		},

		defaultStars: function(star) {

			var $star = $(star);
			var $rating = $star.parent(".rating");

			var initial_rating = this.initialRating[$rating.attr("id")];

			$rating.contents(".star").each( function(i) {

				if (i < initial_rating.stars) {

					if (initial_rating.type === "user") {

						cwRating.starUser($(this));

					} else if (initial_rating.type === "average") {

						cwRating.starAverage($(this));

					}

				} else {

					cwRating.starDefault($(this));

				}

			});

		},

		hoverStars: function(star) {

			var $star = $(star);
			var $rating = $star.parent(".rating");

			var position = this.getStarPosition($rating, $star);

			$rating.contents(".star").each( function(i) {

				if (i <= position) {

					cwRating.starUser($(this));

				} else {

					cwRating.starDefault($(this));

				}

			});

		},

		getStarPosition: function($rating, $star) {

			var position;
			var star = $star.get(0);
			$rating.contents(".star").each( function(i) {

				if (this === star) {

					position = i;

				}

			});

			return position;

		},
		
		getIdFromRating: function($rating) {
		
			var id_attr = $rating.attr("id");
			var id = id_attr.replace("rate-","");
			return id;
		
		},
		
		notClimbingUrlFromType: function(type) {
		
			var url = (type === "photo") ? '/user/ajaxPhotoNotClimbing.app' : '/user/ajaxYtNotClimbing.app';
			return url;
		},
		
		notClimbing: function(id, type) {
		
			$.getJSON(this.notClimbingUrlFromType(type), { "id": id }, this.notClimbingDone);
		
		},

		notClimbingDone: function(json) {
		
			var media_type = (json.media_type === "photo") ? "photo" : "video";
		
			if (json.saved === '1') {
			
				alert("Thank you for marking this "+media_type+" as not climbing.  Changes are updated hourly.");
				var id = json.id;
				$("#"+id+"_not_climbing").hide();
				$("#"+id+"_not_climbing_undo").show();
				
			} else {
				
				alert("We're sorry, there was an error marking this "+media_type+" as not climbing.");

			}
					
		},
		
		notClimbingUndo: function(id, type) {
		
			$.getJSON(this.notClimbingUrlFromType(type), { "id": id, "undo":"1" }, this.notClimbingUndoDone);
		
		},

		notClimbingUndoDone: function(json) {
		
			var media_type = (json.media_type === "photo") ? "photo" : "video";
			
			if (json.saved === '1') {
			
				alert("This "+media_type+" is no longer marked as not climbing.");
				var id = json.id;
				$("#"+id+"_not_climbing_undo").hide();
				$("#"+id+"_not_climbing").show();
				
				
			} else {
				
				alert("We're sorry, there was an error un-doing marking this "+media_type+" as not climbing.");

			}
					
		},

		initialRating: {},

		defaultRatingColor: "#ccc",
		userRatingColor: "#ffcc00",
		averageRatingColor: "#996633",

		defaults: { "saveRatingUrl":"/user/ajaxYtSaveRating.app", "notClimbingUrl":"/user/ajaxYtNotClimbing.app" },
		options: { "notClimbingUrl":"/user/ajaxYtNotClimbing.app" }
	};

	$.fn.cwRating = cwRating.initialize;

}) (jQuery);
