/* slideShow.js */

/*
	2010-Dec-23  dwl  added preGoCallback
	2010-Dec-06  dwl  changed initization of settings based on args type
*/

(function () {
	if (typeof OCMS === "undefined") {
		/* OCMS becomes a global var */
		OCMS = {};
	}
	
	if (OCMS.slideShow === undefined) {
		OCMS.slideShow = function (args) {

			function goTo (nIndex) {
				//console.log("goTo: %d from %d", nIndex, nCurIndex);
				if (nIndex !== nCurIndex) {
				
					if (typeof settings.preGoCallback === 'function') {
						settings.preGoCallback({
							oldIndex: nCurIndex,
							newIndex: nIndex,
							$newItem: $items.eq(nIndex),
							maxIndex: nMaxIndex							
						});
					}
				
					$items.eq(nCurIndex).hide();	//fadeOut(settings.transitionSpeed);
					nCurIndex = nIndex;
					$items.eq(nCurIndex).fadeIn(settings.transitionSpeed);
					// does not reset time to next slide (should it?)
				}
			}
			
			function go (delta) {
				var nOldIndex = nCurIndex;
				
				if (nMaxIndex > 1) {
					if (delta !== 1 && delta !== -1) {
						delta = 1;
					}
	
					//console.log("go: %d %d", nCurIndex, delta);
					
					// show next (or previous!) image
					nCurIndex += delta;
					if (nCurIndex > nMaxIndex) {
						nCurIndex = 0;
					} else {
						if (nCurIndex < 0) {
							nCurIndex = nMaxIndex;
						}
					}
					
					if (typeof settings.preGoCallback === 'function') {
						settings.preGoCallback({
							oldIndex: nOldIndex,
							newIndex: nCurIndex,
							$newItem: $items.eq(nCurIndex),
							maxIndex: nMaxIndex
						});
					}
					
					$items.eq(nOldIndex).fadeOut(settings.transitionSpeed);
					$items.eq(nCurIndex).fadeIn(settings.transitionSpeed);
					
					// remember, we're not always running; this could have been a 'manual' advance
					if (bRunning) {
						if (timeOutId) {		// assert(timeOutId);
							// stop the timer (we're not necessarily here because it's been triggered)
							clearInterval(timeOutId);
							timeOutId = null;
						}
						
						// 60000 ms per minute
						if (settings.stopMinutes && Math.round((new Date() - nRunningStart) / 60000) < settings.stopMinutes) {
							timeOutId = setInterval(go, settings.slideDuration);
						} else {
							pause();
						}
					}
				}
			}	// go
			
			function pause () {
				if (bRunning) {
					bRunning = false;
					clearInterval(timeOutId);
					timeOutId = null;
				}
			}
		
			function resume () {
				if (!bRunning) {
					timeOutId = setInterval(go, settings.slideDuration);
					nRunningStart = new Date();
					bRunning = true;
				}
			}

			function toggle () {
				if (bRunning) {
					pause();
				} else {
					resume();
				}
				
				return bRunning;
			}


			var defaults = {
					slideDuration: 5000,	// does not adjust based on transitionSpeed
					transitionSpeed: 750,
					autoStart: true,
					stopMinutes: 30,
					startAtSlideNumber: 0,
					preGoCallback: null
				},
				ignoreMe = {},
				settings = {};
				
			if (typeof args === "string") {
				settings = defaults;
				settings.selector = args;
			} else {
				// assume parameter object
				settings = $.extend(true, ignoreMe, defaults, args);
			}
				
			var	$items = $(settings.selector),
				nMaxIndex = $items.length - 1,
				timeOutId = null,
				bRunning = false,
				nCurIndex = settings.startAtSlideNumber,
				nRunningStart = 0;

			if (OCMS.inPageEditor) {
				settings.autoStart = false;
			}
			
			if (nMaxIndex > 0) {
				// assumes that all items but the starting one should initially be hidden			
//				$items.filter(function (nIt) { return nIt !== nCurIndex; }).hide();
				$items.not(':eq(' + nCurIndex + ')').hide();
			
				if (settings.autoStart) {
					timeOutId = setInterval(go, settings.slideDuration);
					nRunningStart = new Date();
					bRunning = true;
				}
			}
		
			return {
				pause: pause, 
				resume: resume,
				next: function () { return go(1); },
				previous: function () { return go(-1); },
				getSlideNumber: function () { return nCurIndex; },
				goToSlideNumber: goTo,
				toggle: toggle
			};
		}	// slideShow
	}	// OCMS.slideShow === undefined
})();
