window.addEvent(
	'domready', function() {
		if ($('frm_news')) {
			$('frm_news').addEvent('submit', function(e){
				e.stop();
				var event = new Event(e);
				var frm = new acForm("frm_news",false,"#434343","#FF0000");
				if (!frm.error) {
					event.stop();
					event.preventDefault();
					event.stopPropagation(); 
					acAviso('Cadastrando dados...');
					$("frm_news").set('send', {
						onComplete: function(response) {
							acBgAlert();
							acMsg('Cadastro de E-mail','Seu e-mail foi cadastrado com sucesso!');
							$('e-mailCad').set('value','');
						}
					}).send();
				} else acMsg('ERRO! Atenção!','Cadastre um e-mail válido');
			}); 
		}
	if ($('box_itens')) {
		var wBox = $('box_itens').getStyle('width').toInt();
		if ($('box_itens')) {
			var boxs = $('box_itens').getElements('.box_item').length;
			$('box_itens').setStyles({'width':boxs*wBox,'left':0});
		}
		var tempo = 7000;
		var periodical; 
		var x = 1;
		var acScroll = function () { 

			var sx = $('box_itens').getStyle('left').toInt();
			if ((sx-wBox) <= -$('box_itens').getStyle('width').toInt()) {
				$('box_itens').tween('left', [sx, 0]);
				x = 1;
			}
			else{
				$('box_itens').tween('left', [sx, sx-wBox]);
				x = x + 1;
			}
			$('divBolas').getElements('span[class=numBanner]').setStyle('background-position','0 -11px')
			$('numBanner'+x).setStyle('background-position','0 0')
		}
		periodical = acScroll.periodical(tempo);
		$('divBolas').getElements('span[class=numBanner]').addEvent('click',function() {
			$clear(periodical);
			x = this.get('acID').toInt();
			$('divBolas').getElements('span[class=numBanner]').setStyle('background-position','0 -11px')
			$('numBanner'+x).setStyle('background-position','0 0')
			var sx = $('box_itens').getPosition().x.toInt();
			var sx2 = wBox*(x-1);
			$('box_itens').tween('left', [sx, -sx2]);
			periodical = acScroll.periodical(tempo);
		});
	}
	var local = location.pathname;
	if (local.search(/servicos.asp/i) > -1 ) $('menu_servicos').set('class','aki');
	else if (local.search(/localizacao.asp/i) > -1 ) $('menu_mapa').set('class','aki');
	else if (local.search(/produtos.asp/i) > -1 ) $('menu_produtos').set('class','aki');
	else if (local.search(/dicas.asp/i) > -1 ) $('menu_dicas').set('class','aki');
	else if (local.search(/noticias.asp/i) > -1 ) $('menu_noticias').set('class','aki');
	else if (local.search(/contato.asp/i) > -1 ) $('menu_contato').set('class','aki');
	else $('menu_index').set('class','aki');
});
function acMsg(tit,msg) {
	acBgAlert();
	var acBoxMsg = new Element('div', {'id':'acalertbox'})
		.set('html','<h1>'+tit+'</h1><div class="alertMsg">'+msg+'</div><br/><input type="button" id="acalertsim" value="Confirmar" />')
		.setStyles({
			width: 400,
			align: 'center',
			position: 'fixed',
			'z-index': '1000',
			opacity: 0,
			color: '#000',
			left: (window.getCoordinates().right/2)-205
		})
		.injectInside(document.body);
		$('acalertbox').setStyle('top',(window.getCoordinates().bottom/2)-($('acalertbox').getSize().y/2)-5)
		var myFx = new Fx.Tween('acalertbox');
		myFx.start('opacity', '0', '1');
		if (!$('acalert')) acBgAlert();
		$('acalertsim').addEvent('click',function(){acBgAlert();});
		$('acalert').addEvent('click',function(){acBgAlert()});
}

function acMostra(id) {
	if ($('div_oculta_'+id).getStyle('display') == 'block') {
		$('div_oculta_'+id).setStyle('display','none');
		$('div_desc_'+id).setStyle('display','block');
	}
	else {
		$('div_oculta_'+id).setStyle('display','block');
		$('div_desc_'+id).setStyle('display','none');
	}
}
function acVai(x) {
	$clear(periodical);
	var sx = $('box_itens').getStyle('left').toInt();
	$('box_itens').tween('left', [sx, $('bandiv_'+x).getStyle('left')]);
	
	
	if ((sx-wBox) <= -$('box_itens').getStyle('width').toInt()) {$('box_itens').tween('left', [sx, 0]);}
	else{$('box_itens').tween('left', [sx, sx-wBox]);}
}

function acEnviaEmail(e) {
	var event = new Event(e);
	var frm = new acForm("frm_contato",true,"#434343","#FF0000");
	if (frm.error) {
		event.stop();
		event.preventDefault();
		event.stopPropagation(); 
	}
}

