//these are all scripts to fix all the CSS, HTML etc. 
// that IE 6 still doesn't support yet. This will be necessary for 
// several years to come until the consumer version 
// Longhorn becomes very common.

// This function which I found at 
// http://www.sovavsiti.cz/css/abbr.html
// Thanks Marek Prokop!
// fixes IE's broken implementation <abbr>
function styleAbbr() {
	var oldBodyText, newBodyText, reg
	if (isIE) {
		oldBodyText = document.body.innerHTML;
		reg = /<ABBR([^>]*)>([^<]*)<\/ABBR>/g;
		newBodyText = oldBodyText.replace(reg, '<abbr $1><span class=\"fix\" $1>$2</span></abbr>');
		document.body.innerHTML = newBodyText;
	}
}


// This function is obviously a minor
// variation on function above.
// It's not really adequate. 
// because proper browsers realize that Q tags 
// are language and encoding sensitive
// using the proper quote symbols depending on 
// nesting, context and alphabets.
// This one just stupidly swaps in the 
// English version. Oh well.
function styleQ() {
	var oldBodyText, newBodyText, reg
	if (isIE) {
		oldBodyText = document.body.innerHTML;
		reg = /<Q([^>]*)>([^<]*)<\/Q>/g;
		newBodyText = oldBodyText.replace(reg, '<q $1>&#8220;$2&#8221;</q>');
		document.body.innerHTML = newBodyText;
	}
}

window.onload = function(){
	styleAbbr()
	styleQ()
};

isIE = (document.all) ? true:false;


// This function which I found at 
// http://www.kennsarah.net/archives/000199.shtml
// Thanks Ken & Sarah Walker!
// attempts to fix IE's broken implementation <q>
// Further study:
// http://simon.incutio.com/archive/2003/04/03/fixingQuotesWithJavascript
// http://www.kryogenix.org/days/460.html
function checkQuotes () {
	quotesElements = document.getElementsByTagName("q");
	if (quotesElements.length > 0) {
		q=quotesElements[0];
		if (q.currentStyle) {
			s = q.currentStyle;
		} else if (document.defaultView && document.defaultView.getComputedStyle) {
			s = document.defaultView.getComputedStyle(q,'');
		}
		supportsQuotes = false;
		for (prop in s) {
			if (prop.toLowerCase() == 'quotes') {
				supportsQuotes = true;
				break;
			}
		}
		if (!supportsQuotes) {
			for (var i=0; i<quotesElements.length; i++) {
				q = quotesElements[i];
				q.innerHTML = '&#8220;'+q.innerHTML+'&#8221;';
			}
		}
	}
}

//This is Paul Snowden's style switcher
// It's really only necessary for IE.
// Read more about it here:
// http://alistapart.com/stories/alternate/

function setActiveStyleSheet(title) {
  var i, a, main;
  for (i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if (a.getAttribute("rel") &&
        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") &&
        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") &&
        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);