/*****************************************************************************************
	
	Script	:	Image Flicker
	Desc	:	Add a flicker effect to an image by shifting a 2x image
				from right to left and back in unexpected time intervals
	Author	: 	Reinhold Becze
	
*****************************************************************************************/

function imageFlicker(element) {
	var msec = Math.ceil(Math.random() * 10000);
	//wrapper function because setTimeout allows only a function call without parameter 
	function exec1(){
		logoON(element, 0);	
	}
	setTimeout(exec1, msec);
}
function logoON(element, flickerCount) {
	var first = 0;
	var last = 4;
	var msec = 100;

	document.getElementById(element).style.backgroundPosition = "0px";
	if(Math.random()<0.6) {
		last -= 1;
	}
	if(Math.random()<0.2) {
		last -= 1;
	}
	if(flickerCount < last) {	
		first = flickerCount + 1;
		msec = Math.ceil(Math.random() * 200);
		function exec2(){
			logoOFF(element, first);
		}
		setTimeout(exec2, msec);	
	} else {
		if(Math.random()<0.2){
			document.getElementById(element).style.backgroundPosition = "-800px";
		}
		imageFlicker(element);
	}
}	
function logoOFF(element, flickerCount) {
	msec = Math.ceil(Math.random() * 300);
	document.getElementById(element).style.backgroundPosition = "-800px";
	function exec3() {
		logoON(element, flickerCount);	
	}
	setTimeout(exec3, msec);
}
