// swap_styles.js randomly chooses a style sheet

var headimg = new Image(400, 100);
headimg.src = "/gfx/sun-earther-text-yell.gif";
//headimg.src = "/gfx/sun-earther-text-wasp.gif";

var styles = new Array();

for ( var i = 0; i < 50; i++ ) { styles.push("/css/yellowjacket.css"); }
for ( var i = 0; i < 20; i++ ) { styles.push("/css/spacegroove.css"); }
for ( var i = 0; i < 10; i++ ) { styles.push("/css/web2point1.css"); }
for ( var i = 0; i < 5; i++ ) { styles.push("/css/spacegroove-purp.css"); }
for ( var i = 0; i < 1; i++ ) { styles.push("/css/oldskool.css"); }
//for ( var i = 0; i < 10; i++ ) { styles.push("/css/spacegroove-purp.css"); }


function named_style(stylename) {

	alert(stylename);
	var docstyle = document.getElementById("docstyle");
	docstyle.href="/css/"+stylename+".css";
	
}


function switch_styles() {
	
	var whatstyle = Math.random();
	whatstyle *= (styles.length-1);
	whatstyle = Math.ceil(whatstyle);
	var docstyle = document.getElementById("docstyle");
	
	if (navigator.cookieEnabled) {
		
		if ( document.cookie.indexOf('stylee') != -1) {
		
			document.cookie.match(/stylee=([^;]*)/);
			var cookieStyle = RegExp.$1;
			if ( docstyle.href != cookieStyle ) { docstyle.href = cookieStyle; }
			
		} else {
		
			var exp = new Date();
			var oneMinuteLater = exp.getTime() + (60 * 1000);
			exp.setTime(oneMinuteLater);
			var expiryTime = exp.toGMTString()
			docstyle.href = styles[whatstyle];
			document.cookie = "stylee="+docstyle.href+";expires="+expiryTime+";path=/";
			
		}
		
	} else {

		docstyle.href = styles[whatstyle];

	}

	write_footer_stylefile();
			
}


function set_style(stylee) {
	
	if ( stylee >= styles.length ) {
		
		alert("no such style");
		
	} else {
		
		var docstyle = document.getElementById("docstyle");
		var exp = new Date();
		var oneDayLater = exp.getTime() + (24 * 60 * 60 * 1000);
		exp.setTime(oneDayLater);
		var expiryTime = exp.toGMTString()
		docstyle.href = styles[stylee];
		document.cookie = "stylee="+docstyle.href+";expires="+expiryTime+";path=/";
		
		write_footer_stylefile();
		
	}
	
}

function write_footer_stylefile() {
	
	if (document.getElementById('footer')) {
		
		var docstyle = document.getElementById("docstyle");
		var matchStyleName = new RegExp("\/([^\/]+)\.css$");
		docstyle.href.match(matchStyleName);
		var styleFile = RegExp.$1;
		document.getElementById('footer').innerHTML = "<p>"+styleFile+" style</p>";	
	}

}