
var gacDebug = false;
var gacCSSCookie;
var gacCSSTitle;

/* Functions are based on work described on A List Apart
http://www.alistapart.com/stories/alternate/
by Paul Sowden
http://alistapart.com/authors/paulsowden/

They have been renamed to avoid javascript name clashes.
*/


function gacActivatePreferredStyleSheet()
{
if ((document.getElementsByTagName) == false)
return;

if (gacDebug) { alert('In gacActivatePreferredStyleSheet()'); }

var cookieWasSet=1, titleWasSet=1;
if (!gacIsSet(gacCSSCookie)) {
gacCSSCookie= gacGetCSSCookie();
cookieWasSet = false;
}
if (!gacIsSet(gacCSSTitle)) {
gacCSSTitle= gacCSSCookie ? gacCSSCookie : gacGetPreferredStyleSheet();
titleWasSet = false;
}
if (cookieWasSet==false  ||  titleWasSet==false) {
// The cookie and title should be set together but, we want to be sure.
gacSetActiveStyleSheet(gacCSSTitle);
}
}

function gacSetActiveStyleSheet(title) {
var i, a, main;

if (gacDebug) { alert("In gacSetActiveStyleSheet("+title+")"); }
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 gacGetActiveStyleSheet() {
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 gacGetPreferredStyleSheet() {
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;
}

/* End of functions based on work described on A List Apart
http://www.alistapart.com/stories/alternate/
by Paul Sowden
http://alistapart.com/authors/paulsowden/
*/

// Functions written by Glen Coakley http://glencoakley.org.

function gacSetCookieStyleSheet(title) {
gacSetActiveStyleSheet(title);
gacSetCSSCookie(title);
document.location.reload();
}

function gacGetCSSCookie() {
var str= gacReadCookie("GlenCoakley.org-CSS");
return str;
}

function gacSetCSSCookie(styleTitle) {
gacWriteCookie("GlenCoakley.org-CSS", styleTitle, "");
}




gacActivatePreferredStyleSheet();

/* getElementsBySelector(object, selector)
Version 0.5 - Glen Coakley, April 12th 2005
Cleaned up multiple variable declarations. 

Version 0.4 - Simon Willison, March 25th 2003
ref: http://simon.incutio.com/js/getElementsBySelector.html
*/

function getAllChildren(e) {
return e.all ? e.all : e.getElementsByTagName('*');
}

function getElementsBySelector(object, selector)
{
if (gacDebug) { alert("In getElementsBySelector"); }

if (!document.getElementsByTagName) {
return new Array();
}
var tokenList= selector.split(' ');
var currentContext= new Array(document);
var currentContextIndex= 0;
var bits;
var tagName;
var found;
var foundCount;
var elements;
var h, i, j, k;
var token;
for (i= 0; i < tokenList.length; i++) {
if (!tokenList[i]  ||  tokenList[i].length <= 0) { 
continue; // Skip to next token
}
token= tokenList[i].replace(/^\s+/,'').replace(/\s+$/,'');;
if (token.indexOf('#') > -1) {
bits= token.split('#');
tagName= bits[0];
var id= bits[1];
var element= document.getElementById(id);
if (tagName && element.nodeName.toLowerCase() != tagName) {
return new Array();
}
currentContext= new Array(element);
continue; // Skip to next token
}
if (token.indexOf('.') > -1) {
bits= token.split('.');
tagName= bits[0];
var className= bits[1];
if (!tagName) {
tagName= '*';
}
found= new Array;
foundCount= 0;
for (h= 0; h < currentContext.length; h++) {
if (tagName == '*') {
elements= getAllChildren(currentContext[h]);
} else {
elements= currentContext[h].getElementsByTagName(tagName);
}
for (j= 0; j < elements.length; j++) {
found[foundCount++]= elements[j];
}
}
currentContext= new Array;
currentContextIndex= 0;
for (k= 0; k < found.length; k++) {
if (found[k].className && found[k].className.match(new RegExp('\\b'+className+'\\b'))) {
currentContext[currentContextIndex++]= found[k];
}
}
continue; // Skip to next token
}
if (token.match(/^(\w*)\[(\w+)([=~\|\^\$\*]?)=?"?([^\]"]*)"?\]$/)) { //" 
tagName= RegExp.$1;
var attrName= RegExp.$2;
var attrOperator= RegExp.$3;
var attrValue= RegExp.$4;
if (!tagName) {
tagName= '*';
}
found= new Array;
foundCount= 0;
for (h= 0; h < currentContext.length; h++) {
if (tagName == '*') {
elements= getAllChildren(currentContext[h]);
} else {
elements= currentContext[h].getElementsByTagName(tagName);
}
for (j= 0; j < elements.length; j++) {
found[foundCount++]= elements[j];
}
}
currentContext= new Array;
currentContextIndex= 0;
var checkFunction; // This function will be used to filter the elements
switch (attrOperator) {
case '=': // Equality
checkFunction= function(e) { return (e.getAttribute(attrName) == attrValue); };
break;
case '~': // Match one of space seperated words 
checkFunction= function(e) { return (e.getAttribute(attrName).match(new RegExp('\\b'+attrValue+'\\b'))); };
break;
case '|': // Match start with value followed by optional hyphen
checkFunction= function(e) { return (e.getAttribute(attrName).match(new RegExp('^'+attrValue+'-?'))); };
break;
case '^': // Match starts with value
checkFunction= function(e) { return (e.getAttribute(attrName).indexOf(attrValue) == 0); };
break;
case '$': // Match ends with value - fails with "Warning" in Opera 7
checkFunction= function(e) { return (e.getAttribute(attrName).lastIndexOf(attrValue) == e.getAttribute(attrName).length - attrValue.length); };
break;
case '*': // Match ends with value
checkFunction= function(e) { return (e.getAttribute(attrName).indexOf(attrValue) > -1); };
break;
default :
// Just test for existence of attribute
checkFunction= function(e) { return e.getAttribute(attrName); };
}
currentContext= new Array;
currentContextIndex= 0;
for (k= 0; k < found.length; k++) {
if (checkFunction(found[k])) {
currentContext[currentContextIndex++]= found[k];
}
}
continue; // Skip to next token
}
tagName= token;
found= new Array;
foundCount= 0;
for (h= 0; h < currentContext.length; h++) {
elements= currentContext[h].getElementsByTagName(tagName);
for (j= 0; j < elements.length; j++) {
found[foundCount++]= elements[j];
}
}
currentContext= found;
}
return currentContext;
}



// Functions written by Glen Coakley http://glencoakley.org.

function gacDumpNode(obj)
{
var attrs= '';
for (j=0; j < obj.attributes.length; ++j) {
attrs += obj.attributes[j].nodeName;
if (obj.attributes[j].nodeValue)
attrs += '="' + obj.attributes[j].nodeValue;
attrs += '"; ';
}
return attrs;
}

function gacIsSet(object)
{
if (object == null || object <= 0)
return false;
else return true;
}

function gacLastIndexOfURL(obj, target)
{
var copy= new String(unescape(obj));
var index= copy.lastIndexOf("\\");
if (index <= 0)
index= copy.lastIndexOf("/");
return index;
}



function gacWriteCookie(name, value, pDays) {
var days= pDays;
if (!days) days= 365;
var date= new Date();
date.setTime(date.getTime() + (days*24*60*60*1000));
var expires= "; expires=" + date.toGMTString();
document.cookie= name+"="+value+expires+"; path=/";
}

function gacReadCookie(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 gacPadNumber(number, leftPadWidth, rightPadWidth)
{
var text= new String(number);
if ( leftPadWidth == null) {  leftPadWidth= 0; }
if (rightPadWidth == null) { rightPadWidth= 0; }

var dotLocation= (text.indexOf('.') >= 0) ? text.indexOf('.') : text.length;
while (dotLocation < leftPadWidth)
{
text= '0' + text;
dotLocation= (text.indexOf('.') >= 0) ? text.indexOf('.') : text.length;
}

if (dotLocation != text.length) { ++dotLocation; }
var decimalLength= text.length - dotLocation;
while (decimalLength < rightPadWidth)
{
text= text + '0';
decimalLength= text.length - dotLocation;
}
return text;
}


function gacIsBrowserDaylightSavings()
{
var today= new Date();
var JanFirst= new Date(today.getFullYear(), 1,1);
return (JanFirst.getTimezoneOffset() != today.getTimezoneOffset());
}

function gacIsMPLS_DST()
{
var isMPLS_DST;
var today= new Date();
today.setUTCHours (today.getUTCHours()-6); // adj. for Mpls. timezone
var month= today.getUTCMonth();
var dayOM;
var i;

if (month < 2 || month > 9)      { isMPLS_DST= 0; }
else if (month > 2 && month < 9) { isMPLS_DST= 1; }
else if (month == 2)
{                                // we switch on the first Sunday in March
dayOM= today.getUTCDate();
if (dayOM >= 7) {                    // days >=7 are not the first Sunday
isMPLS_DST= 0;
}
else if (today.getUTCDay() == 0) {   // today is the first Sunday
if (today.getUTCHours() < 2) {
isMPLS_DST= 0;
} else { isMPLS_DST= 0; }
} else {                             // has the first Sunday past?
isMPLS_DST= 0;                    // Assume that we haven't switched
for (i= dayOM; i > 0; i--) {  // brute force check the rest
today.setUTCDate(d);
if (today.getUTCDay() == 0) {    // If there is a Sunday before today.
isMPLS_DST= 1;                // we already switched
break;
}
}
}
}
else {                              // in October, we switch the last Sunday
dayOM= today.getUTCDate();
if (dayOM <= 24) {                    // days <=24 are not the last Sunday
isMPLS_DST= 1;
}
else if (today.getUTCDay() == 0) {  // today is the last Sunday
if (today.getUTCHours() < 1) {
isMPLS_DST= 1;
} else { isMPLS_DST= 1; }
} else {                              // has the last Sunday past?
isMPLS_DST= 0;                     // Assume that we switched
for (i= dayOM; i <= 31; i++) { // brute force check the rest
today.setUTCDate(d);
if (today.getUTCDay() == 0) {     // If there is a Sunday after today.
isMPLS_DST= 1;                 // we haven't switched yet
break;
}
}
}
}
return isMPLS_DST;
}

function gacDayNumToText(day)
{
var days= new Array('Sunday', 'Monday', 'Tuesday', 'Wednesday', 
'Thursday', 'Friday', 'Saturday');
return days[day];
}

function gacMonthNumToText(month)
{
var months= new Array(
'January', 'February', 'March', 'April', 'May', 'June', 'July', 
'August', 'September', 'October', 'November', 'December');
return months[month];
}

function gacToHumanDateTimeFormat(datetime, includeMilliseconds,
isDST, includeTimezone)
{
var td= new Date();
if (datetime != null) { td= datetime; }

var hours= td.getHours(), ampm= 'am';
if (hours > 12) { hours -= 12; ampm= 'pm'; }

var ts= days[td.getDay()] + ", " + months[td.getMonth()] + " " 
+ td.getDate() + ", " + td.getFullYear() + " at " + hours + ":" 
+ gacPadNumber(td.getMinutes(),2,0) + ":" + gacPadNumber(td.getSeconds(),2,0);
if (includeMilliseconds) { ts += "." + td.getMilliseconds(); }
ts += ampm;
if (includeTimezone) { ts += (isDST) ? " CDT" : " CST"; }
return ts;
}

function gacToHumanTimeDateFormat(datetime, includeMilliseconds, 
isDST, includeTimezone)
{
var td= new Date();
if (datetime != null) { td= datetime; }

var hours= td.getHours(), ampm= 'am';
if (hours > 12) { hours -= 12; ampm= 'pm'; }

var ts= hours + ":" + gacPadNumber(td.getMinutes(),2,0) + ":" 
+ gacPadNumber(td.getSeconds(),2,0);
if (includeMilliseconds) { ts += "." + td.getMilliseconds(); }
ts += ampm;
if (includeTimezone) { ts += (isDST) ? " CDT" : " CST"; }
ts += ' on ' + gacDayNumToText(td.getDay()) + ", "
+ gacMonthNumToText(td.getMonth()) + " " + td.getDate() + ", " 
+ td.getFullYear();
return ts;
}

function gacCountDownToDate(textBefore, targetDate, textAfter)
{
var today= new Date(); 
var diff_ms= targetDate - today; // calc diff in millsec
var diffDays= Math.round(diff_ms / 86400000); // millsecs in a day
if (diffDays > 0) { document.write(textBefore + diffDays + textAfter); }
}

// Functions written by Glen Coakley http://glencoakley.org.
var bandwidthWasSet = false;

function gacHeader() {
gacShowElement('myStyleMenu');     // It requires Javascript anyway.
gacShowElement('myBandwidthMenu'); // It requires Javascript anyway.

if (document.getElementById) {
var pc= document.getElementById('page-content');
if (pc) pc.style.width= '50em';
}
}

function gacSetBandwidth(bandwidth)
{
if (bandwidthWasSet)
return;

if (bandwidth == null  ||  bandwidth == '') {
var bwc= gacGetBandwidthCookie();

var HBItems= getElementsBySelector(document, '.high-bandwidth');
var i;
for (i=0; i < HBItems.length; ++i) {
var bwcstr= (bwc == 'low') ? 'hidden' : 'visible';

HBItems[i].style.visibility= bwcstr;
if (bwc == 'low') {
HBItems[i].setAttribute('height', '0');
HBItems[i].setAttribute('width', '0');
} else {
HBItems[i].setAttribute('src', HBItems[i].getAttribute('srchigh'));
}
}
}
else {
gacSetBandwidthCookie(bandwidth);
}
bandwidthWasSet = true;
}

function gacShowElement(id)
{
if (document.getElementById) {
var tm= document.getElementById(id);
if (tm) tm.style.visibility= 'visible';
}
}

function gacGetBandwidthCookie() {
var oldCookie= gacReadCookie("GlenCoakley.org-Bandwidth");
if (oldCookie) return oldCookie;

}

function gacSetBandwidthCookie(bandwidth) {
gacWriteCookie("GlenCoakley.org-Bandwidth", bandwidth, "");
}
function gacGetFileName(URL)
{
var path='', page='';

path = unescape(URL.substring(URL.lastIndexOf("/")+1,URL.length));
if (page == "") page = "index.html";
return page;
}

function gacGetBaseName(URL)
{
var pageName = gacGetFileName(URL, 0);
return unescape(pageName.substring(0,pageName.lastIndexOf(".")));
}

function gacGetFileExt(URL)
{
var pageName = gacGetFileName(URL, 0);
return unescape(pageName.substring(pageName.lastIndexOf(".")+1, pageName.length));
}
