/**********************************************/
//Title: scrollGansta.js
//Author: Nate Vallette
//Description:  Mouseover scrolling of an element.
//Revised: 09/19/2008 Bryan Lumbra
/**********************************************/


//Replace "element_name" with the name of your wrapper div.
//Wrapper must be set to overflow:hidden;
//When scrolling horizontal DO NOT use <ul>.  The overflow will not work properly.
$(function() {
   scrollGangsta("calendar_container");
   scrollGangsta("catwrap");
   scrollGangsta("dwrap");
});



/***DO NOT EDIT BELOW THIS LINE***/
scrollGangsta
function scrollGangsta(thing,ax,ad) {
	if($("#"+thing).length != 0) {
		if(typeof(scrollGangsta[thing]) == 'undefined') {
			if(typeof(ax) == 'undefined') {
				scrollGangsta[thing] = new dtScroller($("#"+thing)[0]);
			}
			else {
				scrollGangsta[thing] = new dtScroller($("#"+thing)[0],ax,ad);
			}
		}
		return scrollGangsta[thing];
	}
}

function dtScroller(obj,axis,addSpeed) {
	this.isActive = true;
	this.scroller = 0;
	this.check = 0;
	this.limit = true;
	this.timeout = 0;
	this.oldMulti = 0;
	this.oldY = 0;
	this.obj = obj;
	this.speed = 50;
	this.multiplier = 10;
	
	this.addSpeed = addSpeed || 0;
	
	this.axis = axis || 'y';
	this.breakpoint = 8;
	
	var parent = this;
	
	this.plane = function(obj,e) {
		obj = $(obj);
		try {
			if(this.axis == 'y') {
				var y = e.pageY - obj.offset().top;
			}
			else {
				var y = e.pageX - obj.offset().left;
				this.breakpoint = 9;
			}
		}
		catch(err) {
			if(this.axis == 'y') {
				var y = window.event.y - obj.offset().top;
			}
			else {
				var y = window.event.x - obj.offset().left;
				this.breakpoint = 9;
			}
		}
		return y;
	};
	
	this.active = function() {
		this.isActive = true;
		//jQ = $;
		tob = $(this.obj);
		
		tob.bind("mousemove", function(event){
			var owner = parent;
			var y = owner.plane(this,event);
			
			ths = $(this);
			
			var offSet = (owner.axis == 'x') ? ths.width() : ths.height();
			
			if( (y - (offSet / 2)) < 0) {
				var scrollDirection = -1;
			}
			else {
				var scrollDirection = 1;
			}
			var h = offSet / 2;
			var percent = (y > h) ? ((y-h)/h) : ((h-y)/h);
			var scrollMultiplier = Math.round(percent * owner.multiplier);
			var scrollSpeed = Math.round(percent * 100);
			scrollSpeed = 100 - scrollSpeed;
			
			
			if(owner.scroller != 0) {
				clearInterval(owner.scroller);
				owner.scroller = 0;
			}
			
			if(scrollMultiplier < owner.breakpoint) {
				if(owner.oldY > y) {
					owner.scrollmonkey(-1,3);
				}
				else if(owner.oldY < y) {
					owner.scrollmonkey(1,3);
				}
				owner.oldY = y;
			}
			else {
				owner.scrollmonkey(scrollDirection,4);
				owner.scroller = setInterval("try{scrollGangsta('"+this.id+"').scrollmonkey("+scrollDirection+",4);}catch(e){if(typeof(scrollGangsta) != 'undefined') { delete scrollGangsta."+this.id+" } clearInterval("+owner.scroller+"); }", owner.speed);//scrollSpeed);
				
				owner.oldY = y;
			}
			owner.check = 1;
			return false;
		});
		
		tob.bind("mouseover", function() {
			owner = parent;
			owner.check = 1;
			if(owner.timeout != 0) {
				clearTimeout(owner.timeout);
				owner.timeout = 0;
			}
		});
		
		tob.bind("mouseout", function(){
			owner = parent;
			if(owner.check == 1) {
				owner.check = 0;
				owner.timeout = setTimeout("try { scrollGangsta."+this.id+".stopScroll(); } catch(e){}",250);
			}
			return false;
		});
	};
	
	this.stopScroll = function() {
		if(this.check == 0) {
			clearInterval(this.scroller);
			this.scroller = 0;
			this.oldMulti = 0;
			this.oldY = 0;
		}
	};
	
	this.scrollmonkey = function(direction, multiplier) {
		var scrollStart = (this.axis == 'x') ? "scrollLeft" : "scrollTop";
		var scrollSize = (this.axis == 'x') ? "scrollWidth" : "scrollHeight";
		
		multiplier = multiplier + this.addSpeed;
		
		if((this.obj[scrollStart] > 0 && direction < 0) || (this.obj[scrollStart] < this.obj[scrollSize] && direction > 0)) {
			this.obj[scrollStart] = this.obj[scrollStart] + (direction * multiplier);
		}
		else {
			clearInterval(this.scroller);
			this.scroller = 0;
		}
	};
	
	this.sleep = function() {
		tob = $(this.obj);
		tob.unbind("mousemove");
		tob.unbind("mouseover");
		tob.unbind("mouseout");
		
		this.isActive = false;
	};
	
	this.active();
		
}