/****** FUNÇÕES PARA ROLAGEM DOS THUMBS ******/
var intervalScroll;

var posScroll0 = 0;
var posScroll1 = 0;
var posScroll2 = 0;
var posScroll3 = 0;
var posScroll4 = 0;
var posScroll5 = 0;

var id_thumb;
var id_cont_thumb;

function rolaThumbs(ps,sid_thumb) {
	var tPulo = 20;
	var largura_cont = $(id_cont_thumb).offsetWidth;
	var largura_fotos = $(id_thumb).offsetWidth;

	switch (sid_thumb) {
		case 'thumbs0':
			var posScroll = posScroll0;
			break;
		case 'thumbs1':
			var posScroll = posScroll1;
			break;
		case 'thumbs2':
			var posScroll = posScroll2;
			break;
		case 'thumbs3':
			var posScroll = posScroll3;
			break;
		case 'thumbs4':
			var posScroll = posScroll4;
			break;
		case 'thumbs5':
			var posScroll = posScroll5;
			break;
	}

	if (typeof(arguments[1]) == 'number')
		posScroll = arguments[1];
	else {
		if (ps == 1) {
			if (posScroll + tPulo > (largura_fotos-largura_cont))
				posScroll = (largura_fotos-largura_cont);
			else
				posScroll += tPulo;
		}
		else {
			if (posScroll-tPulo < 0)
				posScroll = 0;
			else
				posScroll -= tPulo;
		}
	}
	new Effect.Scroll(id_cont_thumb, {x:posScroll, y:0, duration:0});
	
	switch (sid_thumb) {
		case 'thumbs0':
			posScroll0 = posScroll;
			break;
		case 'thumbs1':
			posScroll1 = posScroll;
			break;
		case 'thumbs2':
			posScroll2 = posScroll;
			break;
		case 'thumbs3':
			posScroll3 = posScroll;
			break;
		case 'thumbs4':
			posScroll4 = posScroll;
			break;
		case 'thumbs5':
			posScroll5 = posScroll;
			break;
	}
	
	return false;
}

function ativaScroll(ps,sid_thumb,sid_cont_thumb) {
	id_thumb = sid_thumb;
	id_cont_thumb = sid_cont_thumb;

	rolaThumbs(ps,sid_thumb);
	intervalScroll = setInterval('rolaThumbs(' + ps + ',"' + sid_thumb + '")',1);
}
function desativaScroll() { clearInterval(intervalScroll); }
/************************************************/

/*function addEventosNav() {
	var lis = $('nav').getElementsByTagName("LI");
	for (var i=0;i < lis.length; i++) {
		if (lis[i].className == "ant")
			lis[i].firstChild.onmousedown = function() { ativaScroll(0);return false; }
		else
			lis[i].firstChild.onmousedown = function() { ativaScroll(1);return false; }
			
		lis[i].firstChild.onmouseup = function() { desativaScroll();return false; }
	}
}*/

function showOverlay() {
	var dimensoes = getPageSize();
	if (dimensoes[1] < 750)
		$('mascara_pop').style.height = (dimensoes[1]+120) + "px";
	else
		$('mascara_pop').style.height = dimensoes[1] + "px";
	if (dimensoes[0] < 950)
		$('mascara_pop').style.width = "950px";
	else
		$('mascara_pop').style.width = "100%";
	Element.show('mascara_pop');
}
function popUpProjeto(id, id_img, lang) {
	showOverlay();
	abrePopImg(id,id_img, lang);
}

function popUpMidia(diretorio,id_img) {
	showOverlay();
	abrePopMidia(diretorio,id_img);
}
function popUpCurso(diretorio,id_img) {
	showOverlay();
	abrePopCurso(diretorio,id_img);
}

function apresentaPopUp() {
	if (arguments[0] && arguments[0] == true) {
		var y_teste = getPageScroll(); 
		$('cont_img').style.top = (y_teste[1]+20) + "px";
	}
	Effect.BlindDown('cont_img',{duration:0.4});
	confereImgGrCarregada();
}
function abrePopImg(id, id_img, lang) {
	var url = "inc/ajax_popup_projetos.php";
	var params = "id=" + id + "&id_img=" + id_img + "&origem_ajax=true" + "&lang=" + lang;
	var ajax = new Ajax.Request(url, {method: 'get', parameters:params, asynchronous:true, onSuccess:function(t) { adicionaConteudo(t.responseText,'cont_ext_pop',true,'apresentaPopUp()');}});
}
function abrePopMidia(diretorio,id_img) {
	var url = "inc/ajax_popup_midia.php";
	var params = "diretorio=" + diretorio + "&id_img=" + id_img + "&origem_ajax=true";
	var ajax = new Ajax.Request(url, {method: 'get', parameters:params, asynchronous:true, onSuccess:function(t) { adicionaConteudo(t.responseText,'cont_ext_pop',true,'apresentaPopUp(true)');}});
}
function abrePopCurso(diretorio,id_img) {
	var url = "inc/ajax_popup_curso.php";
	var params = "diretorio=" + diretorio + "&id_img=" + id_img + "&origem_ajax=true";
	var ajax = new Ajax.Request(url, {method: 'get', parameters:params, asynchronous:true, onSuccess:function(t) { adicionaConteudo(t.responseText,'cont_ext_pop',true,'apresentaPopUp(true)');}});
}

