/*
Handle hovers for images, including preloading
$Id: mo.js,v 1.1 2003/03/03 12:19:30 joris Exp $
*/

/*
Switch image from foo0.bar to foo1.bar, or back.
*/
function init() {
	if (TransMenu.isSupported()) {
		TransMenu.initialize();
				
		menu1.onactivate = function() { toggle('button1',document) };
		menu1.ondeactivate = function() { toggle('button1',document) };

		menu2.onactivate = function() { toggle('button2',document) };
		menu2.ondeactivate = function() { toggle('button2',document) };

		menu3.onactivate = function() { toggle('button3',document) };
		menu3.ondeactivate = function() { toggle('button3',document) };

		menu4.onactivate = function() { toggle('button4',document) };
		menu4.ondeactivate = function() { toggle('button4',document) };

		menu5.onactivate = function() { toggle('button5',document) };
		menu5.ondeactivate = function() { toggle('button5',document) };
		
		menu6.onactivate = function() { toggle('button6',document) };
		menu6.ondeactivate = function() { toggle('button6',document) };
		
		menu7.onactivate = function() { toggle('button7',document) };
		menu7.ondeactivate = function() { toggle('button7',document) };
		
		menu8.onactivate = function() { toggle('button8',document) };
		menu8.ondeactivate = function() { toggle('button8',document) };
		
		menu9.onactivate = function() { toggle('button9',document) };
		menu9.ondeactivate = function() { toggle('button9',document) };
	}
}

function toggle(img, doc) {
	if (typeof doc == 'undefined') {
		doc = document;
	}
	var url = doc.images[img].src;
	var lead = url.substr(0, url.lastIndexOf('.') - 1);
	var state = url.substr(url.lastIndexOf('.') - 1, 1);
	var ext = url.substr(url.lastIndexOf('.'));
	state = Math.abs(parseInt(state) - 1);
	doc.images[img].src = lead + state + ext;
}

/*
Preload both on and off version of an image in one go.
*/

function _prel(url) {
	preloaded_pix[preloaded_pix.length] = new Image();
	preloaded_pix[preloaded_pix.length - 1].src = url;
	var lead = url.substr(0, url.lastIndexOf('.') - 1);
	var state = url.substr(url.lastIndexOf('.') - 1, 1);
    if ((state == 0) || (state == 1) ) {
    	var ext = url.substr(url.lastIndexOf('.'));
	    state = Math.abs(parseInt(state) - 1);
    	url = lead + state + ext;
	    preloaded_pix[preloaded_pix.length] = new Image();
	    preloaded_pix[preloaded_pix.length - 1].src = url;
    }
}

/*
Gather all images that need a counterpart to preload:
*/

function _gather_img(doc) {
	var all_imgs = [];
	if (typeof doc == 'undefined') {
		doc = document;
	}
	for (i = 0; i < doc.images.length; i++) {
		if (doc.images[i].src.indexOf("0.") != -1
			|| doc.images[i].src.indexOf("1.") != -1
		) {
			all_imgs[all_imgs.length] = doc.images[i].src;
		}
	}
	for (i = 0; i < doc.layers; i++) {
		gather_img(doc.layers[i].document);
	}
	return all_imgs;
}

/*
Preload mouseover counterparts:
*/

function init_mo() {
    // Create a global array to store the images after they are loaded:
    if (typeof preloaded_pix != 'undefined') {
		window.status = "Warning: 'preloaded_pix' variable will be overloaded!";
    }
	preloaded_pix = [];
	var img = _gather_img();
	for (var i = 0; i < img.length; i++) {
		_prel(img[i]);
	}
	return true;
}
