function addLoadEvent (func) 
{
	var oldonload = window.onload;
	if (typeof window.onload != 'function') 
	{
		window.onload = func;
	}
	else 
	{
		window.onload = function() 
		{
			oldonload();
			func();
		}
	}
}

function getHTTPObject ()
{
	var xhr = false;
	if (window.XMLHttpRequest) 
	{
		xhr = new XMLHttpRequest();
	}
	else if (window.ActiveXObject)
	{
		try 
		{
			xhr = new ActiveXObject('Msxml2.XMLHTTP');
		} catch (e) {
			try 
			{
				xhr = new ActiveXObject('Microsoft.XMLHTTP');
			} catch (e) {
				xhr = false;
			}
		}
	}
	return xhr;
}

function grabFile (file, func)
{
	var request = getHTTPObject();	//Assign an xhr object to this local variable
	if (request) //If the xhr object was successfully created
	{
		request.onreadystatechange = function() //At each stage of the ajax process
		{
			func(request);	//Do this function
		}
		request.open('GET', file, true);	//Open the xhr request to the file supplied to the grabFile function
		request.send(null);	//Send the xhr request without any data (it's a get)
	}
}

function addStyleNav (request)
{
	//Function takes a request object
	if (request.readyState == 4) //If the request has been completed
	{
		if (request.status == 200 || request.status == 304) //And it has gone as we want it to
		{
			document.getElementById('headernav_container').innerHTML = request.responseText;
		}
	}
}

function addNav (request)
{
	//Function takes a request object
	if (request.readyState == 4) //If the request has been completed
	{
		if (request.status == 200 || request.status == 304) //And it has gone as we want it to
		{
			document.getElementById('nav_container').innerHTML = request.responseText;
		}
	}
}

function addArtistSubNav (request)
{
	//Function takes a request object
	if (request.readyState == 4) //If the request has been completed
	{
		if (request.status == 200 || request.status == 304) //And it has gone as we want it to
		{
			document.getElementById('artistSubNavContainer').innerHTML = request.responseText;
		}
	}
}

function addNavigationMarkup ()
{
	grabFile('includes/layout/nav_container.php', addNav);
	grabFile('includes/layout/headernav_container.php', addStyleNav);
	// Do artist subnav
	if (document.getElementById('artistSubNavContainer')) 
	{
		var cont = document.getElementById('artistSubNavContainer');
		var artist = cont.getAttribute('rel');
		grabFile('includes/layout/artist_subnav.php?artistXHR='+artist, addArtistSubNav);
	}
}

addLoadEvent(addNavigationMarkup);
