/**
 * 02 Outubro 2008. version 1.0
 * Script JS para animação de imagens.
 * Dynamix Software
 * Thyago Schleuss
 * thyago@dynamix.com.br
 */
function SimulaFlash() {

	/**
	 * Função principal, responsavel chamada de todos
	 * os outros métodos que realizam a
	 * troca das imagens, é chamada a cada X milisegundos
	 * referente a variavel tempo.
	 */
	this.doAction = function() { 
		obj.setAtual( obj.getAtual() % obj.getTotal() + 1 ); 
		obj.doAnimation( obj.getAtual() , true ); 
		obj.doWait();
	};

	/**
	 * Método que troca as imagens e os textos
	 * para dar o efeito de animação, incluindo a atualizacao
	 * da imagem atual e da ultima mostrada.
	 * Utiliza jQuery para efeito de FadeIn e FadeOut
	 */
	this.doAnimation = function( idx , automatic ) {
		obj.setAtual( idx ); 
		obj.gebi( btPrefix + obj.getAnterior() ).className = classInativo; 
		obj.gebi( btPrefix + idx ).className = classAtivo;
		
		// Caso seja mudança automatica faz o efeito.
		// Caso seja a mudança ocorrida pelo evento do mouse não executa o efeito.
		if( automatic === true ) {
			// Caso exista só uma (1) animação, não realiza o efeito.
			if( obj.getTotal() != 1 ) {
				$( idPrefix + divRootImage ).fadeOut( fadeOutTime , function() {
					obj.gebi( divRootImage ).innerHTML = 
						obj.gebi( divImagePrefix  + idx ).innerHTML;
				});
			}
		} else {
			obj.gebi( divRootImage ).innerHTML = 
				obj.gebi( divImagePrefix  + idx ).innerHTML;
		}
		
		obj.gebi( divRootText ).innerHTML = 
			obj.gebi( divTextPrefix + idx ).innerHTML;
			
		obj.setAnterior( idx );
		
		// Caso seja mudança automatica faz o efeito.
		// Caso seja a mudança ocorrida pelo evento do mouse não executa o efeito.
		if( automatic === true ) {
		
			// Caso exista só uma (1) animação, não realiza o efeito.
			if( obj.getTotal() != 1 ) {
				$( idPrefix + divRootImage ).fadeIn( fadeInTime );
			}
		}
	};

	/**
	 * Atualiza o timer responsavel pela chamada do
	 * método principal
	 */
	this.doWait = function() { 
		clearTimeout( obj.getTimer() );
		obj.setTimer( setTimeout( function() { obj.doAction(); } , obj.getTempo() ) );
	};

	/**
	 * Método usado caso o usuário passe o mouse
	 * sobre os numeros na animação, fazendo assim
	 * com que as imagens mudem conforme o numero
	 */
	this.onTouch = function( idx , action ) {
		if ( action == obj.ON_MOUSE_DOWN )	{
			obj.doWait();
		}
		obj.doAnimation( idx , false );
	};

	/**
	 * Inicia o array de tempos para o simulador.
	 * Caso a quantidade de itens no array for menor
	 * do que a quantidade de itens a ser exibidos,
	 * o array sera completado com o tempo padrao. 
	 */
	this.doInitTimer = function() { 

		// se existir o objeto de tempo
		if( arrTempo ) {
			// se houver mais itens do que a tamanho do array de tempo
			if( arrTempo.length < obj.getTotal() ) {
				for( var i=(arrTempo.length+1); i<obj.getTotal(); i++ ){
					// completa as demais com o tempo padrao
					arrTempo[i] = tempo;
				}
			}
		}

		sFlash.doAction();
	};
	
	/**
	 * Métodos Setters e Getters 
	 */
	this.gebi = function( obj ) {
		return document.getElementById( obj );
	};

	this.setAtual = function( idx ) {
		atual = idx;
	};

	this.setTempo = function( ms ) {

		if( arguments.length == 1 ) {
			tempo = ms;
		}
		else if (arguments.length > 1 ) {
			arrTempo = new Array();
			for( var i = 0; i < arguments.length; i++ ) {
				arrTempo[i] = arguments[i];
			}
		}
	};

	this.setAnterior = function( ant ) {
		anterior = ant;
	};

	this.setTotal = function( tt ) {
		total = tt;
	};

	this.setTimer = function( timeT ) {
		timer = timeT;
	};

	this.getAtual = function() {
		return atual;
	};

	this.getTempo = function() {

		// caso exista o array de tempo
		if( arrTempo ) {
			// se o numero do item atual for menor do 
			//  que a quantidade de itens no array
			if( arrTempo.length > (obj.getAtual()-1)){
				return arrTempo[obj.getAtual()-1];
			}
		}
		return tempo;
	};

	this.getAnterior = function() {
		return anterior;
	};

	this.getTotal = function() {
		return total;
	}

	this.getTimer = function() {
		return timer;
	};

	/**
	 * Váriaveis da classe, responsaveis
	 * pelo funcionamento da animação, temos
	 * a posicao da imagem atual, a posição da
	 * ultima imagem, o tempo em milisegundos entre
	 * as animações, o total de imagens, etc..
	 */
	SimulaFlash.ON_MOUSE_DOWN = 1;			// Apenas para razer referência a ação do mouse
	SimulaFlash.ON_MOUSE_UP = 0;			// Apenas para razer referência a ação do mouse
	var obj = this;							// Referencia a classe
	var atual = 0;							// Animação atual
	var anterior = 1;						// Animação anterior
	var total = 6;							// Total de animações
	var tempo = 6000;						// Tempo entre cada animação
	var arrTempo = null;					// Tempo para cada item da apresentacao
	var fadeOutTime = 100;					// Tempo da animação de FadeOut (sumir)
	var fadeInTime = 500;					// Tempo da animação de FadeIn (aparecer)
	var classAtivo = 'ativo';				// nome da class CSS para efeito opaco
	var classInativo = 'inativo'; 			// nome da class CSS para efeito transparente
	var divRootImage = "corpoImagem";       // Id do div com a imagem principal, mostrada ao usuário
	var divRootText = "txt";				// Id do div com o texto principal, mostrado ao usuário
	var divImagePrefix = "img";				// Prefixo do div que contem as imagens das outras animações
	var divTextPrefix = "text";				// Prefixo do div que contem os textos das outras animações
	var btPrefix = "enum";					// Prefixo do div que contem os botões da animação
	var idPrefix = "#";						// Prefixo do tag ID para chamada
	var classPrefix = ".";					// Prefixo do tag class para chamada
	var timer = null;						// Timer 
}