/* 	Helper scripts for Considered Design -- Moira Burke 9/2002 */

var div_name;   // global variable used for NS4 fix

// List theme stylesheet filenames (in preferred order) here.
// Include the option for no theme (null)
function stylesheetArray() { 
	/*return new Array (	"buildings.css", 
						"gargoyle.css", 
						"allium.css", 
						"plant.css", 
						"dictionary.css",
						"drinking.css",
						"no_theme.css" ); 

*/

	return new Array ( "buildings.css" );
	
}

function stylesheetDescriptionsArray() {
/*
	return new Array (	"Depressed Buildings with No Social Lives",
						"Fernando, The Valencian Gargoyle",
						"Allium for Grandma",
						"Chartreusy Plant",
						"Red Hot Dictionary",
						"Thirsty Around Here",
						"None (No Special Colors or Background Images)");
*/

	return new Array ( "Depressed Buildings with No Social Lives");
}


/* getStylesheet

Determines which stylesheet to serve based on a theme number.
Themes are chosen as follows:

1. 	The first time someone visits the site, or if cookies are disabled, 
	a theme is chosen based on the current hour. That theme is written to a cookie.
	2.	If someone moves from page to page on the site*, the current theme is
	maintained (read from the cookie).

3.	When someone returns to the site (from elsewhere)*, the next theme sequentially
	is chosen and written to a cookie.

4.	The "theme buttons" on the side of the page can be used to switch themes.		

*Note: It's hard to tell if someone is coming from another site or just moving around
on the current site.  document.referrer isn't trustworty, because it's retained even 
when the page reloads.  Instead, when someone loads a page, try to set a separate cookie
called "onsite," which will expire when the browser closes.  If it already exists, the
person is coming from another page on the site.

*/

function getStylesheet() {
	
	var stylesheets = new stylesheetArray();	// create array of stylesheet filenames
	var numStylesheets = stylesheets.length;	// and how many stylesheets is that?
	
	// Determine the theme number.  
	// If there's no cookie, or the value was corrupted, pick a theme based on the hour.
	var theme = getTheme();
	
	// temporary note: I prefer the first theme (buildings) to the others, and so for a little while
	// I'm going to pick that one instead of a random one based on the hour.	
	if( (theme == null) || isNaN(theme) || (theme >= numStylesheets) ) { 
	//	theme = pickThemeOfTheHour(numStylesheets); 
		theme = 0;
	}
	
	
	
	// Is the person coming from another page on this site?
	// If so, keep the theme consistent.
	var onsite = checkAndSetOnsiteCookie();
	if( !onsite ) { theme = chooseNextTheme( theme, numStylesheets );  }
	
	setTheme(theme); 			// write the theme to a cookie
	return stylesheets[theme];	// return the stylesheet for that theme
}




// Looks in a cookie for the theme to use.  There could be multiple cookies, 
// so be sure the "theme" one is returned.
function getTheme( ) {

	// try to find it in a cookie
	var firstChar = document.cookie.indexOf("theme");
	var lastChar = -1;
	
	// if "theme" is there
	if ( firstChar != -1 ) {
		firstChar += 6; // skip characters "theme="
		lastChar = document.cookie.indexOf(';', firstChar);  // figure out where it ends
		if( lastChar == -1 ) { lastChar = document.cookie.length; }		
		return parseInt(document.cookie.substring(firstChar, lastChar));
	}
	else { return null; }
}

// If the theme value was missing or corrupted, this function creates a new value based 
// on the current hour.  That way, if people have cookies disabled, the theme won't change
// every time they go from page to page.  However, the theme will change on the hour.
function pickThemeOfTheHour( numThemes ) {

	// pick a theme based on the current hour
	var now = new Date();
	var hours = now.getHours();
	var new_theme = hours % (parseInt(numThemes)-1); // bypass the "none" theme	
	return new_theme;
}

// Write the given theme number out to a cookie
function setTheme( theme_number ) {
	
	// set the cookie's contents, expiration, path, and domain
	var cookie = "theme=" + theme_number;
	var expires = new Date("December 31, 2010");
		expires = "; expires=" + expires.toGMTString();
	var path = "; path=/";
	var domain = "; domain=.thoughtcrumbs.com";
	
	// and write it out
	document.cookie = cookie + expires + path + domain;
}



