// Define a function to insert the theme switchers
function insertThemeSwitchers() {

	// Create elements
	var themeSwitcher=document.createElement("div");
	var themeSwitcherNext=document.createElement("a");
	var themeSwitcherPrevious=document.createElement("a");
	var themeSwitcherNextSub1=document.createElement("span");
	var themeSwitcherNextSub2=document.createElement("span");
	var themeSwitcherNextSub3=document.createElement("span");
	var themeSwitcherNextSub4=document.createElement("span");
	var themeSwitcherPreviousSub1=document.createElement("span");
	var themeSwitcherPreviousSub2=document.createElement("span");
	var themeSwitcherPreviousSub3=document.createElement("span");
	var themeSwitcherPreviousSub4=document.createElement("span");

	// Attach id attributes
	themeSwitcher.setAttribute("id","s-themeswitcher");
	themeSwitcherNext.setAttribute("id","next-theme");
	themeSwitcherPrevious.setAttribute("id","previous-theme");

	// Attach href attributes
	themeSwitcherNext.setAttribute("href","#next-theme");
	themeSwitcherPrevious.setAttribute("href","#previous-theme");
	
	// Attach activation events
	themeSwitcherNext.onclick=cycleThemeNext;
	themeSwitcherPrevious.onclick=cycleThemePrevious;

	// Find the target location
	var targetContainer=document.getElementById("s-container");

	// Nest the elements
	themeSwitcher.appendChild(themeSwitcherNext);
	themeSwitcher.appendChild(themeSwitcherPrevious);
	themeSwitcherNext.appendChild(themeSwitcherNextSub1);
	themeSwitcherPrevious.appendChild(themeSwitcherPreviousSub1);
	themeSwitcherNextSub1.appendChild(themeSwitcherNextSub2);
	themeSwitcherPreviousSub1.appendChild(themeSwitcherPreviousSub2);
	themeSwitcherNextSub2.appendChild(themeSwitcherNextSub3);
	themeSwitcherPreviousSub2.appendChild(themeSwitcherPreviousSub3);
	themeSwitcherNextSub3.appendChild(themeSwitcherNextSub4);
	themeSwitcherPreviousSub3.appendChild(themeSwitcherPreviousSub4);

	// Insert the nest
	targetContainer.appendChild(themeSwitcher);

}

// Define functions to cycle the theme in each direction
function cycleThemeNext() {
	cycleTheme('next');
}
function cycleThemePrevious() {
	cycleTheme('previous');
}

// Define a function to cycle the theme in any direction
function cycleTheme(direction) {

	// Get the current theme
	var oldTheme=getActiveStyleSheet();

	// Find the next and previous themes
	var nextTheme;
	var previousTheme;
	if (oldTheme=='Orange') {nextTheme='Ice';previousTheme='Cheap';}
	if (oldTheme=='Ice') {nextTheme='Forest';previousTheme='Orange';}
	if (oldTheme=='Forest') {nextTheme='Mars';previousTheme='Ice';}
	if (oldTheme=='Mars') {nextTheme='Ocean';previousTheme='Forest';}
	if (oldTheme=='Ocean') {nextTheme='Yellow Sky';previousTheme='Mars';}
	if (oldTheme=='Yellow Sky') {nextTheme='Water';previousTheme='Ocean';}
	if (oldTheme=='Water') {nextTheme='Fire';previousTheme='Yellow Sky';}
	if (oldTheme=='Fire') {nextTheme='Pomegranate';previousTheme='Water';}
	if (oldTheme=='Pomegranate') {nextTheme='Future';previousTheme='Fire';}
	if (oldTheme=='Future') {nextTheme='Tropic';previousTheme='Pomegranate';}
	if (oldTheme=='Tropic') {nextTheme='Ducks';previousTheme='Future';}
	if (oldTheme=='Ducks') {nextTheme='Cheap';previousTheme='Tropic';}
	if (oldTheme=='Cheap') {nextTheme='Orange';previousTheme='Ducks';}
	
	// Choose the new theme
	if(direction=='next') {
		var newTheme=nextTheme;
	}
	if(direction=='previous') {
		var newTheme=previousTheme;
	}

	// Change the theme
	setActiveStyleSheet(newTheme);

}

// Once the document is loaded, call the function to insert the theme switcher
window.onload=insertThemeSwitchers;


