
// Expires in expdays
function setCookie(name, value, expdays) {   
	var now = new Date();
	var exp = new Date(now.getTime() + (1000*60*60*24*expdays));
	document.cookie = name + "=" + escape(value) + ";" +
					"expires=" + exp.toGMTString() + ";" +
					"path=/";
};

// Cookie has expired
function delCookie(name) {   
	var now = new Date();
	var exp = new Date(now.getTime() - 1);
	document.cookie = name + "=;" +
					"expires=" + exp.toGMTString() + ";" + 
					"path=/";
};

// Fetch a cookie
function getCookie(name) {
	var cname = name + "=";
	var dc = document.cookie;

	if (dc.length > 0) {

		var start = dc.indexOf(cname);

		if (start != -1) {
			start += cname.length;
			var stop = dc.indexOf(";", start);

			if (stop == -1) stop = dc.length;

			return unescape(dc.substring(start,stop));
		}
	}
	return null;
};

function makeover(css) {
	setCookie("layout", css, 365);   // valid 1 year
	window.location.href=window.location.href;
};

function cssGetCookie() {
	return getCookie("layout");
};

function cssDelCookie() {
	delCookie("layout");
};

function insert_css() {
	var s;
	if((s = cssGetCookie()) && (s != "ct.css")) {
		document.styleSheets[0].disabled = true;
		var head = document.getElementsByTagName('head').item(0);
		var styling = document.createElement('style');
		styling.setAttribute('type', 'text/css');
		styling.setAttribute('media', 'all');
		var thecss = document.createTextNode('@import "/css/ct.css"; @import "/css/' + s + '";');
		styling.appendChild(thecss);
		head.appendChild(styling);
	}
};



function change_option(what, num, styler) {
	
	if(styler == 'on') {
		clear_all_options(what, num);
		if(document.getElementById(what + num).className == 'loff') {
			document.getElementById(what + num).className = 'lon';
			document.getElementById(what + num + 'r').className = 'ron';
		}
	}
	else if(styler == 'off') {
		if(document.getElementById(what + num).className == 'lon') {
			document.getElementById(what + num).className = 'loff';
			document.getElementById(what + num + 'r').className = 'roff';
		}

	}
	else if(styler == 'choice') {
		if(num < 20) {
			for (var x = 0; x <= 20; x++) {
				if(document.getElementById(what + x) != null) {
					document.getElementById(what + x).className = 'loff';
					document.getElementById(what + x + 'r').className = 'roff';
				}
			}


			document.getElementById(what + num).className = 'lchoice';
			document.getElementById(what + num + 'r').className = 'rchoice';
			setCookie('album_' + what, num, 365);
		}
	}	
};

function clear_all_options(what, num) {
	for (var x = 0; x <= 20; x++) {
		if(document.getElementById(what + x) != null) {
			if(what + x != what + num) {
				if(document.getElementById(what + x).className != 'lchoice') {
					document.getElementById(what + x).className = 'loff';
					document.getElementById(what + x + 'r').className = 'roff';
				}
			}
		}
	}
};

function choose_options(doit) {
	var rnum = getCookie('album_r');
	var cnum = getCookie('album_c');
	var exif = getCookie('album_exif');
	
	if(doit == 'yes') {

		if(document.getElementById('r' + rnum) != null) {
			document.getElementById('r' + rnum).className = 'lchoice';
			document.getElementById('r' + rnum + 'r').className = 'rchoice';
		}
		else if (document.getElementById('r' + 5).className != null) {
			document.getElementById('r' + 5).className = 'lchoice';
			document.getElementById('r' + 5 + 'r').className = 'rchoice';
			setCookie('album_r', 7, 365);
		}
		
		if(document.getElementById('c' + cnum) != null) {
			document.getElementById('c' + cnum).className = 'lchoice';
			document.getElementById('c' + cnum + 'r').className = 'rchoice';
		}
		else if (document.getElementById('c' + 3).className != null) {
			document.getElementById('c' + 3).className = 'lchoice';
			document.getElementById('c' + 3 + 'r').className = 'rchoice';
			setCookie('album_c', 3, 365);
		}
		
		if(document.getElementById('exif' + exif) != null) {
			document.getElementById('exif' + exif).className = 'lchoice';
			document.getElementById('exif' + exif + 'r').className = 'rchoice';

		}
		else {
			document.getElementById('exif0').className = 'lchoice';
			document.getElementById('exif0r').className = 'rchoice';
			setCookie('album_exif', 0, 365);
		}
		
	}

};