// JavaScript Document

window.onload = onDOMready;

// Will hold the different elements of the Ajax reponse
var content;
var myName;
var imgSrc;

// Will keep track of how many quotes have been displayed
var counter = 0;

var response;

var intervalTimer = 0;
var time = 10000;


function onDOMready()
{
	
	// Gets the content from our XML file
	LoadFile("Quotes.xml");
	
	// This event triggers every 6 seconds, and loads a new quote into the quotebox
	intervalTimer = window.setInterval('InsertNewQuote()',time);
	
}

// Function is called when user clicks the next quote button, and loads in the next quote
function NextQuote()
{
	if(response != null)
	{
		// Sets up our content types
		content = response.getElementsByTagName('Content');
		myName = response.getElementsByTagName('QuoteName');
		imgSrc = response.getElementsByTagName('IMG');
		
		// Counter keeps track of which quote is displayed and "flips the pile" when it reaches the end
		if(counter == content.length-1)
		{
			counter = 0;
		}
		else
		{
			counter++;
		}
		
		// Selects the relevant quote tags in the DOM
		var quoteTag = document.getElementById("Quote");
		var quoteName = document.getElementById("Name");
		var quoteImg = document.getElementById("QuoteImage");
		
		// inserts the values from the Ajax call in the tags
		quoteTag.innerHTML = content[counter].childNodes[0].nodeValue;
		quoteName.innerHTML = myName[counter].childNodes[0].nodeValue;
		quoteImg.src = imgSrc[counter].childNodes[0].nodeValue;
		
		// resets the timer so the quotes will change at an interval again if the users doesnt click the buttons anymore
		clearInterval(intervalTimer);
		intervalTimer = window.setInterval('InsertNewQuote()',time);
		
	}
}

// Function is called when user clicks the previous quote button, and loads in the previous quote
function PreviousQuote()
{
	if(response != null)
	{
		// Sets up our content types
		content = response.getElementsByTagName('Content');
		myName = response.getElementsByTagName('QuoteName');
		imgSrc = response.getElementsByTagName('IMG');
		
		// Counter keeps track of which quote is displayed and "flips the pile" when it reaches the end
		if(counter == 0)
		{
			counter = content.length-1;
		}
		else
		{
			counter--;
		}
		
		// Selects the relevant quote tags in the DOM
		var quoteTag = document.getElementById("Quote");
		var quoteName = document.getElementById("Name");
		var quoteImg = document.getElementById("QuoteImage");
		
		// inserts the values from the Ajax call in the tags
		quoteTag.innerHTML = content[counter].childNodes[0].nodeValue;
		quoteName.innerHTML = myName[counter].childNodes[0].nodeValue;
		quoteImg.src = imgSrc[counter].childNodes[0].nodeValue;
		
		// resets the timer so the quotes will change at an interval again if the users doesnt click the buttons anymore
		clearInterval(intervalTimer);
		intervalTimer = window.setInterval('InsertNewQuote()',time);
		
	}
}

// Function is called at a timed interval, and loads in the next quote
function InsertNewQuote()
{
	// Sets up our content types
	content = response.getElementsByTagName('Content');
	myName = response.getElementsByTagName('QuoteName');
	imgSrc = response.getElementsByTagName('IMG');
	
	// Counter keeps track of which quote is displayed and "flips the pile" when it reaches the end
	if(counter == content.length-1)
	{
		counter = 0;
	}
	else
	{
		counter++;
	}
	
	
	// Selects the relevant quote tags in the DOM
	var quoteTag = document.getElementById("Quote");
	var quoteName = document.getElementById("Name");
	var quoteImg = document.getElementById("QuoteImage");
	
	// inserts the values from the Ajax call in the tags
	quoteTag.innerHTML = content[counter].childNodes[0].nodeValue;
	quoteName.innerHTML = myName[counter].childNodes[0].nodeValue;
	quoteImg.src = imgSrc[counter].childNodes[0].nodeValue;
	
}


// Function loads the specified file and returns it as XML
function LoadFile(fileName)
{
	
	if (window.XMLHttpRequest)
	{
		xhttp = new XMLHttpRequest();
	}
	else
	{
		xhttp = new ActiveXObject("Microsoft.XMLHTTP");
	}
	
	// Create a raondom number to append it to the path, so no caching is acting
	var Myrandom = Math.random();

	var path = fileName + "?randomNumber=" + Myrandom;
	
	
	xhttp.onreadystatechange=function()
  	{
		if (xhttp.readyState==4 && xhttp.status==200)
		{
			//alert("Der er respons");
			response = xhttp.responseXML;
		}
  	}

	xhttp.open("GET", path, true);
	xhttp.send();
}
