/*	-----------------------------------
	GLOBAL VARIABLES
	All the little bits and bobs used
	by our script.
	----------------------------------- */
var tourPages = new Array();
var tourDelay;
var newURL;
var tourSwitch = true;
var timeout = false;


/*	-----------------------------------
	CONFIGURATION
	These store the list of the pages
	in the tour, and the delay between
	paging.
	----------------------------------- */
tourPages = [
	'1.html',
	'2.html',
	'3.html',
	'4.html'
]
tourDelay = 6000; // In milliseconds, so 6000 == 6 seconds.

/*	-----------------------------------
	INITALISE SCRIPT
	This bit sets up everything up nice
	for us.
	----------------------------------- */
window.onload = function() {
	/* Attach the event to the swtich */
	document.getElementById('tourSwitch').onclick = toggleTour;
	/* Check the cookie */
	cookieMuncher();
	/* Grab the filename */
	if (tourSwitch == true) {
		tourPosition();
	}
}

/*	-----------------------------------
	TOUR BRAIN
	This is most of the logic for getting
	the url and comparing the array etc
	----------------------------------- */
function tourPosition() {
	var url = document.URL;
	var slicePoint = url.lastIndexOf('/', url.length);
	var fileName = url.slice(slicePoint + 1, url.length);
	url = url.slice(0, slicePoint + 1);
	/* Compare it against our array */
	var nextPage;
	var thisPage;
	for (var i in tourPages) {
		if (tourPages[i] == fileName) {
			thisPage = parseInt(i);
			nextPage = parseInt(i) + 1;
		}
	}
	/* See if the tour is OVA! */
	var finishTest = thisPage + 1;
	if (tourPages.length != finishTest) {
		newURL = url + tourPages[nextPage];
		timeout = window.setTimeout('onward()', tourDelay);
	}
}

/*	-----------------------------------
	PAGE FORWARDER
	Dead simple function to set the URL
	----------------------------------- */
function onward() {
		window.location.href = newURL;
}

/*	-----------------------------------
	COOKIE MUNCHER
	Parses the file stored on the
	client browser.
	----------------------------------- */
function cookieMuncher() {
	var cookieJar = document.cookie.split(';');
	for (var i in cookieJar) {
		var cookie = cookieJar[i];
		cookie = cookie.split('=');
		var cookieName = cookie[0];
		if (cookieName == 'wohlers_tour') {
			// fudge it a bit since we only have one paramater.
			if (cookie[1] == 'true') {
				tourSwitch = true;
				tourWidget(true);
			}
			else if (cookie[1] == 'false') {
				tourSwitch = false;
				tourWidget(false);
			}
		}
	}
}

/*	-----------------------------------
	TOUR TOGGLE
	lets the user switch of the tour
	and writes the info to a cookie.
	----------------------------------- */
function toggleTour() {
	/* Create the expiry date */
	var expiry = new Date();
	expiry.setTime(expiry.getTime()+(10*24*60*60*1000));
	/* Make the browser eat the COOOOOKIE! */
	if (tourSwitch == true) {
		tourSwitch = false;
		tourWidget(false);
		// clear the interval if it's been set
		if (timeout != false) {
			window.clearTimeout(timeout);
		}
	}
	else {
		tourSwitch = true;
		tourWidget(true);
		tourPosition();
	}
	document.cookie = 'wohlers_tour=' + tourSwitch + '; expires=' +  expiry.toGMTString() + '; path=/;'
}

/*	-----------------------------------
	WIDGET UPDATE
	This is a small function for updating
	the on/off display of the tour widget
	----------------------------------- */
function tourWidget(status) {
	var tourStatus = document.getElementById('tourStatus');
	if (status == true) {
		tourStatus.className ='on';
		tourStatus.innerHTML = 'on';
	}
	else if (status == false) {
		tourStatus.className ='off';
		tourStatus.innerHTML = 'off';
	}
}