function WindowOnload(f) {
	var prev = window.onload;
	window.onload = function() { if (prev) prev(); f(); }
}

// Browser gestalt

Browser = {
 id : function() {return navigator.userAgent.toLowerCase();},
 ver : function() {
	var x = 0;
 	if (this.isIE()) {
		x = this.id().indexOf("msie ");
		ver = parseFloat(this.id().substring(x + 5));
	} else {
	  x = this.id().length - 1;
	  while (x >= 0 && this.id().charAt(x) != ' ' && this.id().charAt(x) != '/')
		  x--;
	  if (x >= 0)
		  ver = parseFloat(this.id().substring(x + 1));
	  else
		  ver = parseFloat(navigator.appVersion);
	}
  	return ver;
 },

 mozVer : function() {return parseFloat(navigator.appVersion);},
 isAdvanced : function() {return Boolean(this.hasDOM() && this.hasCreateElement());},

 isWindows : function() {return this.id().indexOf("win")>0;},
 isMac : function() {return this.id().indexOf("mac")>0;},

 isIE : function() {return this.id().indexOf("msie")>0 && !this.isOpera();},
 isFirefox : function() {return this.id().indexOf("firefox")>0;},
 isGecko : function() {return this.id().indexOf("gecko")>0;},
 isOpera : function() {return this.id().indexOf("opera")>0;},
 isSafari : function() {return this.id().indexOf("safari")>0;},
 isOmniWeb : function() {return this.id().indexOf("omniweb")>0;},
 isKonqueror : function() {return this.id().indexOf("konqueror")>0;},
 isWebTV : function() {return this.id().indexOf("webtv")>0;},
 isNetscape : function() {return !this.isGecko() && this.id().indexOf("compatible")<0;},
 isMozilla : function() {return this.isGecko() && !this.isFirefox() && !this.isSafari() && !this.isNetscape();},

 hasLayers : function() {return Boolean(document.layers);},
 hasAll : function() {return Boolean(document.all);},
 hasImages : function() {return Boolean(document.images);},
 hasForms : function() {return Boolean(document.forms);},
 hasCreateElement : function() {return Boolean(document.createElement);},
 hasCreateDoc : function() {return Boolean(document.implementation && document.implementation.createDocument);},
 hasRegExp : function() {return Boolean(window.RegExp);},
 hasDOM : function() {return Boolean(document.getElementById);},
 hasGEBI : function() {return this.hasDOM();},
 hasGEBTN : function() {return Boolean(document.getElementsByTagName);},
 hasActiveX : function() {return Boolean(window.ActiveXObject);}
};

function findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

// Get a style object for item by id

function getStyleObj(id) {
 var obj = findObj(id);
 if (obj) {
  if ((Browser.hasDOM() || Browser.hasAll()))
   return obj.style;
  else if (Browser.hasLayers())
   return obj;
 }
 alert("JavaScript error: could not find style object for '" + id + "'");
 return null;
}

// Set opacity (%) on an object by id or obj

function setOpacity(pct, id, obj) {
 if (id != '')
 	obj = findObj(id);
 if (obj.filters)
  obj.filters("alpha").opacity = pct;
 else if (obj.style) {
  var amt = pct / 100;
  if (amt > .99999)
  	amt = .99999;
  obj.style.MozOpacity = amt;
  obj.style.khtmlOpacity = amt;
  obj.style.opacity = amt;
 }
}

function randomvalue(low, high) {
    return Math.floor(Math.random() * (1 + high - low) + low);
}


//=============================================================================

function ImagePreloader(images,callback)
{
		// store the callback
		this.callback = callback;

		// initialize internal state.
		this.nLoaded = 0;
		this.nProcessed = 0;
		this.aImages = new Array;

		// record the number of images.
		this.nImages = images.length;

		// for each image, call preload()
		for ( var i = 0; i < images.length; i++ ) 
			this.preload(images[i]);
}
ImagePreloader.prototype.preload = function(image)
{
		// create new Image object and add to array
		var oImage = new Image;
		this.aImages.push(oImage);
		
		// set up event handlers for the Image object
		oImage.onload = ImagePreloader.prototype.onload;
		oImage.onerror = ImagePreloader.prototype.onerror;
		oImage.onabort = ImagePreloader.prototype.onabort;
		
		// assign pointer back to this.
		oImage.oImagePreloader = this;
		oImage.bLoaded = false;
		oImage.source = image;
		
		// assign the .src property of the Image object
		oImage.src = image;
}
ImagePreloader.prototype.onComplete = function()
{
		this.nProcessed++;
		if ( this.nProcessed == this.nImages )
		   this.callback(this.aImages);
}

