var jQ = jQuery;

/** 
 * variables for slideshow
 */
var slideshow_iCurr = -1;
var slideshow_iLast = 0;
var slideshow_iIntervalId = 0;
var slideshow_flashObj = 0;
var slideshow_dataObj;

/** 
 * slideshowShowSlide
 *
 * When Flash isn't working, this is called. Just do a slide change without a transition effect.
 */
function slideshowShowSlide(iNew) {
	// the slideshow_slideSequence variable is set by Drupal in the main document
	var sSlideHTML = slideshow_dataObj.images[slideshow_slideSequence[iNew]];
	jQ(".sSlideShowImage .sImage").empty().append(sSlideHTML);
	slideshow_iCurr = iNew;
}

/** 
 * slideshowBack
 *
 * If Flash is working, route to that. Otherwise, pass the desired slide number 
 * to slideshowShowSlide.
 */
function slideshowBack(sStatus) {
	if (slideshow_flashObj && flashSlideShowProxy) {
		flashSlideShowProxy.call('back');
	}
	else {
		var iNew = slideshow_iCurr - 1;
		if (iNew < 0) {iNew = slideshow_iLast;}
		slideshowShowSlide(iNew);
	}	
}

/** 
 * slideshowNext
 *
 * If Flash is working, route to that. Otherwise, pass the desired slide number 
 * to slideshowShowSlide.
 */
function slideshowNext() {
	if (slideshow_iCurr < 0) {
		// the first time "next" is called, don't do anything, just initialize the slide number
		slideshow_iCurr = 0;
	}
	else if (slideshow_flashObj && flashSlideShowProxy) {
		flashSlideShowProxy.call('next');
	}
	else {
		var iNew = slideshow_iCurr + 1;
		if (iNew > slideshow_iLast) {iNew = 0;}
		slideshowShowSlide(iNew);
	}
}

/** 
 * slideshowPlay
 *
 * Toggle play/stop buttons and set a timer to call slideshowNext periodically
 */
function slideshowPlay() {
	if (slideshow_iIntervalId) {
		clearInterval(slideshow_iIntervalId);
	}
	slideshow_iIntervalId = setInterval("slideshowNext()", 5000);
	jQ(".sBtnPlay").hide();
	jQ(".sBtnStop").show();
	slideshowNext();
}

/** 
 * slideshowStop
 * 
 * Toggle play/stop buttons and clear the timer
 */
function slideshowStop() {
	if (slideshow_iIntervalId) {
		clearInterval(slideshow_iIntervalId);
		slideshow_iIntervalId = 0;
	}
	jQ(".sBtnStop").hide();
	jQ(".sBtnPlay").show();
}

/** 
 * initialization code, once the document is ready
 */
if (isJsEnabled()) {
	// "ready" happens when the document is fully loaded, parsed, and initialized,
	// but possibly before images and stylesheets are loaded
	jQ(document).ready(function() {
		// only if we're on a page that has a slideshow
		if (jQ(".sSlideShow").size()) {
			// get the flash object
			slideshow_flashObj = document.getElementById("flashSlideShow");		
	
			// if the flash object or its proxy weren't loaded, resort to js-based slideshow
			if (!slideshow_flashObj || !flashSlideShowProxy) {
				// the slideshow_jsDataPath variable is set by Drupal in the main document
				var s = HTTPGet(slideshow_jsDataPath);
				s = s.replace(/^\s+/, '');
				slideshow_dataObj = parseJson(s);
				slideshow_iLast = slideshow_dataObj.images.length - 1;
			}
			
			// show buttons and play
			jQ(".sSlideShowButtons").css("visibility", "visible");
			slideshowPlay();
		}
	});
	
}
