$(function() {
//	scrollGangsta('latestNewsBox');
	
	var ticker = $("#latestNewsBox");
	//start the ticker
	animator(ticker.children(":first"));
	//set mouseenter
	ticker.mouseenter(function() {
		//stop current animation
		ticker.children().stop();
    });
    //set mouseleave
	ticker.mouseleave(function() {
		//resume animation
	    animator(ticker.children(":first"));
    });
    
});
// from http://free-latest-blogger-templates.blogspot.com/
// http://bloggerplugnplay.blogspot.com/2009/10/verticle-auto-scroll-news-ticker-using.html
function animator(currentItem) {
    //work out new anim duration
    var distance = currentItem.height();
    duration = (distance + parseInt(currentItem.css("marginTop"))) / 0.02;

    //animate the first child of the ticker
    currentItem.animate({marginTop: -distance}, duration, "linear", function() {
	    //move current item to the bottom
	    currentItem.appendTo(currentItem.parent()).css("marginTop", 0);
	
	    //recurse
	    animator(currentItem.parent().children(":first"));
    }); 
};

/* Start scroll gangsta */  
function scrollGangsta(thing,ax,ad){
	if(ax=="deactivate"){
		scrollGangsta[thing]=null
	}else{
		if($("#"+thing).length!=0){
			if(scrollGangsta[thing]){
				return scrollGangsta[thing]
             }else{
            	 if(ax){
            		 scrollGangsta[thing]=new dtScroller($("#"+thing)[0])
        		 }else{
        			 scrollGangsta[thing]=new dtScroller($("#"+thing)[0],ax,ad)
    			 }
        	 }
		}
	}
}
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;
		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(scrollGangsta) { scrollGangsta["+this.id+"] = null; } clearInterval("+owner.scroller+"); }",owner.speed);
				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;
		scrollGangsta(this.id,"deactivate")
	};
	this.active();
}
/* End scroll gangsta */
