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



// from http://www.kryogenix.org
// by Scott Andrew - http://scottandrew.com
// add an eventlistener to browsers that can do it somehow.
function addEvent(obj, evType, fn)
	{
	if (obj.addEventListener)
		{
		obj.addEventListener(evType, fn, false);
		return true;
		}
	else if (obj.attachEvent)
		{
		var r = obj.attachEvent('on'+evType, fn);
		return r;
		}
	else
		{
		return false;
		}
	}

function floatImages()
	{
	// adapted from http://www.dithered.com/javascript/browser_detect/
	//**************************************************************//
	// sniff user agent
	var userAgent = navigator.userAgent.toLowerCase();

	// if Mozilla 1.4 then quit
	if ((userAgent.indexOf('gecko') != -1) && (userAgent.indexOf('gecko/') + 14 == userAgent.length) && (parseFloat(userAgent.substring(userAgent.indexOf('rv:') + 3)) == '1.4')) return;

	// if Opera then quit
	if (document.all && window.Event) return;
	//**************************************************************//

	// check this browser can cope with what we want to do
	if (!document.getElementById) return;
	var blogDiv = document.getElementById('blog');
	if (!blogDiv) return;
	if (!blogDiv.offsetWidth) return;

	blogDiv.className = (blogDiv.offsetWidth >= 500) ? "float-images" : "block-images";
	}

// Blockquote citations

// Simon Willison's work:
// http://simon.incutio.com/archive/2002/12/20/#blockquoteCitations

// Also Dunstan Orchard's work:
// http://1976design.com/blog/archive/2003/11/10/updates/
function blockquoteCite()
	{
	if (!document.createElementNS)
		{
		document.createElementNS = function(ns, elt)
			{
			return document.createElement(elt);
			}
		}
	quotes = document.getElementsByTagName('blockquote');
	for (i = 0; i < quotes.length; i++)
		{
		var cite = quotes[i].getAttribute('cite');
		// value of cite attribute should only contain URI, not any other
		if ((cite) && (cite != ''))
			{
			newlink = document.createElementNS('http://www.w3.org/1999/xhtml', 'a');
			newlink.setAttribute('href', cite);
			newlink.className = 'cite-link';
			newlink.appendChild(document.createTextNode(cite));
			newdiv = document.createElementNS('http://www.w3.org/1999/xhtml', 'cite');
			newdiv.className = 'blockquote-cite';
			newdiv.appendChild(document.createTextNode('Source: '));
			newdiv.appendChild(newlink);
			quotes[i].appendChild(newdiv);
			quotes[i].removeAttribute('cite');
			}
		}
	}

// Ins and Del tags citations
function insdelCite()
	{
	if (!document.createElementNS)
		{
		document.createElementNS = function(ns, elt)
			{
			return document.createElement(elt);
			}
		}
	var insdel = new Array(2);
	insdel[0] = document.getElementsByTagName('ins');
	insdel[1] = document.getElementsByTagName('del');
	for (var i=0; i<insdel.length; i++)
		{
		if (insdel[i])
			{
			for (var id=0; id<insdel[i].length; id++)
				{
				var isdl = insdel[i][id].getAttribute('cite');
				if ((isdl) && (isdl != ""))
					{
					idlink = document.createElementNS('http://www.w3.org/1999/xhtml', 'a');
					idlink.setAttribute('href', isdl);
					idlink.className = 'cite-link ' + (i == 0 ? 'ins-cite' : 'del-cite');
					idlink.setAttribute('title','citation of ' + (i == 0 ? 'added' : 'deleted') + ' text');
					idlink.appendChild(document.createTextNode('#'));
					insdel[i][id].appendChild(idlink);
					insdel[i][id].removeAttribute('cite');
					}
				}
			}
		}
	}

// Force IE not to show alternate text as tooltip
function noAltTooltip()
	{
	images = document.getElementsByTagName('img');
	for (var i = 0; i < images.length; i++)
		{
		var title = images[i].getAttribute('title');
		var alt = images[i].getAttribute('alt');
		if ((document.all) && (alt) && (!title))
			{
			images[i].setAttribute('title', '');
			}
		}
	}

