function AtivarFancybox(objeto){
	switch (objeto){
		case '.video':
			return jQuery(objeto).click(function() {
				$.fancybox({
					'padding'       : 0,
					'autoScale'     : false,
					'transitionIn'  : 'elastic',
					'transitionOut' : 'elastic',
					'title'         : this.title,
					'width'         : 680,
					'height'        : 495,
					'href'          : this.href.replace(new RegExp("watch\\?v=", "i"), 'v/'),
					'type'          : 'swf'
				});
				return false;
			});
		break;
		case '.ampliar':
			return jQuery(objeto).fancybox({
				'hideOnContentClick': false,
				'padding'           : 3,
				'autoScale'         : false,
				'transitionIn'	    : 'elastic',
				'transitionOut'	    : 'elastic',
				'title'             : this.title
			});
		break;
		default:
			return jQuery(objeto).fancybox({
				'transitionIn'	:	'elastic',
				'transitionOut'	:	'elastic',
				'speedIn'		:	200, 
				'speedOut'		:	200
			});
		break;
	}
}

Callback = function(){return false};

function ampliarFoto(){
	$(document).ready(function(){
		var src = $("img.fotoUsuarioAlbumHome").attr('src');
		
		$('img.fotoUsuarioAlbumHome').bind({
			mouseenter: function(){
				alert("Enter");
				//alert(src);
				//$("#exibeImagem").load("exibeImagem.php");
			},
			mouseleave: function(){
				alert("Leave");	
				//alert(src);
			}
		});
	});
}

function addFavoritos(){
    var url = "http://www.lamacross.com";
    var title = "Lamacross - Você na trilha certa!";
    if (window.sidebar) window.sidebar.addPanel(title, url,"");
    else if(window.opera && window.print){
        var mbm = document.createElement('a');
        mbm.setAttribute('rel','sidebar');
        mbm.setAttribute('href',url);
        mbm.setAttribute('title',title);
        mbm.click();
    }
    else if(document.all){window.external.AddFavorite(url, title);}
}

$(".slideBanner").ready(function() {
    $('.slideBanner').cycle({
		fx     : 'fade' , // ex: fade, scrollUp, shuffle, etc...
		timeout: 5000 
	});
});

$(document).ready(function(){  
	$("#featured").tabs({fx:{opacity: "toggle"}}).tabs("rotate", 8000, true);  
	$("#featured").click(
		function(){ 
			$("#featured").tabs("rotate",0,true);  
		} 
	);
});

function Recarregar(){
	window.document.location = window.document.location;
}

function ValidarEmail(email){
	var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	return filter.test(email);
}

function ValidarFormulario(formulario){
	var ok = true;
	var senha = "";
	
	//campos vazios
	$(formulario).find(".obrigatorio").each(function(){
		if(($(this).hasClass('senha')) && (senha == "")){
			senha = $(this).val();
		}
		
		if(($(this).val() == "") || (($(this).hasClass('email')) && (!ValidarEmail($(this).val()))) || (($(this).hasClass('senha')) && (senha != $(this).val()))){
			$(this).addClass('invalido');
			ok = false;
		}else{
			$(this).removeClass('invalido');
		}
	});
	
	if(!ok){
		alert("Verifique os campos com borda vermelha.");
		return false;
	}
	return true;
}

function Logar(formulario){
	$.post("classes.php", $(formulario).serialize(), function(retorno){
		retorno = $.parseJSON(retorno);
		if(retorno.success){
			//window.document.location.reload(false);
			Recarregar();
		}else{
			alert(retorno.msg);
		}
	});
}

function Deslogar(){
	$.post("classes.php", {objeto:'Usuario', metodo:'Deslogar'}, function(retorno){
		retorno = $.parseJSON(retorno);
		if(retorno.success){
			//window.document.location.reload(false);
			//window.history.go(0);
			//window.document.location = window.document.location;
			Recarregar();
		}else{
			alert(retorno.msg);
		}
	});
}

// Álbum --------------------------------------------------------------------------------------------

function ExcluirAlbum(id){
	if(confirm('Você deseja excluir permanentemente este álbum e todas suas fotos?')){
		$.post("classes.php", {objeto:'Album', metodo:'Excluir', id:id}, function(){
			//window.document.location.reload(false);
			Recarregar();
		});
	}
}

function ExcluirVideo(id){
	if(confirm('Você deseja excluir este vídeo?')){
		$.post("classes.php", {objeto:'Album', metodo:'ExcluirVideo', id:id}, function(){
			//window.document.location.reload(false);
			Recarregar();
		});
	}
}

