document.styleSheets[0].cssRules[1].style.visibility = "hidden";

var imagesCount = 35;
var currentIndex = 0;
var front;
var rear;
var newcat = new Image();
var firstTimeThru = true;
var isRunning = true;
var isFading = true;
var clickCount = 0;
var step = 5;
var interval = 40;
var timeout = 5000;
var headerdiv;
var preloaded = false;
var timeoutId;
var watchdogId = 0;
var pause = 3000;
var pauseTimer;

var images = new Array(imagesCount);
images[0] = new Array("http://merecat.org/jca/clothesline/DSC_5490.jpg","Click to pause");
images[1] = new Array("http://merecat.org/jca/clothesline/DSC_5500.jpg","Click to pause");
images[2] = new Array("http://merecat.org/jca/clothesline/DSC_5502.jpg","Click to pause");
images[3] = new Array("http://merecat.org/jca/clothesline/DSC_5511.jpg","Click to pause");
images[4] = new Array("http://merecat.org/jca/clothesline/DSC_5513.jpg","Click to pause");
images[5] = new Array("http://merecat.org/jca/clothesline/DSC_5518.jpg","Click to pause");
images[6] = new Array("http://merecat.org/jca/clothesline/DSC_5529.jpg","Click to pause");
images[7] = new Array("http://merecat.org/jca/clothesline/DSC_5530.jpg","Click to pause");
images[8] = new Array("http://merecat.org/jca/clothesline/DSC_5535.jpg","Click to pause");
images[9] = new Array("http://merecat.org/jca/clothesline/DSC_5539.jpg","Click to pause");
images[10] = new Array("http://merecat.org/jca/clothesline/DSC_5541.jpg","Click to pause");
images[11] = new Array("http://merecat.org/jca/clothesline/DSC_5543.jpg","Click to pause");
images[12] = new Array("http://merecat.org/jca/clothesline/DSC_5545.jpg","Click to pause");
images[13] = new Array("http://merecat.org/jca/clothesline/DSC_5550.jpg","Click to pause");
images[14] = new Array("http://merecat.org/jca/clothesline/DSC_5552.jpg","Click to pause");
images[15] = new Array("http://merecat.org/jca/clothesline/DSC_5556.jpg","Click to pause");
images[16] = new Array("http://merecat.org/jca/clothesline/DSC_5769.jpg","Click to pause");
images[17] = new Array("http://merecat.org/jca/clothesline/DSC_5770.jpg","Click to pause");
images[18] = new Array("http://merecat.org/jca/clothesline/DSC_5776.jpg","Click to pause");
images[19] = new Array("http://merecat.org/jca/clothesline/DSC_5779.jpg","Click to pause");
images[20] = new Array("http://merecat.org/jca/clothesline/DSC_5782.jpg","Click to pause");
images[21] = new Array("http://merecat.org/jca/clothesline/DSC_5787.jpg","Click to pause");
images[22] = new Array("http://merecat.org/jca/clothesline/DSC_5790.jpg","Click to pause");
images[23] = new Array("http://merecat.org/jca/clothesline/DSC_5791.jpg","Click to pause");
images[24] = new Array("http://merecat.org/jca/clothesline/DSC_5793.jpg","Click to pause");
images[25] = new Array("http://merecat.org/jca/clothesline/DSC_5794.jpg","Click to pause");
images[26] = new Array("http://merecat.org/jca/clothesline/DSC_5799.jpg","Click to pause");
images[27] = new Array("http://merecat.org/jca/clothesline/DSC_5800.jpg","Click to pause");
images[28] = new Array("http://merecat.org/jca/clothesline/DSC_5802.jpg","Click to pause");
images[29] = new Array("http://merecat.org/jca/clothesline/DSC_5804.jpg","Click to pause");
images[30] = new Array("http://merecat.org/jca/clothesline/DSC_5806.jpg","Click to pause");
images[31] = new Array("http://merecat.org/jca/clothesline/DSC_5810.jpg","Click to pause");
images[32] = new Array("http://merecat.org/jca/clothesline/DSC_5811.jpg","Click to pause");
images[33] = new Array("http://merecat.org/jca/clothesline/DSC_5813.jpg","Click to pause");
images[34] = new Array("http://merecat.org/jca/clothesline/DSC_5819.jpg","Click to pause");
function shuffle()
{
	images.sort(compare);
	currentIndex = 0;
}

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

function preloadImage()
{
	if (currentIndex == imagesCount)
	{
	    var currentImage = images[currentIndex - 1][0];
	    var counts = 0;
	    
	    do {
			shuffle();
		} while (currentImage === images[currentIndex][0])
	}
	newcat.onload = setPreloaded;
	newcat.src = images[currentIndex][0];
}

function setPreloaded() {
	preloaded = true;
}

function delay()
{
	preloadImage();
	if (! firstTimeThru) {
		window.setTimeout("setCat()",pause);
	} else {
		setCat();
	}
}

function checkPreload()
{
	if (! preloaded)
	{
		preloaded = true;
		setCat();
	}
}

function setCat() {
if (isRunning) {
	if (preloaded) {
		if (firstTimeThru) {
		    isFading = true;
			// set up main image
			setOpacity(rear, 0);
			front.src = newcat.src;
			//front.setAttribute("alt",images[currentIndex][1]);
			front.setAttribute("title",images[currentIndex][1]);
			setOpacity(front, 0);
			document.styleSheets[0].cssRules[1].style.visibility = "visible";
			crossfade();
		} else {
		    isFading = true;
			rear.src = newcat.src; // this causes a flash under load
			crossfade();
		}
	} else {
		window.setTimeout("setCat()",100);
	}
} else {
	//hang out
	pauseTimer = window.setTimeout(function() {setCat();},250);
}
}

function crossfade() {
	
	if (firstTimeThru) {
		window.setTimeout(function() {fadeIn(front,0);},20);
	} else {
		window.setTimeout(function() {fadeOut(front,100);},0);
		window.setTimeout(function() {fadeIn(rear,0);},20);
	}
}

function switchSrc () {
	if (! firstTimeThru)
	{
		front.src = rear.src;
		//front.setAttribute("alt",images[currentIndex][1]);
		front.setAttribute("title",images[currentIndex][1]);
		//rear.removeAttribute("src");
		setOpacity(front,100);
		setOpacity(rear,0);
		isFading = false;
	}
	isFading = false;
	firstTimeThru = false;
	//clickCount = 0;
	currentIndex++;
	//window.setTimeout("preloadImage()",0);
	isFading = false;
	//clickCount = 0;
	delay();
	//isRunning = true;
}

function switchImage() {
	if (isRunning) {
		isRunning = false;
		front.setAttribute("title","Click to resume");
	} else {
		isRunning = true;
	}
}

function firstTime() {
	//headerdiv = document.getElementById("header");
	front = document.getElementById("front");
	rear = document.getElementById("rear");
	front.onclick = function() {
		switchImage();
	}
	shuffle();
	delay();
}


// 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(objId, opacity);
		opacity += step;
		window.setTimeout(function() {fadeIn(objId,opacity);}, interval);
	} else {
		isFading = false;
		//window.setTimeout("switchSrc()",100);
		switchSrc();
	}
}

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

window.onload = firstTime;
