Btv.Templating.Register([{id:"Episode.Rater",fnc:function($data){var $text = [];var _write = function(text) {$text.push((typeof text == "number")?text:(text||""));};with($data){ Main = function(o, ht) {var $text = [];var _write = function(text) {$text.push((typeof text == "number")?text:(text||""));};_write('\r\n\n<div ');_write(o.Elem('body'));_write('  class=\"episoderater\" style=\"');_write(o.Style());_write('\">');_write(o.InnerHTML());_write(ht.fncLoading(o));_write('</div>\n');;return($text.join(""));};Loading = function(o, ht) {var $text = [];var _write = function(text) {$text.push((typeof text == "number")?text:(text||""));};_write('\r\n\n<div style=\"vertical-align:bottom; text-align:center;\">\n<br /><h2>Loading...</h2><img src=\"http://web.buddytv.com/images/spin2.gif\" />\n</div>\n');;return($text.join(""));};Rater = function(o, ht) {var $text = [];var _write = function(text) {$text.push((typeof text == "number")?text:(text||""));};_write('\r\n\n<div class=\"eprater_left\">\n<div style=\"float:left; width:90px;\">\nYour rating');_write( ht.seasonEpisodeInfo != '' ? ' for ' + ht.seasonEpisodeInfo : '');_write(':\n<div class=\"eprater_number\">\n<span id=\"rating_value\" class=\"rater-rating\">n/a</span>\n</div>\n</div>\n<div id=\"demo_bg\" title=\"Rating Slider\">\n<span id=\"range_bar\"></span>\n<div id=\"thumb\" class=\"slider-btn\"></div>\n</div>\n<div class=\"eprater_logo\">\n<div id=\"rating_icon\" class=\"rating_none\"></div>\n</div>\n</div>\n<div class=\"eprater_right\">\nAverage Rating:\n<div ');_write(o.Elem('avgRating'));_write('  style=\"');_write(o.Style());_write('\">');_write(o.InnerHTML());_write(ht.data.AvgRating.toFixed(1));_write('</div>\nTotal Ratings:\n<div ');_write(o.Elem('ratingCount'));_write('  style=\"');_write(o.Style());_write('\">');_write(o.InnerHTML());_write(ht.data.RatingCount);_write('</div>\n</div>\n<div style=\"clear:both\"></div>\n<div class=\"eprater_take\">Your Take: <span>(250 character limit)</span></div>\n<textarea ');_write(o.Elem('ReviewText'));_write('  style=\"');_write(o.Style());_write('\">');_write(o.InnerHTML());_write(ht.data.UserReviewText);_write('</textarea>\n<div ');_write(o.Elem('SaveSpinGif'));_write('  style=\"height:0px; float:right;');_write(o.Style());_write('\">');_write(o.InnerHTML());_write('\n<img ');_write(o.Elem('imgSavingSpinGif'));_write('  alt=\"Submitting\" style=\"visibility:hidden;');_write(o.Style());_write('\" src=\"http://web.buddytv.com/images/spin2.gif\">');_write(o.InnerHTML());_write('</img>\n</div>\n<div ');_write(o.Elem('btnSubmit'));_write('  class=\"btv_orange_button\" style=\"float:right; padding-top:38px;');_write(o.Style());_write('\">');_write(o.InnerHTML());_write('\n<span class=\"b1\"></span>\n<span ');_write(o.Elem('msgSubmitted'));_write('  style=\"');_write(o.Style());_write('\">');_write(o.InnerHTML());_write('</span>\n<a ');_write(o.Elem('buttonText'));_write('  onclick=\"');_write($OG(ht.fncSubmitRating));_write('()\" class=\"b2\" style=\"');_write(o.Style());_write('\">');_write(o.InnerHTML());_write('Submit</a>\n<span class=\"b3\"></span>\n</div>\n<div style=\"clear:both\"></div>\n<div ');_write(o.Elem('errorMsg'));_write('  class=\"eprater_errormsg\" style=\"');_write(o.Style());_write('\">');_write(o.InnerHTML());_write('</div>\n<a ');_write(o.Elem('linkAllRatingsPage'));_write('  href=\"\" style=\"');_write(o.Style());_write('\">');_write(o.InnerHTML());_write('See All Ratings</a>\n');;return($text.join(""));};
new (function(d, ui) {
var oThis = Btv.Lang.Extend(this, Btv.Cst.BaseTemplate, [$data, true]);
var htUI = ui;
var $el = oThis.El;

		var oElMan = oThis.ElMan();
		var RatingCount = 0;
		var AvgRating = 0;
		var UserRating = 0;

		/* ----  BEGIN - Script for YUI Slider Control   -- */
		function LoadSlider()
		{
			YAHOO.namespace('Btv.EpisodeRater');
			var Dom = YAHOO.util.Dom;

			// Set Slider range in pixels
			var range = 171;

			// Set up tick size in pixels
			var tickSize = range / 19; // =9px -- 19 thumbstops to represent 1.0, 1.5,2.0,...,10.

			// Some ranges for changing the slider background and icon image
			var rating_9 = tickSize * 17,
				rating_7 = tickSize * 13,
				rating_5 = tickSize * 9,
				rating_3 = tickSize * 5;

				var ratingValue = Dom.get("rating_value");
				var ratingIcon = Dom.get("rating_icon");

				var slider = YAHOO.widget.Slider.getHorizSlider("demo_bg", // the id of the slider's background element  // should name elements with cstname to create unique element id and access by oThis.htIds["demo_bg"] 
															"thumb", //sHandleElId <String> the id of the thumb element
															0, //iLeft <int> the number of pixels the element can move left
															range, //iRight <int> the number of pixels the element can move right
															tickSize); //iTickSize <int> optional parameter for specifying that the element should move a certain number pixels at a time.


				// Decorate the Slider instance with some new properties and methods to maintain the highlight element
				YAHOO.lang.augmentObject(slider, {

					// tick size
					_tickSize: tickSize,
					// the size of range in pixels
					_rangeSize: range,

					// The current status
					_status: 'na',

					// The highlight element
					_highlight: Dom.get("range_bar"),

					// A simple getter method for the status
					getStatus: function() { return this._status; },

					_initRating: 0,
					_lock: false,
					onAvailable: function() // overriding onAvailable function. Executes when the slider element is available.
					{
						if (this._initRating >= 1)
						{
							this.setRating(this._initRating);
							if (this._lock)
								this.lock();
							else
								this.unlock();
						}
					},

					lockSlider: function()
					{
						this._lock = true;
						if (this.available)
							this.lock();
					},

					unlockSlider: function() {
						this._lock = false;
						if (this.available)
							this.unlock();
					},
					// A method to get the rating score from the slider
					getRating: function()
					{
						var rating = 0.5 + (0.5 * (this.getValue() / this._tickSize));
						if (rating >= 1)
							return rating.toFixed(1);
						else
							return 'n/a';
					},

					// A method to set the rating score on the slider
					setRating: function(newRating)
					{
						this._initRating = newRating;
						if (this.available)
						{
							var newOffset = 2 * this._tickSize * (newRating - 0.5);
							this.setValue(newOffset, true, true, false);
						}
					},

					updateHighlight: function()
					{
						var newRating = this.getValue();

						if (newRating == 0)
						{
							newStatus = 'rating_none';
						}
						else if (newRating < rating_3)
						{
							newStatus = 'rating_1_2';
						}
						else if (newRating < rating_5)
						{
							newStatus = 'rating_3_4';
						}
						else if (newRating < rating_7)
						{
							newStatus = 'rating_5_6';
						}
						else if (newRating < rating_9)
						{
							newStatus = 'rating_7_8';
						}
						else
						{
							newStatus = 'rating_9_10';
						}

						// Update the highlight class if status is changed
						Dom.replaceClass(this._highlight, this._status, newStatus);
						this._status = newStatus;
						// Adjust the width of the highlight to match inner boundary
						Dom.setStyle(this._highlight, 'width', newRating + 'px');
					}
				}, true);

				// Create an event callback handler for slider's change event to update user's current rating number and icon 
				var setUsersRating = function()
				{
					ratingValue.innerHTML = slider.getRating();
					ratingIcon.className = slider.getStatus();
				};  // end function setUsersRating


				// Attach event handler methods to the slider's change event
				slider.subscribe('change', slider.updateHighlight, slider, true); //$ Note: this method is part of the slider object, but accesses elements "outside" the object.
				slider.subscribe('change', setUsersRating);

				// set slider properties
				slider.animate = true;     // animate the moving of the thumb when a user clicks the slider
				slider.enableKeys = false; // don't allow users to control slider with arrow keys

				// Attach the slider to the YAHOO.example namespace for public probing
				YAHOO.Btv.EpisodeRater.slider = slider;

			}

		// ----  END - Script for YUI Slider Control   -- */

		function HaveData(oRaterData) 
		{
			if (oRaterData == null)
			{
				// if oRaterData is null, then just create a "default" oRaterData object
				oRaterData = { RatingCount: 0, AvgRating: 0, UserRating: 0, UserReviewText: "", ReviewId: -1 }
			}
			RatingCount = oRaterData.RatingCount;
			AvgRating = oRaterData.AvgRating;
			ExistingUserRating = oRaterData.UserRating;
			
			// Load Rater control
			var hasSeasonEpisodeInfo = ((d.seasonEpisodeInfo != undefined) && (d.seasonEpisodeInfo != null) && (d.seasonEpisodeInfo != ""))
			
			if ((hasSeasonEpisodeInfo == true) && (d.urlEpisodePage != undefined) && (d.urlEpisodePage != null) && (d.urlEpisodePage != ""))
			{
				d.seasonEpisodeInfo = "<a href='" + d.urlEpisodePage + "'>" + d.seasonEpisodeInfo + "</a>";
				
			}
			
			var s = htUI.Rater(oThis, { data: oRaterData, fncSubmitRating: SubmitRating, seasonEpisodeInfo: hasSeasonEpisodeInfo ? d.seasonEpisodeInfo : '' });
			oElMan.InnerHTML("body", s);
			
			LoadSlider();
			
			if (oRaterData.ReviewId >= 1)
			{
				// Load Rater with user's existing review information and disable the rating inputs
				YAHOO.Btv.EpisodeRater.slider.setRating(oRaterData.UserRating);
				LockRatingInput(false);
			}

			// set "see all ratings" link
			if ((d.urlAllRatingsPage != undefined) && (d.urlAllRatingsPage != null) && (d.urlAllRatingsPage != "")) {
				oElMan.Attribute('linkAllRatingsPage', 'href', d.urlAllRatingsPage);
			}
			else {
				oElMan.Attribute('linkAllRatingsPage', 'innerHTML', '');
			}
		}
		function SubmitButtonShow() 
		{
			$(oThis.htIds['msgSubmitted']).innerHTML = '';
			$(oThis.htIds['buttonText']).innerHTML = 'Submit';
			$(oThis.htIds['btnSubmit']).setAttribute('class', 'btv_orange_button');
			$(oThis.htIds['buttonText']).onclick = SubmitRating;
		}
		
		function SubmitButtonSubmitting() 
		{
			$(oThis.htIds['msgSubmitted']).innerHTML = ''; 
			$(oThis.htIds['buttonText']).innerHTML = '';
			$(oThis.htIds['btnSubmit']).setAttribute('class', '');

			oElMan.Style('SaveSpinGif', 'height', '');
			oElMan.Style('imgSavingSpinGif', 'visibility', 'visible');
		}
		function SubmitButtonSubmitted() 
		{
			$(oThis.htIds['msgSubmitted']).innerHTML = 'Submitted<br/>';
			$(oThis.htIds['buttonText']).innerHTML = 'Edit Rating';
			$(oThis.htIds['btnSubmit']).setAttribute('class', '');
			$(oThis.htIds['buttonText']).onclick = UnlockRatingInput;
		}

		function SubmitRating()
		{
			var _rating = YAHOO.Btv.EpisodeRater.slider.getRating();
			if (isNaN(_rating))
			{
				$(oThis.htIds['errorMsg']).innerHTML = 'Please select a rating on this episode before submitting.';
				return;
			}
			
			var elReviewText = $(oThis.htIds['ReviewText']);

			if ((elReviewText == undefined) || (elReviewText == null))
			{
				$(oThis.htIds['errorMsg']).innerHTML = 'Your Take could not be submitted at this time. Please try again later.';
				return;
			}
			var _userReviewText = elReviewText.value.replace(/^\s+|\s+$/g, ""); // trim spaces
			if (_userReviewText.length == 0)
			{
				$(oThis.htIds['errorMsg']).innerHTML = 'Please enter Your Take on this episode before submitting.';
				return;
			}
			else if (_userReviewText.length > 250) 
			{
				$(oThis.htIds['errorMsg']).innerHTML = 'Please limit Your Take to 250 characters.';
				return;
			}

			// Verify User is logged in before submitting rating.
			if (!Btv.User.IsLoggedIn())
			{
				Btv.User.InitLogin(SubmitRating);
				return false;
			}

			SubmitButtonSubmitting() 
			d.fncSaveEpisodeRaterData(d.scopeId, d.contentId, _rating, _userReviewText, SaveRatingCallback);
		}

		function LockRatingInput(isOnSave) 
		{
			// lock slider, set review text as read only, and hide submit button
			YAHOO.Btv.EpisodeRater.slider.lockSlider();

			SubmitButtonSubmitted();

			oElMan.Style('ReviewText', 'background-color', 'Transparent');
			$(oThis.htIds['ReviewText']).setAttribute('readonly', 'readonly');
		}
		function UnlockRatingInput() 
		{
			// lock slider, set review text as read only, and hide submit button
			YAHOO.Btv.EpisodeRater.slider.unlockSlider();

			SubmitButtonShow();

			oElMan.Style('ReviewText', 'background-color', '');
			$(oThis.htIds['ReviewText']).removeAttribute('readonly', 1);
		}

		
		function SaveRatingCallback(savedSuccessfully)
		{
			if (savedSuccessfully)
			{	// lock inputs, clear error messages, hide spinning gif and update avg and total ratings
				LockRatingInput(true);

				$(oThis.htIds['errorMsg']).innerHTML = '';
				oElMan.Style('SaveSpinGif', 'height', '0px');
				oElMan.Style('imgSavingSpinGif', 'visibility', 'hidden');

				var newRating = parseFloat(YAHOO.Btv.EpisodeRater.slider.getRating());
				var newAvgRating;
				if (RatingCount > 0) 
				{

					if (ExistingUserRating > 0) 
					{ // has user already rated
						newAvgRating = ((AvgRating * RatingCount) + newRating - ExistingUserRating) / (RatingCount);
					}
					else 
					{
						newAvgRating = ((AvgRating * RatingCount) + newRating) / (RatingCount + 1.0);
						$(oThis.htIds['ratingCount']).innerHTML = 1 + RatingCount;
					}
	
					$(oThis.htIds['avgRating']).innerHTML = newAvgRating.toFixed(1);
				}
				else
				{
					$(oThis.htIds['ratingCount']).innerHTML = 1;
					$(oThis.htIds['avgRating']).innerHTML = newRating.toFixed(1);
				}

				// clear the output cache on the TVShow Episode page
				if ((d.fncEpisodePageRemoveCache != undefined) && (d.fncEpisodePageRemoveCache != null)) 
				{
					d.fncEpisodePageRemoveCache();
				}
			}
			else
			{
				$(oThis.htIds['errorMsg']).innerHTML = 'Your Take could not be submitted at this time. Please try again later.';
			}
		}

		function Render()
		{
			d.fncGetEpisodeRaterData( d.scopeId, d.contentId, HaveData);
			return (htUI.Main(oThis, { fncLoading: htUI.Loading }));
		}

		oThis.DeclareVirtualMethods({ Render: Render });

	})($data, {Main:Main,Loading:Loading,Rater:Rater});
}return $text.join("");}},{id:"Container.BtvModule",fnc:function($data){var $text = [];var _write = function(text) {$text.push((typeof text == "number")?text:(text||""));};with($data){ _write('\n');Main = function(o) {var $text = [];var _write = function(text) {$text.push((typeof text == "number")?text:(text||""));};_write('\n<div ');_write(o.Elem('Root'));_write(' class=\"');_write((o.d.classes && o.d.classes.Module) ? o.d.classes.Module : 'btv_module');_write('\" style=\"');_write(o.Style());_write('\">\n<div class=\"btv_module_head\"><div class=\"c\"></div></div>\n<div class=\"btv_module_body\" ');_write(o.d.bodyStyle ? 'style="' + o.d.bodyStyle + '"' : '');_write('>\n<div class=\"c\">');if(o.d.header) {_write('<h2>');_write(o.d.header);_write('</h2>');}_write(o.d.body.Render());_write('</div>\n</div>\n<div class=\"btv_module_foot\"><div class=\"c\"></div></div>\n</div>\n');;return($text.join(""));};_write('\n');Version3 = function(o) {var $text = [];var _write = function(text) {$text.push((typeof text == "number")?text:(text||""));};_write('\n<div ');_write(o.Elem('Root'));_write(' class=\"content-container\">');_write(o.d.body.Render());_write('</div>\n');;return($text.join(""));};_write('\n');
new (function(d, fncMain, fncVersion3)
{
var s, oThis = Btv.Lang.Extend(this, Btv.Cst.BaseTemplate, arguments);
oThis.d = d;
var blnDisplay = true;
function Render()
{
var fnc = (Btv.iPageVersion == 3) ? fncVersion3 : fncMain;
if (!s) s = fnc(oThis);
return(s);
}
oThis.ElementProps = function(id, scope, name, isInit, params, ht)
{
switch(scope)
{
case "Root":
ht.style.display = blnDisplay ? 'block' : 'none';
break;
}
}
oThis.Display = function(bln)
{
blnDisplay = bln;
oThis.ElMan().Style('Root'
, 'display', (blnDisplay ? 'block' : 'none')
);
}
oThis.DeclareVirtualMethods({
Render: Render
});
})($data, Main, Version3);
_write('\n');}return $text.join("");}}]);
