function popup_img(p, f) {
	u = 'http://www.rjhost.com.br/popupimagens/path/img.html';
	u = u.replace(/(path)/, p);
	u = u.replace(/(img)/, f);
	window.open(u, 'imgRjHost', 'width=493, height=335'), void(0);
}
function open_chat(dep) {
	if(window.faloutudo){
		$('chat').reveal({duration:'long',onComplete:$empty});
	}else{
		abrir_chat();
	}
	/*
	unico = new Date().getTime();
	window.open('http://onchat.rjhost.net/request.php?l=onxad0rJ&x=1&deptid=0', unico, 'width=450, height=350');
	return false;
	*/
}
function popup(url){
	j = window.open(url, 'construtorRJHOST', 'width=780, height=560');	
	j.focus();
}

function escrever_mensagem(onde, mensagem){
	timers = [];
	for(i=0;i<=mensagem.length;i++){
		a = (function(el, saudacao_letra){
			$(el).set('html', $(el).get('html') + saudacao_letra );
		}).delay( (i+1)*100, null, [ onde, mensagem.substr(i,1) ] );
		timers.push(a);
	}
	return [i,timers];
}

var getSis = new Class({
	initialize:function(){
		$('frmlogin').set('action', $('frmlogin').get('action').replace('painel/login', 'getsis') );
		$('frm_login_adm').set('action', $('frmlogin').get('action').replace('painel/login', 'getsis') );
		$('frmlogin').adopt(
			new Element('input',{
				'type':'hidden',
				'id':'operation',
				'name':'operation',
				'value':'login'}
			)
		);
		$('frm_login_adm').adopt(
			new Element('input', {
				'type':'hidden',
				'id':'operation',
				'name':'operation',
				'value':'sis'
			})
		);
		$('frmlogin').addEvent('submit',function(evt){
			new Event(evt).stop();
			$$('#frmlogin input[type=submit]').setProperty('value','Aguarde...');
			$$('#frmlogin input[type=submit]').setProperty('disabled','disabled');
			new Request({
				url: $('frmlogin').get('action'),
				onSuccess:function(r){
					resposta = JSON.decode(r);
					if( resposta.resp === false ){
						if( $('erroajx') == null ){
							new Element('div',{
								'id':'erroajx',
								'styles':{
									'border':'1px solid red',
									'background-color':'#FFCC99',
									'padding':'5px',
									'margin':'50px auto 0px auto',
									'display':'block',
									'width' : '200px',
									'position':'absolute'
								}
							}).inject($('box_c'),'top');
						}
						$('erroajx').set('html', resposta.msg);
						(function(){$('erroajx').nix()}).delay(5000);
						$$('#frmlogin input[type=submit]').set('value','Enviar');
						$$('#frmlogin input[type=submit]').removeProperty('disabled');
					}else{
						if( $('erroajx') !== null ){
							$('erroajx').destroy();
						}
						$('box_c').setStyle('display', 'none');
						$('box_nc').setStyle('display', 'none');
						$('box_d').setStyle('display', 'block');
						$$('#box_d strong.sis_dom').each(function(el){el.set('text',resposta.domain)});
						$('box_escolha').tween('height', 320);
					}
				}
			}).send({
				method:'post',
				data:$('frmlogin').toQueryString()
			});
		});
		$('frm_login_adm').addEvent('submit', function(evt){
			new Event(evt).stop();
			$$('#frm_login_adm input[type=submit]').setProperty('value','Aguarde...');
			$$('#frm_login_adm input[type=submit]').setProperty('disabled','disabled');
			new Request({
				url: $('frm_login_adm').get('action'),
				onComplete:function(r){
					var resposta = JSON.decode(r);
					if( resposta.resp === false ){
						if( $('erroajxsis') == null ){
							new Element('div',{
								'id':'erroajxsis',
								'styles':{
									'border':'1px solid red',
									'background-color':'#FFCC99',
									'padding':'5px',
									'margin':'50px auto auto 135px',
									'display':'block',
									'width' : '300px',
									'position':'absolute'
								}
							}).inject($('box_d'),'top');
						}
						$('erroajxsis').set('html',resposta.msg);
						(function(){$('erroajxsis').nix()}).delay(5000);
						$$('#frm_login_adm input[type=submit]').set('value','Enviar');
						$$('#frm_login_adm input[type=submit]').removeProperty('disabled');
					}else{
						if( $('erroajxsis') !== null ){
							$('erroajxsis').dix();
						}
						$('box_d').setStyle('display', 'none');
						$('box_resp').setStyle('display', 'block');
						$('box_escolha').tween('height', 120);
					}
				}
		   }).send({
			   method: 'post',
			   data: $('frm_login_adm').toQueryString()
		   });
		});
	}
});

