//
// Project  Name: musclefitness.com
// File / Folder: /docs/2007/js/slideshow.js
// File Language: javascript

var slideshowLooper;

function linkSlide(node) {

	var images = node.getElementsByTagName('img');
	for(var n = 0; n < images.length; ++n) {
		
		if(images[n].className.indexOf('backButton') > -1) {
		
			images[n].onclick = slideBack;
		}
		else if(images[n].className.indexOf('pauseButton') > -1) {
			images[n].onclick = function() { clearInterval(slideshowLooper); };
		}
		else if(images[n].className.indexOf('forwardButton') > -1) {
			images[n].onclick = slideForward;
		}
	}
}

function slideBack() {
	var slides = document.getElementById('slideshow').childNodes;
	//alert(slides.length);
	var previousIndex = null;
	for(var n = 0; n < slides.length; ++n) {
		var current = slides[n];		
		if(!current.className || current.className.indexOf('slide') < 0) {
			continue;
		}

		if(current.className.indexOf('active') > -1) {
			current.className = current.className.replace(/ ?active/g, '');
			if(previousIndex != null) {
				break;
			}
		}

		previousIndex =  n;
	}
	slides[previousIndex].className += ' active';
}
function slideForward() {	
	var slides = document.getElementById('slideshow').childNodes;
	var activateNext = false;
	var firstSlideIndex = null;
	for(var n = 0; n < slides.length; ++n) {
		var current = slides[n];
		if(!current.className || current.className.indexOf('slide') < 0) {
			continue;
		}
		if(firstSlideIndex == null) {
			firstSlideIndex = n;
		}

		if(activateNext) {
			current.className += ' active';
			activateNext = false;
		}
		else if(current.className.indexOf('active') > -1) {
			current.className = current.className.replace(/ ?active/g, '');
			activateNext = true;
		}
	}
	if(activateNext) {
		slides[firstSlideIndex].className += ' active';
	}
	//alert();
	//document.getElementById('rtext').innerHTML="11111";
}

function slideshowLoop() {
	slideshowLooper = window.setInterval(slideForward, 640000);
}

function rgSlideshow() {
	var slides = document.getElementById('slideshow').childNodes;
	for(var n = 0; n < slides.length; ++n) {
		if(slides[n].className && slides[n].className.indexOf('slide') > -1) {
			linkSlide(slides[n]);			
		}
	}
	slideshowLoop();
}

var slideshowOldOnload = window.onload;
window.onload = function() {
	if(slideshowOldOnload) {
		slideshowOldOnload();
	}
	rgSlideshow();
}
