﻿/** @fileoverview
  * This handles UI updates related with the "Rate This" stars.
  */

/*global YAHOO, document, Event, window
*/

var Stars = {

	stars: 'vidRateStars',
	text:  'vidRateStarText',

	/** Locates DOM elements that are important to this code bit.
	  * This is run automatically upon DOM ready.
	  */
	init: function () {
		this.stars = document.getElementById(this.stars);
		this.text  = document.getElementById(this.text);
	},

	/** Update the descriptive text when the user mouses-over a given star.
	  * @param  {integer} star
	  * @return {string}  text
	  */
	showStars: function (star) {
		var StarPx = -15 * (5 - star),
			text = [
				'', 'Poor', 'Mediocre', 'Good', 'Very Good', 'Excellent'
			][star];

		if (text === undefined) {
			return null;
		}
		this.stars.style.backgroundPosition = StarPx + 'px 0px';
		this.text.innerHTML = text;
		return text;
	},

	/** Blank out the star text and restore the star graphic to the given
	  * value.
	  * @param  {integer} rating
	  */
	clearStars: function (rating) {
		var StarPx = -15 * (5 - rating);
		this.stars.style.backgroundPosition = StarPx + 'px 0px';
		this.text.innerHTML = '';
	},

	/** Display an informational message to the user. */
	votedStars: function () {
		this.text.innerHTML = 'You have already rated this video.';
	},

	/** Handle a vote when the user clicks on a star.
	  * This fires off an AJAX request containing the video ID and the vote
	  * value.  The server responds with the updated star rating for this
	  * video, which is then displayed to the user.  This also sets a cookie
	  * that indicates that this user has already voted for this video.
	  * @param {string}  pageID
	  * @param {integer} star
	  */
	setStars: function (pageID, star) {
		var url  = 'Rate.asp?PageID=' + encodeURIComponent(pageID),
			post = 'Star=' + encodeURIComponent(star),
			NewStars,
			expires,
			date = new Date();

		date.setTime(date.getTime() + (365 * 24 * 60 * 60 * 1000));
		expires = date.toGMTString();

		YAHOO.util.Connect.asyncRequest('POST', url, {
			scope: this,
			timeout: 5000,
			failure: function (o) {
				window.alert('failure! '); // change this line
			},
			success: function (o) {
				// Set cookie here.
				NewStars = parseInt(o.responseText, 10);
				document.cookie =
					'VID_VOTE_' + pageID + '=Voted; ' +
					'expires=' + expires + '; path=/';

				document.getElementById('vidRateStars').innerHTML =
					'<div id="vidRateStar1" class="vidRateStar" ' +
					'    onmouseover="Stars.votedStars();" ' +
					'    onmouseout="Stars.clearStars(' + NewStars + ');">' +
					'<\/div>\n' +
					'<div id="vidRateStar2" class="vidRateStar" ' +
					'    onmouseover="Stars.votedStars();" ' +
					'    onmouseout="Stars.clearStars(' + NewStars + ');">' +
					'<\/div>\n' +
					'<div id="vidRateStar3" class="vidRateStar" ' +
					'    onmouseover="Stars.votedStars();" ' +
					'    onmouseout="Stars.clearStars(' + NewStars + ');">' +
					'<\/div>\n' +
					'<div id="vidRateStar4" class="vidRateStar" ' +
					'    onmouseover="Stars.votedStars();" ' +
					'    onmouseout="Stars.clearStars(' + NewStars + ');">' +
					'<\/div>\n' +
					'<div id="vidRateStar5" class="vidRateStar" ' +
					'    onmouseover="Stars.votedStars();" ' +
					'    onmouseout="Stars.clearStars(' + NewStars + ');">' +
					'<\/div>\n';
			}
		}, post);

	}
};

var RelatedPosition = {

	Related: 'vidRelatedThumbs',

	/** Locates DOM elements that are important to this code bit.
	  * This is run automatically upon DOM ready.
	  */
	init: function () {
		this.Related = document.getElementById(this.Related);
		this.Related.scrollTop = 210;
	}
};

Event.onDOMReady(Stars.init, Stars, true);

