// Enable auto-advancing of the slideshow
var lastChange = 0; // When was the last slideshow shift?
var slideshowTimeout = 10 * 1000; // How many MS to wait before auto-advancing?
var autoAdvanceOn = true;
var pauseBtnHandle;
function autoAdvance() {
	if (autoAdvanceOn == false) { return false; }
	var now = new Date();
	var curMS = now.getTime();
	if (curMS > lastChange+slideshowTimeout) {
		// Time to change!
		dijit.byId('slideshow').forward()
		lastChange = curMS;
	}
	setTimeout(autoAdvance, 3000); // Call again in 3 seconds to see if 'slideshowTimeout' time has passed
	return true;
}

function disableAdvance() {
	autoAdvanceOn = false;
	console.info("Autoplay is off");
	var btn = dijit.byId("pause"); // Pause button object
	btn.attr("iconClass", "playIcon");
	btn.attr("label", "Play");
	dojo.disconnect(pauseBtnHandle);
	pauseBtnHandle = dojo.connect(btn, "onClick", function(evt) {enableAdvance();});
}

function enableAdvance() {
	var now = new Date();
	lastChange = now.getTime();
	autoAdvanceOn = true;
	if (console.info) { console.info("Autoplay is on") }
	setTimeout(autoAdvance, 1000);	
	var btn = dijit.byId("pause"); // Pause button object
	btn.attr("iconClass", "pauseIcon");
	btn.attr("label", "Pause");
	dojo.disconnect(pauseBtnHandle);
	pauseBtnHandle = dojo.connect(btn, "onClick", function(evt) {disableAdvance();});
}

// Startup/setup function
var slideshowInit = function() {
	if (typeof console != "undefined" && console.info) { console.info("Setting up Slideshow") } // Announce that we're working

	var btn = new dijit.form.Button({"iconClass":"pauseIcon png", "showLabel":false}, "pause");
	pauseBtnHandle = dojo.connect(btn, "onClick", function(evt) {disableAdvance();});

	var slideshowStack = new dijit.layout.TabContainer({tabPosition:"top", style:"margin-top:1em; width:700px; height:300px"}, "slideshow"); // Stack container of slideshow Panes
	slideshowStack.addChild(new dijit.layout.ContentPane({title:"Intro"}, "intro"));
	slideshowStack.addChild(new dijit.layout.ContentPane({title:"User Experience"}, "user_experience"));
	slideshowStack.addChild(new dijit.layout.ContentPane({title:"Protocols"}, "protocol"));
	slideshowStack.addChild(new dijit.layout.ContentPane({title:"Subjects"}, "subject"));
	slideshowStack.addChild(new dijit.layout.ContentPane({title:"Compliance"}, "compliance"));
	slideshowStack.addChild(new dijit.layout.ContentPane({title:"Financials"}, "financials"));
	slideshowStack.addChild(new dijit.layout.ContentPane({title:"Specimens"}, "specimen"));
	slideshowStack.addChild(new dijit.layout.ContentPane({title:"Crescendo"}, "crescendo"));
	dojo.connect(slideshowStack, "onClick", function(evt) { var now = new Date(); lastChange = now.getTime(); }); // Changing tabs resets auto-play

	//slideshowStack.layout();
	slideshowStack.startup(); // Activate the whole unit

	var now = new Date();
	lastChange = now.getTime(); // Update current time
	setTimeout(autoAdvance, 1000); // start the auto-advancement script
}
dojo.require("dojo.fx");
dojo.require("dojo.fx.easing");
dojo.require("dojo.parser");
dojo.require("dijit.form.Button");
dojo.require("dijit.layout.TabContainer");
dojo.require("dijit.layout.ContentPane");

dojo.addOnLoad(slideshowInit); // Initialization function