/*
	randomize.js
	
	v0.06, 2007-03-14, Christian Augustin

*/
/*
	JavaScript 1.3+, W3C DOM Level 2
	MSIE 5+, Mozilla 1+, Netscape 6.2+, Firefox 1+, Safari 1+, Opera 8+
	
	Usage
	
		<img class="randomImg min1 max5" src="example03.jpg" />
	
	If the number of the image file name has more than
	one digit, this number of digits is used. (As you can see it is not
	necessary to use the "first" image as default image ...)
	
	The last digits found in the file name are used to randomize the image.
	
	ToDo
	- "Smart" Randomize (the same image is not shown on consecutive calls).
	- "Shuffle" (every image is shown only once in a cycle).
	- "Cycle" (the images are shown one after the other in given order).
	
	History
	2007-03-14 cma: Slight fix for Firefox 1.5+ (document.write of initial CSS).
	2007-02-07 cma: Optionaly using window.onDomReady() for initialization.
	2006-02-26 cma: Style "class" bug fixed.
	2006-02-24 cma: "Flashing default image" once again (using DOM "replaceChild").
	2006-02-23 cma: Opera replacement bug fixed (functional only with Opera 8+).
	2006-02-23 cma: Flashing default image fixed (make image visible when loaded).
	2006-02-18 cma: Initial setup.
	
*/


/* ===========================
   Constructor function ...
=========================== */

function Randomize(obj) {
	if (!obj || obj.tagName.toLowerCase() != 'img' || !obj.src) return;

	this.min = 0;
	this.max = 0;
	this.oldSrc = obj.src;
	
	/* Extract arguments from className ... */
	var args = (obj.className) ? obj.className.toLowerCase().split(/\s+/) : [];
	
	/* Scan arguments if given ... */
	for (var i=1; i<args.length; i++) {
		var a = args[i];
		if (a.indexOf("min") == 0) {
			var tmp = parseInt(a.substring(3, a.length), 10);
			if (tmp && !isNaN(tmp)) this.min = tmp;
		} else if (a.indexOf("max") == 0) {
			var tmp = parseInt(a.substring(3, a.length), 10);
			if (tmp && !isNaN(tmp)) this.max = tmp;
		}
	}
	
	/* Get the random number ... */
	var randNum = Math.floor(Math.random() * (this.max + 0.999 - this.min)) + this.min;

	/* Look at the img src for number of digits ... */
	var srcNum = this.oldSrc.replace(/^.+\/[^\/]*[^\d](\d+)[^\/\d]*\.[^\/]+$/, '$1');
	
	if (srcNum.length > 1) {
		randNum = "00000000" + randNum;
		randNum = randNum.substring(randNum.length-srcNum.length, randNum.length);
	}

	var newImg = document.createElement('img');
	newImg.className = obj.className;
	newImg.width = obj.width;
	newImg.height = obj.height;
	newImg.setAttribute('style', obj.getAttribute('style'));
	if (obj.id) newImg.id = obj.id;
	obj.parentNode.replaceChild(newImg, obj);
	
	newImg.src = this.oldSrc.replace(/^(.+\/[^\/]*[^\d])\d+([^\/\d]*\.[^\/]+)$/, '$1' + randNum + '$2');
	
	newImg.style.visibility = 'visible'

}



/* =======================
   Instance methods ...
======================= */


/* ==========================
   Class properties ...
========================== */

Randomize.initialCSS = '<style type="text/css">.randomImg {visibility: hidden;}<\/style>';


/* ====================
   Class methods ...
==================== */

Randomize.findImages = function() {
	if (!document.getElementsByTagName) return;
	var imgs = document.images;
	var randImgs = new Array();
	if (imgs) {
		for (var i=0; i<imgs.length; i++) {
			var n = imgs[i];
			if (n.className && n.className.match(/(^|\s)randomImg(\s|$)/)) randImgs[randImgs.length] = n;
		}
	};
	if (randImgs.length) {
		for (var j=0; j<randImgs.length; j++) new Randomize(randImgs[j]);
	};

}




/* =================
   Initialize ...
================= */

if (document.getElementsByTagName 
	&& document.createElement
	&& (!window.opera || (document.createDocumentFragment && !navigator.userAgent.match(/Opera[ \/]7\./)))) {
	
	document.write(Randomize.initialCSS);
	
	Randomize.initComplete = false;
	
	Randomize.init = function() {
		if (Randomize.initComplete) return true;
		
		Randomize.findImages();
		
		Randomize.initComplete = true;
		return true;
	}
	
	if (window.onDomReady) {
		window.onDomReady(Randomize.init);
	} else if (typeof pageComplete == 'function') {
		Randomize.oldPageComplete = pageComplete || function(){};
		pageComplete = function() {
			Randomize.oldPageComplete();
			Randomize.init();
		};
	} else {
		Randomize.oldOnload = window.onload || function(){};
		window.onload = function() {
			Randomize.oldOnload();
			Randomize.init();
		};
	};

}

/* End of randomize.js */
