$(function(){
	$('.flag').children().each(function(){
		$(this).bind('click',set_idioma);
	});
	
	$(window).resize(set_overlay_size).resize();
	
	// Campo de pesquisa de produtos
	$('#produtos_search').bind('keypress',function(e){
		// Verifica se apertou o ENTER
		var keycode;
		if (window.event) keycode = window.event.keyCode;
		else if (e) keycode = e.which;
		if(keycode == 13){
			// Atualiza a p‡gina com a busca
			if($(this).hasClass('categoria'))
			{
				var cat = $.trim($(this).attr('class').replace('categoria','').replace('input_search',''));
				document.location.href = _burl+'produtos/categoria/'+cat+'/'+this.value.replace(/ /gi,'+').replace('/','+')+'.html';
			}
			else
			{
				document.location.href = _burl+'produtos/index/'+this.value.replace(/ /gi,'+').replace('/','+')+'.html';
			}			
		}
		return;
	});
	
	// Campo de pesquisa de noticias
	$('#noticias_search').bind('keypress',function(e){
		// Verifica se apertou o ENTER
		var keycode;
		if (window.event) keycode = window.event.keyCode;
		else if (e) keycode = e.which;
		if(keycode == 13){
			// Atualiza a p‡gina com a busca
			document.location.href = _burl+'noticias/index/'+this.value.replace(/ /gi,'+').replace('/','+')+'.html';			
		}
		return;
	});
	
	// Campo de pesquisa de noticias
	$('#eventos_search').bind('keypress',function(e){
		// Verifica se apertou o ENTER
		var keycode;
		if (window.event) keycode = window.event.keyCode;
		else if (e) keycode = e.which;
		if(keycode == 13){
			// Atualiza a p‡gina com a busca
			document.location.href = _burl+'eventos/index/'+this.value.replace(/ /gi,'+').replace('/','+')+'.html';			
		}
		return;
	});
	
	$("a[rel=image_group]").fancybox({
		'transitionIn'		: 'none',
		'transitionOut'		: 'none',
		'titlePosition' 	: 'over',
		'titleFormat'		: function(title, currentArray, currentIndex, currentOpts) {
			return '<span id="fancybox-title-over">' + (currentIndex + 1) + ' / ' + currentArray.length + (title.length ? ' &nbsp; ' + title : '') + '</span>';
		}
	});
	
	$('#map li a').each(function(e){
		$(this).bind('click',function(ev){
			ev.preventDefault();
			var $uf = $('#c_'+this.id);
			$('.ruf').hide();
			if($uf.length){
				$uf.show();
			} else {
				$('#c_inex').show();
			}
			this.blur();
			return false;
		});
	});
	
	$('#video_viewer').delegate('a','click',function(e){
		e.preventDefault();
		$('#overlay,#video_viewer,.video').hide();		
		return false;
	});
	
	$(".video-trigger").bind('click',function(e){
		e.preventDefault();
		$('#index,body').add(document).add(window).scrollTop();
		var v = $(this).data('video');
		$('.video').hide();
		$('#overlay,#video_viewer,#video_'+v).show();
		$('#focuser').focus();
		return false;
	});
	
	$('#login').children().filter('input,textarea,select').each(function(){
		var $this = $(this);
		// Verifica se tem a classe validate-empty e faz a devida verificaŤ‹o na perda de foco
		if($this.is('.validate-empty')){
			$this.bind('blur',function(){
				validate.empty(this,'#err_'+this.id,'Campo obrigat&oacute;rio');
			});
		}
		// Verifica se tem a classe validate-regex e faz a devida verificaŤ‹o na perda de foco
		if($this.is('.validate-regex')){
			$this.bind('blur',function(){
				validate.regex(this,'#err_'+this.id,'Campo inv&aacute;lido',$(this).data('regex'));
			});
		}
		// Verifica se tem a classe validate-email e faz a devida verificaŤ‹o na perda de foco
		if($this.is('.validate-email')){
			$this.bind('blur',function(){
				validate.email(this,'#err_'+this.id,'E-mail inv&aacute;lido');
			});
		}
	});
	
	$('#ar_tabs').tabs({ fx: { opacity: 'toggle' } });
	
	$('.login').bind('submit',function(e){
		e.preventDefault();
		var action = this.action;
		var $form = $(this);
		$.ajax({
			url:action,
			type:'POST',
			dataType:'text',
			data:'u='+encodeURIComponent($('#u',$form).val())+'&p='+encodeURIComponent($('#p',$form).val()),
			success:function(r){
				r = $.trim(r);
				if(r=='1'){
					document.location.href = document.location.href;
				} else {
					alert(r);
				}
				return;
			}
		});
		return false;
	});
	
	$('#logout_btn').bind('click',function(e){
		e.preventDefault();
		$.ajax({
			url:_burl+'area_restrita/logout.html',
			type:'GET',
			dataType:'text',
			success:function(r){
				document.location.href = document.location.href;
				return;
			}
		});
		return false;
	});
	
	var videos = $('#iframes').html();
	$('#iframes').remove();
	$('#video_viewer').html(videos);
	
	$('.clear-menu-cache').bind('click',function(e){
		$.ajax({
			async:false,
			url:_burl+'index/set_menu_cache.html',
			data:'ch=',
			type:'POST',
			dataType:'text',
			success:function(r) {
				r = $.trim(r);
				if(parseInt(r)!=1) {
					console.log(r);
				}
				return;
			}
		});
	});
});

function set_idioma(e){
	e.preventDefault();
	
	var lg = this.id;
	switch(lg){
		case 'es': lg = 3; break;
		case 'en': lg = 2; break;
		case 'ptBR': lg = 1; break;
		default: return;
	}

	$.ajax({
		async:false,
		url:_burl+'index/set_idioma.html',
		data:'lg='+lg,
		type:'POST',
		dataType:'text',
		success:function(r){
			r = parseInt($.trim(r));
			if(r!=1){
				console.log('error');
			} else {
				document.location.href = document.location.href;
			}
			return;
		}
	});
	
	this.blur();
	return false;
}

function set_overlay_size(e){
	var w = document.getElementById('index').offsetWidth;
	var h = document.getElementById('index').offsetHeight;
	
	$('#overlay').css({
		width:w,
		height:h
	});
	return;
}

function set_menu_cache(e) {
	$('#menu_categorias a.selected').removeClass('selected');

	try {
		$(e.target).addClass('selected');
	} catch(er) {} finally {

		var ch = encodeURIComponent($.trim($('#menu_categorias').parent().html()));
		
		$m = $(e.target).parents('ul');
		$('ul','#menu_categorias').not($m).hide();
	
		$.ajax({
			async:false,
			url:_burl+'index/set_menu_cache.html',
			data:'ch='+ch,
			type:'POST',
			dataType:'text',
			success:function(r) {
				r = $.trim(r);
				if(parseInt(r)!=1) {
					console.log(r);
					//alert(r);
				}
				return;
			}
		});
	}
	return true;
}

function toggle_menu(a) {
	$m = $(a).parent().find('ul:first').toggle(300).parents('ul').andSelf();
	$('ul','#menu_categorias').not($m).hide(300);
}
