window.defaultUrl = "";
window.widgets_css = false;

$(function(){
	
	if(!empty(s = $(document).find("SCRIPT")))
	{
		window.defaultUrl = s.attr("src").split("js/")[0];
	}
	
	$("#content form fieldset select").find("option:odd").addClass("odd");	
	
	$("ul li:first-child").addClass("first");
	$("ul li:last-child").addClass("last");
	
	$("#content table tr:odd").addClass("odd");
	
	$("#categorias li:odd").addClass("odd");
	$("#categorias li:last-child").addClass("last");
	
	showAguarde.create();
	
	$("#activity").ajaxStart(function(){
		window.showAguarde();
	}).ajaxComplete(function(){
		window.hideAguarde();
	});
	
	if($("div.message").length > 0)
	{
		$("div.message").css({"opacity": 0.9}).hide().slideDown("normal");
		window.hideMessage = function(){ $("div.message").stop().slideUp("normal"); };
		setTimeout("window.hideMessage();", 5000);
	}
	
	$("#q").initValue();
	
	if($("div.change-size").length > 0)
	{
		$("div.change-size a").click(function(){
			var $sizes = ["normal", "medium", "large"];
			for(var $i=0; $i<$sizes.length; $i++) $($(this).attr("rel")).removeClass($sizes[$i]);
			$($(this).attr("rel")).addClass($(this).attr("href").substr(1));
			return false;
		});
	}
	
	$("#header h1").flash({
		src: $.url() + "swf/topo.swf",
		wmode: "transparent",
		width: "100%",
		height: "100%"
	});
	
	/* VIDEOS */
	$("div.video a.uri").each(function(){
		var $url = $(this).attr("href");
		$(this).parent().youtube({
			url: $url,
			wmode: "opaque",
			width: "100%",
			height: "100%"
		});
	});
	
	/* CAPA */
	if($(document.body).attr("id") == "capa")
	{
		//showBannerFlutuante();
	}
	
});

function showBannerFlutuante()
{
	$("#window-mask").remove();
	
	var closeBanner = window.closeBanner = function()
	{
		$("#window-mask").fadeOut("normal", function(){
			$(this).remove();
		});
		$("#banner-flutuante").fadeOut("normal");
	}	
	
	var $mask = $("<div id='window-mask'></div>").css({
		"position": "absolute",
		"z-index": 9999,
		"background-color": "#000",
		"display": "none",
		"top": 0,
		"left": 0,
		"width": $(window).width(),
		"height": $(document).height()
	});
	
	$(document.body).prepend($mask);
	
	$mask.fadeTo("normal", 0.3).click(closeBanner);
	
	$("#banner-flutuante").fadeIn("normal").find("a.bt-close").click(function(){
		window.closeBanner();
		return false;
	});
	
	setTimeout("window.closeBanner();", 5000);
}

$.fn.normalizeHeight = function(height)
{
	var maxHeight = 0;
	$(this).each(function(){
		maxHeight = Math.max(maxHeight, $(this).height());
	}).each(function(){
		if(!height)
		{
			$(this).css({
				'padding-bottom': maxHeight-$(this).height()+'px'
			});
		}
		else
		{
			$(this).css({
				'height': (maxHeight) + 'px'
			});
		}
	});
	
	return this;
};

$.url = function(webroot)
{
	if(!webroot)
	{
		if(window.defaultUrl.indexOf(($ext="app")) > -1)
		{
			$url = window.defaultUrl.split($ext);
			return $url[0] + "index.php?/";
		}
		
		return window.defaultUrl;
	}
	else
	{
		return window.defaultUrl;
	}
}

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; }window.defaultUrl = "";
window.widgets_css = false;

$(function(){
	
	if(!empty(s = $(document).find("SCRIPT")))
	{
		window.defaultUrl = s.attr("src").split("js/")[0];
	}
	
	$("#content form fieldset select").find("option:odd").addClass("odd");	
	
	$("ul li:first-child").addClass("first");
	$("ul li:last-child").addClass("last");
	
	$("#content table tr:odd").addClass("odd");
	
	$("#categorias li:odd").addClass("odd");
	$("#categorias li:last-child").addClass("last");
	
	showAguarde.create();
	
	$("#activity").ajaxStart(function(){
		window.showAguarde();
	}).ajaxComplete(function(){
		window.hideAguarde();
	});
	
	if($("div.message").length > 0)
	{
		$("div.message").css({"opacity": 0.9}).hide().slideDown("normal");
		window.hideMessage = function(){ $("div.message").stop().slideUp("normal"); };
		setTimeout("window.hideMessage();", 5000);
	}
	
	$("#q").initValue();
	
	if($("div.change-size").length > 0)
	{
		$("div.change-size a").click(function(){
			var $sizes = ["normal", "medium", "large"];
			for(var $i=0; $i<$sizes.length; $i++) $($(this).attr("rel")).removeClass($sizes[$i]);
			$($(this).attr("rel")).addClass($(this).attr("href").substr(1));
			return false;
		});
	}
	
	$("#header h1").flash({
		src: $.url() + "swf/topo.swf",
		wmode: "transparent",
		width: "100%",
		height: "100%"
	});
	
	/* VIDEOS */
	$("div.video a.uri").each(function(){
		var $url = $(this).attr("href");
		$(this).parent().youtube({
			url: $url,
			wmode: "opaque",
			width: "100%",
			height: "100%"
		});
	});
	
	/* CAPA */
	if($(document.body).attr("id") == "capa")
	{
		//showBannerFlutuante();
	}
	
});

function showBannerFlutuante()
{
	$("#window-mask").remove();
	
	var closeBanner = window.closeBanner = function()
	{
		$("#window-mask").fadeOut("normal", function(){
			$(this).remove();
		});
		$("#banner-flutuante").fadeOut("normal");
	}	
	
	var $mask = $("<div id='window-mask'></div>").css({
		"position": "absolute",
		"z-index": 9999,
		"background-color": "#000",
		"display": "none",
		"top": 0,
		"left": 0,
		"width": $(window).width(),
		"height": $(document).height()
	});
	
	$(document.body).prepend($mask);
	
	$mask.fadeTo("normal", 0.3).click(closeBanner);
	
	$("#banner-flutuante").fadeIn("normal").find("a.bt-close").click(function(){
		window.closeBanner();
		return false;
	});
	
	setTimeout("window.closeBanner();", 5000);
}

$.fn.normalizeHeight = function(height)
{
	var maxHeight = 0;
	$(this).each(function(){
		maxHeight = Math.max(maxHeight, $(this).height());
	}).each(function(){
		if(!height)
		{
			$(this).css({
				'padding-bottom': maxHeight-$(this).height()+'px'
			});
		}
		else
		{
			$(this).css({
				'height': (maxHeight) + 'px'
			});
		}
	});
	
	return this;
};

$.url = function(webroot)
{
	if(!webroot)
	{
		if(window.defaultUrl.indexOf(($ext="app")) > -1)
		{
			$url = window.defaultUrl.split($ext);
			return $url[0] + "index.php?/";
		}
		
		return window.defaultUrl;
	}
	else
	{
		return window.defaultUrl;
	}
}

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; }