// v0.3
$(document).ready(function() {
	
	// scrolling content pane using the scrollTo functionality
    // with lots of additional logic

	// config
	// pixel values for which direction to scroll
	var CONTAINER_HEIGHT = '345'; //do not add px, breaks IE
	var CONTAINER_WIDTH = '0'; //do not add px, breaks IE
	var AUTO_SCROLL = true;
	var MAIN_CONTENT_DIV = 'div.scrollable';
	
    // change here to point to the row groupings from the overflow:hidden css block
	var rows = $(MAIN_CONTENT_DIV).children();
    
    // end config
    
    var scrollingInterval; // initialized, set in the scrollingContentStart function
    var totalRows = rows.length;
    var activeRow = 0;
    rows.eq(0).addClass('active');
    $(MAIN_CONTENT_DIV).scrollTo({top: 0, left: 0});

    var ScrollNextHandler = function(){
    	$(MAIN_CONTENT_DIV).scrollTo({top: '+='+CONTAINER_HEIGHT+'px', left: '+='+CONTAINER_WIDTH+'px'}, 700);
    	incrementActive();
    	checkPlacement();
    	// clear interval so that you don't click back right as it wants to scroll and it
    	// bounces the page.
    	clearInterval(window.scrollingInterval);
    	scrollingContentStart();
    };
    
    var ScrollPrevHandler = function(){
    	$(MAIN_CONTENT_DIV).scrollTo({top: '-='+CONTAINER_HEIGHT+'px', left: '-='+CONTAINER_WIDTH+'px'}, 700);
    	decrementActive();
    	checkPlacement();
    	// clear interval so that you don't click back right as it wants to scroll and it
    	// bounces the page.
    	clearInterval(window.scrollingInterval);
    	scrollingContentStart();
    }; 
    
    
	function checkPlacement(){
		if (activeRow == 0){
			//at the beginning
			$('.prevPage').addClass('disabled');
			$('.prevPage').unbind('click');			
		}
		
		if ((activeRow + 1) == totalRows){
			// at the end
			$('.nextPage').addClass('disabled');
			$('.nextPage').unbind('click');
		}
		
		// re-add handlers and enable
		// if there is a next button to push, enable it and add click
		if ((activeRow +1) < totalRows){
			// remove click so we don't add a double event
			$('.nextPage').unbind('click');
			
			$('.nextPage').removeClass('disabled');
			$('.nextPage').bind('click', ScrollNextHandler);
		}
		
		if (activeRow > 0){
			$('.prevPage').unbind('click');
			
			$('.prevPage').removeClass('disabled');
			$('.prevPage').bind('click', ScrollPrevHandler);
		}
     }
     
     function incrementActive(){
    	 if ((activeRow+1) < totalRows){
    		 rows.eq(activeRow).removeClass('active');
    		 activeRow++;
    		 rows.eq(activeRow).addClass('active');
    	 }
     }
     
     
     function decrementActive(){
    	 if ((activeRow) > 0){
    		 rows.eq(activeRow).removeClass('active');
    		 activeRow--;
    		 rows.eq(activeRow).addClass('active');
    	 }
     }
     
     function scrollingContentStart(){
    	 if (totalRows != 1 && AUTO_SCROLL){
    	    	window.scrollingInterval = setInterval(function(){
    		    	if ((activeRow+1) == totalRows){
    		    		// we hit the end, scroll back to the beginning
    		    		$(MAIN_CONTENT_DIV).scrollTo({top: 0, left: 0}, 700);
    		    		rows.eq(activeRow).removeClass('active');
    		    		activeRow = 0;
    		    		rows.eq(0).addClass('active');
    		    		checkPlacement();
    	    		}
    	    		else{ 
    	    		// move foward one
    	    		$(MAIN_CONTENT_DIV).scrollTo({top: '+='+CONTAINER_HEIGHT+'px', left: '+='+CONTAINER_WIDTH+'px'}, 700);
    	    		incrementActive();
    	    		checkPlacement();
    	    		}
    	    		
    	    	}, 10000);
    	 }
     }
     
    // set up the default handlers
	$('.nextPage').click(ScrollNextHandler);
	$('.prevPage').click(ScrollPrevHandler);
	
 	// initialize the classes and positioning
    checkPlacement();
    
    // automate the scroller    
    scrollingContentStart();
});

