/* The primary impetus for this script was to provide a default cat picture, as
the original JavaScript this replaces made no allowance for browsers
with JavaScript disabled; they saw no picture at all.
I had seen my first JavaScript crossfade at slayeroffice 
(http://slayeroffice.com/code/imageCrossFade/xfade2.html)
and was impressed, so I thought, what the heck, while I'm at it...
Grateful acknowledgement to some really smart people whose code I adapted 
or stole outright, including:
Richard Rutter http://clagnut.com/sandbox/imagefades/
Dean Edwards http://dean.edwards.name/weblog/2006/06/again/?full
Peter-Paul Koch http://www.quirksmode.org/js/contents.html
brothercake.com
*/

var imagesCount = 21;
var currentIndex = 0;
var imageTag;
var imageTag2;
var newcat;
var firstTimeThru = true;
var isFading = false;
var clickCount = 0;
var step = 5;
var interval = 30;
var listitems;
var randomURLTag;

var images = new Array(imagesCount);
images[0] = new Array("/cats/emma-new.jpg","Emma");
images[1] = new Array("/cats/isaiah-new.jpg","Isaiah");
images[2] = new Array("/cats/kenny-new.jpg","Kenny");
images[3] = new Array("/cats/woody2-new.jpg","Woody");
images[4] = new Array("/cats/orange-new.jpg","Orange kitty");
images[5] = new Array("/cats/samson1-new.jpg","Samson");
images[6] = new Array("/cats/samson2-new.jpg","Samson");
images[7] = new Array("/cats/dylan-new.jpg","Dylan");
images[8] = new Array("/cats/cisco-new.jpg","Cisco");
images[9] = new Array("/cats/julius2-new.jpg","Julius");
images[10] = new Array("/cats/julius3-new.jpg","Julius");
images[11] = new Array("/cats/julius4-new.jpg","Julius");
images[12] = new Array("/cats/tabitha.jpg","Tabitha");
images[13] = new Array("/cats/snuggles-new.jpg","Snuggles");
images[14] = new Array("/cats/abby.jpg","Abby");
images[15] = new Array("/cats/woody1-new.jpg","Woody");
images[16] = new Array("/cats/julius7.jpg","Julius");
images[17] = new Array("/cats/julius-isaiah.jpg","Julius and Isaiah");
images[18] = new Array("/cats/hunter1.jpg","Hunter");
images[19] = new Array("/cats/hunter2.jpg","Hunter");
images[20] = new Array("/cats/logan1.jpg","Logan");

function removeDefaultCat () {
	if (document.getElementById) {
		// set the visibility of the default cat pic to hidden so it doensn't "flash"
		document.styleSheets[0].cssRules[1].styleSheet.cssRules[2].style.visibility = "hidden";
		var headerdiv = document.getElementById("header");
		var guestcatimg = document.getElementById("guestcat");
		if (headerdiv && guestcatimg) {
			try {
				firstTime();
			} catch(errMsg) {
				window.setTimeout("removeDefaultCat()",100);
			}
		} else {
			window.setTimeout("removeDefaultCat()",100);
		}
	} else {
		window.setTimeout("removeDefaultCat()",100);
	}
}

function shuffle()
{
	images.sort(compare);
	currentIndex = 0;
}

function compare(a, b)
{
  if (Math.random() * 2 > 1) { return 1; }
  else { return -1; }
}

function preloadImage() {
	isFading = true;
	if (currentIndex == imagesCount)
	{
		shuffle();
	}
	newcat = new Image();
	newcat.onload = setCat;
	newcat.src = images[currentIndex][0];
}

function setCat() {
	if (firstTimeThru) {
		// set up main image
		setOpacity(imageTag2, 0);
		imageTag.src = newcat.src;
		imageTag.setAttribute("alt",images[currentIndex][1]);
		imageTag.setAttribute("title",images[currentIndex][1] + ". Click for more cats. Collect all "+imagesCount+"!");
		setOpacity(imageTag, 0);
		crossfade();
	} else {
		// remove utility image (behind main image), load it with new image and re-insert it
		var headerdiv = document.getElementById("header");
		var guestcat2img = document.getElementById("guestcat2");
		if (headerdiv && guestcat2img) {
			headerdiv.removeChild(guestcat2img);
		}
		imageTag2 = document.createElement("img");
		setOpacity(imageTag2, 0);
		imageTag2.setAttribute("id","guestcat2");
		imageTag2.src = newcat.src; // this causes a flash under load
		var guestcatimg = document.getElementById("guestcat");
		headerdiv.insertBefore(imageTag2,guestcatimg);
		crossfade();
	}
}

function crossfade() {
	if (firstTimeThru) {
		window.setTimeout("fadeIn('guestcat',0)",20);
		currentIndex++;
	} else {
		//window.setTimeout("fadeOut('guestcat',100)",0);
		//window.setTimeout("fadeIn('guestcat2',0)",20);
		window.setTimeout("crossfader(100, 0)",0);
		window.setTimeout("switchSrc()",1600);
	}
}

function switchSrc () {
	imageTag.src = imageTag2.src;
	imageTag.setAttribute("alt",images[currentIndex][1]);
	imageTag.setAttribute("title",images[currentIndex][1]+". Click for more cats. Collect all "+imagesCount+"!");
	imageTag2.removeAttribute("src");
	setOpacity(imageTag,100);
	setOpacity(imageTag2,0);
	isFading = false;
	clickCount = 0;
	currentIndex++;
}

