


function getTotalWidth(elm){
	var tot = 0;
	var kiddies = elm.children();
	kiddies.each(function(){
		var w = $(this).width();
		var pr = parseInt($(this).css('padding-right').replace('px',''));
		var pl = parseInt($(this).css('padding-left').replace('px',''));
		var mr = parseInt($(this).css('margin-right').replace('px',''));
		var ml = parseInt($(this).css('margin-left').replace('px',''));
		var tw = w + pr + pl + mr + ml;
		tot += tw;
	});
	return tot;
}




jQuery.fn.tanyascroll = function(options){
	
	options = jQuery.extend({speed: 10, center: 100, alignment:'horizontal'},options);
	
	//console.log(options.alignment);
	
	return this.each(function(){
		
		var scroller = $(this);
		var container = scroller.parent();
		var contents = scroller.children();
		
		
		if (options.alignment == 'horizontal'){
			scroller.width(getTotalWidth(scroller));
		}
		
		
		$(container).hover(function(e){
			this.active = true;
			var self = this;
			jQuery(this).scroll_center(e, options);
			this.timer = window.setInterval(function(){ jQuery.scroll_do(self, options); },50);
		}, function(){
			clearInterval(this.timer);
			this.active = false;
		});
		
		
		jQuery(container).mousemove(function(e){
			jQuery(this).scroll_center(e,options);
		});
		
		
	});
}





jQuery.scroll_do = function(t, o){
	
	var y = t.offset;
	
	var oOuter = $(t);
	var trg = oOuter.children()[0];	// ASSUME THAT THE SCROLLING THING IS THE FIRST CHILD OF THE CONTAINER
	var oInner = $(trg);
	
	
	var outer_size = (o.alignment == 'horizontal') ? oOuter.width() : oOuter.height();
	var inner_size = (o.alignment == 'horizontal') ? getTotalWidth(oInner) : oInner.height();
	
	if (( Math.abs(y) > o.center ) && ( inner_size > outer_size )){

		var yoffset = Math.round(y * o.speed/100);
		
		// If I understood why trg and oInner were returning different things, I might be able to say why offsetLeft returns 'undefined' on oInner.
		// In any case, it doesn't. So get the inner offset - the offset of the scroller from the *container* rather than from the viewport - from trg.
		var osl = trg.offsetLeft
		var ost = trg.offsetTop
		
		var newy = (o.alignment == 'horizontal') ? (osl + yoffset) : (ost + yoffset);
		
		var max = 0;
		var min = -(inner_size - outer_size);
		
		if (( newy > max ) || ( newy < min)){
			newy = ( newy > max ) ? max : min;
		}
		
		var sCSSAxis = (o.alignment == 'horizontal') ? "left" : "top";
		//console.log(o.alignment);
		oInner.css(sCSSAxis, newy+"px");

	}

};




jQuery.fn.scroll_center = function(e, o){
	return this.each(function(){
		var thisOffset = $(this).offset();
		var size		= (o.alignment == 'horizontal') ? $(this).width() : $(this).height();
		var mousepos	= (o.alignment == 'horizontal') ? e.pageX : e.pageY;
		var ofst		= (o.alignment == 'horizontal') ? thisOffset.left : thisOffset.top;
		var xof	=  size / 2 - ( mousepos - ofst);
		this.offset = xof;
	});
}






