/* Multi-CSS Script 
 *
 * Written by Sindeko (J. Bird)
 * sindeko@gmail.com
 * For TutorialRoom.com and GRC students
 * Last updated: March 6, 2008
 *
 * This script allows you to have multiple css styles connected to one page.
 *
 * REQUIREMENTS:
 *  1. You must have at least 3 css stylesheets, named style1.css, style2.css, and style3.css.
 *
 *  2. These stylesheets must be in a folder called css.
 *
 *  3. To specifically target a stylesheet, you attach the information to the URL
 *      [example: http://www.tutorialroom.com/example.html?css=1]
 *   
 *   	You may group the css variable with others if needed, using the '&' to separate variables.
 *		[?textfile=text1.txt&css=style2.css&profile=1]
 *
 *  4. You can put the number in (css=1 for style1.css, css=4 for style4.css), or you may put 
 *      the filename in, in case the stylesheet has a different name (css=specialstylesheet.css)
 *
 *  5. You have to have any other css removed from your page you link this from.  Otherwise the
 *      formatting in the web page may overwrite the formatting in your stylesheets.
 *
 *  6. You can stop the random part of the script from triggering by linking to the add-on also:
 *      http://www.tutorialroom.com/scripts/multicss_norandom.js
 * 
 *  7. You can now attach multiple stylesheets (not randomly).  All you do is list them in the url.
 * 		[example: http://www.tutorialroom.com/example.html?css=style1.css,style5.css,pages.css]
 */


function getFilename(cssNum) {

	// This formats a number [1] into a default stylesheet [style1.css].
	return "style" + cssNum + ".css";
}

var cssToLoad = 0;

// This section is for the add on to stop the random effect from happening
try {
	
	// Tries to reference stopRandom, defined in the add-on script.
	// If add-on isn't linked, this will throw an error. Edited:(Changed stopRandom = StopRandom to turn off random load because catch(err) didn't work.)
	stopRandom = true;
	
} catch(err) {
	
	// This only catches if stopRandom hasn't been defined yet (ie. the add-on isn't linked).
	var stopRandom = true;	
	
}

function writeCSS() {

	// Declare variables being used in this function.
	var URL = window.location.href;
	var setVars = '';
	
	
	// Checks to see if a specific style is defined.
	if (URL.indexOf("?") >= 0 && URL.indexOf("css=") >= 0) {
		
		// A specific style is given in the URL.
		// We need to get the variables from the URL (everything after the ?).
		setVars = URL.substr(URL.indexOf("?") + 1);
		
		// This part gives us the variable we need, the value of the css variable.
		// css=myfile.css will set myfile.css to the cssToLoad variable.
		// css=2 will set 2 to the cssToLoad variable.
		cssToLoadList = setVars.substr(setVars.indexOf("css=") + 4);
		
		// This checks to see if there are more than just the css variable in the URL.
		// The & separates URL variables, so if there is &, there are extra variables on the end that we need to take care of.
		if (cssToLoadList.indexOf("&") >= 0) {
		
			// This cuts off all the other variables, leaving us with what we need.
			cssToLoadList = cssToLoadList.substr(0, cssToLoadList.indexOf("&"));
		}
		
		// Create an array of the css files to get, so it can be looped through.
		var cssArray = cssToLoadList.split(',');
		
		// Loop through the array.
		for(var i=0;i<cssArray.length;i++){
			
			// Set cssToLoad to the current element of the array.
			cssToLoad = cssArray[i];
			
			// Because cssToLoad can be either a file or a number, we need to convert the number.
			// The !isNaN stands for 'is not not a number'.  The double negative cancels out to leave 'is a number'.
			if (!isNaN(cssToLoad)) {
				
				// Sends to the above function to receive the required formatting.
				cssToLoad = getFilename(cssToLoad);
			}
			
			// This writes the stylesheet into the page, and the page will then load the correct stylesheet.
			document.write('<link href="css/' + cssToLoad + '" rel="stylesheet" type="text/css" />');
		
		}
		
	} else if (stopRandom) {

		// Load up the default style since random is turned off and none was specified.
		document.write('<link href="css/style1.css" rel="stylesheet" type="text/css" />');
		
	} else {
	
		// This section is for when the URL doesn't have a specific css file to choose.
		// This code will choose randomly from the default 3 styles.
		cssToLoad = Math.floor(Math.random() * 3) + 1;
		
		// Format the number to get the stylesheet.
		cssToLoad = getFilename(cssToLoad);
		
		// This writes the random stylesheet into the page.
		document.write('<link href="css/' + cssToLoad + '" rel="stylesheet" type="text/css" />');
		
	}
	
}
// Call the above function to run.
writeCSS();