// Choose the next theme and write that out to a cookie.  
function chooseNextTheme( old_theme, numThemes ) {
	
	// make sure variables are proper integers
	old_theme = parseInt(old_theme);
	numThemes = parseInt(numThemes);
	
	// skip to the first theme if (a) you're on the last real theme or (b) you're on the "none" theme 
	return (old_theme != (numThemes-1)) ? (old_theme + 1) % (numThemes - 1) : (old_theme + 1) % numThemes;
}



// Older browsers need a few more tweaks, so these are declared in separate
// stylesheets which will eventually be dropped as they become obsolete.
// Given a Browser Detector object (see below), return an array of stylesheets 
// to be applied.
function getMoreStylesForOldBrowsers( bd ) {
	
	var more_stylesheets = new Array();
	var count = 0;
	
	// IE 5 and 6 on the PC
	if ( bd.browser == "IE" ) {
	   if( bd.platform.indexOf("Mac") == -1 ) {
	      if ( parseInt(bd.majorver) <= 6 ) { more_stylesheets[count++] = "ie6.css"; }
	      if ( parseInt(bd.majorver) <= 5 ) { more_stylesheets[count++] = "ie5.css"; }
	   }
	   else {
	      if ( parseInt(bd.majorver) < 6 ) {
	      	more_stylesheets[count++] = "ie5mac.css";
	      }
	   }
	}
	

	// Netscape 4
	if (bd.browser == "Netscape" && bd.majorver < 5) { more_stylesheets[count++] = "netscape4.css"; }	
	
	return more_stylesheets;
}

// The first time the page is visited during the current browser session, set a
// cookie saying the person has entered.  Check this cookie when choosing theme
// stylesheets to see if a brand new theme should be applied.
// Returns true if the cookie has already been set.
function checkAndSetOnsiteCookie() {
	
	// if the onsite value has already been set, return true
	if( document.cookie.indexOf("onsite") != -1 ) {	return true; }
	
	// otherwise, set it
	var cookie = "onsite=true";
	var path = "; path=/";
	var domain = "; domain=.thoughtcrumbs.com";
	document.cookie = cookie + path + domain;
}



function displayBook() {
	var args = displayBook.arguments;
	displayItem( "book", args[0], args[1], args[2], args[3] );
}

function displayCD() {
	var args = displayCD.arguments;
	displayItem( "cd", args[0], args[1], args[2], args[3] );
}

// Write the HTML for a book or CD.  Pulls book jacket image and links to Amazon.com.
// In the case of a CD, "author" means "artist" and "isbn" means "asin."
function displayItem() {
	
	var args = displayItem.arguments;
	var argc = args.length;
	var type, title, author, isbn, url, review;
	
	type = args[0];   // must have type of "book" or "cd"
	title = args[1];  // must have title
	if( argc > 2 ) { author = args[2]; }
	if( argc > 3 ) { 
		if( args[3].indexOf("http") == -1 ) { isbn = args[3]; }
		else { url = args[3]; }
	}
	if( argc > 4 ) { review = args[4]; }

	// HTML to be written out
	var typediv = 				"<div class='" + type + "'>";
	var isbn_link_and_image = 	"<a href='http://www.amazon.com/exec/obidos/ASIN/"+ isbn +"/considereddes-20'><img src='http://images.amazon.com/images/P/" +  isbn + ".01.THUMBZZZ.jpg' class='" + type + "cover' align='left'></a>";
	var titlespan = 			"<span class='" + type + "_title'>";
	var isbn_link = 			"<a href='http://www.amazon.com/exec/obidos/ASIN/" +  isbn + "/considereddes-20'>";
	var url_link = 				"<a href='" + url + "'>";
	var end_link = 				"</a>";
	var end_span = 				"</span>";
	var authorspan = 			" - <span class='" + type + "_author'>" + author + "</span>";
	var reviewdiv =				"<div class='" + type + "review'>" + review + "</div>";
	var end_div = 				"</div>";
	
	if (!document.layers || (typeof div_name == "undefined") ) {
		document.write(typediv);
		if( isbn ) { document.write(isbn_link_and_image); }
		document.write(titlespan);
		if( isbn ) { document.write(isbn_link); }
		else if( url ) { document.write(url_link); }
		document.write(title);
		if( isbn || url ) { document.write(end_link); }
		document.write(end_span); 
		if( author ) { document.write(authorspan); }
		if( review ) { document.write(reviewdiv); }
		document.write(end_div);
	}
	else {
		document.layers[div_name].document.write(typediv);
		if( isbn ) { document.layers[div_name].document.write(isbn_link_and_image); }
		document.layers[div_name].document.write(titlespan);
		if( isbn ) { document.layers[div_name].document.write(isbn_link); }
		else if( url ) { document.layers[div_name].document.write(url_link); }
		document.layers[div_name].document.write(title);
		if( isbn || url ) { document.layers[div_name].document.write(end_link); }
		document.layers[div_name].document.write(end_span); 
		if( author ) { document.layers[div_name].document.write(authorspan); }
		if( review ) { document.layers[div_name].document.write(reviewdiv); }
		document.layers[div_name].document.write(end_div);
	}
}

