// Função acionada depois que todas as imagens são lidas
functionLoader = function() {
	inicializarVideos();
	//inicializarPatrocinadores();
	inicializarPublicidade();
	FS.create();
};
// Funções da página
setPageEvents = function () {

};

// Vídeos
var arrVideos = new Array();
var numVideoAtual = 0;
trocaVideo = function (a) {
	try {
		numVideoAtual += a;
		var numVideos = arrVideos.length;
		if (numVideoAtual<0) {
			numVideoAtual = numVideos-1;
		}
		if (numVideoAtual>=numVideos) {
			numVideoAtual = 0;
		}
		$('videosImg').innerHTML = arrHTML[3];
		$('videoTitulo').innerHTML = arrVideos[numVideoAtual].tit;
		$('videoResumo').innerHTML = arrVideos[numVideoAtual].res;
		new imgPreLoad('uploads/imagens/'+arrVideos[numVideoAtual].img, function (e, _img) {
			if (e) {
				$('videosImg').innerHTML = '<table border="0" cellpadding="0" cellspacing="0" class="borda" align="center"><tr><td width="150" height="74"><img onmouseover="playMoldura(this);" onclick="location = \'mediacenter.php?id='+arrVideos[numVideoAtual].id+'\';" alt="" src="'+_img+'" /></td></tr></table>';
			} else {
				$('videosImg').innerHTML = 'Imagem n&atilde;o dispon&iacute;vel!';
			}
		});
	} catch (e) {
		alert((isIE) ? e.description : e);
	}
};

inicializarVideos = function () {
	try {
		var xml = new xmlConnection();
		with (xml) {
			if (create()) {
				setURL('videosXML.php');
				setMethod('POST');
				add('nocache', nocache(4));
				add('acao','home');
				onComplete = function () {
					setChildName('v');
					arrVideos = new Array();
					numVideoAtual = 0;
					for (var i = 0; i<getCountItens(); i++) {
						arrVideos[i] = new Object();
						arrVideos[i].id = getAttByName('id', i);
						arrVideos[i].img = getAttByName('img', i);
						arrVideos[i].tit = getAttByName('tit', i);
						arrVideos[i].res = getAttByName('res', i);
					}
					trocaVideo(0);
				};
				execute();
			}
		}
	} catch (e) {
		alert((isIE) ? e.description : e);
	}
};
inicializarEnquete = function () {
	try {
		var xml = new xmlConnection();
		with (xml) {
			if (create()) {
				setURL('enqueteXML.php');
				setMethod('POST');
				add('nocache', nocache(4));
				onComplete = function () {
					$('enquete.votar').src = "imagens/enqueteBotaoVotar.gif";
					$('enquete.carregando').innerHTML = '';
					$('enquete.carregando').style.display = "none";
					setChildName('p');
					$('enquete.titulo').innerHTML = getAttByName('pergunta', 0);
					setChildName('a');	
					if(getCountItens() != 0){
						$('enquete.votar').style.display = "block";
						var alternativas = '<table width="100%" border="0" cellspacing="0" cellpadding="0" style="padding-top:2px;padding-bottom:2px;">';							
						for(var i =0; i<getCountItens(); i++){
							alternativas += '<tr><td width="5%"><input name="enquetedb" type="radio" value="'+i+'" id="alternativa'+i+'" /></td>';
							alternativas += '<td width="95%" style="font:normal 11px Arial;color:#4d4d4b;vertical-align:middle;text-align:left;cursor:_pointer;" onclick="$(\'alternativa'+i+'\').checked = true;">'+getAttByName('alternativa', i)+'</a></td></tr>';
						}
						alternativas += '</table>';
						$('enquete.alternativas').style.paddingLeft = '10px';
						$('enquete.alternativas').innerHTML = alternativas;						
					}else{
						setChildName('r');
						if(getCountItens() != 0){
							$('enquete.votar').style.display = "none";
							var resposta = "";						
							for(var i =0; i<getCountItens(); i++){
								resposta += getAttByName('alternativa',i)+' - <b>'+getAttByName('votal',i)+' voto(s)</b><br /><table style="height:9px;" border="0" cellpadding="0" cellspacing="0"><tr>';
								resposta += '<td style="width:5px;height:9px;vertical-align:middle;"><img src="imagens/enqueteResultado1.gif" width="5" height="9" /></td>';
								resposta += '<td style="vertical-align:middle;"><img src="imagens/enqueteResultado2.gif" width="'+getAttByName('tb',i)+'" height="9" /></td>';
								resposta += '<td style="width:5px;height:9px;vertical-align:middle;"><img src="imagens/enqueteResultado3.gif" width="5" height="9" /></td>';
								resposta += '<td width="5" style="padding-left:5px;font:normal 11px Arial;color:#199fb3;vertical-align:middle;font-weight:bold;">'+getAttByName('pc',i)+'%</td>';
								resposta += '</tr></table><br>';
							}
							setChildName('t');
							resposta += '<div style="margin:5px;text-align:center; font:normal 12px Arial;color:#333;font-weight:bold;">Total de voto(s): '+getAttByName('total',0)+'</div>';							
							$('enquete.alternativas').style.paddingLeft = '15px';
							$('enquete.alternativas').innerHTML = resposta;
						}else{
							$('enquete.votar').style.display = "none";
							$('enquete.titulo').innerHTML = "Nenhuma enquete cadastrada!";
							$('enquete.alternativas').innerHTML = "";
						}
					}
				};
				execute();
			}
		}
	} catch (e) {
		alert((isIE) ? e.description : e);
	}
};

