if (typeof(ASPERITY) == 'undefined') {
	ASPERITY	= {};
	ASPERITY.UI	= {};
}

ASPERITY.UI.Video	= function()
{
	var scrollplane;
	var scroller;
	var totalitems = 0;
	var itemwidth = 0;
	var totalwidth = 0;
	var view = 0;
	
	var init	= function() {
		initScroller();
	};
	
	var initScroller = function() {
		
		scroller = $('.videobox .scrollwindow');
		
		scroller.css('overflow', 'hidden').wrapInner('<div class="scrollplane"></div>');
		
		$('.videobox').append('<a id="video-next" href="#"><img src="/img/video-next.gif" alt="Next" /></a>')
						.append('<a id="video-prev" href="#"><img src="/img/video-prev.gif" alt="Previous" /></a>');
		
		var scrollplane = $('.videobox .scrollplane');
		
		totalitems = scrollplane.find('li').length;
		var item = scrollplane.find('li:first');
		itemwidth = parseInt(item.outerWidth(true), 10);		
		totalwidth = (totalitems) * itemwidth;
		
		scrollplane.css('width', totalwidth+10);
		
		$('#video-next').click(function(e){
			e.preventDefault();
			var currentLeft = parseInt(scrollplane.css('left').replace('px', ''), 10);
			var newLeft = currentLeft - scroller.width();
			
			if ((0-totalwidth) >= newLeft) {
				newLeft = 0;
				view	= 0;
			}else{
				view++;
			}
			scrollplane.animate({left: newLeft});
		});
		
		$('#video-prev').click(function(e){
				e.preventDefault();
				var currentLeft = parseInt(scrollplane.css('left').replace('px', ''), 10);
				var newLeft = currentLeft + scroller.width();
			
				if (newLeft > 0) {
					var adjustment = 0;
					if (totalitems % 4 == 0) adjustment = -1;
					newLeft = 0 - (Math.floor(totalitems / 4)+adjustment)*(itemwidth*4);
				}
						
				scrollplane.animate({left: newLeft});
		});
	};
	
	
	return {
		init: init
	};
	
}();

jQuery(function($) { ASPERITY.UI.Video.init(); });