function displayYear(year) {
	document.write("<div class='year'>" + year + "</div>");
}

function setDivName(name) { 
	if( document.layers ) { div_name = name; }
}

function resetDivName() { 
	if( document.layers ) { div_name = undefined; }
}


// Write out the HTML for a website in the portfolio
function displayWebsite(abbr, name, description, linktext ) {

	if (!document.layers || (typeof div_name == "undefined") ) {
		document.write("<a name='" + abbr + "' href='web/" + abbr + "' target='_blank'><img class='website' src='web/images/" + abbr + "_small.gif' align='left' alt='View demo of " + name + "'></a>");
		document.write("<div class='item_header'>" + name + "</div>");
		document.write("<div class='item_description'>" + description + "<br />");
		document.write("<a href='web/" + abbr + "' target='_blank'>" + linktext + "</a></div>");
	}
	else {
		document.layers[div_name].document.write("<div class='item_header'>" + name + "</div>");
		document.layers[div_name].document.write("<div class='item_description'>" + description + "<br />");
		document.layers[div_name].document.write("<a href='web/" + abbr + "' target='_blank'>" + linktext + "</a></div>");
	}
}


// Write out the HTML for a multimedia project in the portfolio
function displayMultimedia(abbr, name, description, linktext ) {

	if (!document.layers || (typeof div_name == "undefined") ) {
	
	document.write("<a name='" + abbr + "' href='multimedia/" + abbr + ".html' target='_blank'><img class='multimedia' src='multimedia/images/" + abbr + "_small.jpg' align='left' alt='View more detail of " + name + "'></a>");
	document.write("<div class='item_header'>" + name + "</div>");
	document.write("<div class='item_description'>" + description + "<br />");
	document.write("<a href='multimedia/" + abbr + ".html' target='_blank'>" + linktext + "</a></div>");
	
	}
	else {
	document.layers[div_name].document.write("<div class='item_header'>" + name + "</div>");
	document.layers[div_name].document.write("<div class='item_description'>" + description + "<br />");
	document.layers[div_name].document.write("<a href='multimedia/" + abbr + ".html' target='_blank'>" + linktext + "</a></div>");
	}
}

// Write out the HTML for a graphic design project in the portfolio
function displayGraphic( abbr, name, description, linktext ) {

	if (!document.layers || (typeof div_name == "undefined") ) {
	document.write("<a name='" + abbr + "' href='graphic/" + abbr + ".html' target='_blank'><img class='graphic' src='graphic/images/" + abbr + "_small.jpg' align='left' alt='View more detail of " + name + "'></a>");
	document.write("<div class='item_header'>" + name + "</div>");
	document.write("<div class='item_description'>" + description + "<br />");
	document.write("<a href='graphic/" + abbr + ".html' target='_blank'>" + linktext + "</a></div>");
	}
	else {
	document.layers[div_name].document.write("<div class='item_header'>" + name + "</div>");
	document.layers[div_name].document.write("<div class='item_description'>" + description + "<br />");
	document.layers[div_name].document.write("<a href='graphic/" + abbr + ".html' target='_blank'>" + linktext + "</a></div>");

	}
}


