/*	
 *	jQuery endlessScroller 0.1
 *	More info:
 *	www.supadupa.co.za/endless-scroll
 *	
 *	Copyright (c) 2010 Murray Crowe
 *	www.supadupa.co.za
 *
 *	Licensed under the MIT license.
 *	http://www.opensource.org/licenses/mit-license.php
 
 *	http://www.learningjquery.com/2007/10/a-plugin-development-pattern
 */

(function($) {
	
	$.fn.endlessScroll = function(options){
		
		$.fn.endlessScroll.settings = $.extend({}, $.fn.endlessScroll.defaults, options);
		var settings = $.fn.endlessScroll.settings;
		
		return this.each(function(){
								  
			$this = $(this);
			
			$this.css({
				position: 'relative',
				overflow: 'hidden'
			});
			
			if(settings.direction == 'up' || 'down'){
				$this.children().css('display','block');
				$this.wrapInner('<span class="scroll-section" />');
				// don't animate if dimensions are less than container
				if($this.children('.scroll-section').height() > $this.height()){
					$this.children('.scroll-section').clone().appendTo($this);
					$this.wrapInner('<span class="scroll-holder" style="position: relative;display: -moz-inline-box;display: inline-block;*zoom: 1; *display: inline;" />');
					$.fn.endlessScroll.play($this.children('.scroll-holder'));
					if(settings.pauseOnHover){
						$this.hover(
							function(){
								//console.log(123);
								$this.children('.scroll-holder').stop();
							},
							function(){
								$.fn.endlessScroll.resume($this.children('.scroll-holder'));
							}
						);
					}
					//console.log('settings.direction');
					//console.log(distance);
				}
			}else{
				$this.children().css('display','inline');
				$this.wrapInner('<span class="scroll-section" style="white-space:nowrap;text-align: left;"/>');
				// don't animate if dimensions are less than container
				if($this.children('.scroll-section').width() > $this.width()){
					$this.children('.scroll-section').clone().appendTo($this);
					$this.wrapInner('<span class="scroll-holder" style="position: relative;display: -moz-inline-box;display: inline-block;zoom: 1; *display: inline; white-space:nowrap;" />');
					distance = $this.find('.scroll-section').first().width();
					$this.children().hover(function(){$.fn.endlessScroll.pause($(this))},function(){$.fn.endlessScroll.resume($(this))});
					$.fn.endlessScroll.play($this.children('.scroll-holder'), settings.direction, distance, settings.speed);
					//console.log('left');
				}
			}
		
		});
	};
	$.fn.endlessScroll.play = function(obj, currpos){
		
		//console.log(this.settings);
		var currpos = currpos || 0;
		
		switch(this.settings.direction){
			case 'up':
				//console.log(start);
				if(currpos){
					var distance = currpos + obj.height() / 2;
				} else {
					var distance = obj.height() / 2;
				}
				var percentComplete = currpos / distance;
				var props = {top: "-=" + distance};
				var css = {top: 0 + currpos};
				break;
			case 'down':
				endy = obj.parent().height() - obj.height()/2;
				if(currpos){
					distance = -1 * (endy + currpos);
					percentComplete = (endy / currpos);
					//console.log(distance);
				} else {
					distance = obj.height();
					percentComplete = 1;
					//console.log(distance);
				}
				console.log(percentComplete);
				starty = obj.parent().height() - obj.height();
				starty = (currpos)? currpos : starty;
				props = {top: endy};
				css = {top: starty + 'px'};
				break;
			case 'right':
				startx = obj.parent().width() - obj.width();
				endx = obj.parent().width() - obj.width()/2;
				props = {left: endx};
				css = {left: startx + currpos + 'px'};
				break;
			default: //aka left or anything else...
				props = {left: "-=" + distance};
				css = {left: 0 - currpos};
				break;
		}
		
		//console.log(obj);
		obj.css(css);
		time = distance * 1000 * (percentComplete + 1) / this.settings.speed;
		obj.animate(
			props, time, 'linear', function(){$.fn.endlessScroll.play(obj)}
		);
	}
	
	$.fn.endlessScroll.resume = function(obj){
		switch(this.settings.direction){
			case 'up': 
				var currpos = $(obj).css('top');
				break;
			case 'down': 
				var currpos = $(obj).css('top');
				break;
			default: // left or right
				var currpos = $(obj).css('left');
		}
		
		currpos = parseInt(currpos);
		//console.log(start);
		$.fn.endlessScroll.play(obj, currpos);
	};
	
	// defaults
	$.fn.endlessScroll.defaults = {
		direction:		'left',
		speed:	 		100,
		scrollbar:		false,
		pauseOnHover:	false //todo
	};
	
})(jQuery);
