
/*
 *  FLASH MENU & SCROLL MENU
 *  Desenvolvido por: Fábio Augustin Neis.
 * 
 *  EQUIPE X-NEO
 *  Data: 15/04/2008
 *
 *  Funcionamento Geral:
 *		noticias = new Sequencia();
 *  	noticias.incluir(texto, imagem, link);
 *  	window.onload = flash_menu.init;
 *
 *  Movimentação:
 *		flash_menu.play();
 *	 	flash_menu.pause();
 *		flash_menu.carregar('esquerda');
 *  	flash_menu.carregar('direita');
 *  	flash_menu.carregar('primeira');
 *		flash_menu.carregar('ultima');
 *		flash_menu.carregar(0-9);
 */
 
var mouse = 0;	
 
var flash_menu = {
	largura : 475, // Largura máxima para o count
	count : 0, // Contador pro laço da barra
	size : 25, // Largura por volta da barra
	barra_speed : 10,  // Velocidade da barra
	play_speed : 10000,  // Velocidade do play em segundos
	fade_speed : 20, // Velocidade do fade
	play_id: '', // Id do setTimeout
	text : '', // Texto a ser inserido
	max_caracteres : 100, //Max de caracteres para reduzir a fonte
	max_palavras : 25, //Max de palavras
	img : '', // Imagem a ser inserida
	dest : '', // Destino a ser inserido
	interval : '', 
	
	
	
	init : function() {
	   document.getElementById('flash_text2').onmouseover = function(){
			document.getElementById('flash_text').style.cssText = 'opacity:0.85;filter:alpha(opacity:85);';
			document.getElementById('flash_text2').style.color = '#CCCCCC';
			mouse = 1;
		}
		document.getElementById('flash_text2').onmouseout = function(){
			document.getElementById('flash_text').style.cssText = 'opacity:0.7;filter:alpha(opacity:70);';
			document.getElementById('flash_text2').style.color = '#FFFFFF';
			mouse = 0;
		}
		document.getElementById('flash_img_click').onmouseover = function(){
			document.getElementById('flash_text').style.cssText = 'opacity:0.85;filter:alpha(opacity:85);';
			document.getElementById('flash_text2').style.color = '#CCCCCC';
			mouse = 1;
		}
		document.getElementById('flash_img_click').onmouseout = function(){
			document.getElementById('flash_text').style.cssText = 'opacity:0.7;filter:alpha(opacity:70);';
			document.getElementById('flash_text2').style.color = '#FFFFFF';
			mouse = 0;
		}

		flash_menu.play();
		
	},
	
	set_text : function(v) {
		this.text = v;
	},
	
	set_img : function(v) {
		this.img = v;
	},

	set_dest : function(v) {
		this.dest = v;
	},	
	
	get_dest : function() {
		return this.dest;
	},
	
	mostra : function() {


		
		this.count += this.size;
		if(mouse==1){			
			document.getElementById('flash_text').style.cssText="width:"+this.count+"px;opacity:0.85;filter:alpha(opacity:85);";
		}else{
			document.getElementById('flash_text').style.cssText="width:"+this.count+"px;opacity:0.7;filter:alpha(opacity:70);";
		}

		if(navigator.userAgent.indexOf("Firefox") != -1 || navigator.userAgent.indexOf("Opera") != -1) {
			if(mouse==1){				
				this.count += this.size;
				document.getElementById('flash_text').style.cssText="width:"+this.count+"px;opacity:0.85;filter:alpha(opacity:85);";
				this.count += 10;
				document.getElementById('flash_text').style.cssText="width:"+this.count+"px;opacity:0.85;filter:alpha(opacity:85);";
			}else{
				this.count += this.size;
				document.getElementById('flash_text').style.cssText="width:"+this.count+"px;opacity:0.7;filter:alpha(opacity:70);";
				this.count += 10;
				document.getElementById('flash_text').style.cssText="width:"+this.count+"px;opacity:0.7;filter:alpha(opacity:70);";
			}

			
		} else
			document.getElementById('flash_text2').style.top = "363px";
		
		if(this.count > this.largura) {
			document.getElementById('flash_text2').innerHTML = this.reduz_texto();
			document.getElementById('flash_text2').onclick = flash_menu.redireciona;
			document.getElementById('flash_img_click').onclick = flash_menu.redireciona;			
			clearInterval(this.interval);
		}
		
		
		
	},
	
	reduz_texto : function() {
		var texto = '';
		if(this.text.length < this.max_caracteres) {
			texto = this.text;
			if(mouse == 1){
				document.getElementById('flash_text').style.cssText = "padding:5px 10px 10px 5px;opacity:0.85;filter:alpha(opacity:85);";
			}else{
				document.getElementById('flash_text').style.cssText = "padding:5px 10px 10px 5px;opacity:0.7;filter:alpha(opacity:70);";
			}
		} else {
			if(mouse == 1){
				document.getElementById('flash_text').style.cssText = "padding:5px 10px 10px 5px; font-size:14px; font-weight:bold;opacity:0.85;filter:alpha(opacity:85);";
			}else{
				document.getElementById('flash_text').style.cssText = "padding:5px 10px 10px 5px; font-size:14px; font-weight:bold;opacity:0.7;filter:alpha(opacity:70);";
			}
			
			if(this.text.length < this.max_caracteres*2) {
				texto = this.text;
			} else {
				var novoTexto = this.text.split(' ');
				if(novoTexto.length < this.max_palavras)
					texto = this.text;
				else {
					for(x=0;x<this.max_palavras;x++) {
						novoTexto[x]
						texto += novoTexto[x]+' ';
					}
					texto += '...';
				}
			}
		}
		return texto;
	},
	
	redireciona : function() {
		window.location = flash_menu.get_dest();
	},
	
	carregar : function (modo) {
		this.count = 0;
		var novo = '';
		switch(modo) {
			case 'primeira':
				novo = noticias.primeira().split('|');
			break;
			case 'ultima':
				novo = noticias.ultima().split('|');
			break;
			case 'direita':
				novo = noticias.direita().split('|');
			break;
			case 'esquerda':
				novo = noticias.esquerda().split('|');
			break;
			default: 
				if (modo >= 0 && modo < noticias.getQuant())
					novo = noticias.escolhe(modo).split('|');
				else
					novo = noticias.primeira().split('|');
			break;
		}

		if(this.play_id == -1) {
			noticias.setContador(noticias.getContador()-1);
		} else {

			//ELIMINA O TEMPO DO ANTERIOR E INICIA NOVAMENTE
			if(this.play_id) {
				window.clearTimeout(this.play_id);
				this.play_id = window.setTimeout("flash_menu.play()", this.play_speed);
			}

			flash_menu.set_text(novo[0]);
			flash_menu.set_img(novo[1]);
			flash_menu.set_dest(novo[2]);
			document.getElementById('flash_text2').innerHTML = '';
			document.getElementById('flash_img').style.cssText = "background:#FFFFFF url(/images/noticias/destaques/"+this.img+") top left;";
			document.getElementById('flash_text').style.cssText= "width:0px; padding:0;";
			this.fade(1);
			this.interval = setInterval('flash_menu.mostra()', this.barra_speed);

			var imagens = document.getElementById('flash_buttons_img2').getElementsByTagName('img');
			if(imagens.length > 0) {
				for(x=0;x<imagens.length;x++) {
					if(x == noticias.getContador()) {
						imagens[x].className = 'selected';
					} else {
						imagens[x].className = '';
					}
				}
			}
		}
	},
	
	fade: function(opac) {
		var passed = parseInt(opac);
		var newOpac = parseInt(passed+10);
		if ( newOpac < 100 ) {
			document.getElementById('flash_img').style.opacity = '.'+newOpac;
			document.getElementById('flash_img').style.filter = "alpha(opacity:"+newOpac+")";
			window.setTimeout("flash_menu.fade('"+newOpac+"')", this.fade_speed);
		}
	},
	
	play: function() {
		document.getElementById('play').src = '/images/layout/noticias_destaques/destaques_play-hover.jpg';
		document.getElementById('pause').src = '/images/layout/noticias_destaques/destaques_pause.jpg';
		document.getElementById('play').parentNode.onclick = function() { }
		document.getElementById('pause').parentNode.onclick = function() {	flash_menu.pause(); }		
		this.play_id = window.setTimeout("flash_menu.play()", this.play_speed);
		flash_menu.carregar('direita');

	},
	
	pause: function() {
		document.getElementById('play').src = '/images/layout/noticias_destaques/destaques_play.jpg';
		document.getElementById('pause').src = '/images/layout/noticias_destaques/destaques_pause-hover.jpg';
		document.getElementById('play').parentNode.onclick = function() { flash_menu.play(); }
		document.getElementById('pause').parentNode.onclick = function() {	}
		window.clearTimeout(this.play_id);
		this.play_id = -1;
	},
	
	anterior: function() {
		if(this.play_id == -1)
			this.play_id = 0;
		
		this.carregar('esquerda');

		if(this.play_id == 0)
			this.play_id = -1;

	},
	
	proximo: function() {
		
		if(this.play_id == -1)
			this.play_id = 0;

		this.carregar('direita');

		if(this.play_id == 0)
			this.play_id = -1;
			
	}
}