// Write out the HTML for a CD cover in the portfolio
function displayPortfolioCD( abbr, name ) {

	if (!document.layers || (typeof div_name == "undefined") ) {

	document.write("<a name='" + abbr + "' href='graphic/" + abbr + ".html' target='_blank'><img class='graphic' src='graphic/images/" + abbr + "_front_small.jpg' align='left' alt='View more detail of " + name + "'><img class='graphic' src='graphic/images/" + abbr + "_back_small.jpg' align='left' alt='View more detail of " + name + "'></a>");
	document.write("<div class='item_header'>" + name + "</div>");
	document.write("<a href='graphic/" + abbr + ".html' target='_blank'>More detail</a><br />");
	document.write("<a href='../listening/compilations.shtml#" + abbr + "' target='_blank'>Song list</a>");
	}
	else {
	document.layers[div_name].document.write("<div class='item_header'>" + name + "</div>");
	document.layers[div_name].document.write("<a href='graphic/" + abbr + ".html' target='_blank'>More detail</a><br />");
	document.layers[div_name].document.write("<a href='../listening/compilations.shtml#" + abbr + "' target='_blank'>Song list</a>");

	}
}


function tagLine() {
	var title = new Array();
	
	title[0]="Just add water";
	title[1]="Because you didn't have anything to do today";
	title[2]="Meow";
	title[3]="If you're smart enough, there will always be free food";
	title[4]="Animal. No! mineral";
	title[5]="Because indecision is a virtue";
	title[6]="Where do you want to take me today?";
	title[7]="Tofu is yummy";
	title[8]="If you love me you'll buy me things";
	title[9]="Who is john galt?";
	title[10]="I can see you";
	title[11]="Word up, dog";
	title[12]="Revelling in nerdliness";
	title[13]="The revolution is coming";
	title[14]="Grammar.  it's where it's at.";
	title[15]="Two, three, cha-cha-cha";
	title[16]="Answering life's biggest questions";
	title[17]="Craving broccoli";
	title[18]="I am I Don Quixote";
	title[19]="Spreading memes like no one's business";
	title[20]="Subtly amused";
	title[21]="Making four trips to dictionary.com per day";
	title[22]="Please try not to spill anything";
	title[23]="Looking for a boy who can sing";
	title[24]="Wondering when this whole Internet thing is going to catch on";
	title[25]="Go on wit' yo bad self";
	title[26]="You can never have too many toys";
	title[27]="One coffee too many";
	title[28]="Keepin' it real";
	title[29]="Shampoo. Rinse. Repeat.";
	title[30]="Cheese and crackers for everyone";
	title[31]="Secretly holding out for ally and the biscuit";
	title[32]="You may have already won";
	title[33]="Groupie for hire";
	title[34]="Fightin' some crime";
	title[35]="No matter where you go, there you are";
	title[36]="Why did the monkey fall out of the tree?";
	title[37]="Chai is good for the soul";
	title[38]="Is it lust or love?";
	title[39]="Do you hear the music, too?";
	title[40]="525,600 minutes of ooey gooey fun";
	title[41]="Articulate and stuff";
	title[42]="Et voila";
	title[43]="Back to you, Kent";
	title[44]="Lamenting the loss of ben folds five";
	title[45]="Jude is not obscure";
	title[46]="Eek!";
	title[47]="Donde esta tu cerebro?";
	title[48]="Bring it on";
	title[49]="Tastes like chicken";
	title[50]="Play that funky music white boy";
	title[51]="A powerful alternative to maybe";
	title[52]="Behold the power of cheese";
	title[53]="There is no spoon";
	title[54]="Let's just be friends";
	title[55]="Wishing life were choreographed";
	title[56]="If i were a bell i'd be ringing";
	title[57]="I totally forgot what i was going to say";
	title[58]="Getting smarter by the minute";
	title[59]="Oldschool";

	var now = new Date();
	var seconds = now.getSeconds();	
	return title[seconds];
}





/* The following scripts were written by other people */



/*
BrowserDetector()
Parses User-Agent string into useful info.

Source: Webmonkey Code Library
(http://www.hotwired.com/webmonkey/javascript/code_library/)

Author: Richard Blaylock
Author Email: blaylock@wired.com

Usage: var bd = new BrowserDetector(navigator.userAgent);
*/


// Utility function to trim spaces from both ends of a string
function Trim(inString) {
  var retVal = "";
  var start = 0;
  while ((start < inString.length) && (inString.charAt(start) == ' ')) {
    ++start;
  }
  var end = inString.length;
  while ((end > 0) && (inString.charAt(end - 1) == ' ')) {
    --end;
  }
  retVal = inString.substring(start, end);
  return retVal;
}

