/* ----------------------------------------------------------------------
 * js/yosUI.js : javascript-based user interface controls
 * ----------------------------------------------------------------------
 */

var yosUI = yosUI || {};

(function ($) {
	
	//
	// Scroller - displays a list of div with nice horizontal scrolling movement
	//
	yosUI.newScroller = function(scrollingDivList, DivContainerID, options) {
		// init
		var that = {
			scrollingDivList: scrollingDivList,
			curScrollDivIndex: options.startDiv	? options.startDiv : 0,				// initial Div to display
			scrollingDivLookAhead: options.lookAhead ? options.lookAhead : 3,				// number of Divs to preload following current Div
			scrollingDivScrollSpeed: options.scrollSpeed ? options.scrollSpeed : 0.25,	// time (in seconds) each scroll takes
			containerWidth: options.containerWidth ? options.containerWidth : 200,			// height of DIV containing Divs
			containerHeight: options.containerHeight ? options.containerHeight : 200,		// width of DIV containing Divs
			DivContainerID: DivContainerID ? DivContainerID : 'scrollingDivs',
			DivCounterID: options.DivCounterID,
			DivTitleID: options.titleID,
			noVertCentering: options.noVertCentering,
			noHorizCentering: options.noHorizCentering
		};
		
		// methods
		that.getCurrentIndex = function() {
			return this.curScrollDivIndex;
		}
		that.scrollToNextDiv = function() {
			this.scrollToDiv(1);
		}
		that.scrollToPreviousDiv = function() {
			that.scrollToDiv(-1);
		}
		that.scrollToDiv = function(offset, dontUseEffects) {
			var targetDivIndex = that.curScrollDivIndex + offset;
			if ((targetDivIndex < 0) || (targetDivIndex >= that.scrollingDivList.length)) { return false; }
			
			// create new Div container divs if needed
			var i;
			var maxDivIndex = targetDivIndex + that.scrollingDivLookAhead;
			if (maxDivIndex >= that.scrollingDivList.length) { maxDivIndex = that.scrollingDivList.length - 1; }
			var minDivDiv = targetDivIndex - that.scrollingDivLookAhead;
			if (minDivDiv < 0) { minDivDiv = 0; }
			
			for(i=minDivDiv; i <= maxDivIndex; i++) {
				if ($('#' + this.DivContainerID + 'scrollingDiv' + i).length == 0) {
					$('#' + that.DivContainerID).append('<div class="scrollerEntry" id="' + this.DivContainerID + 'scrollingDiv' + i + '">' + that.scrollingDivList[i].content + '</div>');
					$('#' + this.DivContainerID + 'scrollingDiv' + i).css('left', (that.containerWidth * i)  + "px");
				}
			}
			
			// do scroll
			if (dontUseEffects) {
				$('#' + that.DivContainerID).css('left', (targetDivIndex * -1 * that.containerWidth) + "px");
			} else {
				$('#' + that.DivContainerID).animate(
					{
						left: (targetDivIndex * -1 * that.containerWidth) + "px"
					},
					that.scrollingDivScrollSpeed * 1000
				);
			}
			if (that.DivTitleID) {
				$('#' + that.DivTitleID).html(that.scrollingDivList[targetDivIndex].title);
			}
			if (that.DivCounterID) {
				$('#' + that.DivCounterID).html("(" + (targetDivIndex + 1) + "/" + that.scrollingDivList.length + ")");
			}
			that.curScrollDivIndex = targetDivIndex;
		}
		
		that.scrollToDiv(0, true);
		
		return that;
	}
})(jQuery);
