﻿/** @fileoverview
  * This handles UI updates related with the "Rate This" stars.
  */

/*extern $, Event, YAHOO, alert, document
*/
/*global Stars */
/*members Connect, addListener, asyncRequest, backgroundPosition, 
    clearStars, cookie, failure, getElementsByTagName, getTime, init, 
    innerHTML, onDOMReady, responseText, scope, setStars, setTime, 
    showStars, stars, style, success, text, timeout, toGMTString, util, 
    visibility, votedStars
*/




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 = $(this.stars);
		this.text  = $(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);
		var 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);
		var post = 'Star=' + encodeURIComponent(star);
		var NewStars;
		var date = new Date();
		date.setTime(date.getTime() + (365 * 24 * 60 * 60 * 1000));
		var expires = date.toGMTString();

		YAHOO.util.Connect.asyncRequest('POST', url, {
			scope: this,
			timeout: 5000,
			failure: function (o) {
				alert('failure! '); // change this line
			},
			success: function (o) {
				// Set cookie here.
				NewStars = parseInt(o.responseText, 10);
				document.cookie = 'MainLineHealthVideo' + pageID + '=Voted; expires=' + expires + '; path=/';
				$('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);

	}
};

RelatedPosition = {

	Related: 'vidRelatedThumbs',

	/** Locates DOM elements that are important to this code bit.
	  * This is run automatically upon DOM ready.
	  */
	init: function () {
		this.Related = $(this.Related);
		this.Related.scrollTop = 210;
	}
};

Event.onDOMReady(Stars.init, Stars, true);
//Event.onDOMReady(RelatedPosition.init, RelatedPosition, true);