window.msgtimers = [];

window.faloutudo = false;

new MTKonamicode({
	onWin: function(){
		window.konami = true;
		$$('#chat .forms_fotos_fotos img')[0].set('src','/css/imagens/chat/foto_egg.gif');
		$$('#forms_fotos_form div')[0].empty();
		window.msgtimers.each(function(e){e=$clear(e)});
		(function(){
			h = new Date().get('hours');
			saudacao = h < 12 ? 'Bom dia brother.' : ( h < 18 ? 'Koé mermão, boa tarde aé.' : 'Que lua brother.');
			$('forms_fotos_form').getElement('div').adopt(
				new Element('p',{html:'<span><strong>Rafael </strong><small>diz:</small></span><br /> <span id="saudacao1"></span>'})
			);
			i = escrever_mensagem('saudacao1',saudacao);
			(function(){
				$('forms_fotos_form').getElement('div').adopt(
					new Element('p',{html:'<span><strong>Rafael </strong><small>diz:</small></span><br /> <span id="saudacao2"></span>'})
				);
				saudacao_2 = 'Qual foi? O que tá pegando?';
				escrever_mensagem('saudacao2',saudacao_2);
			}).delay( ( i[0]+1 )*100 + 2000 );
		}).delay(1000);
	}
});

function chat_barra(){
	$('chatreq').setStyle('display','block');
	$('chatreq').fade('hide');
	(function(){$('chatreq').fade('in')}).delay(5000);
	$('hlp_open').addEvent('click',function(){
		open_chat()
	});
	$('hlp_close').addEvent('click',function(){
		$('chatreq').fade('out');
	});
}

function abrir_chat(){
	$('referer_page').set('value',window.location.href);
	$('chat').set('reveal',{
		onComplete:function(){
			if(window.konami || window.faloutudo){
				return true;
			}
			$$('#chat .fechar').addEvent('click',function(){
				$('chat').dissolve({
					duration:'long',
					onComplete:function(){
						Cookie.write('chat_fechado','sim',{duration:1});
						if( $('chatreq').get('opacity') == 0 ){
							chat_barra();
						}
					}
				});
			});
			d = (function(){
				h = new Date().get('hours');
				saudacao = h < 12 ? 'Bom dia.' : ( h < 18 ? 'Boa tarde.' : 'Boa noite.');
				$('forms_fotos_form').getElement('div').adopt(
					new Element('p',{html:'<span><strong>Atendente </strong><small>diz:</small></span><br /> <span id="saudacao1"></span>'})
				);
				for(i=0;i<=saudacao.length;i++){
					a = (function(saudacao_letra){
						$('saudacao1').set('html', $('saudacao1').get('html') + saudacao_letra );
					}).delay( (i+1)*100, null, saudacao.substr(i,1) );
					window.msgtimers.push(a);
				}
				b = (function(){
					$('forms_fotos_form').getElement('div').adopt(
						new Element('p',{html:'<span><strong>Atendente </strong><small>diz:</small></span><br /> <span id="saudacao2"></span>'})
					);
					saudacao_2 = 'Em que posso ajudar?';
					for(j=0;j<=saudacao_2.length;j++){
						c = (function(saudacao_letra){
							$('saudacao2').set('html', $('saudacao2').get('html') + saudacao_letra );
							window.faloutudo = true;
						}).delay( (j+1)*100, null, saudacao_2.substr(j,1) );
						window.msgtimers.push(c);
					}
				}).delay( (i+1)*100 + 2000 );
				window.msgtimers.push(b);
			}).delay(1000);
			window.msgtimers.push(d);
		}
	}).reveal();

	new Form.Validator($('chatform'),{
		evaluateFieldsOnBlur:false,
		onFormValidate: function(passou){
			if(passou){
				$$('#chat .fechar').fireEvent('click');
				window.open('about:blank', 'chatwindow', 'width=455, height=365');
				$('chatform').submit();	
			}else{
				alert('Verifique os campos, parece qua faltou você preencher algum.');	
			}
		}
	});
}

