/*
The purpose of this code is to alternately hide and show a series of 
divs.  Alternating pause and play buttons permit the user to control the
movement.

This code requires coexistence of jQuery to work.

Authored by Monte Schmiege, 06/10/09

To activate the code, include the following in the head of the html.
	$(document).ready(function () {
		jF.Init('panelFrame','BtnPlay', 'BtnPause', 5000);
	})

The three strings are id names.  The value is the cycle time in milliseconds.
It's best to see an example, such as jFrame.html in tmtsweb.

It's very likely there could be some streamlining or improving of this
code.  Once I got it working, I quit, because I have other things to do.

*/

function jF() {
}
	jF.panelCnt = -1;
	jF.panelCur = 0;
	jF.playing = true;
	jF.timer = -1;
	jF.panels = '';
	jF.panelName = '';
	jF.time = 5000;
	jF.running = false;
	jF.cycles = 3;
	jF.cycleCnt = 0;

	jF.Toggle = function() {
		if (jF.running == false) {
			jF.running = true;
			if (jF.playing) {
				clearTimeout(jF.timer);
				$(jF.buttonPlayId).css('display','');
				$(jF.buttonPauseId).css('display','none');
				jF.playing = false;
			} else {
				jF.playing = true;
				jF.cycleCnt = 0;
				jF.Change(true);
			}
			jF.running = false;
		}
	}

	jF.Change = function(go) {
		if (jF.running == go) {
			jF.running = true;
			clearTimeout(jF.timer);
			jF.panelName = jF.panels[jF.panelCur];
			$(jF.panelName).fadeOut(1000, function() {
				jF.panelCur += 1;
				if (jF.panelCur > jF.panelCnt) { jF.panelCur = 0; }
				jF.panelName = jF.panels[jF.panelCur];
				$(jF.panelName).fadeIn(1000);
				if (jF.panelCur == jF.panelCnt) {
					jF.cycleCnt += 1;
				}
				if (jF.cycleCnt < jF.cycles) {
					jF.timer = setTimeout("jF.Change(false)", jF.time);
					$(jF.buttonPauseId).css('display','');
					$(jF.buttonPlayId).css('display','none');
				} else {
					$(jF.buttonPlayId).css('display','');
					$(jF.buttonPauseId).css('display','none');
					jF.playing = false;
					jF.cycleCnt = 0;
				}
			});
			jF.running = go;
		}
		return true;
	}
	

	jF.Init = function(panelFrameId, buttonPlayId, buttonPauseId, cycleTimeMs, cycles) {
		var childIds = [];
		jF.panelFrameId = '#' + panelFrameId;
		jF.buttonPlayId = '#' + buttonPlayId;
		jF.buttonPauseId = '#' + buttonPauseId;
		jF.time = cycleTimeMs;
		jF.cycles = cycles;
		$(jF.panelFrameId).find('.panel').each(function() {
		  childIds.push(this.id);
		});
		for (var i = 0; i< childIds.length; i++) {
			childIds[i] = '#' + childIds[i];
		}
		jF.panels = childIds;
		jF.panelCnt = childIds.length - 1;
		if (jF.panelCnt > -1) {
			jF.panelCur = 0;
			jF.timer = setTimeout("jF.Change(false)", jF.time);
		}
	}



