/**
Dynamic Quote Changer        [Ver 0.9 2003-11-17]
Written by Greg Wogan-Browne [wogan@iinet.net.au]

This script dynamically changes text for an element, typically a DIV, in a web
site. It is primarily intended for showing off a whole bunch of quotes, etc.
Among the advanced features of this script are the ability to fade the quote
in and out (only supported on IE and Mozilla) and also to control where the
line breaks appear in the quote if there is not enough room to display the
entire quote on one line.

Quotes are grabbed from the array 'quotes', defined directly below this
comment. This array can hold both string literals and arrays of string
literals. Strings by themselves indicate to the script that you do not care
where the line break appears (if one is needed), let the browser handle it.
However, using an array of strings means that you do not want line breaks
to occur in the strings themselves, only in the spaces between strings.
Ugh, I could have explained that better. Lets try an example:
var quotes = [
	"This is quote one. No special line break handling.",
	["A line break will appear here", "this is after the line break"],
	["This string will not have a line break."],
	"Another normal \"string\", with 'quotes' embedded."
];
Hopefully that'll set you right.

Microsoft Internet Explorer notes:
 * Fading won't work in IE unless the element has 'Layout'. This is such a
   fucking ugly problem with IE. I could rant for days. Anyway, this means
   elements like DIV's and SPAN's won't fade out unless they have width or
   height specified in their style. Table cells and other such block elements
   do work however. I'm so seriously.
   More Info: http://dean.edwards.name/IE7/notes/#layout

Todo list:
 * Create function to allow pausing/unpausing of quote rotation.
 * Create function to set specific quote dynamically (from list or custom).
 * Add more persistance functionality (using cookies):
   + Allow saving of one custom quote.
   + Remember last quote.
   + Remember paused state.
 */

var quotes = [
	["Keep him tied, It makes him well,", "He's getting better, Can't you tell?"], // Metallica, Welcome Home (Sanitarium)
	["Death is my sect, Guess my religion"], // Ice-T, Colors
	["Bred to kill, Not to care;", "Do just as we say"], // Metallica, Disposable Heroes
	["No minutes to rest,", "No moments to pray"], // Rage Against The Machine, Maria
	["Let the whole world look in,", "Who cares who sees anything?"], // Deftones
	["No Hope = No Fear"], // Soulfly
	["Your law's the flaw upon this nation,", "Your lies despised by generations"], // Machine Head
	["Death is fine, Give me mine"], // Slipknot
	["I'd rather kill myself", "than put you through the pain again"], // Sevendust
	["Cause I'm through when the two", "hits the six and it's summer"], // Deftones
	["You can't see California", "without Marlon Brando's eyes"], // Slipknot
	["You can't kill me 'cause", "I'm already inside you"], // Slipknot
	["Only one of us walks away"], // Slipknot
	["Inflict strain upon the structure,", "Collapsing below my pressure"], // Fear Factory, Edgecrusher
	["I have become one with your lies"], // Fear Factory
	["Fuck integrity"], // Spineshank
	["I'm gonna die standing upon my feet,", "You're gonna die grovelling", "on your knees"], // Machine Head
	["The more intense the sense of ignorance,", "The more intense then is the pain"], // Machine Head
	["So glad that you could be so blind"], // Machine Head, I'm Your God Now
	["Let freedom ring with a shotgun blast"], // Machine Head
	["I'll be the trembling in your breath,", "Trickle of blood upon your flesh"], // Machine Head
	["Dark bodies floating in darkness"], // Fear Factory, Invisible Wounds (Dark Bodies)
	["Can't take me apart!"], // Fear Factory, Linchpin
	["Behind tired eyes the demons stir,", "The tears go uncried"], // Snot, The Box
	["“I think he should have known better”"], // Corrosion Of Conformity, Dirty Windows
	["All hell can't stop us now"], // Rage Against The Machine?
	["Where is your saviour now?"], // Fear Factory
	["What the fuck is different", "I can't believe I'm doing this"], // Slipknot
	["Damn it man I knew it was a mistake"], // Slipknot
	["Said no one to deliver you", "from your ignorance"], // Corrosion Of Conformity, Deliverance
	["I cried when angels deserve to die"], // System Of A Down, Chop Suey
	["Take what you want,", "it just might set you free"], // Corrosion Of Conformity, Take What You Want
	["Pick her up and put her down, the crowd", "screams aloud “She's not breathing”"], // Corrosion Of Conformity, Shelter
	["You know that I don't love you,", "I never did"], // Muse
	["I think I heard a shot"], // Rage Against The Machine
	["“Fear me! Fear me!” I cried, but no one did", "'cause they were dead"], // Nick Cave & The Bad Seeds
	["Another slap in the face,", "I know this time of the year", "is a revillusion"], // Tantric, Revillusion
	["Dark night of my soul"], // Fear Factory, Timelessness
	["Can't happen to me,", "Won't do it to me"], // Down, Ghosts Along The Mississippi
	["If it hurts to stay then go,", "If you go then don't go far"], // Medication, Underground
	["Got no time for a damage case!"], // Motorhead, Damage Case
	["Crawling on dignity,", "That which you do to me"], // American Head Charge, Song For The Suspect
	["That's all she wanted to hear"], // Corrosion Of Conformity
	["This house is clean, baby,", "This house is clean!"], // Metallica, Dirty Window
	["This moment is the rest of your life,","I'm coming at ya with a kitchen knife"], // Lollipop Lust Kill, Bury You
	["What do you want me to be?","You treat me like a disease"], // Lollipop Lust Kill, Like A Disease
	["Too much war, and not enough blood…"], // Full Scale
	["Movers, shakers, money makers, deal breakers and dogs,","In this game you've got to lose a couple of your pawns"], // Clutch, Power Player
	["Engineer the future now. Damn tomorrow, future now!","Throw the switches, prime the charge,","Yesterday's for mice and gods."] // Clutch, Mice & Gods
];

