/*	
skin.js 
This script apply a random display to the css and images of a site.

27.11.2003 - v 0.1 - Q&D - RSR - Manuel Spuhler 
5.12.03 - loop included + use getElementById + bug of missing id fixed


/* ---------------------------------------------------------------------
| mini how-to
| START : place the <script> before the call of css
| ### Should works with default css img { visibility:hidden; } ### 
only /green/ image should be hidden, possible in css2 target? now the class switch is used
| END : call the method SetImages() before </html> so all images are loaded
| 
'--------------------------------------------------------------------- */

// detect browser msie5 with a very simple method
var is_bad_ie = ((navigator.userAgent.indexOf("MSIE 5.") != -1) && (navigator.userAgent.indexOf("Opera") == -1));

/* CONFIG - all images and id -- will be dynamic one day */
var aSrc = new Array('c3_logo_1.gif',"c3_logo_2.gif",'flash_info-39x39.gif','live-39x39.gif','search_index-29x29.gif', 'search-29x29.gif', 'print-29x29.gif','favoris-29x29.gif','sekikichante-29x29.gif','ecouter-29x29.gif','ecrire-29x29.gif', 'search-29x29.gif'); ////img src
var aId = new Array('logo_front','logo2','info','live','search','search2','print','heart','sound','audio','mail','search3'); 

/* ------[cookies]-------------------------------------------------*/

function setCookie (name, value) {
	var argv=setCookie.arguments;
	var argc=setCookie.arguments.length;
	var expires=(argc > 2) ? argv[2] : null;
	var path=(argc > 3) ? argv[3] : null;
	var domain=(argc > 4) ? argv[4] : null;
	var secure=(argc > 5) ? argv[5] : false;
	document.cookie=name+"="+escape(value)+
		((expires==null) ? "" : ("; expires="+expires.toGMTString()))+
		((path==null) ? "" : ("; path="+path))+
		((domain==null) ? "" : ("; domain="+domain))+
		((secure==true) ? "; secure" : "");// don't set the expire, so it ends when the browser quit
}

function getCookieVal(offset) {
	var endstr=document.cookie.indexOf (";", offset);
	if (endstr==-1)
      		endstr=document.cookie.length;
	return unescape(document.cookie.substring(offset, endstr));
}

function getCookie (name) {
	var arg=name+"=";
	var alen=arg.length;
	var clen=document.cookie.length;
	var i=0;
	while (i<clen) {
		var j=i+alen;
		if (document.cookie.substring(i, j)==arg)
			return getCookieVal (j);
			i=document.cookie.indexOf(" ",i)+1;
			if (i==0) break;
	}
	return null;
}

function killCookie() {
		var pathname=location.pathname;
		var myDomain=pathname.substring(0,pathname.lastIndexOf('/')) +'/';
		var date_exp = new Date();
		date_exp.setTime(date_exp.getTime()-(1000)); // Heure dŽjˆ expirŽe
		setCookie("skin","",date_exp);
}

/* ------[various functions]---------------------------------------*/
// detect, set, preload, display images
function SetImages() {
	document.body.style.visibility="hidden";
	if (color != "green") { // green is the default, don't get tired
		var color_folder = "/images/" + color + "/"; // server absolute path
		for (i=0; i<aSrc.length; i++) {
			if (document.getElementById(aId[i]) != undefined) { // detect if exists
				var sSrc = color_folder + aSrc[i]; //set var image
				tmp = new Image; tmp.src = sSrc; // Preload image
				//if(document.getElementById(aId[i]).complete == true) {
				document.getElementById(aId[i]).src = sSrc;
					//document.getElementById(aId[i]).style.visibility = "visible";// display it
				//}
			}
		}
	}
	// display all
	document.body.style.visibility="visible";
  	//}
}

function handleError() { 
        window.status=("Caramba, oune peu trop à droite!");
}

/* ------[main, init for each page]-------------------------------------- */ 

if (getCookie("skin") == null) { // if true, means cookie are enabled or set 
		var acceptsCookies = false; // This code tests whether the user accepts cookies.
		if(document.cookie == '') {
			document.cookie = 'acceptsCookies=yes'; // Try to set a cookie.
			if(document.cookie.indexOf('acceptsCookies=yes') != -1) {
				acceptsCookies = true; 
			}// If it succeeds, set variable
		} else { // there was already a cookie
			acceptsCookies = true;
		}
		if (acceptsCookies) {
			var color;
			var rnd_num = Math.round(Math.random()*3); //random
			if (rnd_num == 0) {
				color = "yellow" // shoul be set in init array
			 } else if (rnd_num == 1) {
				color = "green"
			} else {
				color = "red"
			}
			setCookie("skin",color); 
		} else {
			color = "green" //default color if nothin' works
		}
} else {
	color = getCookie("skin");
}
// as we write the css in js, the default css should be before the script, and be replaced, so if there's no js enabled you can still see the correct layout. If u have Netscape 4.7, go away!
if (! is_bad_ie) {
	document.write("<link href='/css/" + color + ".css' rel='stylesheet' type='text/css'>");	
} else {
	document.write("<link href='/css/green.css' rel='stylesheet' type='text/css'>");	
}
window.onerror = handleError; // get rid of errors
killCookie(); // TEMP 