//var URL = 'http://192.168.1.103/listaimpressa/site';
var URL = 'http://' + location.host;

$(document).ready(function(){
	
	var ga = document.createElement('script'); ga.type = 'text/javascript';
	ga.src = URL + '/js/jquery.translate-1.3.9.min.js';
	var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
		
	$('#en').click(function(){
		$('body').translate('en',{
			start: function(){
				$('body').append('<div id="loading" style="display: none;z-index: 500;top:0; left:0; position: fixed; width: 100%; height: 100%;filter:alpha(opacity=65);background: url('+ URL + '/imgs/layout/carregando.gif) center no-repeat #000; opacity:.6; -moz-opacity:0.6;text-align:center;"></div>');
				$('#loading').fadeIn();
			},
			complete: function(){
				$('#loading').fadeOut();
			},
			timeout: 5000,
			error: function(){
				$('#loading').fadeOut();
			}
		});
	})
	$('#es').click(function(){
		$('body').translate('es',{
			start: function(){
				$('body').append('<div id="loading" style="display: none;z-index: 500;top:0; left:0; position: fixed; width: 100%; height: 100%;filter:alpha(opacity=65);background: url('+ URL + '/imgs/layout/carregando.gif) center no-repeat #000; opacity:.6; -moz-opacity:0.6;text-align:center;"></div>');
				$('#loading').fadeIn();
			},
			complete: function(){
				$('#loading').fadeOut();
			},
			timeout: 5000,
			error: function(){
				$('#loading').fadeOut();
			}
		});
	})
	
	//LIMPAR FORMULÁRIOS
	$('input.defaultValue').click(function(){
		if($(this).val() == $(this).attr('defaultValue')){
			$(this).val('');	
		}
	})
	$('input.defaultValue').blur(function(){
		if($(this).val() == ''){
			$(this).val($(this).attr('defaultValue'));
		}
	})
	
	//ESCONDER BARRA DE PESQUISA
	$('a#esconder').bind('click', function(){
		if($('#barra_procura').height() != 140){
			$('#barra_procura').animate({height: 140},500)	
			$(this).html('aumentar barra de pesquisa')
			var esconde = 1;
		}else{
			$('#barra_procura').animate({height: 238},500)	
			$(this).html('diminuir barra de pesquisa')
			var esconde = 0;
		}
		$.ajax({url: URL + '/funcoes/barra_procura_altura.php?esconde='+esconde, error: function(){alert('erro')}})
	})
	
	//ALTERAR COR
	$('a#cor').bind('click', function(){
		if($(this).attr('alt') == 1){
			var cor = 2;
		}else{
			var cor = 1;
		}
		$.ajax({
			url: URL + '/funcoes/barra_procura_cor.php?cor='+cor,
			dataType: 'html',
			success: function(data){
				$('head').append(data)
			},
			error: function(){alert('Por favor, tente novamente mais tarde.')}
		})
	})
	
	//PROCURAR
	$('#procurar').bind('click', function(){
		var procuro = $('input.procuro').val();
		var localizado = $('input.localizado').val();
		if((procuro == 'Exemplo: Restaurante') && (localizado == 'Exemplo: Bairro Bela Vista')){
			$('input.procuro').focus();
			return false;
		}else if((procuro == "") && (localizado == "")){
			$('input.procuro').focus();
			return false;
		}else{
			$.ajax({
				url: URL + '/funcoes/busca_salva.php',
				type: 'POST',
				data: 'procuro=' + procuro + '&localizado=' + localizado,
				dataType: 'html',
				success: function(data){
					$('head').append(data)
				},
				error: function(){
					alert('Por favor, tente novamente mais tarde.')
				}
			})
		}
	})
	
	//FORMULÁRIO
	$('form').submit(function(e){									  
	var url = $(this).attr('action')
	var serializeDados = $(this).serialize()
	e.preventDefault()
	$.ajax({
			url: URL + '/' + url, 
			dataType: 'html',
			type: 'POST',
			timeout: 50000,
			data: serializeDados,
			beforeSend: function(){
				var altura = ($('body').height() / 2);
				$('body').append('<div id="loading" style="display: none;z-index: 500;top:0; left:0; position: fixed; width: 100%; height: 100%;filter:alpha(opacity=65);background: url('+ URL + '/imgs/layout/carregando.gif) center no-repeat #000; opacity:.6; -moz-opacity:0.6;text-align:center;"></div>');
				$('#loading').fadeIn()
			},
			complete: function(){
				$('#loading').fadeOut()
			},
			success: function(data, textStatus){
				alert(data)
			},
			error: function(xhr,er){
				alert('Por favor, tente novamente mais tarde.')
			}		
		})
	})
	
	//FAVORITOS
	$("a.fav").click(function(e){
		e.preventDefault();
		var url = $(this).attr('href');
		var title = $(this).attr('alt');
		  
		if($.browser.mozilla == true){
			window.sidebar.addPanel(title, url, '');
			return false;
		}else if($.browser.msie == true){  
			window.external.AddFavorite(url, title);
			return false;
		}else{
			alert('Pressione as teclas CTRL + D para adicionar aos favoritos.');
		}
	});
	
	$('.publicidade, .publicidadeg').cycle({
		fx: 'fade'
	});
	
    $("#procuro").simpleAutoComplete( URL + '/funcoes/' + "procuro.php");

})

function setHome(){
	if($.browser.mozilla == true){
		document.body.style.behavior='url(#default#homepage)';
    	document.body.setHomePage(window.location.href);
	}else if($.browser.msie == true){  
		document.body.style.behavior='url(#default#homepage)';
    	document.body.setHomePage(window.location.href);
	}else{
		alert('Para executar este comando, defina manualmente no seu navegador de internet.');
	}
}