function trocaImg(id, n_img) {
	Effect.Fade('img_gr');
	Effect.Appear('img_loading');
	$('nav_imgs').innerHTML = '<li class="loading">carregando...</li>';
	var url = "inc/ajax_nav_popup.php"
	var params = "id=" + id + "&id_img=" + n_img;
	var ajax = new Ajax.Request(url, {method: 'get', parameters:params, asynchronous:true, onComplete:function(t) { trocarImagem(t.responseText, id, n_img); }});
}
function trocaImgMidia(diretorio, n_img) {
	Effect.Fade('img_gr');
	Effect.Appear('img_loading');
	$('nav_imgs').innerHTML = '<li class="loading">carregando...</li>';
	var url = "inc/ajax_nav_midia.php"
	var params = "diretorio=" + diretorio + "&id_img=" + n_img;
	var ajax = new Ajax.Request(url, {method: 'get', parameters:params, asynchronous:true,
						onComplete:function(t) {
							trocarImagem(t.responseText, diretorio, n_img, true);
						}});
}
function trocaImgCurso(diretorio, n_img) {
	Effect.Fade('img_gr');
	Effect.Appear('img_loading');
	$('nav_imgs').innerHTML = '<li class="loading">carregando...</li>';
	var url = "inc/ajax_nav_curso.php"
		var params = "diretorio=" + diretorio + "&id_img=" + n_img;
	var ajax = new Ajax.Request(url, {method: 'get', parameters:params, asynchronous:true,
		onComplete:function(t) {
		trocarImagem(t.responseText, diretorio, n_img, 'cursos');
	}});
}
function trocarImagem(navegacao, id, n_img) {
	img_nova = new Image();
	img_nova.onload=function(){
		if (img_nova.width < img_nova.height)
			$('cont_img').className = 'ver';
		else
			$('cont_img').className = 'hor';
		$('cont_imgs_gr').innerHTML = "";	
		var img_loading = document.createElement("img");
		img_loading.src = "imagens/fotos/loading.gif";
		img_loading.alt = "Carregando...";
		img_loading.id = "img_loading";
		//alert(img_nova + " - " + img_loading);
		$('cont_imgs_gr').appendChild(img_loading);
		$('cont_imgs_gr').appendChild(img_nova);
		apresentaImgTrocada();
		adicionaConteudo(navegacao, 'nav_imgs');
	}
	img_nova.id = "img_gr";
	img_nova.style.display = "none";
	if (arguments[3] && arguments[3] == true)
		img_nova.src = "imagens/fotos/midias/" + id + "/gr/" + n_img + ".jpg";
	else if (arguments[3] && arguments[3] == 'cursos')
		img_nova.src = "imagens/fotos/cursos/" + id + "/gr/" + n_img + ".jpg";
	else
		img_nova.src = "imagens/fotos/projetos/" + id + "/gr/" + n_img + ".jpg";
}
function apresentaImgTrocada() {
	if (arguments[0] && arguments[1]) {
		var img_fade = arguments[0];
		var img_appear = arguments[1];
	}
	else {
		var img_fade = 'img_loading';
		var img_appear = 'img_gr';
	}
	Effect.Fade(img_fade,{duration:0.3});
	Effect.Appear(img_appear,{duration:0.3});
}
function confereImgGrCarregada() {
	if (window.opera)
	{
		if (arguments[0])
			eval(arguments[0]);
		apresentaImgTrocada();
	}
	else
	{
		if (arguments[0])
			eval(arguments[0]);
		Event.observe('img_gr', 'load', apresentaImgTrocada, false);
	}
}
function desativaPopUp() {
	Element.hide('cont_img');
	Element.hide('mascara_pop');
}
function adicionaConteudo(cont, id_layer) {
	cont=cont.replace(/\+/g," ");
	cont=unescape(cont);
	$(id_layer).innerHTML = cont;
	
	if (arguments[2] && arguments[2] == true) {
		eval(arguments[3]);
		//if (window.opera)
			//apresentaPopUp();
		//else
			//Event.observe(arguments[3], 'load', eval(arguments[4]), false);
	}
}