function ExcluirFoto(id){
	if(confirm('Você deseja excluir esta foto?')){
		$.post("classes.php", {objeto:'Album', metodo:'ExcluirFoto', id:id}, function(){
			//window.document.location.reload(false);
			Recarregar();
		});
	}
}

function ExcluirEvento(id){
	if(confirm('Você deseja excluir este evento?')){
		$.post("classes.php", {objeto:'Evento', metodo:'Excluir', id:id}, function(){
			//window.document.location.reload(false);
			Recarregar();
		});
	}
}


function UsuarioSalvar(formulario){
	var dados = $(formulario).serialize()+"&objeto=Usuario&metodo=Salvar&logar=true";
	$.post("classes.php", dados, function(retorno){
		retorno = $.parseJSON(retorno);
		if(retorno.success){
			//window.document.location.reload(false);
			Recarregar();
		}else{
			alert(retorno.msg);
		}
	});
}

function LimparSenha(campo1,campo2){
	var texto = '******';
	campo1 = $('#'+campo1);
	campo2 = $('#'+campo2);
	
	if(campo1.val() == texto){
		campo1.val('');
	}
	
	if(campo2.val() == texto){
		campo2.val('');
	}
}

function PreencherSenha(campo1,campo2){
	var texto = '******';
	campo1 = $('#'+campo1);
	campo2 = $('#'+campo2);
	
	if(campo1.val() == ''){
		campo1.val(texto);
	}
	
	if(campo2.val() == ''){
		campo2.val(texto);
	}
}

function sleep(milliseconds) {
  var start = new Date().getTime();
  for (var i = 0; i < 1e7; i++) {
    if ((new Date().getTime() - start) > milliseconds){
      break;
    }
  }
}

function CarregarConteudo(alvo, endereco){
	$.get(endereco, function(retorno){
		$(alvo).html(retorno);
	});
}

function Comentar(formulario, tabelaReferencia, idReferencia){
	$.post("classes.php", {objeto: 'Comentario', metodo: 'Salvar', tabelaReferencia: tabelaReferencia, idReferencia: idReferencia, texto: $(formulario).find('textarea').val()}, function(retorno){
		alert('Comentário enviado com sucesso!');
		//window.document.location.reload(false);
		Recarregar();
	});
}

function EventoSalvar(formulario){
	var dados = $(formulario).serialize()+"&objeto=Evento&metodo=Salvar";
	$.post("classes.php", dados, function(retorno){
		retorno = $.parseJSON(retorno);
		/*if(retorno.success){
			window.document.location.reload(false);
		}else{
			alert(retorno.msg);
		}*/
	});
}

function TrocarSenha(formulario){
	$('#formLogin button').hide();
	$('#formLogin .feedback').html('Enviando instruções...');
	
	var dados = $(formulario).serialize()+"&objeto=Usuario&metodo=TrocarSenha";
	$.post("classes.php", dados, function(retorno){
		retorno = $.parseJSON(retorno);
		$('#formLogin button').show();
		$('#formLogin .feedback').html('');
		alert(retorno.msg);
	});
}

function GetIdAlbum(){
	return idAlbum;
}

// Anuncio --------------------------------------------------------------------------------------------

function ExcluirVideoAnuncio(id){
	if(confirm('Você deseja excluir este vídeo?')){
		$.post("classes.php", {objeto:'Anuncio', metodo:'ExcluirVideo', id:id}, function(){
			//window.document.location.reload(false);
			Recarregar();
		});
	}
}

function ExcluirFotoAnuncio(id){
	if(confirm('Você deseja excluir esta foto?')){
		$.post("classes.php", {objeto:'Anuncio', metodo:'ExcluirFoto', id:id}, function(){
			//window.document.location.reload(false);
			Recarregar();
		});
	}
}

function CriarAnuncio(){
	if(logado){
		$.blockUI({ message: "Criando anúncio..." });
		$.post("classes.php", {objeto:'Anuncio', metodo:'Salvar', rascunho:true}, function(retorno){
			retorno = $.parseJSON(retorno);
			$.unblockUI;
			if(retorno.success){
				window.document.location = "/?pagina=cadastroAnuncio&id="+retorno.id;
			}else{
				alert("Erro ao criar rascunho do anúncio");
				return true;
			}
		});
	}else{
		Callback = function(){
			CriarAnuncio();
			return true;
		}
		$.fancybox({
            'href': '/paginas/cadastroLogin.php',
			'hideOnContentClick': false,
			'padding'           : 3,
			'autoScale'         : false,
			'transitionIn'	    : 'elastic',
			'transitionOut'	    : 'elastic',
			'title'             : this.title
        });
	}
	return false;
}
