/*
* ARQUIVO: gerais.js
* AUTOR: Nícolas
* VERSÃO: 1.0
*/
gl = 0;
function menuDrop()
{
	obj = document.getElementById('drop');
	base = document.getElementById('select');
	obj.style.left = findPosX(base) - 1 + "px";
	obj.style.top = findPosY(base) + 18 + "px";
	if( gl == 0 )
	{
		abreDiv('drop','display');
		gl = 1;
	}
	else
	{
		fechaDiv('drop','display');
		gl = 0;
	}
	
}

function delay(funcao, tempo)
{
	setTimeout(funcao, tempo);		
}

function abreDiv(id, metodo)
{
	obj = document.getElementById(id);
	if(metodo == 'display')
	{
		obj.style.display = 'block';
	}
	else
	{
		obj.style.visibility = 'visible';
	}
}

function fechaDiv(id, metodo)
{
	obj = document.getElementById(id);
	if(metodo == 'display')
	{
		obj.style.display = 'none';
	}
	else
	{
		obj.style.visibility = 'hidden';
	}
}

function findPosX(obj)
{
	var curleft = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curleft += obj.offsetLeft
			obj = obj.offsetParent;
		}
	}
	else if (obj.x)
		curleft += obj.x;
	return curleft;
}

function findPosY(obj)
{
	var curtop = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curtop += obj.offsetTop
			obj = obj.offsetParent;
		}
	}
	else if (obj.y)
		curtop += obj.y;
	return curtop;
}


/**********************************************************
* Função makeRequest(url). Faz um request com AJAX.
* Está sendo usado no calendário do site
* Busca feita no Wikipédia, retornou a página
* http://developer.mozilla.org/en/docs/AJAX:Getting_Started
* Resumindo, a função recebe uma url, e a executa por
* baixo dos panos, retorna pela função retornaValor
* Pesquisa por: Nícolas.
* Data: 16/02/2006.
* Data de alteração: n/d.
**********************************************************/

var http_request = false;

function makeRequest(url) {
	http_request = false;

	if (window.XMLHttpRequest) { // Mozilla, Safari,...
		http_request = new XMLHttpRequest();
		if (http_request.overrideMimeType) {
			http_request.overrideMimeType('text/xml');
			// See note below about this line
		}
	} else if (window.ActiveXObject) { // IE
		try {
			http_request = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				http_request = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {}
		}
	}

	if (!http_request) {
		alert('Giving up :( Cannot create an XMLHTTP instance');
		return false;
	}
	http_request.onreadystatechange = retornaValor;
	http_request.open('GET', url, true);
	http_request.send(null);

}

/*
0 (não inicializado) 
1 (carregando) 
2 (carregado) 
3 (interativo) 
4 (completo) 
*/
function retornaValor() {

	if (http_request.readyState == 1) {
		document.getElementById('retCalendario').innerHTML = "";
	}
	
	if (http_request.readyState == 4) {
		document.getElementById('retCalendario').innerHTML = http_request.responseText;
	}

}

function testaVazio(campo,comparacao,formulario,mensagem,erro) 
{
	if (erro == true)
		return true;
	obj1 = eval("document."+formulario+"."+campo);
	if (obj1.value == comparacao)
	{
		alert (mensagem);
		obj1.focus();
		return true;
	}
	else 
		return false;
}


function fechaFoto()
{
	var obj_foto 	= document.getElementById("foto");
	var obj_img 	= document.getElementById("imagem");
	var obj_descr	= document.getElementById("desc_foto");
	obj_foto.style.display 	= 'none';
	obj_img.src = "";
	obj_descr = "";
}