votar = function(){
	if(radio('enquetedb') == null){
		SA.open('Selecione uma alternativa!');
	}else{
		try {
			var xml = new xmlConnection();
			with (xml) {
				if (create()) {
					setURL('enqueteXML.php');
					setMethod('POST');
					add('acao','votar');
					add('al',radio('enquetedb'));					
					add('nocache', nocache(4));
					onComplete = function () {
						inicializarEnquete();
					};
					$('enquete.votar').src = "imagens/enqueteBotaoVotarEnviando.gif";
					execute();
				}
			}
		} catch (e) {
			alert((isIE) ? e.description : e);
		}		
	}
}
/* Eventos da página */
window.onstopscroll = function() {
	// Posiciona o Alerta, quando aberto
	SA.refresh();
	// Posiciona a imagem, quando aberta
	refreshImagem(true);
	SOC.alinha();
};
window.onload = function() {
	// Posiciona a seta e cria a "tween" para a mesma
	inicializarEnquete();
	new tween('setaBusca');
	var newLeft = getPos('buscaProfdeSaude').x+((getSize('buscaProfdeSaude').x-13)/2);
	with ($('setaBusca').style) {
		top = topSetaBusca+'px';
		left = newLeft+'px';
		display = 'block';
	}
	// Seta os eventos
	setAllEvents();
	// Inicia o preloader das imagens
	loadNextImage();
	// ALPHA
	new alpha('molduraImagem');
	new alpha('fundoBranco');
	// Cria o evento da moldura
	document.onmousemove = function(e) {
		e = e || event;
		stopMoldura(e);
	};
	molduraAtiva = true;
	// Inicia o preloader das imagens
	loadSiteImages();
	
	if (primeiraVez == '1') {
		SA.onClose = function() {
			SA.onClose = function() {};
			SA.open('<br />Para acessar o seu painel personalizado, entre com seu login e senha nos campos acima.<br />');
		};
		SA.open('Seu cadastro foi ativado com sucesso!');
	}
	
};
window.onresize = function() {
	SOC.alinha();
	// Posiciona a seta em baixo do item atual
	var newLeft = getPos(arrBusca[btBuscaAtivo]).x+((getSize(arrBusca[btBuscaAtivo]).x-13)/2);
	$('setaBusca').stopTween();
	with ($('setaBusca').style) {
		top = topSetaBusca+'px';
		left = newLeft+'px';
		display = 'block';
	}
	// Posiciona o fundo branco e a janela de busca avançada
	if (tipoBusca == 2) {
		posicionaFundoBranco();
	}
	// Posiciona o Alerta, quando aberto  
	SA.refresh();
};