// CSS3 experiment with opaque moving elements and dynamic background.
// DOM browsers only.
// 2003 f0b1c at hyperware dot net

// 2008 v2 ;]

var IEver = (navigator.appName=='Microsoft Internet Explorer') ? parseFloat((new RegExp("MSIE ([0-9]{1,}[.0-9]{0,})")).exec(navigator.userAgent)[1]) : 99;
var isIE = ( IEver <= 6 ) ? 1 : 0;
var isDOM = !isIE && document.getElementById?true:false;

function init(cycles){
	if(isDOM || isIE){
		var i, div1,div2;
		var iheigt=innerheight();
		// if(isDOM) document.getElementById('noIE').style.visibility='hidden';
		for(i=0;i<cycles;i++){
			hyperdiv('movingdiv','div1'+i, 4, randomvalue(0,(iheigt-20)),randomvalue(30,60), randomvalue(1,20));
			hyperdiv('movingdiv','div2'+i, randomvalue(5,100), randomvalue(0,(iheigt-20)),randomvalue(10,50), randomvalue(1,20));
			randommove('div1'+i, 40, randomvalue(15,30));
			movedown('div2'+i, randomvalue(0,4), randomvalue(1,15));
		}
		if(isDOM){
			
			body0 = document.getElementsByTagName('body')[0];
				
			if(body0.addEventListener){
				body0.addEventListener('mousedown', function(){body0.className ='psycho'}, false);
				body0.addEventListener('mouseup', function(){body0.className =''}, false);					
			}
			else{
				body0.attachEvent('onmousedown', function(){body0.className ='psycho'});
				body0.attachEvent('onmouseup', function(){body0.className =''});					
			}
			
			//psycho clicks .]
			setTimeout("psychobackground()",randomvalue(100,200));		
		}
	}
}

function randomvalue(min, max){ 
	// central randomizer unit .)
	var range = max-min+1;
	return (Math.floor(Math.random() * 
	Math.pow(10,("" + range).length)) % range) + parseInt(min);
}

function innerheight() {
	var screenheight;
	if( typeof(window.offsetHeight) == 'number' ) {
	// non IE
		screenheight = window.offsetHeight;
	} else if(document.documentElement && document.documentElement.clientHeight) {
	// IE 6+ std. comp. mode
		screenheight = document.documentElement.clientHeight;
	} else if(document.body && document.body.clientHeight) {
	// IE 4
		screenheight = document.body.clientHeight;
	}
	return parseInt(screenheight);
}

function hyperdiv(cssclass, id, height, top, opacity, zindex){

	var doc = window.document;
	doc.writeln('<div id="' + id + '" class="' + cssclass + '"></div>');
	
	element = doc.getElementById(id);
	
	element.style.zIndex = zindex;
	element.style.height = height;
	element.style.top = top;
	this.style = this.element.style
	
	if (isIE) {
		//element.style.filter = 'alpha(opacity='+opacity+')'; // wtf? disables dynamic background :((
		element.style.opacity = opacity/100; //will work some day, maybe ;) 
	}
	else if (isDOM) {
		element.style.opacity = opacity/100;
	}
}

function randommove(id, sleep, step){
	//object move up and down
	
	var obj = document.getElementById(id);
	var top = parseInt(obj.style.top);
	var height = parseInt(obj.style.height);	
	
	if(top < 0){
		step= Math.abs(step);
	}
	else if(top > (innerheight()-height-step-1)){
		step= - Math.abs(step);
	}
		
	obj.style.top = (top + step) + "px";
	setTimeout("randommove('"+id+"', "+sleep+", "+step+")", sleep);
}

function movedown(id, sleep, step){
	//object move down and rewind trace
	
	var obj = document.getElementById(id);
	var top = parseInt(obj.style.top);
	var height = parseInt(obj.style.height);	
	
	if(top < 0){
		step= Math.abs(step);
	}
	else if(top > (innerheight()-height-step-1)){
		top=0;		
	}

	obj.style.top = (top + step) + "px";
	setTimeout("movedown('"+id+"', "+sleep+", "+step+")", sleep);
}

function narrow(id, sleep, step, oheight){ 
	//useless yet
	
	var obj = document.getElementById(id);
	var height = parseInt(obj.style.height);
	
	if(height>(step + 10)){
		obj.style.height= parseInt(height - step) + "px";
		setTimeout("narrow('"+id+"', "+sleep+", "+step+", "+oheight+")", sleep);
	}
	else{
		obj.style.height=oheight + "px";
		return true;	
	}
}

function psychobackground(sleep){
	document.getElementsByTagName('body')[0].className ='psycho';
	setTimeout("document.getElementsByTagName('body')[0].className ='none'",randomvalue(50,150));
	setTimeout("psychobackground()",randomvalue(150,400));
}