// ----------------------------------------------
// StyleSwitcher functions written by Paul Sowden
// http://www.idontsmoke.co.uk/ss/
// - - - - - - - - - - - - - - - - - - - - - - -
// For the details, visit ALA:
// http://www.alistapart.com/stories/alternate/


if (document.images) {
	buttonUp = new Image(); buttonUp.src = ""; // Never use bitmaps for the web.
	buttonDown1 = new Image(); buttonDown1.src = "./templates/lellou/images/gallery.jpg";
	buttonDown2 = new Image(); buttonDown2.src = "./templates/lellou/images/gallery.gif";
	buttonDown3 = new Image(); buttonDown3.src = "./templates/lellou/images/gallery.gif";
	buttonDown4 = new Image(); buttonDown4.src = "./templates/lellou/images/gallery.gif";
	buttonDown5 = new Image(); buttonDown5.src = "./templates/lellou/images/gallery.gif";
	buttonDown6 = new Image(); buttonDown6.src = "./templates/lellou/images/gallery.gif";
	buttonDown7 = new Image(); buttonDown7.src = "./templates/lellou/images/gallery.gif";
}

function turn_off(cell) {
	if (document.images != null) {
		cell.style.backgroundImage = "url('" + buttonUp.src + "')";
	}
}

function turn_on(cell, me) {
	if (document.images != null) {
		if(me == 1)
			cell.style.backgroundImage = "url('" + buttonDown1.src + "')";
		else if(me == 2)
			cell.style.backgroundImage = "url('" + buttonDown2.src + "')";
		else if(me == 3)
			cell.style.backgroundImage = "url('" + buttonDown3.src + "')";
		else if(me == 4)
			cell.style.backgroundImage = "url('" + buttonDown4.src + "')";
		else if(me == 5)
			cell.style.backgroundImage = "url('" + buttonDown5.src + "')";
		else if(me == 6)
			cell.style.backgroundImage = "url('" + buttonDown6.src + "')";
		else if(me == 7)
			cell.style.backgroundImage = "url('" + buttonDown7.src + "')";
	}
}

function setActiveStyleSheet(title) {
  var i, a, main;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title")) {
      a.disabled = true;
      if(a.getAttribute("title") == title) a.disabled = false;
    }
  }
}

function getActiveStyleSheet() {
  var i, a;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title") && !a.disabled) return a.getAttribute("title");
  }
  return null;
}

function getPreferredStyleSheet() {
  var i, a;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1
       && a.getAttribute("rel").indexOf("alt") == -1
       && a.getAttribute("title")
       ) return a.getAttribute("title");
  }
  return null;
}

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 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;
}

window.onload = function(e) {
  var cookie = readCookie("style");
  var title = cookie ? cookie : getPreferredStyleSheet();
  setActiveStyleSheet(title);
}

window.onunload = function(e) {
  var title = getActiveStyleSheet();
  createCookie("style", title, 365);
}

var cookie = readCookie("style");
var title = cookie ? cookie : getPreferredStyleSheet();
setActiveStyleSheet(title);