function marcaMenuOrdenacao(cat) {
	var itens_menu = $("menu_ordenacao").getElementsByTagName("a");

    for(var i=0;i<itens_menu.length;i++)
    {
    	if (itens_menu[i].className == "ativo")
    		itens_menu[i].className = "";
    	if (itens_menu[i].href.search("cat=" + cat) != -1)
	    	itens_menu[i].className = "ativo";
	}
}
function ordenaPecas(cat, inicio, lang) {
	$('projetos').innerHTML = '<img src="imagens/fotos/loading.gif" alt="Carregando..." class="loading" />';
	$('nav_ant_prox').innerHTML = "&nbsp;";
	
	marcaMenuOrdenacao(cat);

	var params = "cat=" + cat + "&inicio=" + inicio + "&lang=" + lang;
	
	var url = "inc/ajax_nome_cat.php";
	var ajax = new Ajax.Request(url, {method: 'get', parameters:params, asynchronous:true, onSuccess:function(t) { adicionaConteudo('\'' + t.responseText + '\'','teste');}});	
	
	var url = "inc/ajax_ordena_projetos.php";
	var ajax = new Ajax.Request(url, {method: 'get', parameters:params, asynchronous:true, onSuccess:function(t) { adicionaConteudo(t.responseText,'projetos');}});	

	var url = "inc/ajax_nav_projetos.php";
	var ajax = new Ajax.Request(url, {method: 'get', parameters:params, asynchronous:true, onSuccess:function(t) { adicionaConteudo(t.responseText,'nav_ant_prox');}});	

}

function mudaImagemDestaque(id_proj,id_img) {
	$('nav_destaque').innerHTML = '<li class="u">&nbsp;</li><li>&nbsp;</li><li>&nbsp;</li>';

	var params = "id_projeto=" + id_proj + "&id_img=" + id_img;
	
	var url = "inc/ajax_nav_destaque.php";
	var ajax = new Ajax.Request(url, {method: 'get', parameters:params, asynchronous:true, onSuccess:function(t) { adicionaConteudo(t.responseText,'nav_destaque');}});	

	var imagens = $('cont_img_destaque').getElementsByTagName("img");
	var txt_alt = imagens[0].alt;
	var src_img_atual = imagens[0].src;
	var img_atual = src_img_atual.substring((src_img_atual.lastIndexOf('/')+1),src_img_atual.length);
	
	$('cont_img_destaque').innerHTML = '<img src="imagens/fotos/projetos/' + id_proj + '/destaque/' + id_img + '.jpg" alt="' + txt_alt + '" style="display:none;" /><img src="imagens/fotos/projetos/' + id_proj + '/destaque/' + img_atual + '" alt="' + txt_alt + '" />';
	var imgs_novas = $('cont_img_destaque').getElementsByTagName("img");

	if (window.opera)
		efetivaTroca();
	else
		Event.observe(imgs_novas[0], 'load', efetivaTroca, false);
	
	function efetivaTroca() {
		Effect.Fade(imgs_novas[1]);
		Effect.Appear(imgs_novas[0]);
	}
}

function alteraDestaque(id_destaque, lang) {
	$('projeto_destaque').innerHTML = '<img src="imagens/fotos/loading.gif" alt="Carregando..." class="loading" />';

	var params = "id_destaque=" + id_destaque;
	
	var url = "inc/ajax_altera_destaque.php";
	var ajax = new Ajax.Request(url, {method: 'get', parameters:params, asynchronous:true, onSuccess:function(t) { adicionaConteudo(t.responseText,'projeto_destaque');}});	
	
}

function getPageScroll(){

	var yScroll;

	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
	} else if (document.documentElement && document.documentElement.scrollTop){	 // Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
	} else if (document.body) {// all other Explorers
		yScroll = document.body.scrollTop;
	}

	arrayPageScroll = new Array('',yScroll) 
	return arrayPageScroll;
}

function getPageSize(){
	
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}


	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
}

/********************************/
/*** FUNCIONAMENTO NEWSLETTER ***/
/********************************/
function cadastraNewsletter(email, lang) {
	$('status_mailing').className = 'erro'; 
	Element.show('status_mailing'); 
	if (lang == "pt")
		$('status_mailing').innerHTML = "Carregando...";
	else
		$('status_mailing').innerHTML = "Loading...";
	var url = "mailing_ajax.php";
	var params = "email=" + email;
	var ajax = new Ajax.Request(url, {method: 'get', parameters:params, asynchronous:true, onSuccess:function(t) { limpaCamposNews(t.responseText);}});
}
function limpaCamposNews(cont) {
	cont=cont.replace(/\+/g," ");
	cont=unescape(cont);
	//alert(cont.search("erro"));
	if (cont.search("sucesso") != -1) {
		$("email_mailing").value = "Digite aqui seu e-mail";
		$("status_mailing").className = 'sucesso';
	}
	else
		$("status_mailing").className = 'erro';
	adicionaConteudo(cont,'status_mailing');
}
function apagarInput(id_input, valor_input){
	if($(id_input).value == valor_input)
		$(id_input).value = "";
	return false;
}

function reescreverInput(id_input, valor_input){
	if($(id_input).value == "")
		$(id_input).value = valor_input;
	return;
}
/********************************/
/*** FUNCIONAMENTO NEWSLETTER ***/
/********************************/