/** 
 * B> Javascript.
 *
 * Javascript for the site.
 * 
 * @author thebrentc@gmail.com
 * @version 2 
 */
 
//window.onerror = function() { return true; }

function loadNav()
{
	if (document.location.href.indexOf("index.html") > -1 || document.location.href == "http://www.thebrentc.net/" || document.location.href == "http://www.brentc.net/") return;
	http = new XMLHttpRequest();
	http.onload = function() 
	{
		if (http.readyState == 4) 
		{	
			if (http.status != 200) return;
			var response = (http.ResponseText)?http.ResponseText:http.responseText;		
			response = response.substr(response.indexOf('<nav>')+5); response = response.substr(0,response.indexOf('</nav>'));
			var e = document.getElementsByTagName('nav')[0];
			e.innerHTML = response;		
		}
	};
	http.open( "POST", "index.html", true );
	http.send( null );
}

function loadMenu()
{	
	http2 = new XMLHttpRequest();
	http2.onload = function() 
	{
		if (http2.readyState == 4) 
		{	
			if (http2.status != 200) return;
			var response = (http2.ResponseText)?http2.ResponseText:http2.responseText;					
			response = response.substr(response.indexOf('<ul')); response = response.substr(0,response.lastIndexOf('</ul>')+5);
			var e = document.getElementById('nav');
			e.innerHTML += response;
restoreSettings();
		}
	};
	http2.open( "POST", "menu.html", true );
	http2.send( null );
}

function iWin_lite(location)
{
/* div with close link and iframe */

	// div
	var newwindiv = document.createElement("div");
	newwindiv.id = "idiv";		
	document.body.appendChild(newwindiv);		
	document.getElementById("idiv").setAttribute("className","idiv");			
	document.getElementById("idiv").setAttribute("class","idiv");					
	document.getElementById("idiv").innerHTML = '<a title="Close" href="javascript:var result = document.body.removeChild(document.getElementById(\'idiv\'));" style="border: solid 1px silver; float: right; font-weight: bold; margin:0.1em;">X</a>';		
	
	// iframe
	var newwin = document.createElement("iframe");
	newwin.setAttribute("src",location);				
	document.getElementById("idiv").appendChild(newwin);		
}

function changeStyle(which)
{
    if (which != document.getElementById('stylesheet').href)
	document.getElementById('stylesheet').href = which;
    if (checkSettingsEnabled()) window.sessionStorage.stylesheetSetting = document.getElementById('stylesheet').href;	    
}

function changeBackground(which)
{
    document.body.style.backgroundImage = which; 
    if (checkSettingsEnabled()) window.sessionStorage.backgroundSetting = document.body.style.backgroundImage;       
}

function saveSettings()
{
	if ('sessionStorage' in window && window['sessionStorage'] !== null)
	{		
	   if (!checkSettingsEnabled()) return;
	    window.sessionStorage.stylesheetSetting = document.getElementById('stylesheet').href;	    
	    window.sessionStorage.backgroundSetting = document.body.style.backgroundImage;    
	}
}

function restoreSettings()
{
	if ('sessionStorage' in window && window['sessionStorage'] !== null)
	{	
		if (!checkSettingsEnabled()) return;		
		var stylesheet = sessionStorage.getItem('stylesheetSetting');
		if (stylesheet)	if (stylesheet != document.getElementsByTagName('head')[0].getElementsByTagName('link')[0].href)
			document.getElementsByTagName('head')[0].getElementsByTagName('link')[0].href = stylesheet;        
		var background = sessionStorage.getItem('backgroundSetting');              
		if (background)	if (background != document.body.style.backgroundImage)
			document.body.style.backgroundImage = background;

		document.getElementById('enableSettingsLink').className = "disabled";
		document.getElementById('removeSettingsLink').className = "active";
	}
}

function enableSettings()
{
	if ('sessionStorage' in window && window['sessionStorage'] !== null)
	{	
		window.sessionStorage.settingsEnabled = true;
		document.getElementById('enableSettingsLink').className = "disabled";
		document.getElementById('removeSettingsLink').className = "";
	}
}

function checkSettingsEnabled()
{
	if ('sessionStorage' in window && window['sessionStorage'] !== null)
	{	
		var settingsEnabled = window.sessionStorage.settingsEnabled;
		return settingsEnabled == "true";	
	}
	else 
	{
		return false;
	}
}

function removeSettings()
{
	if ('sessionStorage' in window && window['sessionStorage'] !== null)
	{	
		window.sessionStorage.removeItem('stylesheetSetting');
		window.sessionStorage.removeItem('backgroundSetting');
		window.sessionStorage.removeItem('settingsEnabled');
		window.location.reload();
	}
}
window.onload = function()
{
//	loadNav();
//	loadMenu();	// calls restoreSettings()
// restoreSettings(); // disabled
}


