// ---------- List Carousel ----------

RHG.Classes.ListCarousel = new RHG.Class();
RHG.Classes.ListCarousel.selectors = {
	"panelsContainerSelector": ".list-carousel-panels",
	"panelsScrollSelector": ".list-carousel-panel-scroll",	
	"panelsSelector": ".list-carousel-panel"
};
RHG.Classes.ListCarousel.classFunction = function(configObject) {
	var classFunctionPub = {};
	var thisClass = RHG.Classes.ListCarousel;
	
	var selectors = thisClass.selectors;
	var targetJq; // Main HTML element jQuery wrapped 
	var currentTabIndex = 0;
	var panelsJq;
	var panelsContainerJq;
	var panelsScrollJq;	
	
	var testRight = function(panelIndex) {
		// Are there still panels out of view to the right?
		var firstPanelOutOfView = null;
		for (var k = panelIndex + 1; k < panelsJq.length; k++) {
			var relativeRightEdge = (panelsJq.eq(k).position().left - panelsJq.eq(panelIndex).position().left) + panelsJq.eq(k).innerWidth();
			if (relativeRightEdge >= panelsContainerJq.width()) {
				firstPanelOutOfView = k;
				break;
			}
		}	
		return firstPanelOutOfView;
	}
	
	
	// PRE-INIT	
	var showPanel = function(panelIndex) {	

		if (testRight(panelIndex) == null) {
			nextControlButton.disable();
		} else {
			nextControlButton.enable();
		}
		
		if (panelIndex == 0) {
			prevControlButton.disable();
		} else {
			prevControlButton.enable();
		}
		
		var scrollCarouselTo = panelsJq.eq(panelIndex).position().left;


		panelsScrollJq.stop().animate({
			"margin-left": -scrollCarouselTo
		}, {
			duration: 600,
			easing: "swing"
		});		

		currentTabIndex = panelIndex;
	}	
	var showPreviousPanel = function() {
		// Iterate past all the next panels, looking for the one that is over the edge
		var previousPanel = null;
		for (var i = currentTabIndex; i >= 0; i--) {			
			previousPanel = i;			
						
			var relativeRightEdge = (panelsJq.eq(currentTabIndex).position().left - panelsJq.eq(i).position().left) + panelsJq.eq(currentTabIndex).innerWidth();
			if (relativeRightEdge >= panelsContainerJq.width()) {
				previousPanel = i;								
				break;
			}
		}
		showPanel(previousPanel);
		
		if(configObject.onScroll){
		    configObject.onScroll(configObject.uniqueTargetNodeOrSelector, 'left');
		}		
	}
	var showNextPanel = function() {
		// Iterate past all the next panels, looking for the one that is over the edge
		var nextPanel = null;
		for (var i = currentTabIndex; i < panelsJq.length; i++) {			
			// If we use this panel, are there still panels out of view
			var firstPanelOutOfView = testRight(i);
			if (firstPanelOutOfView !== null) {
				// Is this panel *currently* out of view
				var relativeRightEdge = (panelsJq.eq(i).position().left - panelsJq.eq(currentTabIndex).position().left) + panelsJq.eq(firstPanelOutOfView).innerWidth();
				if (relativeRightEdge >= panelsContainerJq.width()) {
					// This panel is the first one currently out of view
					nextPanel = i;								
					break;
				}
			} else {
				// This panel brings everything else into view
				nextPanel = i;	
				break;
			}			
		}
		showPanel(nextPanel);
		
		if(configObject.onScroll){
		    configObject.onScroll(configObject.uniqueTargetNodeOrSelector, 'right');
		}		
	}	
	
	var prevControlButton = new RHG.Utilities.ControlButton("Previous", "list-carousel-previous", function(){
		showPreviousPanel();
	});
	var nextControlButton = new RHG.Utilities.ControlButton("Next", "list-carousel-next", function(){
		showNextPanel();
	});	
	
	
	
	// INIT	
	classFunctionPub.init = function() {
		targetJq = jQuery(configObject.uniqueTargetNodeOrSelector)
		panelsJq = targetJq.find(selectors.panelsSelector)	
		panelsContainerJq = targetJq.find(selectors.panelsContainerSelector);
		panelsScrollJq = targetJq.find(selectors.panelsScrollSelector);
		
		if (panelsJq.length > 0) {
			panelsContainerJq.before(prevControlButton.elemJq);
			panelsContainerJq.after(nextControlButton.elemJq);
			prevControlButton.enable();
			nextControlButton.enable();															
			showPanel(0);
		}
		
		targetJq.addClass('js-active');		
	}

	return classFunctionPub;
}