var scroll_menu = {
	obj : '', // Objeto que será inicializado
	obj_d : '', // Objeto que direciona o scroll para direita
	obj_e : '', // Objeto que direciona o scroll para esquerda
	tempo : '',  // Variável que mantem o setInterval
	interval : 5, // Intervalo do setInterval
	contador: 0, // Contador
	max_contador: 60, // Maximo que o contador pode chegar min:21 ao aumentar a velocidade incrementa tb no max_contador ( 1:9 )
	velocidade: 11, // Velocidade do scroll
	elem_atual: 1, // Elemento atual
	elem_quantidade: 0, // Total de elementos
	cor_off : '#555555', // Cor do obj_d/obj_e ao desativar
	cor_on : '#FFFFFF', // Cor do obj_d/obj_e ao ativar
	cursor_off : 'default', // Cursor definido ao desativar
	cursor_on : 'pointer', // Cursor definido ao ativar
	
	
	init : function(o, q, d, e) {
		this.obj = o;
		this.elem_quantidade = q;
		this.obj_d = d;
		this.obj_e = e;
		
		if(navigator.userAgent.indexOf("MSIE 6") != -1) {
			this.max_contador += 3;
			//this.velocidade += 1
		} else if(navigator.userAgent.indexOf("Firefox") != -1) {
			this.max_contador = 21;
			this.velocidade = 1
		}
		
		if(this.elem_quantidade == 1) {
			this.obj_d.style.cssText = 'color:'+this.cor_off+'; cursor:'+this.cursor_off;
			this.obj_d.onclick = '';
			this.obj_e.style.cssText = 'color:'+this.cor_off+'; cursor:'+this.cursor_off;
			this.obj_e.onclick = '';
		} else {
			this.obj_e.style.cssText = 'color:'+this.cor_off+'; cursor:'+this.cursor_off;
			this.obj_e.onclick = '';
		}
	},
	
	direita : function() {
		this.elem_atual++;
		if(this.elem_atual <= this.elem_quantidade) {
			this.tempo = setInterval("scroll_menu.move('+')", this.interval);
		} else {
			this.elem_atual--;
		}
	},
	
	esquerda : function() {
		this.elem_atual--;
		if(this.elem_atual >= 1) {
			this.tempo = setInterval("scroll_menu.move('-')", this.interval);
		} else {
			this.elem_atual++;
		}
	},
	
	move : function(direcao) {

		if(this.obj_d) {
			this.obj_d.style.cssText = 'color:'+this.cor_off+'; cursor:'+this.cursor_off;
			this.obj_d.onclick = '';
		}
		
		if(this.obj_e) {
			this.obj_e.style.cssText = 'color:'+this.cor_off+'; cursor:'+this.cursor_off
			this.obj_e.onclick = '';
		}
		
		if(this.contador < this.max_contador) {
			this.contador += this.velocidade;
			if(direcao == '+')
				document.getElementById(this.obj).scrollLeft += this.contador;
			else
				document.getElementById(this.obj).scrollLeft -= this.contador;
		} else {
			clearInterval(this.tempo);
			this.contador = 0;
			
			if(this.obj_d && this.elem_atual < this.elem_quantidade) {
				//this.obj_d.style.cssText = 'color:'+this.cor_on+'; cursor:'+this.cursor_on;
				this.obj_d.onclick = function() {
					scroll_menu.direita(this);
				}
			}
			
			if(this.obj_e && this.elem_atual > 1) {
				//this.obj_e.style.cssText = 'color:'+this.cor_on+'; cursor:'+this.cursor_on;
				this.obj_e.onclick = function() {
					scroll_menu.esquerda(this);
				}
			}			
			
		}
	}
}

