/*

Author: Mark McDonnell [ mark [at] storm-media [dot] co [dot] uk ]

Application Date: 2006.01.16 / Monday.

DOM Version: W3C Document Object Module.

Browser Issues: N/A

*/

// Event Function

function fnAddEvent(obj, evType, fn, useCapture)
{
	
	if (obj.addEventListener)
	// Gecko browsers (Mozilla / Firefox)
	{
		obj.addEventListener(evType, fn, useCapture);
		return true;
	}
	else if (obj.attachEvent)
	// Internet Explorer 5+
	{
		var r = obj.attachEvent('on' + evType, fn);
		return r;
	}
	else
	{
		// Return the function as the event handler could not be attached.
		return false
	}

}

// Triggers

	fnAddEvent(window, 'load', fnSetNavigation);
	fnAddEvent(window, 'load', fnSetNavigationIF);
	fnAddEvent(window, 'load', fnHighlightSideNavigation);
	fnAddEvent(window, 'load', fnLoopHomeImage);
	fnAddEvent(window, 'load', fnSetIFSideNavigation);
	fnAddEvent(window, 'load', fnIFHideScrewpiling);
	fnAddEvent(window, 'load', fnCheckIFServices);

// Triggered

	function fnSetNavigation()
	{
		/*
			This function loops through all the hyperlinks inside the "un-ordered list" navigation
			and adds rollover events to them so they display a different image when the *rollover* mouse event happens.
		*/
		
		if(!document.getElementById('id-Navigation'))
		{
			// If the required element does not exist then simply return the function.
			return;
		}
		
		var storeNavigation = document.getElementById('id-Navigation');		
		var storeLinks = storeNavigation.getElementsByTagName('a');
		
		for(var i=0; i<storeLinks.length-1; i++)
		{
			/*
				The statement doesn't loop through all the hyperlinks (i.e. see the loop runs "length-1").
				This is because the last link "instant foundations" doesn't have a rollover graphic.
			*/
			
			eval("storeLinks[i].onmouseover = function(){fnNavigationRollovers(" + i + ", 'over');}")
			eval("storeLinks[i].onmouseout = function(){fnNavigationRollovers(" + i + ", 'out');}")
			
			/*
				The anonymous function was not evaluating the variable 'i' correctly.
				Once the anonymous function was called, the 'i' variable equalled the final loop number?
				
				To solve this issue I used the 'eval()' method to run the mouse event
				and escaped the string when it reached the 'i' variable inside the anonymous function
				so its value was kept the same as the loops current iteration.
			*/
		}
	}
	
	function fnSetNavigationIF()
	{
		/*
			This function loops through all the hyperlinks inside the "un-ordered list" navigation for Instant Foundation pages
			and adds rollover events to them so they display a different image when the *rollover* mouse event happens.
		*/
		
		if(!document.getElementById('id-Navigation-InstantFoundations'))
		{
			// If the required element does not exist then simply return the function.
			return;
		}
		
		var storeNavigation = document.getElementById('id-Navigation-InstantFoundations');		
		var storeLinks = storeNavigation.getElementsByTagName('a');
		
		for(var i=0; i<storeLinks.length-1; i++)
		{
			/*
				The statement doesn't loop through all the hyperlinks (i.e. see the loop runs "length-1").
				This is because the last link "instant foundations" doesn't have a rollover graphic.
			*/
			
			eval("storeLinks[i].onmouseover = function(){fnNavigationRollovers(" + i + ", 'over', '-InstantFoundations');}")
			eval("storeLinks[i].onmouseout = function(){fnNavigationRollovers(" + i + ", 'out', '-InstantFoundations');}")
			
			/*
				The anonymous function was not evaluating the variable 'i' correctly.
				Once the anonymous function was called, the 'i' variable equalled the final loop number?
				
				To solve this issue I used the 'eval()' method to run the mouse event
				and escaped the string when it reached the 'i' variable inside the anonymous function
				so its value was kept the same as the loops current iteration.
			*/
		}
	}
	
	function fnNavigationRollovers(i, state, sElement)
	{
		/*
			This function takes arguments passed from either the function "fnSetNavigation" or "fnSetNavigationIF".
			It then changes the source of the relevant navigation image.
		*/
		
		if(sElement != '-InstantFoundations')
		{
			sElement = 'id-Navigation';
		}
		else
		{
			sElement = 'id-Navigation-InstantFoundations';
		}
		
		if(state == '' || state == null)
		{
			// The 'state' argument is important to work out which mouse event has triggered.
			// If it's not equal to anything relevant then simply return the function.
			return;
		}
		
		if(sElement == 'id-Navigation')
		{
			var storeNavigation = document.getElementById(sElement);		
			var storeLinks = storeNavigation.getElementsByTagName('a');
			
			var arrNavImages = new Array();
			arrNavImages[0] = '/Assets/Images/Navigation-Profile';
			arrNavImages[1] = '/Assets/Images/Navigation-Services';
			arrNavImages[2] = '/Assets/Images/Navigation-CaseStudies';
			arrNavImages[3] = '/Assets/Images/Navigation-ContactUs';
			arrNavImages[4] = '/Assets/Images/Navigation-Downloads';
			arrNavImages[5] = '/Assets/Images/Navigation-InstantFoundation';
			
			// Get the image within the link and then use the 'switch' statement (see below) to reset the image source.
			var storeImage = storeLinks[i].getElementsByTagName('img')
			
			switch(state)
			{
				case 'over':
					storeImage[0].setAttribute('src', arrNavImages[i] + '_O.gif');
					break;
				case 'out':
					storeImage[0].setAttribute('src', arrNavImages[i] + '.gif');
					break;
			}			
		}
		else if(sElement == 'id-Navigation-InstantFoundations')
		{
			var storeNavigation = document.getElementById(sElement);		
			var storeLinks = storeNavigation.getElementsByTagName('a');
			
			var arrNavImages = new Array();
			arrNavImages[0] = '/Assets/Images/Navigation-InstantFoundations_Contact';
			arrNavImages[1] = '/Assets/Images/Navigation-InstantFoundations_Return';
			
			// Get the image within the link and then use the 'switch' statement (see below) to reset the image source.
			var storeImage = storeLinks[i].getElementsByTagName('img')
			
			switch(state)
			{
				case 'over':
					storeImage[0].setAttribute('src', arrNavImages[i] + '_O.gif');
					break;
				case 'out':
					storeImage[0].setAttribute('src', arrNavImages[i] + '.gif');
					break;
			}
		}
	}
	
	function fnHighlightSideNavigation()
	{
		/*
			This function takes the URL from the browser location bar and strips out the last section.
			It then loops through the side navigation links and tries to match the link locations against the current location.
			If the iteration of the loop came across a link that matched the current location then the script highlights the link.
		*/
		
		var storeURL = document.location.href;
		var storeLastChar = storeURL.lastIndexOf('/');
		var storePage = storeURL.substring(storeLastChar);
		
		// If the user has not typed in the full address (i.e. http://www.domain.com/)
		// Replace the variable value with '/Default.asp'.
		if(storePage == '/')
		{
			storePage = '/Default.asp'
		}
		
		if(!document.getElementById('id-SideNavigation'))
		{
			// If the required element does not exist then return the function.
			return;	
		}
		
		// If the required element does exist then store it in a variable.
		var storeNavigation = document.getElementById('id-SideNavigation');		
		var storeLinks = storeNavigation.getElementsByTagName('a');
		
		// Loop through all the links in the main navigation bar
		for(var i=0; i<storeLinks.length; i++)
		{
			// Pull out the correct string information from each href found.
			var storeCurrentLink = storeLinks[i].getAttribute('href');
			var storeCurrentLastChar = storeCurrentLink.lastIndexOf('/');
			storeCurrentLink = storeCurrentLink.substring(storeCurrentLastChar);
			
			// If the current href does not match the page location 'continue' the statement.
			if(storeCurrentLink != storePage)
			{
				continue;
			}
			else
			{
				// Compliant browsers do not recognise the 'style.' before 'setAttribute'
				// If 'style.attribute' returns 'undefined' or similar then it is a compliant browser in use.
				if(storeLinks[i].style.setAttribute == 'undefined' || storeLinks[i].style.setAttribute == null || storeLinks[i].style.setAttribute == '')
				{
					// For Mozilla & Gecko rendering engines
					storeLinks[i].setAttribute('style','background-image: url("/Assets/Images/Icon-SideNav_ArrowRollover.gif"); background-color: #D4D5E7; color: #0C2D83;');
				}	
				else
				{
					// For Internet Explorer
					storeLinks[i].style.setAttribute('cssText','background-image: url("/Assets/Images/Icon-SideNav_ArrowRollover.gif"); background-color: #D4D5E7; color: #0C2D83;');	
				}				
			}
		}
	}
	
	function fnLoopHomeImage()
	{
		/*
			This function sets a 5 second interval on the following function 'fnStartImageLoop'.
		*/
		
		if(!document.getElementById('id-HomeAnimation'))
		{
			// If the required element does not exist, then return the function.
			return;
		}
		
		setInterval("fnStartImageLoop()","5000"); // Every 5 seconds call function
	}
	
	function fnStartImageLoop()
	{
		/*
			This function is called by "fnSetRandomHomeImage" and is called via setInterval every 5 seconds.
		*/
		
		if(!document.getElementById('id-HomeAnimation'))
		{
			// If the required element does not exist, then return the function.
			return;
		}
		
		//alert(iHomeImageCounter);
		
		document.getElementById('id-HomeAnimation').src = '/Assets/Images/Home-Animation' + iHomeImageCounter + '.jpg';
		
		// If the global variable is greater than the number of available images then reset the value back to 1.
		(iHomeImageCounter >= 6) ? iHomeImageCounter = 1 : iHomeImageCounter += 1;
	}
	
	function fnSetIFSideNavigation()
	{
		/*
			This function first hides all the sub navigations in the Instant Foundations pages.
			Then adds an onClick event to the "services" link
		*/
		
		if(!document.getElementById('id-IF-SubNavigation')) // If the required element does not exist, then return the function.			
		{
			return;
		}
		else // else hide the navigation using JavaScripts CSS-DOM, this way the menu is still accessible without JavaScript.
		{
			var storeSubNav = document.getElementById('id-IF-SubNavigation');
			storeSubNav.style.display = 'none';
		}
		
		if(!document.getElementById('id-IF-SubNav')) // If the required element does not exist, then return the function.
		{
			return;
		}
		else // else 
		{
			var storeSubNavLink	= document.getElementById('id-IF-SubNav');
			storeSubNavLink.onclick = function() { fnToggleIFSubNav(toggleIFSubNav); }
		}
	}
	
	function fnToggleIFSubNav(state)
	{
		if(state == 'on')
		{
			toggleIFSubNav = 'off'
			document.getElementById('id-IF-SubNavigation').style.display = 'block';
		}
		else if(state == 'off')
		{
			toggleIFSubNav = 'on'
			document.getElementById('id-IF-SubNavigation').style.display = 'none';
		}
	}
	
	function fnIFHideScrewpiling(obj)
	{
		if(!document.getElementById('id-ScrewpilingContent'))
		{
			// If the required element does not exist, then return the function.
			return;	
		}
		else
		{
			storeE = document.getElementById('id-ScrewpilingContent');	
		}
		
		storeEDiv = storeE.getElementsByTagName('div');
		
		for(var i=0; i<storeEDiv.length; i++)
		{
			storeEDiv[i].style.display = 'none';
		}
		
		if(obj == null || obj == '' || obj == '[object]' || obj == '[object Event]')
		{
			// do nothing
		}
		else
		{
			document.getElementById('id-ScrewpilingContent' + obj).style.display = 'block';
		}
	}
	
	function fnCheckIFServices()
	{
		/*
			This function takes the URL from the browser location bar and strips out the last section.
			It then loops through the Instant Foundation 'services' side navigation links and tries to match the link locations against the current location.
			If the iteration of the loop came across a link that matched the current location then the script keeps the dropdown menu displayed rather than setting the display to 'none'.
		*/
		
		var storeURL = document.location.href;
		var storeLastChar = storeURL.lastIndexOf('/');
		var storePage = storeURL.substring(storeLastChar);
		
		// If the user has not typed in the full address (i.e. http://www.domain.com/)
		// Replace the variable value with '/Default.asp'.
		if(storePage == '/')
		{
			storePage = '/Default.asp'
		}
		
		if(!document.getElementById('id-IF-SubNavigation'))
		{
			// If the required element does not exist then return the function.
			return;	
		}
		
		// If the required element does exist then store it in a variable.
		var storeNavigation = document.getElementById('id-IF-SubNavigation');		
		var storeLinks = storeNavigation.getElementsByTagName('a');
		
		// Loop through all the links in the main navigation bar
		for(var i=0; i<storeLinks.length; i++)
		{
			// Pull out the correct string information from each href found.
			var storeCurrentLink = storeLinks[i].getAttribute('href');
			var storeCurrentLastChar = storeCurrentLink.lastIndexOf('/');
			storeCurrentLink = storeCurrentLink.substring(storeCurrentLastChar);
			
			// If the current href does not match the page location 'continue' the statement.
			if(storeCurrentLink != storePage)
			{
				continue;
			}
			else
			{
				storeNavigation.style.display = 'block';
				
				// Compliant browsers do not recognise the 'style.' before 'setAttribute'
				// If 'style.attribute' returns 'undefined' or similar then it is a compliant browser in use.
				if(storeLinks[i].style.setAttribute == 'undefined' || storeLinks[i].style.setAttribute == null || storeLinks[i].style.setAttribute == '')
				{
					// For Mozilla & Gecko rendering engines
					storeLinks[i].setAttribute('style','color: #0C2D83; background-image: url("/Assets/Images/Icon-SideNav_ArrowRollover.gif"); background-color: #D4D5E7;');
				}	
				else
				{
					// For Internet Explorer
					storeLinks[i].style.setAttribute('cssText','color: #0C2D83; background-image: url("/Assets/Images/Icon-SideNav_ArrowRollover.gif"); background-color: #D4D5E7;');	
				}	
			}
		}
	}

// Misc Functions

	function insertAfter(newElement, targetElement)
	{
		var parent = targetElement.parentNode;
		if(parent.lastChild == targetElement)
		{
			parent.appendChild(newElement);
		}
		else
		{
			parent.insertBefore(newElement, targetElement.nextSibling);
		}
	}
	
	function fnSetIFSideNavigationRollovers(i)
	{
		var storeID = i.getAttribute('id');
		document.getElementById(storeID).onmouseout = function(){ alert(storeID); }
	}
	
// Global Variables

	var iHomeImageCounter = 2;
	var toggleIFSubNav = 'on';
	