/* 
 core.js - functions used on most pages
 thanks to Paul Snowdon of ALA for the original style switcher code
 tweaked here for multiple sets of user-selectable stylesheets.
*/

/* global variables */
var myPersonality;
var myFontSize;

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

function getActiveStyleSheet(theclass) {
  var i, a;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1 
       && a.getAttribute("title") 
       && a.className == theclass
       && !a.disabled) 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;
}

function initStyles() {
  var cookie = readCookie("personality");
  myPersonality = cookie == "null" ? "default" : cookie ? cookie : "default"; /* global */
  setActiveStyleSheet(myPersonality, "personality");
  cookie = readCookie("fontsize");
  myFontSize = cookie ? cookie : "smallest"; /* global */
  setActiveStyleSheet(myFontSize, "fontsize");
}

function writeStylePreferences() {
  var pers = getActiveStyleSheet("personality");
  var font = getActiveStyleSheet("fontsize");
  createCookie("personality", pers, 365);
  createCookie("fontsize", font, 365);
}

function setPersonalityIcon(title) {
  document.getElementById("candlelink").className = (title == "candle" ? "persiconthis" : "");
  document.getElementById("uahanailink").className = (title == "uahanai" ? "persiconthis" : "");
  document.getElementById("defaultlink").className = (title == "default" ? "persiconthis" : "");
}

function setTextSizeIcon(title) {
  document.getElementById("smtextlink").className = (title == "smallest" ? "texticonthis" : "");
  document.getElementById("medtextlink").className = (title == "medium" ? "texticonthis" : "");
  document.getElementById("lgtextlink").className = (title == "largest" ? "texticonthis" : "");
}

/* amazon gimcracks*/
function initAmazon() {
  var undef = "undefined";
  if (typeof oo != undef) {
    var ooIdx = readCookie("oo");
    showAmazonItem(oo, "oo", new Number(ooIdx ? ooIdx : 0)); // home page
  } else if (typeof si != undef) {
    var siIdx = readCookie("si");
    showAmazonItem(si, "si", new Number(siIdx ? siIdx : 0)); // sites
  } else if (typeof di != undef) {
    var diIdx = readCookie("di");
    showAmazonItem(di, "di", new Number(diIdx ? diIdx : 0)); // design
  } else if (typeof fa != undef) {
    var faIdx = readCookie("fa");
    showAmazonItem(fa, "fa", new Number(faIdx ? faIdx : 0)); // family
  } else if (typeof mu != undef) {
    var muIdx = readCookie("mu");
    showAmazonItem(mu, "mu", new Number(muIdx ? muIdx : 0)); // music
  } else if (typeof ba != undef) {
    var baIdx = readCookie("ba");
    showAmazonItem(ba, "ba", new Number(baIdx ? baIdx : 0)); // faith
  } else if (typeof fl != undef) {
    var flIdx = readCookie("fl");
    showAmazonItem(fl, "fl", new Number(flIdx ? flIdx : 0)); // influential fiction
  } else if (typeof am != undef) {
    var amIdx = readCookie("am");
    showAmazonItem(am, "am", new Number(amIdx ? amIdx : 0)); // anime
  } else if (typeof xm != undef) {
    var xmIdx = readCookie("xm");
    showAmazonItem(xm, "xm", new Number(xmIdx ? xmIdx : 0)); // WPF/XAML
  }
}

function showAmazonItem(amarray, amarrayname, idx) {
  var linkprefix = "http://www.amazon.com/exec/obidos/ASIN/";
  var linksuffix = "/deepgraysea-20";
  var imgprefix = "images/";
  var titlesuffix = " at Amazon.com";
  // preload new image
  var newAPix = new Image();
  newAPix.src = imgprefix + amarray[idx][2]; 

  var alink = document.getElementById("amazonlink"); // image link
  if (alink) {
    alink.href = linkprefix + amarray[idx][1] + linksuffix;
    alink.title = amarray[idx][0] + titlesuffix;
  }
  var atitle = document.getElementById("amazontitle"); // text link
  if (atitle) {
    atitle.href = linkprefix + amarray[idx][1] + linksuffix;
    atitle.title = amarray[idx][0] + titlesuffix;
    atitle.firstChild.nodeValue = amarray[idx][0];
  }
  var ablurb = document.getElementById("amblurb"); // editorial
  if (ablurb && ablurb.firstChild) {
    ablurb.firstChild.nodeValue = amarray[idx][3];
  }
  var apix = document.getElementById("amazonpix"); // image
  if (apix) {
    apix.src = newAPix.src;
    apix.alt = amarray[idx][0] + titlesuffix;
  }

  var nextidx = new Number(idx + 1);
  createCookie(amarrayname, nextidx >= amarray.length ? 0 : nextidx, 365);
}

initStyles(); /* fires immediately */

function init() {
  setPersonalityIcon(myPersonality);
  setTextSizeIcon(myFontSize);
  initAmazon();
}
window.onload = init; /* fires after page load */
window.onunload = writeStylePreferences; /* write style prefs on unload */

/* for loading popup windows in the sites section */
function popup(theLink, theWidth, theHeight) {
  /* some images larger than 800x600, sigh */
  var availW = screen.availWidth ? screen.availWidth : 775;
  var availH = screen.availHeight ? screen.availHeight : 575;
  theWidth = theWidth > availW ? availW : theWidth;
  theHeight = theHeight > availH ? availH : theHeight;
  var winStuff = "scrollbars,resizable,width=" + theWidth + ",height=" + theHeight;
  var newWin = window.open(theLink.href, theLink.target, winStuff);
  newWin.focus();
}

