//========================================================================//
//====================== Build by: Bas Slagter ===========================//
//====================== Date: 04-03-2008 ================================//
//====================== Free for use ====================================//
//========================================================================//
//========================================================================//
	
		function rand(n){ return Math.round(Math.random() * n); }	
		
		var fader = {
			elemid:				'',
			block1:       null,
			block2:       null,
			images:       new Array(),
			timespan:     2000,
			speed:				40,
			currentIndex: 0,
	
			init: function(elementid){
				this.elemid = elementid;
				this.createSecondBlock();
				this.createFirstBlock();
				this.start();
			},
			
			addImage: function(imgsrc, iurl){
				var tmpimg = document.createElement('img');
				tmpimg.src = imgsrc;
				tmpimg.alt = ( iurl ) ? iurl : '';
				this.images.push(tmpimg);
			},
			
			goToUrl: function(){
				document.location.href = document.getElementById('iurl').innerHTML;
			},
			
			setTimeSpan: function(sec){
				this.timespan = (sec * 1000);
			},
			
			setSpeed: function(speed){
				this.speed = 100 - speed;
			},
			
			setImage: function(block, index){
				block.style.backgroundImage = "url('" + this.images[index].src + "')";
				
				block = ( this.block1 ) ? this.block1 : this.block2;
				if( this.images[index].alt !== '' ){
					this.setTargetUrl(block, this.images[index].alt);
				}else if( this.images[index].alt == '' ){
					block.style.cursor = 'default';
					block.onclick = null;
				}
			},
			
			setTargetUrl: function(block, iurl){
				var wrapper = document.getElementById('iurl');
				
				if( !wrapper ){
					wrapper = document.createElement('span');
					wrapper.id = 'iurl';	
					wrapper.style.visibility = 'hidden';
					block.appendChild(wrapper);		
				}	
				
				wrapper.innerHTML = iurl;	
				block.onclick = fader.goToUrl;
				block.style.cursor = 'pointer';
			},
			
			switchTargetUrl: function(){
				var nextIndex = ( (this.currentIndex + 1)  > this.images.length -1 ) ? 0 :  this.currentIndex + 1;	
				this.setTargetUrl(this.block2, this.images[nextIndex].alt);
			},
			
			loadNextImage: function(){
				var nextIndex = ( (this.currentIndex + 1)  > this.images.length -1 ) ? 0 :  this.currentIndex + 1;								
				this.setImage(this.block2, nextIndex);			
			},
			
			switchImages: function(){
				this.currentIndex = ( (this.currentIndex + 1)  > this.images.length -1 ) ? 0 :  this.currentIndex + 1;	
				this.setImage(this.block1, this.currentIndex);		
				
				this.loadNextImage();		
				this.resetOpacity();	
				this.fadeOut();			
			},
			
			resetOpacity: function(){
				this.block1.style.opacity = 100;
				this.block1.style.filter = 'alpha(opacity=100)';
				this.block1.style.MozOpacity = '1';
				this.block1.style.khtmlOpacity = 100;
			}, 
			
			createFirstBlock: function(){
				var curblock = document.getElementById(this.elemid);
				
				this.block1                = document.createElement('div');
				this.block1.style.width    = this.block2.offsetWidth + 'px';
				this.block1.style.height   = this.block2.offsetHeight + 'px';
				this.block1.style.position = 'absolute';
				
				this.setImage(this.block1, 0);
				this.currentIndex = 0;
				
				curblock.style.background = 'none';
				curblock.appendChild(this.block1);
			},
			
			createSecondBlock: function(){
				var curblock = document.getElementById(this.elemid);
				
				this.block2 = document.createElement('div');
				this.block2.style.width           = curblock.offsetWidth + 'px';
				this.block2.style.height          = curblock.offsetHeight + 'px';
				this.block2.style.position        = 'absolute';
					
				this.setImage(this.block2, 1);
				
				document.getElementById(this.elemid).appendChild(this.block2);
			}, 
			
			start: function(){	
				setTimeout("fader.fadeOut()", this.timespan);	
				setTimeout("fader.switchTargetUrl()", this.timespan);	
			},
			
			fadeOut: function(){			
				for(var i=1; i<=99; i++){			
					setTimeout("fader.fade(" + (100 - i) + ")", Math.round(i * this.speed)); 	
				}
				
				setTimeout("fader.switchImages()", Math.round(i * this.speed) + this.timespan);			
			},
			
			fade: function(opacity){
			  var mozopacity =  ( opacity < 10 ) ? '0' + opacity : opacity;
			  this.block1.style.opacity = opacity;
				this.block1.style.filter = 'alpha(opacity=' + opacity + ')';
				this.block1.style.MozOpacity = '0.' + mozopacity;
				this.block1.style.KHTMLOpacity  = opacity;
			}
		};
	
		fader.setTimeSpan(3);
		fader.setSpeed(80); // max 100
		fader.addImage('images/header1.jpg');
		fader.addImage('images/header2.jpg');
		fader.addImage('images/header3.jpg');
		fader.addImage('images/header4.jpg');
		fader.addImage('images/header5.jpg');
