var pagina = "";

function carregaFormulario(paginaParam)
{	
	pagina = paginaParam;
	
	var bg_popup = $("<div class='bg_popup'></div>");
		
	bg_popup.css({ "opacity": "0.7"});
		
	if($.browser.msie && $.browser.version.substr(0,1)=="6")
	{		
		var windowWidth = document.documentElement.clientWidth-24;
		var windowHeight = document.documentElement.clientHeight-24;		
		var janelaHeight = 0;
		
		if($("body").innerHeight() > windowHeight+20)
		{
			janelaHeight = $("body").innerHeight();
		}
		else
		{
			janelaHeight = windowHeight+20;	
		}
		
		bg_popup.css("height", janelaHeight);
		bg_popup.css("width", windowWidth+20);
		bg_popup.css("position","absolute");
		bg_popup.css("z-index",1);
		bg_popup.css("left",0);
		bg_popup.css("top",0);
 	}
	
	$("body").prepend(bg_popup);	
	
	$(".bg_popup:first").fadeIn("fast", 
		function()
		{		
			$.ajax(
				{
					contentType : "application/xhtml+xml; charset=ISO-8859-1",
					url: pagina,
					cache: false,
					//dataType: "html",
					error: function (XMLHttpRequest, textStatus, errorThrown)
					{
						abreErro("Não foi possível carregar o formulário. Tente novamente.",null);
					},
					success: function (data, textStatus) 
					{
						abrePopup($(data),"");
					}
				}
			);			
		}
	);	
}

function getNextZ()
{
    var maxZ = Math.max.apply
    	(null,$.map($('body > *'), 
	    	function(e,n)
	    	{
	            return parseInt($(e).css('z-index'))||1 ;
	        }
    	)
    );
    
    return maxZ+1;
}

function abreErro(mensagem,campo)
{
	if($(".bg_popup").size()==0)
	{
		$("body").prepend("<div class='bg_popup'></div>");		
		//$(".bg_popup:first").css({ "opacity": "0.7" , "z-index": ""+getNextZ()+"" });
		$(".bg_popup:first").fadeIn("fast", 
			function()
			{
				abrePopup("<form><p class='mensagem_erro'>"+mensagem+"</p></form>", "Erro");
				$(".popup_fechar:first").click
				(
					function()
					{
						$(campo).focus();
					}
				);		
			}
		);	
	}
	else
	{
		//$(".bg_popup:first").css({"z-index": ""+getNextZ()+"" });
		abrePopup("<form><p class='mensagem_erro'>"+mensagem+"</p></form>", "Erro");
		$(".popup_fechar:first").click
		(
			function()
			{
				$(campo).focus();
			}
		);	
	}
}


function abrePopup(elemento,titulo)
{	
	var popup = $("<div class='popup'></div>");
	//$("body").prepend("<div class='popup'></div>");	
	
	//$(".bg_popup:last").remove();
	//$(".popup:first").css({ "z-index": ""+getNextZ()+"" });	
	//$(".popup:first").css({ "z-index": 99});
	popup.prepend(elemento);
	
	if($.browser.msie && $.browser.version.substr(0,1)=="6")
	{
		$(popup).css("position","absolute");
		$(popup).css("z-index",3);
	}
	
	$(".bg_popup:first").after(popup);
	
	centralizaPopup();
	
	$(".popup:first").fadeIn("fast");
	$(window).resize(function()
	{
		centralizaPopup();
	});
	
	
	if($("#texto > .conteudo").height() < $("#texto > .conteudo").innerHeight())
	{
		iniciaScroll();
	}
	
}

function centralizaPopup()
{
	var windowWidth = document.documentElement.clientWidth-24;
	var windowHeight = document.documentElement.clientHeight-24;
	var popupHeight = $(".popup:first").height();
	var popupWidth = $(".popup:first").width();
		
	$(".popup:first").css({
		"position": "fixed",
		"top": windowHeight/2-popupHeight/2,
		"left": windowWidth/2-popupWidth/2
	});
	//only need force for IE6
		
	if($.browser.msie && $.browser.version.substr(0,1)=="6")
	{
		$(".popup:first").css("position","absolute");
		$(".popup:first").css("z-index",3);
		
		var janelaHeight = 0;
		
		if($("body").innerHeight() > windowHeight+20)
		{
			janelaHeight = $("body").innerHeight();
		}
		else
		{
			janelaHeight = windowHeight+20;	
		}
		
		$(".bg_popup:first").css("height", janelaHeight);
		$(".bg_popup:first").css("width", windowWidth+20);
		$(".bg_popup:first").css("margin-bottom", 1-windowHeight);
		$(".bg_popup:first").css("position","absolute");
		$(".bg_popup:first").css("z-index",1);
		$(".bg_popup:first").css("left",0);
		$(".bg_popup:first").css("top",0);
 	}
}

