﻿
function sliderManager()
{
	this.context = null;
	this.direction = null;//'horizontal'/'vertical'
	this.stop = function(){
		$(this.context).attr('data-stopScroll', true);
		clearInterval(this.intervalId);
	}
	this.start = function(){
		$(this.context).attr('data-stopScroll', false);
		this.move();
	}
	
	this.move = function(){
		
		var ctx = this.context;
		var direction = this.direction;



		var scrollFunc = function(){
			if($(ctx).attr('data-stopScroll') == 'false'){
				var uiVal = $(ctx).attr('data-scrollValue');
				//alert($(ctx).html());
				if(direction == 'horizontal')
				{
					var maxScroll = $(ctx).attr("scrollWidth") - $(ctx).width();
					//alert($(ctx).attr("scrollWidth") +'-'+ $(ctx).width());
					$(ctx).animate({scrollLeft: uiVal *
					 (maxScroll / 100) }, 1500, null, 
					 function()
					 {
						$(ctx).attr({scrollLeft: uiVal * (($(ctx).attr("scrollWidth") -
								  $(ctx).width()) / 100)});
						$(ctx).attr('data-scrollValue', parseInt(uiVal) + 5);
						
						if(parseInt($(ctx).attr('scrollLeft')) == parseInt(maxScroll))
						 {
							$(ctx).attr('data-scrollValue', 0);
							uiVal = 0;
							$(ctx).attr({scrollLeft: 1});		
							scrollFunc();
						 }
						 else{
								setTimeout(scrollFunc, 1500);
							}
					 });
				}
				else
				{
					var maxScroll = $(ctx).attr("scrollHeight") - $(ctx).height();
					$(ctx).animate({scrollTop: uiVal *
					 (maxScroll / 100) }, 1500, null, 
					 function()
					 {
						$(ctx).attr({scrollTop: uiVal * (($(ctx).attr("scrollHeight") -
								  $(ctx).height()) / 100)});
						$(ctx).attr('data-scrollValue', parseInt(uiVal) + 5);
						if(parseInt($(ctx).attr('scrollTop')) == parseInt(maxScroll))
						 {
							$(ctx).attr('data-scrollValue', 0);
							uiVal = 0;
							$(ctx).attr({scrollTop: 1});		
							scrollFunc();
						 }
						 else{
								setTimeout(scrollFunc, 1500);
							}
					 });
				}		  
			}
		};
		scrollFunc();
	}
};



$.fn.listSlider = function(options){
	var manager = new sliderManager();
	manager.direction = options.direction;
	manager.context = $('.' + manager.direction + '-slides-container:first', this);
	
	$(manager.context).attr('data-stopScroll', false);
	$(manager.context).hover(function(){
		manager.stop();
	},
	function(){
		manager.start();
	});
	$('.slider-handler', this).mousedown(function(e){
			manager.stop();
	});
	$('.slider-handler', this).mouseup(function(e){
			manager.start();
	});
//	$('.slider-handler', this).slider({
//		animate: true,
//		change: function(e, ui){
//			
//			var maxScroll = $(manager.context).attr("scrollWidth") - 
//							  $(manager.context).width();
//			$(manager.context).animate({scrollLeft: ui.value * 
//				 (maxScroll / 100) }, 1000);
//			$(manager.context).attr('data-scrollValue', parseInt(ui.value) + 5);	
//		},
//		slide: function(e, ui){
//			 var maxScroll = $(manager.context).attr("scrollWidth") - 
//                  $(manager.context).width();
//			 $(manager.context).attr({scrollLeft: ui.value * (maxScroll / 100) });
//			 
//		}
//	  });
	

	manager.start();
	$('.right-hotspot a', this).click(function(e){

		manager.forceMove('right');
	});

	$('.left-hotspot a', this).click(function(e){

		manager.forceMove('left');
	});
}