function Sequencia() {
	this.imagens = [];
	this.textos = [];
	this.destinos = [];
	this.quant = 0;
	this.contador = -1;
	
	this.incluir = function(texto, imagem, destino) {
		this.textos[this.quant] = texto;
		this.imagens[this.quant] = imagem;
		this.destinos[this.quant] = destino;
		this.quant++;
	}
	
	this.direita = function() {
		if(this.contador == this.textos.length-1)
			this.contador = -1;
		
		this.contador++;
		return this.textos[this.contador]+'|'+this.imagens[this.contador]+'|'+this.destinos[this.contador];
	}
	
	this.esquerda = function() {
		if(this.contador == 0)
			this.contador = this.textos.length;
			
		this.contador--;
		return this.textos[this.contador]+'|'+this.imagens[this.contador]+'|'+this.destinos[this.contador];
	}
	
	this.primeira = function() {
		return this.textos[0]+'|'+this.imagens[0]+'|'+this.destinos[0];
	}
	
	this.ultima = function() {
		var ultimo = this.textos.length-1;
		return this.textos[ultimo]+'|'+this.imagens[ultimo]+'|'+this.destinos[ultimo];
	}
	
	this.escolhe = function(n) {
		if(n >= 0 && n < this.textos.length) {
			this.contador = n;
			return this.textos[n]+'|'+this.imagens[n]+'|'+this.destinos[n];
		} else
			return this.textos[0]+'|'+this.imagens[0]+'|'+this.destinos[0];
	}

	this.getContador = function() {
		return this.contador;
	}
	
	this.setContador = function(v) {
		this.contador = v;
	}	
	
	this.getQuant = function() {
		return this.quant;
	}
}