//-----------------------
//-----------------------
//---STYLE SWITCHER--------
//-----------------------
//-----------------------




function getCookie(Name) { 
var re=new RegExp(Name+"=[^;]+", "i"); //creates a "regular expression" that is set up to search for the name passed into the function "mysheet" this regular expression will be looking for "mysheet="+ any number of characters as long as it does not contain ";" and because of the i it will not be case sensitive.
if (document.cookie.match(re)) //if a match to the RE is found in the document cookies then...
return document.cookie.match(re)[0].split("=")[1] // splits the cookie ex."mysheet=red" into mysheet and red then returns its value?
return null
}

function setCookie(name, value, days) {
var expireDate = new Date()
//set "expstring" to either future or past date, to set or delete cookie, respectively
var expstring=(typeof days!="undefined")? expireDate.setDate(expireDate.getDate()+parseInt(days)) : expireDate.setDate(expireDate.getDate()-5)//what is happening here?
document.cookie = name+"="+value+"; expires="+expireDate.toGMTString()+"; path=/"; //sets the new cookie with the name value and expiry date given
}

function setStylesheet(title){ //Main stylesheet switcher function. 
	var i, cacheobj, altsheets=[""] //sets each of the varibles to ""  ***
	for(i=0; (cacheobj=document.getElementsByTagName("link")[i]); i++) {
		if(cacheobj.getAttribute("rel").toLowerCase()=="alternate stylesheet" && cacheobj.getAttribute("title")) { //if this is an alternate stylesheet with title
			cacheobj.disabled = true
			altsheets.push(cacheobj) //store reference to alt stylesheets inside array
			if(cacheobj.getAttribute("title") == title){ //enable alternate stylesheet with title that matches parameter
				cacheobj.disabled = false//enable chosen style sheet
			}
		}
	}
}

function chooseStyle(styletitle, days){ //Interface function to switch style sheets plus save "title" attr of selected stylesheet to cookie
	if (document.getElementById){
		setStylesheet(styletitle)
		setCookie("mysheet", styletitle, days)
	}
}

var selectedtitle=getCookie("mysheet");

if (document.getElementById && selectedtitle!=null){ //load user chosen style sheet from cookie if there is one stored
	setStylesheet(selectedtitle)	
}


//-----------------------
//-----------------------
//-END OF STYLE SWITCHER--
//-----------------------
//-----------------------
function lastLink(){

var urlArray =location.href.split("/")
var lastVal=urlArray.length-1
var currentPageName=urlArray[lastVal].split(".")[0]

checkElem=document.getElementById("highlightColor");

if(window.getComputedStyle){
var newColor=window.getComputedStyle(checkElem,null).color;
document.getElementById(currentPageName).style.color=newColor;
}
else if(checkElem.currentStyle){

var newColor=checkElem.currentStyle.color;
document.getElementById(currentPageName).style.color=newColor;
}
}
function styleSwitch(styletitle, days) {
	chooseStyle(styletitle, days);
	lastLink();
}
