// General use. Functions to remove no-JS warning and set up alternative stylesheets.
var stylesheets = [];
var stylesheetIndex=0;
window.onload=initAll;

function initAll() {
  // Remove the absence-of-Javascript warning, if appropriate
  cancelJSWarning();
  setOnClick();
  findStylesheets();
  getStylerCookie();
  document.getElementById('body_all').style.display = 'block';
  pageSpecific();
}

function cancelJSWarning() {
  var jsw=document.getElementById("js_warning");
  var jswp=jsw.firstChild;
  jsw.removeChild(jswp);
}

function findStylesheets() {
  // Pick up all the stylesheets, and store their titles in an array
  var lnks = document.getElementsByTagName('link');
  for (var i = lnks.length - 1; i >= 0; i--) {
    // If the link tag contains a stylesheet, store its name
    if (lnks[i].getAttribute('rel').indexOf('style')> -1 && lnks[i].getAttribute('title')) {
      stylesheets[stylesheetIndex]=lnks[i].getAttribute('id'); 
      lnks[i].disabled = true;
      stylesheetIndex++;
    }
  }
  document.getElementById('styler_inner').onclick=changeStylesheet;
}
function changeStylesheet() {
  // Pick a stylesheet to use
  var oldStylesheet=document.getElementById(stylesheets[stylesheetIndex]);
  stylesheetIndex++;
  if (stylesheetIndex==stylesheets.length) {stylesheetIndex=0;}
  var newStylesheet=document.getElementById(stylesheets[stylesheetIndex]);
  oldStylesheet.disabled = true;
  newStylesheet.disabled = false;
  document.getElementById('styler_setting').innerHTML="Setting: " + stylesheets[stylesheetIndex];
  setStylerCookie();
} 

function getStylerCookie() {
  // Check for styler cookie
  stylesheetIndex=0;
  if (document.cookie != "") {
    // If we have a cookie file, split it into individual cookies
    siteCookies=document.cookie.split("; ");
    // Look through the cookies for the stylerTitle one
    for (var i=0;i<siteCookies.length;i++) {
      if (siteCookies[i].split("=")[0]=="stylerTitle") {
	// If we've found the stylerTitle cookie, get its value (the title string)
	stylerTitle=siteCookies[i].split("=")[1];
	// Look for the appropriate stylesheet
	for (var j=0;j<stylesheets.length;j++) {
	  // If we've found the stylesheet by title, get its index in the stylesheet array
	  if (stylesheets[j]==stylerTitle) { 
	    stylesheetIndex=j;//alert(j+" "+stylerTitle);
	    break;
	  }
	}
	break;
      }
    }
  }
  // Even if we didn't find a cookie, update the current style
  document.getElementById(stylesheets[stylesheetIndex]).disabled = false;
  document.getElementById('styler_setting').innerHTML="Setting: " + stylesheets[stylesheetIndex];
}

function setStylerCookie() {
  // Store the current stylesheet name in the cookie
  var expiryDate = new Date();
  expiryDate.setMonth(expiryDate.getMonth()+12);
  document.cookie = "stylerTitle=" + stylesheets[stylesheetIndex] + ";expires=" + expiryDate.toGMTString();
}

function setOnClick() {
  var pics=document.getElementsByTagName('img');
  for(var i=0;i<pics.length;i++) {
    if(pics[i].parentNode.id=='ads') {
      // Trap ctrl-click on advert images!
      pics[i].onclick=function(e) {
	if (window.event) e = window.event; 
	 var elt = e.srcElement? e.srcElement : e.target; 
	 if (e.ctrlKey) {elt.style.display="none";}
	} 
    }
  }
} 