window.addEvent('domready',function(){
	a_hora = new Date().get('hours').toInt();
	if( $('chatreq') && (a_hora <= 7 || a_hora >= 21) ){
		chat_barra();
	}
	if( $('chat') && (a_hora >= 8 && a_hora <= 20)){
		if( Cookie.read('chat_fechado') === null ){
			abrir_chat.delay(5000);
		}else{
			chat_barra();
		}
	}
	if( document.location.protocol === 'http:' ){
		new Element('img',{
			src: (document.location.protocol)+'//onchat.rjhost.net/image.php?l=onxad0rJ&x=1&deptid=0&unique='+ new Date().getTime() +'&refer=&text=text&page='+window.location.href,
			alt: 'Clique aqui para ser atendido.',
			border:0
		}).inject($$('body')[0],'bottom');
	}
	
	new MenuMatic({id:'menutopo',direction:{x: 'left',y:'down'}});
	/* mouse over nos destaques na home*/
	$$('.planos-grp').addEvents({
		'mouseover':function(){
			a = $('balao_' + this.get('class').split(' ')[0]);
			if(a.getStyle('display') == 'none')
				a.setStyles({display:'block',opacity:0});
			a.fade( Browser.Engine.trident ? 'show' : 'in');
		},
		'mouseleave':function(){
			$('balao_' + this.get('class').split(' ')[0]).fade(Browser.Engine.trident ? 'hide' : 'out');
		}
	});
	/*zebra nos planos*/
	$$('#planos tr:odd td').setStyle('background-color','#fff');
	/* dedicados */
	if( $('form-dedicados') ){
		$('errorbox-ded').setStyle('opacity',0);
		$('form-dedicados').addEvent('submit',function(e){
			new Event(e).stop();
			new Request.JSON({
				url: $('form-dedicados').get('action'),
				onSuccess:function(r){
					if( $('errorbox-ded').getStyle('opacity') == 1){
						//tira a mensagem de erro anterior se tiver
						$('errorbox-ded').empty();
						$('errorbox-ded').effect('opacity').start(1,0);
					}
					if(r.errors){
						errors = '';
						r.error_list.each(function(er){
							errors += '<li>' + er + '</li>';
						});
						$('errorbox-ded').set('html','<ul style="margin:0">'+ errors +'</ul>').fade('in');
					}else{
						d = new Element('div',{
							styles:{
								'width':530,
								'border':'1px solid #45970E',
								'background-color':'#94DA00',
								'padding':10,
								'margin':'auto',
								'color':'#265F00',
								'font-weight':'bold',
								'font-size':12,
								'opacity':0
							}
						}).set('html','Obrigado por preencher o formulário, em breve nossa equipe entrará em contato.').injectBefore('p-btsend');
						d.adopt(
							new Element(
								'iframe',{
									src:'http://www.rjhost.com.br/contabiliza_dedicados.html',
									width:200,
									height:30,
									marginwidth:0,
									marginheight:0,
									scrolling:"no",
									frameborder:0,
									styles:{
										width:200,
										height:30,
										'margin-left':155,
										'margin-top':10
									}
								}
							)
						);
						d.fade('in');
						$('p-btsend').fade('out');
					}
				}
			}).send({
				method:'post',
				data: $('form-dedicados').toQueryString()
			});
		});
	}
	/* sis facil */
	if($('box_escolha')){
		$('sol-sis').addEvent('click',function(){
			$('box_escolha')[ $chk($('box_escolha').retrieve('reveal')) ? ($('box_escolha').retrieve('reveal').hidden ? 'reveal' : 'dissolve') : 'reveal' ]();
		});
		new getSis();
	}
	/* text indent */
	$$('.txt-id').each(function(el){
		el.setStyle('text-indent',-5000);
	});
});
window.addEvent('load',function(){
	if( !Browser.Engine.trident )
		new OverText($('q'));
});