// target frames per second
var FPS = 12;
var SECONDSBETWEENFRAMES = 1 / FPS;
function ettGallery (id, im) {
	this.image = new Image();
	this.imageant = new Image();
	this.canvas = null;
	this.context2D = null;
	this.currentTime = 0;
	//var sineWave = 0;
	var self = this;
	
	this.interv = null
	this.i = 0;
	this.overlay = new Image();
	this.images = im;
	this.id = id;
	
}
ettGallery.prototype.init = function () {
	var self = this;
	var draw = function () {
		self.currentTime += 0.05;
		self.context2D.clearRect(0, 0, self.canvas.width, self.canvas.height);
		if (self.imageant.src) {
			self.context2D.globalAlpha = 1;
			self.context2D.drawImage(self.imageant, 0, 0, self.canvas.width, self.canvas.height);
		}
		//self.context2D.restore();
		self.context2D.globalAlpha = self.currentTime;
		self.context2D.drawImage(self.image, 0, 0, self.canvas.width, self.canvas.height);
		self.context2D.globalAlpha = 1;
		if (OVERLAYIMAGE)
			self.context2D.drawImage(self.overlay, -1, -1, parseInt(self.canvas.width)+2, parseInt(self.canvas.height)+2);
		if (self.currentTime>=1) {
			self.imageant.src = self.image.src;
			clearInterval(self.interv);
		}
	}
	if (this.images) {
		this.canvas = document.getElementById(this.id);
		if (this.canvas) {
			this.context2D = this.canvas.getContext('2d');
			this.i=0;
			
			if (this.context2D) {
				
				var timeout = function () {
					self.currentTime = 0;
					
					self.image.onload = function () {
						//context2D.save();
						self.interv = setInterval(draw, SECONDSBETWEENFRAMES * 1000);
						if (self.images.length>1) {
							self.i++;
							if (self.i>=self.images.length) self.i=0;
							setTimeout(timeout, 4000);
						}
					};
					self.image.src = self.images[self.i];
					
				};
				
				if (OVERLAYIMAGE) {
					this.overlay.onload = timeout;
					this.overlay.src = OVERLAYIMAGE;
				} else {
					timeout();
				}
			} else {
				alert('Erro!');
			}
		}
	}
	$(window).unload(function () {clearInterval(this.interv); });
}
