//Scrolly - Bottom to Top Scroll
ScrollyU = function(newslist){
		//Options
		
		var Scrolly = this;
		//The main element - container
		Scrolly.el = newslist;
		//The height of the container
		Scrolly.scrollHeight = 'auto';
		//Default speed if no other is set
		Scrolly.scrollSpeed = 3;
		//The generated wrapper
		Scrolly.scrolly_scroll;
		//All elements in the scroller
		Scrolly.all;
		Scrolly.moveWith;
		Scrolly.time;
		
		Scrolly.started = false;
		
		//Initiate Scrolly
		Scrolly.start = function(){
			//If initiating now ...
			if(!Scrolly.started){
				
				Scrolly.started = true;
				
				Scrolly.scrollHeight = Scrolly.el.height();
				
				Scrolly.scrollSpeed = Scrolly.el.attr('data-speed') > 9 ? 9 : Scrolly.el.attr('data-speed');
				Scrolly.calculateSpeed();
				
				Scrolly.scrolly_scroll = $('<div class="scrolly_scroll" />').append(Scrolly.el.contents()).appendTo(Scrolly.el);
				
				//Must make pause button?
				Scrolly.el.attr('data-pause') == 'true' ? Scrolly.el.append(Scrolly.generatePause()) : '';
				//Must make speed bar?
				Scrolly.el.attr('data-bar') == 'true' ? Scrolly.el.append(Scrolly.generateBar()) : '';
				
				Scrolly.prepare();
			} else {
				//If already started, just loop to the next element
				Scrolly.go();
			}
		},
		//Assigning styles and mouseover events. Pause on mouseover
		Scrolly.prepare = function(){
			Scrolly.el.css({
				'overflow': 'hidden',
				'position': 'relative'
			});
			
			Scrolly.scrolly_scroll.css({
				'position': 'absolute',
				'top': '0px'
			});
			
			Scrolly.el.mouseenter(function(){
				$(this).children('.pause').show(200);
				$(this).children('.speedBar').show(200);
				Scrolly.scrolly_scroll.stop(true, false);
			});
			
			Scrolly.el.mouseleave(function(event){
				Scrolly.scrollSpeed == 0 ? '' : $(this).children('.pause').hide(200);
				$(this).children('.speedBar').hide(200);
				Scrolly.go(true);
			});
			
			Scrolly.el.height() < Scrolly.scrolly_scroll.height() ? Scrolly.go() : '';
			
		}
		//Calculate the scrolling distance and the time according to distance and speed
		Scrolly.go = function(mouseouter){
			
			if(!mouseouter){
			
				Scrolly.scrolly_scroll.css('top', '0px');
				
				Scrolly.scrolly_scroll.append(Scrolly.scrolly_scroll.children().first().clone());
				
				Scrolly.all = Scrolly.scrolly_scroll.children();
				
				Scrolly.moveWith = $($(Scrolly.all).get(1)).position().top;
				
				Scrolly.time = Scrolly.moveWith*(50/Scrolly.scrollSpeed);
			} else {
				Scrolly.time = (Scrolly.moveWith - parseInt(Scrolly.scrolly_scroll.css('top'))*-1)*(50/Scrolly.scrollSpeed);
			}
			
			Scrolly.time > 0 ? Scrolly.vertical() : '';
			
		}
		//Make the animation
		Scrolly.vertical = function(){
			Scrolly.scrolly_scroll.animate({
				'top': (Scrolly.moveWith*-1) + 'px'
			}, Scrolly.time, 'linear', function(){
				//On finish animation, remove the moved element and continue to next
				$($(Scrolly.all).get(0)).remove();
				Scrolly.timer();
			});
		}
		
		Scrolly.timer = function(){
			//Start again - loop
			Scrolly.start();
		}
		//Function which gerenates pause button
		Scrolly.generatePause = function(){
			return $('<a href="#" class="pause" style="display: none;"></a>').bind('click', function(event){
				event.preventDefault();
				
				Scrolly.scrollSpeed !=0 ? $(this).attr('data-speed', Scrolly.scrollSpeed) : '';
				Scrolly.scrollSpeed !=0 ? Scrolly.scrollSpeed = 0 : Scrolly.scrollSpeed = $(this).attr('data-speed');

			});
		}
		//Function which gerenates speed bar
		Scrolly.generateBar = function(){
			
			var bar = '';
			bar += '<div class="speedBar" style="display: none">';
				bar += '<a href="javascript:;" class="slider" data-speed="1" style="top:' + (Math.ceil(Scrolly.scrollSpeed*10)-8) +  'px;">' + Math.ceil(Scrolly.scrollSpeed) + '</a>';
			bar += '</div>';
			
			return $(bar).bind({
				mousedown: function(event){
					event.preventDefault();
					$(this).mousemove(function(event){
						event.preventDefault();
						
						var p = (event.pageY - $(this).parent().offset().top) - 22
						
						if(p > 0 && (p+18) < $(this).height()){
							$(this).children('.slider').css('top', p + 'px').html(Math.ceil($(event.target).position().top/10));
						}
					});
				},
				mouseup: function(event){
					event.preventDefault();
					$(this).unbind('mousemove');
					
					$(event.target).hasClass('slider') ? '' : ($(event.target) = $(this).children('.slider'));
					
					$(event.target).html(Scrolly.scrollSpeed = Math.ceil($(event.target).position().top/10));
					Scrolly.calculateSpeed();
				}
			});
			
		}
		
		Scrolly.calculateSpeed = function(){}
		Scrolly.start();
}
//Scrolly - Top to Bottom Scroll
ScrollyD = function(newslist){
		//Options
		
		var Scrolly = this;
		//The main element - container
		Scrolly.el = newslist;
		//The height of the container
		Scrolly.scrollHeight = 'auto';
		//Default speed if no other is set
		Scrolly.scrollSpeed = 3;
		//The generated wrapper
		Scrolly.scrolly_scroll;
		//All elements in the scroller
		Scrolly.all;
		Scrolly.moveWith;
		Scrolly.time;
		
		Scrolly.started = false;
		
		//Initiate Scrolly
		Scrolly.start = function(){
			//If initiating now ...
			if(!Scrolly.started){
				
				Scrolly.started = true;
				
				Scrolly.scrollHeight = Scrolly.el.height();
				
				Scrolly.scrollSpeed = Scrolly.el.attr('data-speed') > 9 ? 9 : Scrolly.el.attr('data-speed');
				Scrolly.calculateSpeed();
				
				Scrolly.scrolly_scroll = $('<div class="scrolly_scroll" />').append(Scrolly.el.contents()).appendTo(Scrolly.el);
				
				//Must make pause button?
				Scrolly.el.attr('data-pause') == 'true' ? Scrolly.el.append(Scrolly.generatePause()) : '';
				//Must make speed bar?
				Scrolly.el.attr('data-bar') == 'true' ? Scrolly.el.append(Scrolly.generateBar()) : '';
				
				Scrolly.prepare();
			} else {
				//If already started, just loop to the next element
				Scrolly.go();
			}
		},
		//Assigning styles and mouseover events. Pause on mouseover
		Scrolly.prepare = function(){
			Scrolly.el.css({
				'overflow': 'hidden',
				'position': 'relative'
			});
			
			Scrolly.scrolly_scroll.css({
				'position': 'absolute',
				'bottom': function(){
					Scrolly.defaultPosition = (Scrolly.scrollHeight-(Scrolly.scrolly_scroll.height() - Scrolly.scrolly_scroll.children().last().position().top)) + 'px';
					
					if (parseInt(Scrolly.defaultPosition) < Scrolly.scrollHeight){
						Scrolly.defaultPosition = '0px';
					}
					return Scrolly.defaultPosition;
				}
			});
			
			Scrolly.el.mouseenter(function(){
				$(this).children('.pause').show(200);
				$(this).children('.speedBar').show(200);
				Scrolly.scrolly_scroll.stop(true, false);
			});
			
			Scrolly.el.mouseleave(function(event){
				Scrolly.scrollSpeed == 0 ? '' : $(this).children('.pause').hide(200);
				$(this).children('.speedBar').hide(200);
				Scrolly.go(true);
			});
			
			Scrolly.el.height() < Scrolly.scrolly_scroll.height() ? Scrolly.go() : '';
			
		}
		//Calculate the scrolling distance and the time according to distance and speed
		Scrolly.go = function(mouseouter){
			
			if(!mouseouter){
			
				Scrolly.scrolly_scroll.css('bottom', Scrolly.defaultPosition);
				
				Scrolly.scrolly_scroll.prepend(Scrolly.scrolly_scroll.children().last().clone());
				
				Scrolly.all = Scrolly.scrolly_scroll.children();
				
				Scrolly.moveWith = Scrolly.scrolly_scroll.height() - $($(Scrolly.all).get(-1)).position().top;
				
				Scrolly.time = Scrolly.moveWith*(50/Scrolly.scrollSpeed);
			} else {
				Scrolly.time = (Scrolly.moveWith - parseInt(Scrolly.scrolly_scroll.css('bottom'))*-1)*(50/Scrolly.scrollSpeed);
			}
			
			Scrolly.time > 0 ? Scrolly.vertical() : '';
			
		}
		//Make the animation
		Scrolly.vertical = function(){
			Scrolly.scrolly_scroll.animate({
				'bottom': (Scrolly.moveWith*-1) + 'px'
			}, Scrolly.time, 'linear', function(){
				//On finish animation, remove the moved element and continue to next
				$($(Scrolly.all).get(-1)).remove();
				Scrolly.timer();
			});
		}
		
		Scrolly.timer = function(){
			//Start again - loop
			Scrolly.start();
		}
		//Function which gerenates pause button
		Scrolly.generatePause = function(){
			return $('<a href="#" class="pause" style="display: none;"></a>').bind('click', function(event){
				event.preventDefault();
				
				Scrolly.scrollSpeed !=0 ? $(this).attr('data-speed', Scrolly.scrollSpeed) : '';
				Scrolly.scrollSpeed !=0 ? Scrolly.scrollSpeed = 0 : Scrolly.scrollSpeed = $(this).attr('data-speed');

			});
		}
		//Function which gerenates speed bar
		Scrolly.generateBar = function(){
			
			var bar = '';
			bar += '<div class="speedBar" style="display: none">';
				bar += '<a href="javascript:;" class="slider" data-speed="1" style="top:' + (Math.ceil(Scrolly.scrollSpeed*10)-8) +  'px;">' + Math.ceil(Scrolly.scrollSpeed) + '</a>';
			bar += '</div>';
			
			return $(bar).bind({
				mousedown: function(event){
					event.preventDefault();
					$(this).mousemove(function(event){
						event.preventDefault();
						
						var p = (event.pageY - $(this).parent().offset().top) - 22
						
						if(p > 0 && (p+18) < $(this).height()){
							$(this).children('.slider').css('top', p + 'px').html(Math.ceil($(event.target).position().top/10));
						}
					});
				},
				mouseup: function(event){
					event.preventDefault();
					$(this).unbind('mousemove');
					
					$(event.target).hasClass('slider') ? '' : ($(event.target) = $(this).children('.slider'));
					
					$(event.target).html(Scrolly.scrollSpeed = Math.ceil($(event.target).position().top/10));
					Scrolly.calculateSpeed();
				}
			});
			
		}
		
		Scrolly.calculateSpeed = function(){}
		Scrolly.start();
}


$(window).load(function(){
	//Get all Elements and determine what direction to scroll them
	$('[data-scrolly=yes]').each(function(){
		$(this).attr('data-direction') == 'down' ? new ScrollyD($(this)) : new ScrollyU($(this));
	});
	
});
