/*-------------------------------------------------------------------------------------------------
	RETORNA A POSIÇÃO DO SCROLLBAR DO NAVEGADOR
-------------------------------------------------------------------------------------------------*/

function getPageScroll()
{

	var xScroll, yScroll;

	if (self.pageYOffset) 
	{
		yScroll = self.pageYOffset;
		xScroll = self.pageXOffset;
	} 
	else if (document.documentElement && document.documentElement.scrollTop)// Explorer 6 Strict
	{	 
		yScroll = document.documentElement.scrollTop;
		xScroll = document.documentElement.scrollLeft;
	} 
	else if (document.body) // all other Explorers
	{
		yScroll = document.body.scrollTop;
		xScroll = document.body.scrollLeft;	
	}

	arrayPageScroll = new Array(xScroll,yScroll) 
	return arrayPageScroll;
}



/*-------------------------------------------------------------------------------------------------
	RETORNA O TAMANHO DA JANELA
-------------------------------------------------------------------------------------------------*/

function getPageSize()
{
	
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) 
	{	
		xScroll = window.innerWidth + window.scrollMaxX;
		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
	{	
		if(document.documentElement.clientWidth)
		{
			windowWidth = document.documentElement.clientWidth; 
		} 
		else 
		{
			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;
	}	
	
	if(yScroll < windowHeight)
	{
		pageHeight = windowHeight;
	}
	else 
	{ 
		pageHeight = yScroll;
	}

	if(xScroll < windowWidth)
	{	
		pageWidth = xScroll;		
	} 
	else 
	{
		pageWidth = windowWidth;
	}

	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
}


/*-------------------------------------------------------------------------------------------------
	RESIZE IFRAME
-------------------------------------------------------------------------------------------------*/

function resizeIframe(iframe, janela) 
{
	var arrayPageSize = getPageSize();
	var largura = arrayPageSize[0];
	var altura  = arrayPageSize[1];
	
	try {
	
		frame    = janela.document.getElementById(iframe);
		innerDoc = (frame.contentDocument) ? frame.contentDocument : frame.contentWindow.document;
		
		objToResize = (frame.style) ? frame.style : frame;
		
		if(innerDoc=="[object]")
		{
			tamanho = innerDoc.body.scrollHeight; //IE
			auxilio = 0;
		}
		else
		{
			tamanho = innerDoc.body.offsetHeight; //Mozilla
			auxilio = 0;
		}
		
		objToResize.height = Number(tamanho);
		document.getElementById('topo').focus(); //Hack
	}
	
	catch (e) 
	{
		window.status = "";
	}
}

/*-------------------------------------------------------------------------------------------------
	REDIRECT IFRAME
-------------------------------------------------------------------------------------------------*/

function redirectIframe(frame,pagina)
{
	this[frame].location = pagina;
}

/*-------------------------------------------------------------------------------------------------
	MUDA PÁGINA
-------------------------------------------------------------------------------------------------*/

function mudaPagina(url)
{
	location.href = url;
}


/*-------------------------------------------------------------------------------------------------
	POPUP CENTRALIZADO
-------------------------------------------------------------------------------------------------*/

function abrePopup(pagina,largura,altura,rolagem)
{
	var posX;
	var posY;
	posX = (window.screen.width - largura) / 2;
	posY = (window.screen.height - altura) / 2;
	window.open(pagina,"_blank","width="+largura+",height="+altura+",top="+posY+",left="+posX+",scrollbars="+rolagem+",toolbar=no,location=no,directories=no,menubar=no,resizable=no,menubar=no");
}


/*-------------------------------------------------------------------------------------------------
	POPUP POSIÇÃO PADRÃO
-------------------------------------------------------------------------------------------------*/

function abrePopupMenor(pagina,largura,altura,rolagem)
{
	window.open(pagina,"_blank","width="+largura+",height="+altura+",top=80,left=80,scrollbars="+rolagem+",toolbar=no,location=no,directories=no,menubar=no");
}


/*-------------------------------------------------------------------------------------------------
	CARREGA DIV DE CURSOS
-------------------------------------------------------------------------------------------------*/

function initCurso(cor, calculo)
{
	var arrayPageScroll = getPageScroll();
	var posy			= arrayPageScroll[1];
	var posx			= arrayPageScroll[0];
	var xdiv			= (arrayPageSize[0]-970)/2;
	
	if (calculo == "S")
	{
		var hdiv	 = $("#"+cor).height();
		var iframe	 = $("#conteudo").parent().height();
		var posfinal = (iframe+110)-(hdiv+38);
		
		$("#"+cor).css("top", posfinal+"px");
	}
	else
	{
		$("#"+cor).css("top", "345px");	
	}
	
	$("#"+cor).css("left", xdiv+"px");
	$("#"+cor).fadeIn("slow");
}

function initCursoOLD(cor, calculo)
{
	$("#divCursos").attr("corAba", cor);
	
	$("#divCursosLoader").ajaxStart(function(){ 
		
		var x_div = (arrayPageSize[0]-208)/2;
		var y_div = (arrayPageSize[1]-13)/2;
		
		$("#divBgCursos").show();
		$("#divBgCursos").width(arrayPageSize[0]);
		$("#divBgCursos").height(arrayPageSize[1]);
		
		$(this).show(); 
		$(this).css("left", x_div+"px");
		$(this).css("top", y_div+"px");
		
		$("#divCursos").fadeOut("fast"); 
		
	});
	
	$("#divCursosLoader").ajaxStop(function(){ 
		
		var arrayPageScroll = getPageScroll();
		var posy			= arrayPageScroll[1];
		var posx			= arrayPageScroll[0];
		var xdiv			= (arrayPageSize[0]-970)/2;
		
		if (calculo == "S")
		{
			var hdiv	 = $("#divCursos").height();
			var iframe	 = $("#conteudo").parent().height();
			var posfinal = (iframe+110)-(hdiv+38);
			
			$("#divCursos").css("top", posfinal+"px");
		}
		else
		{
			$("#divCursos").css("top", "345px");	
		}
		
		$("#divCursos").css("left", xdiv+"px");
		$("#divCursos").fadeIn("slow");
		$("#divBgCursos").hide();
		$(this).hide();
		
	});
	
	$.post("curso_"+cor+".php", { cor: cor }, function(data) { $("#divCursos").empty().html(data); });	
}

function endCurso()
{
	$("#divCursos").fadeOut("fast");
}

function getCurso(url)
{
	conteudo.location = url;
	endCurso();	
}


/*-------------------------------------------------------------------------------------------------
	BUSCA CIDADES DE ACORDO COM O ESTADO
-------------------------------------------------------------------------------------------------*/

function buscaCidade(e, c)
{
	$(e).change(function(){
		
		$(c).empty().html("<option>Carregando lista de cidades...</option>");
		
		$.ajax({ 
			type: 'POST', 
			url: 'acoes.php?acao=busca_cidade', 
			data: { uf : $(e).val() }, 
			dataType: 'xml',
			success: function(xml) {
				
				var opt  = "";
					opt += "\n<option value=\"\"></option>\n";
					
				$(xml).find("cidade").each(function(){ 
                    
					var nome = $(this).find('nome').text();
					var id   = $(this).find('id').text();
					
					opt += "<option value=\""+nome+"\">"+nome+"</option>\n"; 
					
				});
				
				$(c).empty().html(opt);
			}
		});
	});		
}