﻿
function imageFader(container, options, data) {
	var self = this;
	
	this.options = options;
	if(options = null) options = new Object();
	this.container = container;
	this.data = data;
	this.current = -1;
	this.next = 0;
	this.timeout = null;
	this.images = new Array();
	this.width = 0;
	this.height = 0;
	this.flip = 1;
	this.transAmount = 0;
	
	this.setOpacity = function(element) {
		element.style.opacity = element.opacity;
		element.style.MozOpacity = element.opacity;
		element.style.filter = 'alpha(opacity=' + (Math.round(element.opacity*100)) + ')';
	}
	
	this.createImage = function() {
		var i = document.createElement('img');
		i.style.width = self.width + 'px';
		i.style.height = self.height + 'px';
		i.style.position = 'absolute';
		i.style.left = i.style.top = '0px';
		i.style.backgroundColor = 'white';
		i.opacity = 0;
		i.loaded = false;
		self.setOpacity(i);
		return i;
	}
	
	this.createLink = function() {
		var el = document.createElement('a');
		el.style.width = self.width + 'px';
		el.style.height = self.height + 'px';
		el.style.position = 'absolute';
		el.style.zIndex = 120;
		el.innerHTML = '&nbsp;'
		el.style.display = 'none';
		el.style.textDecoration = 'none';
		el.href = '#';

		el.style.backgroundColor = 'white';
		el.opacity = 0;
		self.setOpacity(el);

		self.link = el;
		self.container.appendChild(el);
	}
	
	this.preloadImages = function() {
		for(var i=0; i<self.data.length; i++) {
			util.cacheImage(self.data[i].file);
		}
	}
	
	this.init = function() {
		self.preloadImages();
		self.getNext();
		self.width = self.container.offsetWidth;
		self.height = self.container.offsetHeight;
		self.container.style.position = 'relative';
		self.container.style.width = self.width + 'px';
		self.container.style.height = self.height + 'px';
		self.container.style.overflow = 'hidden';
		self.container.innerHTML = '';

		self.container.appendChild(self.images[0] = self.createImage());
		self.container.appendChild(self.images[1] = self.createImage());
		self.createLink();
	}

	this.getNext = function() {
		if(self.options.random) {
			self.next = Math.floor(Math.random() * (self.data.length - 0.01));
			if(self.next == self.current) {
				while(self.next == self.current) {
					self.next = Math.round(Math.random() * (self.data.length - 1));
				}
			}
		} else {
			self.next = self.current + 1;
			if(self.next > self.data.length) self.next = 0;
		}
	}

	this.startFade = function() {
		self.flip = 1 - self.flip;
		
		/*self.images[1 - self.flip].opacity = 0.2;
		self.setOpacity(self.images[1 - self.flip]);*/
		
		var im = self.images[self.flip];
		im.opacity = 0;
		self.setOpacity(im);
		
		self.images[1 - self.flip].style.zIndex = 100;
		im.style.zIndex = 101;
		self.getNext();
		
		im.src = self.data[self.next].file;

		self.timeout = setTimeout(self.fade, 20);
	}
	
	this.fade = function() {
		clearTimeout(self.timeout);
		var im = self.images[self.flip];
		var dat = self.data[self.next];
		
		var diff = (1 - im.opacity) / dat.fadeSpeed;
		if(diff < 0.01) diff = 0.01;
		im.opacity = im.opacity + diff;
		
		if(im.opacity > 1) im.opacity = 1;
		self.setOpacity(im);
		if(im.opacity < 1) 
		{
			self.timeout = setTimeout(self.fade, 10);
		} 
		else 
		{
			self.current = self.next;
			
			if(self.data[self.current].link) 
			{
				self.link.style.display = 'block';
				self.link.href = self.data[self.current].link;
			} 
			else 
			{
				self.link.style.display = 'none';
			}
			self.timeout = setTimeout(self.startFade, self.data[self.current].delay);
		}
	}

	this.init();	
	this.timeout = setTimeout(self.startFade, 0);
}


