$.fn.tv = function(){
	var c  = $(this);
	
	// alterado no ciclo de chefs - coloca uma div por cima de tudo que tem um background branco transparente, pra img se mesclar ao bg do site.
	$("<div />").attr("class","tv-mask").appendTo(c);
		
	$("<div />").attr("class","tv-inner").appendTo(c);
	var ci = $(".tv-inner",c);
	$("img",c).each(function(i){
		var h   = Number($(this).height());
		var src = $(this).attr("src");
		$("<span />").attr("id","spantv"+i).css({ backgroundImage: "url("+src+")", height: h+"px" }).appendTo(ci).hide();
		$(this).remove();
	});
	$("span:first-child",ci).show();
	function anima(qm,pr){
		$("span:eq("+pr+")",ci).fadeIn(3000);
		$("span:eq("+qm+")",ci).fadeOut(3000);
	}
	var intval = ""
	function startInt(){
		if(intval == "") {
			intval=window.setInterval(function() { 
				var v = $("span:visible",ci).index();
				if ((v-1) == -1) {
					var p = ($("span",ci).length)-1;
				} else {
					var p = v-1;
				}
				anima(v,p);
			},8000)
		}
	};
	startInt();
};

$.fn.aviso = function() {
	var cssimg = { width: "75px", float: "left" };
	$("<div />").css({
		 border: "1px solid #F7941D",
		 background: "#FEEFDA",
		 textAlign: "center",
		 clear: "both",
		 height: "75px",
		 position: "relative"
	 }).attr("id","ie").prependTo("body").append(
		$("<a />").css({
			position: "absolute",
			right: "3px",
			top: "3px"
		 }).html("<img src='http://www.ie6nomore.com/files/theme/ie6nomore-cornerx.jpg' style='border: none;' alt='Feche este aviso'/>").click(function(){
			$("#ie").remove(); 
		 })
	).append(
		$("<div />").css({
			width: "640px",
			margin: "0 auto",
			textAlign: "left",
			padding: "0",
			overflow: "hidden",
			color: "black"
		}).append(
			$("<div />").css(cssimg).html("<img src='http://www.ie6nomore.com/files/theme/ie6nomore-warning.jpg' alt='AVISO!'/>")
		).append(
			$("<div />")
				.css({ width: "275px", float: "left", fontFamily: "sans-serif" })
				.append($("<div />").css({ fontSize: "14px", marginTop: "12px", fontWeight: "bold" }).html("Voce esta usando um navegador desatualizado"))
				.append($("<div />").css({ fontSize: "12px", lineHeight: "12px" }).html("Para navegar melhor neste site, por favor, atualize seu navegador."))
		).append(
			$("<div />").css(cssimg).html("<a href='http://pt-br.www.mozilla.com/pt-BR/' target='_blank'><img src='http://www.ie6nomore.com/files/theme/ie6nomore-firefox.jpg' style='border: none;' alt='Get Firefox 3.5'/></a>")
		).append(
			$("<div />").css(cssimg).html("<a href='http://www.microsoft.com/downloads/details.aspx?FamilyID=341c2ad5-8c3d-4347-8c03-08cdecd8852b&DisplayLang=pt-br' target='_blank'><img src='http://www.ie6nomore.com/files/theme/ie6nomore-ie8.jpg' style='border: none;' alt='Get Internet Explorer 8'/></a>")
		).append(
			$("<div />").css(cssimg).html("<a href='http://www.apple.com/br/safari/download/' target='_blank'><img src='http://www.ie6nomore.com/files/theme/ie6nomore-safari.jpg' style='border: none;' alt='Get Safari 4'/></a>")
		).append(
			$("<div />").css("float","left").html("<a href='http://www.google.com/chrome?hl=pt-BR' target='_blank'><img src='http://www.ie6nomore.com/files/theme/ie6nomore-chrome.jpg' style='border: none;' alt='Get Google Chrome'/></a>")
		)
	); 	
};

var ie6 = $.browser.msie&&($.browser.version <= "6.0");
var ie7 = $.browser.msie&&($.browser.version = "7.0");

$.fn.botao = function() {
	function validaEmail(email) {
		var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
		return pattern.test(email);
	}	
	var botao = $(this);
	$("<a />").addClass("submit").text(botao.attr("value")).attr("href","#").insertAfter(botao).click(function(){
		$("div.s span").fadeOut(200);
		var form = botao.parents("form");
		erro = false;		
		$(".obr",form).each(function(){
			$(this).removeClass("erro");
			if($(this).val() == "") {
				$(this).addClass("erro");
				erro = true;
			};
			if($(this).hasClass("email")) {
				if(!validaEmail($(this).attr("value"))) {
					$(this).addClass("erro");
					erro = true;
				};
			};
		});		
		if (erro==true) {
			$("div.s span",form).removeClass("sucesso loading").addClass("erro").text("Verifique os campos marcados!").fadeIn(200);			
			clearTimeout(t);
			var t = setTimeout("$(\"div.s span\").fadeOut(200)",8000);			
		} else {
			$("div.s span",form).fadeOut(200).removeClass("sucesso erro").addClass("loading").text("Enviando...").fadeIn(200);
			form.submit();
		};
		return false;
	 });
}

$(function(){
	$('header a[href*=#], a[href=#host]').click(function(){
		if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') && location.hostname == this.hostname) {
			var $target = $(this.hash);
			$target = $target.length && $target || $('[name=' + this.hash.slice(1) +']');
			if ($target.length) {
				var targetOffset = $target.offset().top;
				$('html,body').animate({scrollTop: targetOffset}, 1000);
				return false;
			}
		}
	});
	$(".tv").each(function(){ $(this).tv(); });
	if (ie7 || ie6) {
		$("html,body").css("overflowX","hidden");
		if(ie6) $(this).aviso();
	}
	$("input:submit").each(function(){ $(this).botao(); });
	$("form").submit(function(){
		$("form div.s a").click(function(){ return false; });
		$.post($(this).attr("action"), $(this).serialize(), function(data) {
			$("div.s span").hide().removeClass("erro loading").addClass("sucesso").text(data).fadeIn(200);
			$("input,textarea,select",this).val("");
			$("form div.s a").click(function(){ return true; });
			setTimeout("$(\"div.s span\").fadeOut(200)",8000);	
		});
		return false;
	});
	
	$("table .e").mouseenter(function(){
		$("div.b[id!=b_"+$(this).attr("id")+"]").fadeOut(200);
		$("div.b[id=b_"+$(this).attr("id")+"]").fadeIn(200);
	});
		
	$("div.b").mouseleave(function(){
		clearTimeout(t);
		var t = setTimeout("$(\"div.b\").fadeOut(200)",2000);		
	});	
	
});

if (navigator.userAgent.match(/(iPod|iPhone|iPad)/)) {
	$(document).bind('scroll', function() {
		$('header').css('top', window.scrollY + "px");
	});
}