ImagePreloader.prototype.onload = function()
{
		this.bLoaded = true;
		this.oImagePreloader.nLoaded++;
		this.oImagePreloader.onComplete();
}
ImagePreloader.prototype.onerror = function()
{
		this.bError = true;
		this.oImagePreloader.onComplete();
}
ImagePreloader.prototype.onabort = function()
{
		this.bAbort = true;
		this.oImagePreloader.onComplete();
}


//
// Page Initializers
//

var	photoCount = 5;
var	fObj = new Array;
var firstImg;
var holdStill = null;

function photoObject() {
	this.image = new Image;
	this.opacity = 0;
	this.state = 0;
	this.timer = null;
}

function initHome() {
	for (var n = 1; n <= photoCount; ++n) {
		fObj[n] = new photoObject();
		fObj[n].image = findObj('photo' + n);
		fObj[n].opacity = 0;
	}

	firstImg = randomvalue(1, photoCount);
	var imgs = ['pix/photo' + firstImg + '.jpg', 'pix/depressor.gif'];

	if (Browser.isSafari()) {
		var mac = new Image;
		mac.src = imgs[0];
		fadeFirst([mac]);
	} else
		var ip = new ImagePreloader(imgs, fadeFirst);
}

function fadeFirst(aImages) {
	fObj[firstImg].image.src = aImages[0].src;
	photo(firstImg, 1);
	for (var n = 1; n <= photoCount; ++n)
		if (n != firstImg)
			fObj[n].image.src = 'pix/photo' + n + '.jpg';
}

function photo(n, wait) {
	clearTimeout(holdStill);
	if (!wait) {
		holdStill = setTimeout("photo(" + n + ",1)", 250);
		return;
	}

	// fade out the others
	for (var j = 1; j <= photoCount; j++)
		if (n != j && fObj[j].opacity)
		   	fade(j, 1, 0);

	fade(n, 1, 1);
}

function fade(n, start, state) {
	var o = fObj[n];

	if (start) {
		clearInterval(o.timer);
		o.state = state;
		o.timer = setInterval('fade(' + n + ')', 30);
	}

	var opac = o.opacity + (o.state ? 10 : -10);
	if (opac < 0) opac = 0;
	if (opac > 100) opac = 100;
	setOpacity(o.opacity = opac, '', o.image);
	if (!opac || opac == 100)
		clearInterval(o.timer);
}


//
// Photo div fader
//


var cube_imgs = new Array;

function fade_in_cube(n, opac, fade_speed, start_delay) {
	if (!start_delay) {
		opac += fade_speed;
		setOpacity(opac, '', cube_imgs[n]);
	}
	if (opac <= (100 - fade_speed))
		setTimeout("fade_in_cube(" + n + "," + opac + "," + fade_speed + ",0)", start_delay ? start_delay : 20);
}

function fade_cubes() {
	cube_imgs = document.getElementById('left_photos').getElementsByTagName('div');
	for (i = 0; i < cube_imgs.length; i++)
		fade_in_cube(i, 0, 3 /* randomvalue(1, 4) */, (i + 1) * 300 /*randomvalue(0, 1000)*/);
}

function fade_photo() {
	cube_imgs = document.getElementById('photo').getElementsByTagName('img');
	for (i = 0; i < cube_imgs.length; i++)
		fade_in_cube(i, 0, 5, 0);
}


//
// Admin mode
//

function delset(url, installed) {
	if (confirm('Are you sure you wish to delete the "' + url + '" proof set?'))
		document.location = '?a=delset&dn=' + escape(url) + '&installed=' + installed;
}