var acForm = new Class({
	initialize: function(form,msg,cor01,cor02){
		if ($('erroForm')) $('erroForm').destroy();
		if ($('erroForm2')) $('erroForm2').destroy();
		if ($('erroForm3')) $('erroForm3').destroy();
		var error = false;
		var status;
		$(form).getElements('*[class*=acObg]').each(function(el){
			status = true;
			if (el.getProperty('type') == 'checkbox' && !el.getProperty('checked')) status = false;
			else if (!el.get('value')) status = false;
			else if(el.getProperty('acTipo') == 'email' && !el.get('value').match(/^[^@\s<&>]+@([-a-z0-9]+\.)+[a-z]{2,}$/gi)){
				if (msg) {
					var acBoxMsg = new Element('div', {'id':'erroForm2'})
					.set("html","E-mail inválido. Favor tentar mais uma vez.")
					.inject($(form),'before');
				}
				status = false;
				this.error = true;
			}
			else if(el.getProperty('acTipo') == 'confirma_senha' && $('senha').get('value') != el.get('value')){
				var acBoxMsg = new Element('div', {'id':'erroForm2'})
				.set("html","As senhas digitadas não conferem. Favor tentar mais uma vez.")
				.inject($(form),'before');
				status = false;
				this.error = true;
				$(document.body).scrollTo(0,0);
			}
			else if (el.getProperty('acTipo') == 'cpf'){
				 var cpf = el.get('value');
				 var cpferro = false;
                 if (cpf.length < 11) cpferro = true;
                 else {
					 var nonNumbers = /\D/;
		             if (nonNumbers.test(cpf)) cpferro = true;
			         if (cpf == "00000000000" || cpf == "11111111111" || cpf == "22222222222" || cpf == "33333333333" || cpf == "44444444444" || cpf == "55555555555" || cpf == "66666666666" || cpf == "77777777777" || cpf == "88888888888" || cpf == "99999999999") cpferro = true;
				}
				if (!cpferro) {
					var a = [];
					var b = new Number;
					var c = 11;
					for (i=0; i<11; i++){
						a[i] = cpf.charAt(i);
						if (i < 9) b += (a[i] * --c);
		             }
			         if ((x = b % 11) < 2) { a[9] = 0 } else { a[9] = 11-x }
					 b = 0;
		             c = 11;
				     for (y=0; y<10; y++) b += (a[y] * c--);
		             if ((x = b % 11) < 2) { a[10] = 0; } else { a[10] = 11-x; }
				     if ((cpf.charAt(9) != a[9]) || (cpf.charAt(10) != a[10])){
                       cpferro = true;
		             }
				} 
               if (cpferro) { 
					 var acBoxMsg = new Element('div', {'id':'erroForm3'})
					.set("html","CPF inválido. Favor tentar mais uma vez.")
					.inject($(form),'before');
					status = false;
					this.error = true;
				}
			}
			if (!status) {
				error = true;
				if (el.getPrevious()) el.getPrevious().setStyle('color',cor02);
				else if (el.getNext()) el.getNext().setStyle('color',cor02);
			} else {
				if (el.getPrevious()) el.getPrevious().setStyle('color',cor01);
				else if (el.getNext()) el.getNext().setStyle('color',cor01);
				el.set('value',el.get('value').clean());
			}
		});
		if (error) {
			if (msg) {
				$(document.body).scrollTo(0,0);
				var acBoxMsg = new Element('div', {'id':'erroForm'})
				.set("html","Por favor, complete os dados que faltam.")
				.inject($(form),'before');
			}
			this.error = true;
		} 
		else this.error = false;
	}
});

function acAviso(msg) {
	acBgAlert();
	var acBoxMsg = new Element('div', {'id':'acalertbox'})
		.set("html",msg+"<br/><img src='imagens/load.gif' />")
		.setStyles({
			height: 60,
			width: 400,
			align: 'center',
			position: 'absolute',
			opacity: 1,
			color: '#000',
			top: (window.getCoordinates().bottom/2)-35,
			left: (window.getCoordinates().right/2)-205
		})
		.injectInside(document.body);
}
function acBgAlert() {
	if (Browser.Engine.name == 'trident' && Browser.Engine.version < 5) ie6 = true;
	else ie6 = false;
	
	var formtags = $$('select','textarea');
	if ($('acalert')) {
		$(document.body).getElements('*[id^=acalert]').destroy();
		if (ie6 && formtags) formtags.setStyle('visibility', 'visible');
	}
	else {
		if (formtags.length > 0 && ie6) formtags.setStyle('visibility', 'hidden');
		var acBox = new Element('div', {"id":"acalert"})
			.setStyles({
				opacity: 0.8,
				background: '#fff',
				position: 'fixed',
				top: 0,
				left: 0,
				'z-index': '800',
				width: window.getScrollSize().x,
				height: window.getScrollSize().y
			})
			.injectInside(document.body);
	}
}