function mostraFoto (galeria, img, descr, refer, tam_desc)
{
	var obj_foto 	= document.getElementById("foto");
	var obj_img 	= document.getElementById("imagem");
	var obj_descr	= document.getElementById("desc_foto");
	var obj_refer	= document.getElementById(refer);
	var obj_site 	=  document.getElementById('corpo');
	// Pega a posição da foto que será aberta
	if (document.all) 
		altura_site = document.body.clientHeight;
	else 
		altura_site = window.innerHeight;
	
	largura_site = obj_site.offsetWidth;
	
	y = altura_site / 4;
	x = (largura_site / 2) - (tam_desc / 2) + 100;

	// Estipula a posição top e left que pegou antes
	obj_foto.style.top 	= y + "px";
	obj_foto.style.left = x + "px";
	// Se a div estiver com display = none então altera
	if(obj_foto.style.display == 'none')
	{
		obj_foto.style.display = 'block';
		
		//fotoAjax('abrefoto.php?galeria='+galeria+'&imagem='+img);
		
		obj_img.src = "imagens/galeria/"+ galeria + "/grandes/" + img;
		obj_descr.style.width = tam_desc + "px";
		obj_descr.innerHTML = descr;
	}
	else
	{
		fechaFoto();
	}
}

function mostraDiv(campo)
{				
	if( document.getElementById(campo).style.display == 'none' )
	{
		document.getElementById(campo).style.display = 'block';			
	}
	else
	{
		document.getElementById(campo).style.display = 'none';			
	}
}

function mostraDivagenda(calendario)
{				
	makeRequest('inc/inc_calendario.php?' + calendario);
	
	if( document.getElementById('base_calendario').style.display == 'none' )
	{
		document.getElementById('base_calendario').style.display = 'block';			
	}
	
	if( document.getElementById('fechar_agenda').style.display == 'none' )
	{
		document.getElementById('fechar_agenda').style.display = 'block';			
	}

}

function fechaDivagenda(campo)
{				
	document.getElementById(campo).style.display = 'none';
}


function abreEvento(id, id2, direcao, fixo)
{
	objPri = document.getElementById(id2);
	objDiv = document.getElementById(id);
	
	leftL = findPosX(objPri);
	topT = findPosY(objPri);

	var IE = document.all?true:false
	
	// If NS -- that is, !IE -- then set up for mouse capture
	if (!IE) document.captureEvents(Event.MOUSEMOVE)
	
	
	var tempX = 0
	var tempY = 0
	function getMouseXY(e) 
	{
		if (IE) 
		{ 
			tempX = event.clientX + document.body.scrollLeft
			tempY = event.clientY + document.body.scrollTop
		} 
		else 
		{  // grab the x-y pos.s if browser is NS
			tempX = e.pageX
			tempY = e.pageY
		}  
		// catch possible negative values in NS4
		if (tempX < 0){tempX = 0}
		if (tempY < 0){tempY = 0}  

		if(fixo != 1)
		{
			if(direcao == 0)
			{
				objDiv.style.left = tempX - 340 + "px";
				objDiv.style.top = tempY - 320 + "px";
			}
			else
			{
				objDiv.style.left = tempX  + "px";
				objDiv.style.top = tempY - 10  + "px";
			}
		}
	}
	
	document.onmouseover = getMouseXY;
	objDiv.style.display = 'block';
}

function fechaEvento(id)
{
	objPri = document.getElementById('calBase');
	objDiv = document.getElementById(id);
	
	objDiv.style.display = 'none';
}

function opacity(id, opacStart, opacEnd, millisec) {
    //speed for each frame
    var speed = Math.round(millisec / 100);
    var timer = 0;

	//determine the direction for the blending, if start and end are the same nothing happens
    if(opacStart > opacEnd) {
		for(i = opacStart; i >= opacEnd; i--) {
			setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
            timer++;
        }
		//document.getElementById(id).style.display = 'none';
	} else if(opacStart < opacEnd) {
        for(i = opacStart; i <= opacEnd; i++)
            {
            setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
            timer++;
		}
    }

}

//change the opacity for different browsers
function changeOpac(opacity, id) {
    var object = document.getElementById(id).style;
    object.opacity = (opacity / 100);
    object.MozOpacity = (opacity / 100);
    object.KhtmlOpacity = (opacity / 100);
    object.filter = "alpha(opacity=" + opacity + ")";
	if(opacity > 0) document.getElementById(id).style.display = 'block';
	else document.getElementById(id).style.display = 'none';
} 

function shiftOpacity(id, millisec) {
    
	//if an element is invisible, make it visible, else make it ivisible
    if(document.getElementById(id).style.opacity != 1 ) 
	{
		opacity(id, 0, 100, millisec);
    } 
	else
 	{
		opacity(id, 100, 0, millisec);
	}
}

