/*****************************************************************************
								Generic
******************************************************************************/

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

function bookmarksite(title, url) {
	if (document.all)
		window.external.AddFavorite(url, title);
	else if (window.sidebar) {
		window.sidebar.addPanel(title, url, "");
	}
}

// Size method
var Get = new Object;
Get.size = function(obj) {
	var size = 0, key;
	for(key in obj) {
		if (obj.hasOwnProperty(key)) size++;
	}
	return size;
}


	
$(document).ready(function() {
	// Hacked para diferentes eventos\triggers
	$('a.lightbox').lightBox({fixedNavigation:true, slideshow:true, nextSlideDelay:4000});
	$('a.lightbox1').lightBox({fixedNavigation:true});
	$('.gallery a').lightBox({fixedNavigation:true});
	
	
	/* Slideshow por grupos, hacked */
	var k = 0;
	$('.count_slideshow_spec').each(function() { k++; });
	for(i=0; i<k; i++) $("a.slideshow_spec_"+i).lightBox({fixedNavigation:true, slideshow:false,});


		
	$('#zoomFoto').lightBox({fixedNavigation:true});

	// Voltar ao topo
	$(".voltarTopo").click(function() {
		$.scrollTo(0,800);
	});

	// zoom image
	$('.zoom').zoomimage();
	
	$(".png").pngFix();


	// Regular hover
	$(".hover").hover(function() {
		var img = $(this).attr("src");
		var hover = img.replace(".png","-hover.png");
		$(this).attr({"src":hover});
	}, function() {
		var img = $(this).attr("src");
		var hover = img.replace("-hover.png",".png");
		$(this).attr({"src":hover});
	});
	// preload images
	$(".hover").each(function() {
		var img = this.src;
		$("body").append('<img id="deleteThis" src="'+img.replace(".png","-hover.png")+'" style="display:none">');
		$("#deleteThis").remove();
	});



	// Newsletter
	$("#emailNewsletter").focus(function() {
		var value = $(this).attr("value");
		if(value == "e-mail") $(this).attr({"value":""});
	});
	$("#emailNewsletter").blur(function() {
		var email = $(this).attr("value");
		if(checkEmail(email) == false) $(this).attr({"value":"e-mail"});
	});
	// Inscrever em newsletter
	$("#emailNewsletter").keypress(function(e) {
		if(e.which == 13) {
			var email = $("#emailNewsletter").val();
			$.ajax({
				type: "POST",
				url: "/requests/newsletter.php",
				data: "email="+email,
				success: function(msg){
					alert(msg);
					window.location=location.href;
				}
			});
		}
	});
	$("#InscreveNewsletter").click(function() {
		var email = $("#emailNewsletter").val();
		$.ajax({
			type: "POST",
			url: "/requests/newsletter.php",
			data: "email="+email,
			success: function(msg){
				alert(msg);
				window.location=location.href;
			}
		});
	});


	// Procurar artista
	$("#procurarArtista").focus(function() {
		var value = $(this).attr("value");
		if(value == "nome de artista") $(this).attr({"value":""});
	});
	$("#procurarArtista").blur(function() {
		var value = $(this).attr("value");
		if(value == "") $(this).attr({"value":"nome de artista"});
	});
	$("#procurarArtista").keypress(function(e) {
		if(e.which == 13) procurarArtista();
	});
	procurarArtista = function() {
		var value = $("#procurarArtista").attr("value");
		window.location="/artistas/procurar/"+value;
	}



	// Popup Open Generic
	var genericPopup = new Object();
	$(".popupNoFixedTrigger").click(function(e) {
		genericPopup.id = "#"+$(this).parent().attr("rel");
		genericPopup.trigger = this;
		var mouseX = e.pageX;
		var mouseY = e.pageY;
		$(genericPopup.id).css({"left":mouseX-300, "top":mouseY+10}).show();
		genericPopup.open = true;
	});
	// Close Generic popup
	$(document).click(function(event) {
		if(!genericPopup.open) return 0;
		if(genericPopup.trigger != event.target) {
			if($(genericPopup.id).css("display") == "block") {
				var parentEls = $(event.target).parents().map(function () {
					return this.id;
				}).get().join(",").split(",");
				var len = parentEls.length;
				var counter = null;
				for(i=0; i<=len;i++) {
					if(parentEls[i] == genericPopup.id.replace('#','')) counter++;
				}
				if(counter == null) $(genericPopup.id).hide();
			}
		}

	});
	



	// Recomendar amigo - seu nome
	$("#seuNome").focus(function() {
		var val = $(this).val();
		if(val == "seu nome") $(this).val("");
	});
	$("#seuNome").blur(function() {
		var val = $(this).val();
		if(val == "") $(this).val("seu nome");
	});
	$("#emailAmigo").focus(function() {
		var value = $(this).attr("value");
		if(value == "e-mail de amigo") $(this).attr({"value":""});
	});
	$("#emailAmigo").blur(function() {
		var email = $(this).attr("value");
		if(checkEmail(email) == false) $(this).attr({"value":"e-mail de amigo"});
	});
	
	// Request recomendar a amigo
	recomentarAmigoRequest = function() {
		data = new Object();
		$("#recomendar-amigo-wrapper input").each(function(obj,i) {
			data[$(i).attr("name")] = $(i).val();
		});
		//JSON recomendar amigo
		$.ajax({
			type: "GET",
			url: "/requests/recomendar-amigo.php",
			data: data,
			cache: false,
			contentType: "application/json; charset=utf-8",
			dataType: "json",
			success: function(msg){
				if(msg == "ok") {
					alert("Mensagem enviada com sucesso");
					var key;
					for(key in data) $("input[name='"+key+"']").val("");
				} else {
					alert("Preencha os campos correctamente.");
				}
			}
		});
	}
	$("#recomentarAmigo").click(function() {
		recomentarAmigoRequest();
	});
	$("#seuNome, #emailAmigo").keypress(function(e) {
		if(e.which == 13) recomentarAmigoRequest();
	});


	$("#frmSondagem").submit(function() {
		return false;
	})

	/* enviar voto na sondagem */
	$("#sondagemButton").click(function() {
		$("#frmSondagem .erro").empty();

		var sondagem = $("#frmSondagem .sondagem").val();
		var voto = $("#frmSondagem input[@name='voto']:checked").val();

		if(!voto) {
			voto = $("#frmSondagem .voto-texto").val();
			if(voto == "")
				var erro = "Escolha uma opção";
		}

		if(sondagem && voto && !erro) {
			$.ajax({
				type: "post",
				data: "sondagem="+sondagem+"&voto="+voto,
				cache: false,
				async: true,
				url: "/requests/sondagens.php",
				success: function(event) {
					$("#frmSondagem").empty();
					if(event == "success") {
						$("#frmSondagem").append("<p>Obrigado pelo seu voto</p>");
					}
					else if(event == "duplicate") {
						$("#frmSondagem").append("<p>Só pode votar uma vez</p>");
					}
					else {
						$("#frmSondagem").append("<p>Erro inesperado</p>");
					}
				}
			});
		}
		else
			$("#frmSondagem .erro").append(erro);
	});
});