quotes.id      = 'quote'; // id of the element that contains the quote.
quotes.delay   = 10000;   // time a quote is shown for, in ms.
quotes.current = 0;       // index of the current quote.
quotes.timer   = null;    // id of current timeout.

// Prefer to use DOM 2 Events, that way other onload handlers will also execute
if(document.implementation && document.implementation.hasFeature
	&& document.implementation.hasFeature('Events','2.0')) {
	window.addEventListener('load', function(event) {
		fade(quotes.id, 1, true);
	}, true);
	window.addEventListener('unload', function(event) {
		window.clearTimeout(quotes.timer);
	}, true);
} else {
	window.onload = function(event) { fade(quotes.id, 1, true); };
	window.onunload = function(event) { window.clearTimeout(quotes.timer); };
}

function fade(name, opac, out) {
	quotes.timer = null;
	var element = document.getElementById(name);
	if(!element)
		throw 'Script Error: Element with id "'+ name +'" not found!';

	if(element.style.opacity != null)			// Standard
		element.style.opacity = opac;
	else if(element.style.MozOpacity != null)	// Early Mozilla
		element.style.MozOpacity = opac == 1 ? 0.99 : opac; // Older mozilla browsers would flicker when setting opac == 1
	else if(element.style.filter != null)		// Internet Explorer
		element.runtimeStyle.filter = 'alpha(opacity=' + Math.floor(100*opac) +')';

	if(!out && opac >= 1) {
		quotes.timer = window.setTimeout('fade(\''+name+'\',1,true);', quotes.delay);
		return;
	} else if(out && opac <= 0) {
		var i = quotes.current;
		if(quotes.length > 1)
			while(i == quotes.current)
				i = Math.floor(Math.random()*quotes.length);
		if(element.firstChild)
			element.replaceChild(createQuote(quotes[i]), element.firstChild);
		else
			element.appendChild(createQuote(quotes[i]));
		quotes.current = i;
		out = false;
	}

	if(out)      opac -= 0.05;
	else         opac += 0.05;
	if(opac < 0) opac = 0;
	if(opac > 1) opac = 1;
	
	quotes.timer = window.setTimeout('fade(\''+name+'\','+opac+','+out+');', 40);
}

function makeSpan(str, nowrap) {
	var span = document.createElement('span');
	span.appendChild(document.createTextNode(str));
	if(nowrap)
		span.style.whiteSpace = 'nowrap';
	return span;
}

function createQuote(strs) {
	var div = document.createElement('div');
	if(typeof strs == 'string')
		div.appendChild(makeSpan(strs, false));
	else if(strs.length)
		for(var c=0 ; c<strs.length ; c++) {
			div.appendChild(makeSpan(strs[c], true));
			div.appendChild(document.createTextNode(" "));
		}
	return div;
}