function BrowserDetector(ua) {

// Defaults
  this.browser = "Unknown";
  this.platform = "Unknown";
  this.version = "";
  this.majorver = "";
  this.minorver = "";

  uaLen = ua.length;

// ##### Split into stuff before parens and stuff in parens
  var preparens = "";
  var parenthesized = "";

  i = ua.indexOf("(");
  if (i >= 0) {
    preparens = Trim(ua.substring(0,i));
        parenthesized = ua.substring(i+1, uaLen);
        j = parenthesized.indexOf(")");
        if (j >= 0) {
          parenthesized = parenthesized.substring(0, j);
        }
  }
  else {
    preparens = ua;
  }

// ##### First assume browser and version are in preparens
// ##### override later if we find them in the parenthesized stuff
  var browVer = preparens;

  var tokens = parenthesized.split(";");
  var token = "";
// # Now go through parenthesized tokens
  for (var i=0; i < tokens.length; i++) {
    token = Trim(tokens[i]);
        //## compatible - might want to reset from Netscape
        if (token == "compatible") {
          //## One might want to reset browVer to a null string
          //## here, but instead, we'll assume that if we don't
          //## find out otherwise, then it really is Mozilla
          //## (or whatever showed up before the parens).
        //## browser - try for Opera or IE
    }
        else if (token.indexOf("MSIE") >= 0) {
      browVer = token;
    }
    else if (token.indexOf("Opera") >= 0) {
      browVer = token;
    }
        //'## platform - try for X11, SunOS, Win, Mac, PPC
    else if ((token.indexOf("X11") >= 0) || (token.indexOf("SunOS") >= 0) ||
(token.indexOf("Linux") >= 0)) {
      this.platform = "Unix";
        }
    else if (token.indexOf("Win") >= 0) {
      this.platform = token;
        }
    else if ((token.indexOf("Mac") >= 0) || (token.indexOf("PPC") >= 0)) {
      this.platform = token;
        }
  }

  var msieIndex = browVer.indexOf("MSIE");
  if (msieIndex >= 0) {
    browVer = browVer.substring(msieIndex, browVer.length);
  }

  var leftover = "";
  if (browVer.substring(0, "Mozilla".length) == "Mozilla") {
    this.browser = "Netscape";
        leftover = browVer.substring("Mozilla".length+1, browVer.length);
  }
  else if (browVer.substring(0, "Lynx".length) == "Lynx") {
    this.browser = "Lynx";
        leftover = browVer.substring("Lynx".length+1, browVer.length);
  }
  else if (browVer.substring(0, "MSIE".length) == "MSIE") {
    this.browser = "IE";
    leftover = browVer.substring("MSIE".length+1, browVer.length);
  }
  else if (browVer.substring(0, "Microsoft Internet Explorer".length) ==
"Microsoft Internet Explorer") {
    this.browser = "IE"
        leftover = browVer.substring("Microsoft Internet Explorer".length+1,
browVer.length);
  }
  else if (browVer.substring(0, "Opera".length) == "Opera") {
    this.browser = "Opera"
    leftover = browVer.substring("Opera".length+1, browVer.length);
  }

  leftover = Trim(leftover);

  // # Try to get version info out of leftover stuff
  i = leftover.indexOf(" ");
  if (i >= 0) {
    this.version = leftover.substring(0, i);
  }
  else
  {
    this.version = leftover;
  }
  j = this.version.indexOf(".");
  if (j >= 0) {
    this.majorver = this.version.substring(0,j);
    this.minorver = this.version.substring(j+1, this.version.length);
  }
  else {
    this.majorver = this.version;
  }


} // function BrowserCap




// Derived from the Webmonkey Code Library -- fixes the Netscape 4 resize bug
function netscape4_css_fix() {
 if (document.n4fix.netscape4_css_fix.initWindowWidth != window.innerWidth || document.n4fix.netscape4_css_fix.initWindowHeight != window.innerHeight) {
    document.location = document.location;
  }
}

function netscape4_css_fix_check() {
  if ((navigator.appName == 'Netscape') && (parseInt(navigator.appVersion) == 4)) {
    if (typeof document.n4fix == 'undefined'){ document.n4fix = new Object; }
   
    document.n4fix.netscape4_css_fix = new Object;
    document.n4fix.netscape4_css_fix.initWindowWidth = window.innerWidth;
    document.n4fix.netscape4_css_fix.initWindowHeight = window.innerHeight;
    window.onresize = netscape4_css_fix;
  
  }
}