function switchImage() {
	clickCount++;
	if (clickCount == 1) {
		firstTimeThru = false;
		if (!isFading && clickCount > 0) {
			isFading = true;
			preloadImage();
		}
	}
}

function createSrc1() {
	if (document.createElement) {
		if (document.getElementById) {
			imageTag2 = document.createElement("img");
			setOpacity(imageTag2, 0);
			imageTag2.setAttribute("id","guestcat2");
			var headerdiv = document.getElementById("header");
			var menubgdiv = document.getElementById("menubg");
			headerdiv.insertBefore(imageTag2,menubgdiv);
			document.styleSheets[0].cssRules[1].styleSheet.cssRules[2].style.visibility = "visible";
		}
	}
}

function createSrc2() {
	if (document.createElement) {
		if (document.getElementById) {
			imageTag = document.getElementById("guestcat");//document.createElement("img");
			setOpacity(imageTag, 0);
//			imageTag.setAttribute("id","guestcat");
			imageTag.onclick = function() {
				switchImage();
			}
			var headerdiv = document.getElementById("header");
			var menubgdiv = document.getElementById("menubg");
			headerdiv.insertBefore(imageTag,menubgdiv);
			document.styleSheets[0].cssRules[1].styleSheet.cssRules[2].style.visibility = "visible";
		}
	}
}

function firstTime() {
	shuffle();
	createSrc1();
	createSrc2();
	preloadImage();
}


// clagnut.com
function setOpacity(obj, opacity) {

	opacity = (opacity == 100)?99.999:opacity;

	// IE/Win
	obj.style.filter = "alpha(opacity="+opacity+")";
	
	// Safari<1.2, Konqueror
	obj.style.KHTMLOpacity = opacity/100;
	
	// Older Mozilla and Firefox
	obj.style.MozOpacity = opacity/100;
	
	// Safari 1.2, newer Firefox and Mozilla, CSS3
	obj.style.opacity = opacity/100;
}

// clagnut.com
function fadeIn(objId,opacity) {
	var obj = document.getElementById(objId);
	if (opacity <= 100) {
		setOpacity(obj, opacity);
		opacity += step;
		window.setTimeout("fadeIn('"+objId+"',"+opacity+")", interval);		
	} else {
		isFading = false;
	}
}

// clagnut.com
function fadeOut(objId,opacity) {
	if (document.getElementById) {
		obj = document.getElementById(objId);
		if (opacity >= 0) {
			setOpacity(obj, opacity);
			opacity -= step;
			window.setTimeout("fadeOut('"+objId+"',"+opacity+")", interval);

		}
	}
}
//window.setTimeout("fadeOut('guestcat',100)",0);
//window.setTimeout("fadeIn('guestcat2',0)",20);
		
function crossfader(opacity1, opacity2)
{
	if (opacity1 >= 0) {
		setOpacity(imageTag, opacity1);
		opacity1 -= step;
	}
	if (opacity2 <= 100) {
		setOpacity(imageTag2, opacity2);
		opacity2 += step;
	}
	window.setTimeout("crossfader("+opacity1+", "+opacity2+")", interval);
}

function randomURL() {
	//return a random URL from the blogroll
	if (Math.random) {
		var validItem = false;
		while (validItem == false)
		{
			var ranNum = Math.round(Math.random()*(listitems.length));
			if (listitems[ranNum].parentNode.getAttribute("class") == "blogrollgroup") //it's not in the blogroll
			{
				if (listitems[ranNum].childNodes[0].href != undefined) //it's a "heading"
				{
					validItem = true;
				}
			}
		}
		return listitems[ranNum].childNodes[0].href;
	}
}

function calladdsrc() {
	listitems = document.getElementsByTagName("li");
	var blogrollTag = document.getElementById("blogrollIntro");
	randomURLTag = document.createElement("a");
	randomURLTag.href = randomURL();
	randomURLTag.appendChild(document.createTextNode(" Click here to visit a random link from the blogroll."));
	randomURLTag.onclick = function () {
	setRandomHref();
	return false;}
	blogrollTag.appendChild(randomURLTag);
	
	window.setTimeout("addsrc()",5000);
}

function setRandomHref() {
	var currentHref = randomURLTag.href;

	randomURLTag.href = randomURL();

	window.location.href = currentHref;

}

function addsrc() {
	if (document.getElementById) {
		var footerdiv = document.getElementById("footer");
		var scriptTag = document.createElement("script");
		scriptTag.src = "com/footer.js";
		scriptTag.type = "text/javascript";
		footerdiv.appendChild(scriptTag);
	}
}

window.onload = calladdsrc;

// dean.edwards.name
// setTimeout gives them enough time, otherwise removeDefaultCat runs twice
/* for Safari and OmniWeb */
// default cat still flashes briefly with OmniWeb
if (/KHTML|OmniWeb|Opera/i.test(navigator.userAgent)) { // sniff
	window.setTimeout("removeDefaultCat();",0);
/* for Firefox/Opera - getting flashes with Firefox, too */
//} else if (document.addEventListener) {
//	document.addEventListener("DOMContentLoaded", removeDefaultCat, false);
} else {
/* for other browsers */
	removeDefaultCat();
}
