// JavaScript Document
/**
* These two variables need to be registered to the window object because
* HttpRequest handlers are fired by the browser outside of any object and these are necessary for
* slide handling.
* 
* slideTotal is an integer which sets the high slide ID in order to make sure no slide is called that doesn't exist
* slideIndex is the ID of the current slide.
*/
window.slideTotal = 1;
window.slideIndex = 1;

/**
*Control structure for slides
*/
function slideControl() {
	// getCount checks countSlides.php for current slide index.
	this.getCount = function() {
		count = new createXMLHttpRequest();
		count.open('GET','./php/countSlides.php',true);
		function parseStatus() {
			if (count.readyState == 4) {
				if (count.status == 200) {
					//set global slide total variable
					window.slideTotal = count.responseText;
				} else {
					// server error
				}
			}
		}
		count.onreadystatechange = parseStatus;
		count.send(null);
	}
	/*
	* followStat is the staus of the follow button
	* followCheck is a container for the "follow" interval ID set here so it is in the appropriate namespace
	*/
	var followStat = 0; //set follow off
	var followCheck;
	this.next = function() {
		if((slideIndex + 1) <= window.slideTotal){
			window.slideIndex++;
			document.getElementById('slideImg').style.backgroundImage = "url('./slides/"+slideIndex+".jpg')";
			document.getElementById('follow').style.backgroundImage = "url('./style/img/follow-off.png')";
			// clear followCheck interval
			clearInterval(followCheck);
			followStat = 0;
			if((slideIndex + 1) > window.slideTotal){
				document.getElementById('forward').style.display = 'none';
			}
			if(document.getElementById('back').style.display == 'none') {
				document.getElementById('back').style.display = 'block';
			}
		}
	}
	this.previous = function() {
		if((slideIndex -1) >= 1){
			window.slideIndex--;
			document.getElementById('slideImg').style.backgroundImage = "url('./slides/"+slideIndex+".jpg')";
			document.getElementById('follow').style.backgroundImage = "url('./style/img/follow-off.png')";
			// clear followCheck interval
			clearInterval(followCheck);
			followStat = 0;
			if((slideIndex -1) < 1){
				document.getElementById('back').style.display = 'none';
			}
			if(document.getElementById('forward').style.display == 'none') {
				document.getElementById('forward').style.display = 'block';
			}
		}
	}
	this.follow = function() {
		if(followStat == 1) {
			document.getElementById('follow').style.backgroundImage = "url('./style/img/follow-off.png')";
			// clear followCheck interval
			clearInterval(followCheck);
			followStat = 0;
		}else{
			document.getElementById('follow').style.backgroundImage = "url('./style/img/follow-on.png')";
			// set currentSlide to run every 3 seconds
			followCheck = setInterval(currentSlide,3000);			
			followStat = 1;
		}
	}
	// debugging method to echo current slide index.
	this.echo = function() {
		alert(slideIndex);
	}
}
// creating object to control slides
var slideStuff = new slideControl();
//setting slideTotal appropriately.
slideStuff.getCount();
