(function() {
	var $caption;

	function updateCaption(e, slider) {
		//console.log(slider);
		var _cur = slider.currentPage;
		var $img = slider.$el.find('.activePage img');
		var _alt = $img.attr('alt');
		$caption.stop().animate({
			opacity : 0
		}, 200, function() {
			$caption.html(_alt);
			$caption.animate({
				opacity : 1
			}, 200)
		});
	}

	function init() {
animateSprite();
		$caption = $('#gallery-caption');
		$('#gallery-slider').anythingSlider({
			autoPlay : true,
			hashTags : false,
			
			
			autoPlay		: true,
				autoPlayLocked:true,
				startStopped: true,
				pauseOnHover        : true,  
				delay		: 6000,
			resizeContents : false,
			buildArrows : false,
			buildNavigation : true,
			buildStartStop : false,
			forwardText : "",
			backText : "",
			infinite : true,
			startPanel : 1,
			animationTime : 400
		});
		$('.anythingSlider').bind('slide_complete', updateCaption);
		updateCaption(null, $('#gallery-slider').data('AnythingSlider'));
	}
	
	
	var $steps = 9;
	var $aH = -84;
	var $sH = 0;
	var $rSteps=1;
	
	function animateSprite(){
		
		$ran = Math.floor(Math.random() * (15000 - 6000 + 1) + 6000);
		
		
	    setInterval(function() {
         animateSpriteDown();
    	}, $ran );	
	}
	
	function animateSpriteDown(){
		$sH = $aH * $rSteps;
	
		$('.run-for-kids').animate({
    		backgroundPosition: 'left '+$sH
  		},28,function(){  
	 		$rSteps += 1; 
	  		if($rSteps == $steps){		$rSteps = 8; animateSpriteUp(); return;}
		    animateSpriteDown();
  		});	
	}
	
	function animateSpriteUp(){
			$rSteps -= 1; 
		$sH = $aH * $rSteps;
		$('.run-for-kids').animate({
    		backgroundPosition: 'left '+$sH
  		},28,function(){  
	 	
	  		if($rSteps == 0){$rSteps = 1; 
 return;}
		    animateSpriteUp();
  		});	
	}
	
	
	
	$(window).load(init);
})();

