//Global Variables - Initial setup
var frameNum = 0;
var leftCount = 0;
var rightCount = 0;
var newLeftPosition = 0;
var timer = 0;
var slidingTimer = 0;

// Set interval between auto changing of slides. Default = 7000 (7 Seconds)
var interval = 7000; // 1000 = 1 Second

//Function to extend drop down menu, set highlight colours
function extendMenu(element)
{
	hideMenus();
	var menu = element.childNodes;
	var menuLength = menu.length;
	for (var a = 0; a < menuLength; a++)
	{
		if ((menu[a].className == 'MenuExtendDiv') && (menu[a].nodeName == 'DIV'))
		{
			menu[a].style.display = 'block'; //Show Menu
		}
		if (menu[a].nodeName == 'A')
		{
			menu[a].style.backgroundColor = '#83b494'; //Background colour of drop down menu links
			menu[a].style.color = 'white'; //Text color of drop down menu links
			menu[a].style.paddingBottom = '5px';
		}
	}
	
}
//Function to hide all drop down menus, reset colours
function hideMenus()
{
	var menu = document.getElementsByTagName('div');
	var menuLength = menu.length;
	for (var a = 0; a < menuLength; a++)
	{
		if (menu[a].className == 'MenuExtendDiv')
		{
			menu[a].style.display = 'none'; //Hide Menu
		}
	}
	
	var menu2 = document.getElementsByName('mainmenu');
	var menu2Length = menu2.length;
	for (var b = 0; b < menu2Length; b++)
	{
		menu2[b].style.backgroundColor = 'transparent'; //Clear Background colour of main menu links
		menu2[b].style.color = '#8db096'; //Reset Top link colour
		menu2[b].style.paddingBottom = '7px';
	}
	document.getElementById('MainMenuActive').style.backgroundColor = '#fafafa'; //Reset Active Page Link Background colour
	document.getElementById('MainMenuActive').style.color = '#365e40'; //Reset active page text colour
	document.getElementById('MainMenuActive').style.paddingBottom = '7px';
}

//Function to load Javascript Slider
function loadSlider()
{
	callAjax('slidingcontent.html');
}

//AutoSlide frames to the left
function autoSlide()
{
	slideLeft();
}

//Function to cause a slide to the left
function slideLeft()
{
	if (frameNum > 0)
	{
		clearInterval(slidingTimer);
		clearInterval(timer);
		document.getElementById('SliderLeftButton').disabled = true;
		document.getElementById('SliderRightButton').disabled = true;
		if (leftCount < frameNum)
		{
			newLeftPosition = parseInt(document.getElementById('SliderContentDiv').style.left) - 910;
			slidingTimer = setInterval('leftSlide()', 10);
			leftCount++;
			rightCount--;
		}
		else
		{
			newLeftPosition = 0;
			slidingTimer = setInterval('rightSlide()', 10);
			leftCount = 0;
			rightCount = frameNum;
		}
		timer = setInterval('autoSlide()', interval);
	}
}

//Function to perform actual left sliding movement
function leftSlide()
{
	var slid = parseInt(document.getElementById('SliderContentDiv').style.left);
	if (slid > newLeftPosition)
	{
		document.getElementById('SliderContentDiv').style.left = (slid - 20) + 'px';
	}
	else
	{
		clearInterval(slidingTimer);
		document.getElementById('SliderContentDiv').style.left = newLeftPosition + 'px';
		document.getElementById('SliderLeftButton').disabled = false;
		document.getElementById('SliderRightButton').disabled = false;
	}
}

//Function to cause a slide to the right
function slideRight()
{
	if (frameNum > 0)
	{
		clearInterval(slidingTimer);
		clearInterval(timer);
		document.getElementById('SliderLeftButton').disabled = true;
		document.getElementById('SliderRightButton').disabled = true;
		if (rightCount < frameNum)
		{
			newLeftPosition = parseInt(document.getElementById('SliderContentDiv').style.left) + 910;
			slidingTimer = setInterval('rightSlide()', 10);
			rightCount++;
			leftCount--;
		}
		else
		{
			newLeftPosition = (frameNum * -910);
			slidingTimer = setInterval('leftSlide()', 10);
			leftCount = frameNum;
			rightCount = 0;
		}
		timer = setInterval('autoSlide()', interval);
	}
}


//Function to perform actual right sliding movement
function rightSlide()
{
	var slid = parseInt(document.getElementById('SliderContentDiv').style.left);
	if (slid < newLeftPosition)
	{
		document.getElementById('SliderContentDiv').style.left = (slid + 20) + 'px';
	}
	else
	{
		clearInterval(slidingTimer);
		document.getElementById('SliderContentDiv').style.left = newLeftPosition + 'px';
		document.getElementById('SliderLeftButton').disabled = false;
		document.getElementById('SliderRightButton').disabled = false;
	}
}

//Helper function to simplify image rollover replacement
//Assumes rolloverimage is in same folder as original image and is named
//[original image name]_over.[origina; image extension]
// eg. original image: hello.jpg should have rollover image: hello_over.jpg
function rollover(element)
{
	var elsrc = element.src.slice(0, element.src.length - 4);
	var ext = element.src.slice(-4);
	element.src = elsrc + '_over' + ext;
}

//Helper function to replace rollover image with original image
function rollout(element)
{
	var elsrc = element.src.slice(0, element.src.length - 9);
	var ext = element.src.slice(-4);
	element.src = elsrc + ext;
}

//Ajax function, also sets up slider limits and gets number of frames in slider
//Each frame must be a div of class: SlidingContentFrame
function callAjax(url)
{
	if (window.ActiveXObject)
	{
		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
	else
	{
		xmlhttp = new XMLHttpRequest();
	}
	xmlhttp.onreadystatechange = function()
	{
		if (xmlhttp.readyState==4)
		{
			frameNum = xmlhttp.responseText.match(/class="SlidingContentFrame"/gi).length - 1;
			if (frameNum > 0)
			{
				timer = setInterval('autoSlide()', interval);
			}
			rightCount = frameNum;
			document.getElementById('SlidingContentDiv').innerHTML = xmlhttp.responseText;
			document.getElementById('SliderContentDiv').style.width = ((frameNum +1) * 910) + 'px';
		}
	}
	xmlhttp.open("GET", url, true);
	xmlhttp.send();
}


