/* scott main dot com
 *
 * JavaScript for dynamically inserting the list of selectable style sheets, swapping them, and 
 * modifying the hyperlink's style to indicate the current style being used
 */

var style_cookie = "scottmain_style" + "=";
var design = getCookie(style_cookie);                    // get the stylesheet cookie
if (design) makeStyle(design);   // if there is a cookie, switch to the cookie stylesheet

/* 
 * these actions are performed each time a new page is loaded,
 * in order to insert the stylesheet links, use and highlight the current stylesheet.
 */
function LoadSwap() {
	loadStylesList();                            // add the stylesheet links to the page
	highlightStyle();                            // highlight the stylesheet currently used
}

addLoadEvent(LoadSwap);

/* 
 * this function is executed when a stylesheet link is clicked.
 * 
 * params
 *  stylelink - "this" object from the anchor from which the event was fired.
 */
function swapStyle(stylelink) {
	var design = stylelink.firstChild.nodeValue;   //the text value of the anchor
	
	makeStyle(design);                 // the style swapping action
	refreshStylesList();                           // sets each link's class to empty
	stylelink.parentNode.className = "thisstyle";  // sets "this" link to the "thisstyle" class
	return false;                                  // return false so to nullify the href
}

/* 
 * the real style swapping action.
 * 
 * params
 *  new_sheet - the name of the style sheet to swap in
 */
function makeStyle(new_sheet) {
	var new_sheet_link = getThisSheet(new_sheet);
	var current_sheet_link = getCurrentSheet();
	
	current_sheet_link.setAttribute("href", "/portfolio/css/" + new_sheet + ".css");  // set the current stylesheet to be an alternative
	
	document.cookie = style_cookie + new_sheet;        // and set our cookie
	
}

/* 
 * dynamically creates and adds the list of style sheets to the page
 */
function loadStylesList() {
	var stylesdiv = document.getElementById("styles");      // find the "styles" div
	var stylenames = getSheetNames();                       // get the stylesheet names
	
	var ul = document.createElement("ul");                  // create the list element,
	var li = document.createElement("li");                  // the first list item,
	var text = document.createTextNode("style sheets");     // and the text for the list item
	var a;
	
	stylesdiv.appendChild(ul);                              // append the list element to the div,
	ul.appendChild(li);                                     // the list item to the list,
	li.appendChild(text);                                   // and the text to the list item.
	
	for (var i = 0; i < stylenames.length; i++) {           // then loop through the array of names
	    li = document.createElement("li");                  // and, for each, create a list item
		a = document.createElement("a");                    // and an anchor,
		a.setAttribute("href", "#");                        // set the anchor's href,
		a.onclick = function() { return swapStyle(this); }  // set the anchor's onclick event,   
	    text = document.createTextNode(stylenames[i]);      // and insert the hyperlink text.
	
		ul.appendChild(li);                                 // then append these elements to the list.
		li.appendChild(a);
		a.appendChild(text);
	}
}

/*
 * retrives the title of all stylesheets in the HTML header
 */
function getSheetNames() {
	var links = document.getElementsByTagName("link");                  // get all link objects
	var sheets = new Array();                                           // create a new array for the sheet titles
	var name;
	var s = 0;

	for (var i = 0; i < links.length; i++) {                            // loop through all the link objects found
		if(links[i].getAttribute("rel").indexOf("stylesheet") != -1) {  // and find ones that say 'stylesheet' anywhere in the 'rel'
			sheets[s] = links[i].getAttribute("title");                 // add them to the new array
			s++;                                                        // increment the sheet titles array
		} 
	}
	return sheets;                                                      // return the array of stylesheet titles
}

/* 
 * retrives the primary stylesheet link object from the HTML header
 */
function getCurrentSheet() {
	var links = document.getElementsByTagName("link");      // get all link objects

	for (var i = 0; i < links.length; i++) {                // loop through all the link objects found
		if(links[i].getAttribute("rel") == "stylesheet") {  // and find one that's a stylesheet (not an alternative)
			var sheet = links[i];                           // and set the object to a variable
			break;
		}
	}
	return sheet;                                           // and return that object
}

/* 
 * retrives the given stylesheet link object from the HTML header
 */
function getThisSheet(sheet_name) {
	var links = document.getElementsByTagName("link");      // get all link objects

	for (var i = 0; i < links.length; i++) {                // loop through all the link objects found
		if(links[i].getAttribute("title") == sheet_name) {  // and find one that is titled with the given name
			var sheet = links[i];                           // and set the object to a variable
			break;
		}
	}
	return sheet;                                           // and return that object
}

/* 
 * retrieves my stylesheet cookie
 */
function getCookie(cookiename) {
	if (document.cookie) {                                             // if there is a cookie object
		for (var i = 0; i < document.cookie.length; i++) {             // loop through the cookie object
			var offset = i + cookiename.length;                        // the index of where my cookie's VALUE starts
			if (document.cookie.substring(i, offset) == cookiename) {  // if the string between the string between the current index and the offset is my cookie name
				var endindex = document.cookie.indexOf(";", offset);   // get the index of where my cookie's VALUE ends
				if (endindex == -1) {                                  // ...and if it's -1, then it's the last cookie
					endindex = document.cookie.length;                 // so there's no ';' and the endinex is the end of the cookie
				}
				
				return document.cookie.substring(offset, endindex);    // get and return the string that exists between the index of where my cookie's VALUE starts and the index of where it ends
			}
		}
	} else {
		return null;                                                   // if there is no cookie
	}
}

/* 
 * makes each link's class attribute empty, so that only "this" link will be highlighted.
 * note that only the list item's with a link are searched.
 */
function refreshStylesList() {
	var stylesdiv = document.getElementById("styles");  // get a handle for the div with the stylesheet names
	var links = stylesdiv.getElementsByTagName("a");    // make an array for each link in the div
	
	for (var i = 0; i < links.length; i++) {            // loop through the array of links
		links[i].parentNode.className = "";             // and set each of the anchor's parent list item class to empty
	}
}
	
/* 
 * highlights the stylesheet that is currently in use.
 */
function highlightStyle() {
	var stylesdiv = document.getElementById("styles");    // get a handle for the div with the stylesheet names
	var links = stylesdiv.getElementsByTagName("a");      // make an array for each link in the div
	var sheet = getCurrentSheet();                               // get a handle for the primary stylesheet link in the header
	var design = sheet.getAttribute("href");              // get the value of the stylesheet's reference
	design = design.substring(design.indexOf('/') + 1, design.indexOf("."));    // get the name of the stylesheet, excluding the '.css' extension
	
	for (var i = 0; i < links.length; i++) {              // loop through the array of links in the div
		if (links[i].firstChild.nodeValue == design) {    // if the value of the link is the name of the current primary stylesheet
			links[i].parentNode.className = "thisstyle";  // set this link's parent list item to the highlighter class
			break;
		}
	}
}