window.defaultUrl = "";

$(function(){
	
	if(!empty(s = $(document).find("SCRIPT")))
	{
		window.defaultUrl = s.get(0).src.toString().split("js/")[0];
	}

	var a = document.links;
	for(var i=0; i<a.length; i++)
	{
		if(a[i].rel && a[i].rel == "thumb")
		{
			a[i].onclick = ampliaHandler;
		}
	}	
	
	showAguarde.create();
	
	$("#activity").ajaxStart(function(){
		window.showAguarde();
	}).ajaxComplete(function(){
		window.hideAguarde();
	});

	$("#conteudo form fieldset select option:odd").addClass("odd");
	
	/* menu de extras like combo box */
	$("#conteudo .user-menu ul.menu li a.extras").click(function(){
		
		$(this).parent().find("ul").show();
		$(document.body).unbind().mousedown(function(e){
			try {
				if(empty($($target(e)).parents("ul.menu")))
				{
					$("#conteudo .user-menu ul.menu li a.extras").parent().find("ul").hide();
				}
			} catch(ex) {}
		}).keyup(function(e){
			e.key = (e.which + 1 || e.keyCode + 1) - 1 || 0;
			if(e.key == 27)
			{
				$("#conteudo .user-menu ul.menu li a.extras").parent().find("ul").hide();
			}
		});
		
		return false;
		
	});
	
	if($("#topo .data .month").length > 0)
	{
		var $months = ['-', 'janeiro', 'fevereiro', 'março', 'abril', 'maio', 'junho', 'julho', 'agosto', 'setembro', 'outubro', 'novembro', 'dezembro'];
		$("#topo .data .month").text($months[parseFloat($("#topo .data .month").text())]);
	}
	
	$("#conteudo form").enterAsTab();
	
});

function ampliaHandler()
{	
	ampliarImg(this.href.toString());
	return false;
}

function ampliarImg(src)
{
	window.open(window.defaultUrl + "show/?src=" + src, "aw", "width=200, height=200,resizable=yes, status");
}

function showAguarde()
{
	$("#activity").remClass("disabled");
	
	//fixed activity...
	window.onscroll = showAguarde.fixed;
	window.onscroll();
	
	showAguarde.inProcess = true;	
}
showAguarde.create = function()
{
	var d = $('<div id="activity" class="disabled">Carregando Dados...</div>');
	$(document.body).append(d);
}
showAguarde.fixed = function()
{
	var a = $("#activity");
	if(!empty(a))
	{
		var y1 = document.documentElement.scrollTop || 0;
		var y2 = document.body.scrollTop || 0;
		var y3 = window.scrollY || 0;
		
		var y = Math.max(y1, Math.max(y2, y3));
			
		a.css({"top": (y + 200) + "px"});
	}
	else
	{
		window.onscroll = function(){};
		delete window.onscroll;
	}
}
showAguarde.inProcess = false;

function hideAguarde(){ $("#activity").addClass("disabled"); showAguarde.inProcess = false; }