function fechaPopup()
{
	if($.browser.msie && $.browser.version.substr(0,1)=="6")
	{
		$(".popup:first").remove();
		$(".bg_popup:first").remove();
	}
	else
	{
		$(".popup:first").fadeOut("fast", 
			function()
			{	
				$(".popup:first").remove();
				$(".bg_popup:first").fadeOut
				("fast",
					function()
					{
						$(".bg_popup:first").remove();			
					}				
				);	
			}
		);
	}
}

function iniciaScroll()
{
	var tamanho = $("#texto:first").height();
	var tamanhoInterno = $("#texto:first > .conteudo:first").innerHeight();
	var diferenca = tamanhoInterno - tamanho;
	
	var barra_rolagem = $("<div id='barra_rolagem'></div>");	
	var btUp = $("<div id='up'></div>");	
	var btDown = $("<div id='down'></div>");
	barra_rolagem.append(btUp);	
	barra_rolagem.append(btDown);	
	
	if($.browser.msie && $.browser.version.substr(0,1)=="7")
	{		
		barra_rolagem.css("float","left");
		barra_rolagem.css("clear","left");
		barra_rolagem.css("top",0);
		barra_rolagem.css("left",0);
		barra_rolagem.css("margin-left",486);
		barra_rolagem.css("margin-right",-511);
		$(".tipo_popup").css("top",0);
		$(".tipo_popup").css("left",0);
		$(".tipo_popup").css("float","left");
		$(".tipo_popup").css("clear","left");
		$(".tipo_popup").css("position","relative");
		$(".tipo_popup").css("margin-bottom",-$(".tipo_popup").innerHeight());
		$(".tipo_popup").css("margin-right",-$(".tipo_popup").width());
		barra_rolagem.css("position","relative");
		//barra_rolagem.css("z-index",90);
		//barra_rolagem.css("margin-bottom",-501);
		//barra_rolagem.css("margin-right",-511);
		//barra_rolagem.css("margin-left",-511);
		//barra_rolagem.css("margin-left",-416);
		
		
		btUp.css("position","relative");
		
		btDown.css("position","relative");
		$(".tipo_popup").after(barra_rolagem);
 	}	
	else if($.browser.msie && $.browser.version.substr(0,1)=="6")
	{		
		barra_rolagem.css("float","left");
		barra_rolagem.css("clear","left");
		barra_rolagem.css("top",0);
		barra_rolagem.css("left",0);
		barra_rolagem.css("margin-left",236);
		barra_rolagem.css("margin-right",-511);
		$(".tipo_popup").css("top",0);
		$(".tipo_popup").css("left",0);
		$(".tipo_popup").css("float","left");
		$(".tipo_popup").css("clear","left");
		$(".tipo_popup").css("position","relative");
		$(".tipo_popup").css("margin-bottom",-$(".tipo_popup").innerHeight());
		$(".tipo_popup").css("margin-right",-$(".tipo_popup").width());
		barra_rolagem.css("position","relative");
		//barra_rolagem.css("z-index",90);
		//barra_rolagem.css("margin-bottom",-501);
		//barra_rolagem.css("margin-right",-511);
		//barra_rolagem.css("margin-left",-511);
		//barra_rolagem.css("margin-left",-416);
		
		
		btUp.css("position","relative");
		
		btDown.css("position","relative");
		$(".tipo_popup").after(barra_rolagem);
 	}
	else
	{
		$(".tipo_popup").before(barra_rolagem);	
	}
	
		
	var objeto = $("#texto:first > .conteudo:first");	
	
	objeto.css("top",0);
		
	$("#down").mousedown
	(
	 	function()
		{				
			objeto.animate
			(
				{
					top: (-diferenca)+"px"
				}, 
				1000
			);
		}
	);
	
	$("#up").mousedown
	(
	 	function()
		{			
			objeto.animate
			(
				{
					top: "0px"
				}, 
				1000
			);
		}
	);
		
	$("#down").mouseup
	(		
	 	function()
		{				
			objeto.stop();
		}
	);
		
	$("#up").mouseup
	(		
	 	function()
		{				
			objeto.stop();
		}
	);	
}

