window.onload = initFader;

function initFader() {
	var fader = new Fader();
	fader.addImage('img/home/home1.jpg');
	fader.addImage('img/home/home2.jpg');
	fader.addImage('img/home/home3.jpg');
	fader.addImage('img/home/home4.jpg');
	fader.addImage('img/home/home5.jpg');
	fader.start();
}

function Fader() {
	this.images = new Array();
	this.count = 1;
	this.foreground = getElementsByClassName(document, 'div', 'fader')[1];
	this.background = getElementsByClassName(document, 'div', 'fader')[0];
	this.opacity = 0;
	
	this.addImage = function(image) {
		this.images.push(image);
		//preload images
		var img = new $e('img');
		img.src = image;		
		img.style.display = 'none';
		document.body.appendChild(img);		
	}
	
	
	this.start = function() {
		var self = this;
		setTimeout(function() { self.change() }, 1000);
	}
	
	
	this.change = function() {
		this.background.style.backgroundImage = this.foreground.style.backgroundImage;
		this.foreground.style.backgroundImage = 'url(' + this.images[this.count++ % this.images.length] + ')';
		
		this.opacity = 0;
		this.foreground.style.filter = 'alpha(opacity=' + (this.opacity*100) + ')';
		this.foreground.style.opacity = this.opacity;
				
		this.fade();
		
		var self = this;
		setTimeout(function() { self.change() }, 7500);
		
	}
	
	
	this.fade = function() {
		this.opacity += 0.01;
		this.foreground.style.opacity = this.opacity;
		this.foreground.style.filter = 'alpha(opacity=' + (this.opacity*100) + ')';
		
		if (this.opacity < 1) {
			var self = this;
			setTimeout(function() { self.fade(); }, 10);
		}
	}
}