// cookie related code:
// cookie functions from http://www.quirksmode.org

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}


window.onbeforeunload = onbeforeunloadFunction;

function onbeforeunloadFunction() {
  var cookieValue = parseInt(currentImg);
  createCookie('storedCurrentImg',cookieValue,1);
}


// the following array is an array of id's.
// each id represents a div
// the fader loops through these items.
var the_images = new Array ('photo_a','photo_b','photo_c','photo_d','photo_e','photo_f','photo_g','photo_h','photo_i','photo_j','photo_k','photo_l','photo_m','photo_n','photo_o','photo_p','photo_q');
var theArray = the_images;
var theReadCookie = readCookie('storedCurrentImg');
if (theReadCookie != null && theReadCookie != 'undefined' && parseInt(theReadCookie) < 17) { 
  currentImg = theReadCookie;
}
else {
  var ran_unrounded=Math.random()*15;
  var ran_number=Math.floor(ran_unrounded);
  var currentImg = ran_number;
}
var length = eval(theArray).length;
// after the page loads ...
window.onload = init;
// ... we'll start the fader
function init() {
  for(i=0; i<the_images.length; i=i+1) {
    document.getElementById(the_images[i]).className = 'photos ' + the_images[i];
  }
  for(i=0; i<the_images.length; i=i+1) {
    setOpacity(the_images[i],'0');
  }
  setOpacity(the_images[currentImg],'100');
  setOpacity('photo_div','0');
  setTimeout('fader(' + currentImg + ')',timeBetweenFades)
}
