﻿// The minified version of this gets inserted inline with the first instance of the Video Scroller control on a page.
var vmeScroll =
{
	init: function(controlId, count, currentPage /* 1-based page index; page number */)
		{
			var liHeight = 70;
			var pageSize = liHeight * count; // 420
			var videoList = $("#video-list-" + controlId);
			if (videoList.is(":animated"))
			{
				return;
			}
			var listHeight = videoList.height();
			var totalPages = Math.ceil(listHeight / pageSize);// - 1; // Pages start at zero, so subtract 1 from the total
			var currentPosition = pageSize * (currentPage - 1); // Math.abs(videoList.position().top);
			
			
			   
			if(currentPage > 1)
            {
                videoList.animate({"top": ("-" + currentPosition + "px")}, "normal", "swing");
                
                $("#video-list-previous-" + controlId).attr("src", "Images/btn_scroll_up.gif").css("cursor", "pointer");
	
			    if (currentPage == totalPages)
			    {
				    $("#video-list-next-" + controlId).attr("src", "Images/btn_scroll_down_grey.gif").css("cursor", "auto");
			    }
            }
      
            
            var start = 0;
            var end = count * (currentPage + 1);
                     
            for(var n = 0; n < end; n++)
            {
                var img = $("#" + controlId + "-" + n + "-tn");
                               
                if (img.attr("src") && img.attr("src").indexOf("Images/tn_placeholder.gif") >= 0)
				{
					img.attr("src", img.attr("ivs"));
				}
            }

		},
		
		
	next: function(controlId, count)
		{
			var liHeight = 70;
			var pageSize = liHeight * count;
			var videoList = $("#video-list-" + controlId);
			if (videoList.is(":animated"))
			{
				return;
			}
			var listHeight = videoList.height();
			var totalPages = Math.ceil(listHeight / pageSize) - 1; // Pages start at zero, so subtract 1 from the total
			var currentPosition = Math.abs(videoList.position().top);
			var currentPage = Math.floor(currentPosition / pageSize);
			//alert("next>currentPage" + currentPage);
			if (currentPage < totalPages)
			{
				var newPosition = Math.min((currentPosition + pageSize), (listHeight - liHeight));
				
				
				if ($.isAppleDevice())
				    //videoList.css("top", (newPosition * (-1)) + "px");
				    videoList.animate({ "top": ("-" + newPosition + "px") }, 2000, "swing");
				else
				    videoList.animate({"top": ("-" + newPosition + "px")}, "normal", "swing");
				
			
				
				var start = (currentPage + 2) * count;
				var idSet = "#" + controlId + "-" + start + "-tn";
				for (var i = start, j = 1; j != (count * 2); j++)
				{
					idSet += (",#" + controlId + "-" + (i + j) + "-tn");
				}
				var replaceThumbnail = function(i)
						{
							if (this.src.indexOf("Images/tn_placeholder.gif") >= 0)
							{
								this.src = $(this).attr("ivs");
							}
						};
				$(idSet).each(replaceThumbnail);
				$("#video-list-previous-" + controlId).attr("src", "Images/btn_scroll_up.gif").css("cursor", "pointer");
				if (++currentPage == totalPages)
				{
					$("#video-list-next-" + controlId).attr("src", "Images/btn_scroll_down_grey.gif").css("cursor", "auto");
				}
			}
		},
	previous: function(controlId, count)
		{
			var pageSize = 70 * count;
			var videoList = $("#video-list-" + controlId);
			if (videoList.is(":animated"))
			{
				return;
			}
			var currentPosition = Math.abs(videoList.position().top);
			if (currentPosition != 0)
			{
				var newPosition = Math.max((currentPosition - pageSize), 0);
				
				if ($.isAppleDevice())
				    //videoList.css("top", (newPosition * (-1)) + "px");
				    videoList.animate({ "top": ("-" + (newPosition + "px")) }, 2500, "swing");
				else
				    videoList.animate({"top": ("-" + (newPosition + "px"))}, "normal", "swing");
				
				
				var currentPage = Math.floor(newPosition / pageSize);
				var totalPages = Math.ceil(videoList.height() / pageSize) - 1; // Pages start at zero, so subtract 1 from the total
				if (currentPage <= 0)
				{
					$("#video-list-previous-" + controlId).attr("src", "Images/btn_scroll_up_grey.gif").css("cursor", "auto");
				}
				if (currentPage < totalPages)
				{
					$("#video-list-next-" + controlId).attr("src", "Images/btn_scroll_down.gif").css("cursor", "pointer");
				}
			}
		},
		
		
		_log: function(msg)
            {
                if (window.console && console.log)
                { // firebug logger
                    console.log(msg);
                }
            }
};
