// Funcoes do player
function player (i) {
		// Total de noticias do player		
		var total =  $('.item-player').size();

		
		var options = {};
		
		// Esconde a noticia atual
		$('#player-'+i).hide();		
		
		// Tiro a cor do link atual
		$('#yer-'+i).css('background','#C8E6F3');
		
		// Adiciona mais 1 no contador
		i++;
		
		// Se o contador for maior que o total contador = 1
		if (i > total) {
			i = 1;
		}
		
		// Mostro a proxima noticia
		$('#player-'+i).fadeIn(1000);	
		
		// Coloca o link da atual
		$('#yer-'+i).css('background','#4396CA');
		
		
		// Chamo a funcao de novo daqui 5 segundos 
		interPlayer = setTimeout('player ('+i+');',5000);
}


// JavaScript Document
$(function (){
	
	/*var tempo;
	var intervalo = 4000; //Intervalo entra as mudanças de slides. Definido em milisegundos.
	
	function campo(campo1) {
		return document.getElementById(campo1);
	}


	var $A = Array.from = function(iterable) {
		if (!iterable) return [];
		if (iterable.toArray) {
			return iterable.toArray();
		} else {
			var results = [];
			for (var i = 0; i < iterable.length; i++)
				results.push(iterable[i]);
			return results;
		}
	};

	Function.prototype.bind = function() {
		var __method = this, args = $A(arguments), object = args.shift();
		return function() {
			return __method.apply(object, args.concat($A(arguments)));
		};
	};


	function addEvent(obj, evType, fn) { 
		if (typeof obj == "string") {
			if (null == (obj = document.getElementById(obj))) {
				throw new Error("Elemento HTML não encontrado. Não foi possível adicionar o evento.");
			}
		}
		if (obj.attachEvent) {
			return obj.attachEvent(("on" + evType), fn);
		} else if (obj.addEventListener) {
			return obj.addEventListener(evType, fn, true);
		} else {
			throw new Error("Seu browser não suporta adição de eventos.");
		}
	}

	function nextSlide() {
		var divs = campo('slides').getElementsByTagName('div');
		var i;
		for (i = 0; i < divs.length; i++) {
			if (divs[i].className == 'visible') {
				divs[i].className = 'hidden';
				campo('link' + (i + 1)).className = 'inativo'
				i = (i == divs.length - 1) ? 0 : i + 1;
				divs[i].className = 'visible';
				campo('link' + (i + 1)).className = 'ativo'
				break;
			}
		}
	}


	var as = campo('links').getElementsByTagName('a');
	var i;
	for (i = 0; i < as.length; i++) {
		var f = function(v) {
			setSlide(as[v]);
		}
		addEvent(as[i], 'click', f.bind(this, i));
	}

	function setSlide(link) {
		var divs = campo('slides').getElementsByTagName('div');
		var i;
		for (i = 0; i < divs.length; i++) {
			divs[i].className = 'hidden';
		}
		var lis = campo('links').getElementsByTagName('li');
		for (i = 0; i < lis.length; i++) {
			lis[i].className = 'inativo';
		}
		link.parentNode.className = 'ativo';
		i = link.firstChild.data;
		
		t = campo('slide' + i).className = 'visible';
		clearInterval(tempo);
	}
	
	tempo = setInterval('nextSlide()', intervalo); */


	// Coloca a cor no atual
	$('#yer-1').css('background','#CFDDDD');
	
	// Inicia o player
	if ($('.item-player').size() > 1) {
		setTimeout('player (1);',5000);
	}
	// Caso so exista uma noticia
	else {
		$('#player-1').fadeIn(1000);			
	}


	// Pego o click na div da noticia e Mostro ela no parte principal do player
	$('.player-noticia').click(function(event){
		
		// Pego o id da noticia
        var id = '#pla' + $(this).attr('id');
		
		// Escondo o restante das noticias
        $('.item-player').hide();
		$('.player-noticia').css('background','#C8E6F3');
		
		// Mostro a noticia clicada
        $(id).show();
		$(this).css('background','#C8E6F3');
		// Paro player
        clearInterval(interPlayer);
		
		// Pego o numero da noticia atual
		var atual = id.replace('#player-','');
		
		// Famo a funcao do player a parti da noticia atual
		atual = atual - 1;
		setTimeout('player ('+atual+');',0);				
		
		// Cancela acao de click do link			
		event.preventDefault(event);
		return false;
	});	
	

	
	/* Ao mudar a opcao do select da faculdade, lista as questoes e libera campos */
	$("#sl_votar").change(function() {
		if($("#sl_votar").val()) {
			$("#loading").show();
			var options = {
				target: "#resultado_votacao",
				success: function() {
					$("#loading").hide("slow");
				}
			};
			$('#fr_votar').ajaxSubmit(options);
		}
	});
	/* Fim change select voto*/
	
	/* reportar defeito em video */
	$("#reportar_defeito").click(function() {
		$("#fr_reportar").toggle("slow");
	});
	/* Fim change select voto*/
	/* limpar campo da busca */
	$("#txt_busca").click(function() {
		$('#txt_busca').val("");
	});
	/* limpar campo da busca */
	$("#txt_busca_bottom").click(function() {
		$('#txt_busca_bottom').val("");
	});
	/* faz busca */
	$("#btn_search_top").click(function() {
		if($.trim($('#txt_busca').val()) != '' && $('#txt_busca').val() != 'Digite sua busca') {
			$('#form_search_top').submit();
		} else {
			alert("Digite algo para buscar");
		}
	});
	/* preenche o campo com o valor padrao */
	$("#txt_busca").blur(function() {
		if($.trim($('#txt_busca').val()) == '') {
			$('#txt_busca').val('Digite sua busca');
		};
	});

	$("#txt_busca_bottom").blur(function() {
		if($.trim($('#txt_busca_bottom').val()) == '') {
			$('#txt_busca_bottom').val('Digite sua busca');
		};
	});
	
	/* envio do form */
	$("form[@title=Formulario]").submit(function() {
		var enviar_ok = true;
		var form_name = $(this).attr('name');
		
		$("#loading").show();

		/* checar campos */
		$('#'+form_name+' :input[@title=requerido] ').each(function(){
			if($.trim($("#"+this.id).val()) == ''){
				$("#"+this.id).css({ background: "#FF9F9F" });
				enviar_ok = false;
			} else {
				$("#"+this.id).css({ background: "#B8F5B1" });
			}
		});

		if(enviar_ok) {
			var options = {
				success: function(msg) {
					$("#loading").hide("slow");
					// sucesso no envio
					
					if($.trim(msg) == "") {
						alert('E-mail enviado com sucesso. Em breve retornaremos. Obrigado!');
						$('#'+form_name).resetForm();
					} else {
						alert($.trim(msg));
					}
				}
			};
		
			$(this).ajaxSubmit(options);
			
			return false; // faz o submit normal
		} else {
			$("#loading").hide("slow");
			alert('Preencha os campos obrigatórios.');
			return false; //cancela submit normal
		}
	});
		$("#it_divulgue").focus(function() {$("#it_divulgue").select();	});	
	/* emoticons */
	$("img[@title=emoticons]").click(function() {
		var simbolo = $(this).attr('alt');
		var conteudo = $("textarea").val();
		
		$("textarea").val(conteudo + simbolo);
	});

});

/******** adicionar aos favoritos ********
* @uso onclick=javascript:addbookmark(); *
*****************************************/
function addbookmark() {
	var url      = document.location;
	var title    = document.title;

	if (window.sidebar) window.sidebar.addPanel(title, url,"");

	else if(window.opera && window.print){
		var mbm = document.createElement('a');
		mbm.setAttribute('rel','sidebar');
		mbm.setAttribute('href',url);
		mbm.setAttribute('title',title);
		mbm.click();
	}
	else if(document.all) {
		window.external.AddFavorite(url, title);
	}
}

// enter muda campo
function handleEnter (field, event) {
	var keyCode = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;
	if (keyCode == 13) {
		var i;
		for (i = 0; i < field.form.elements.length; i++)
		if (field == field.form.elements[i])
		break;
		i = (i + 1) % field.form.elements.length;
		field.form.elements[i].focus();
		return false;
	}
	else
	return true;
}

