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() {


	// Categorias	
	$(".cat-name").click(function() {
		var sub = $(this).next();
		sub.toggle();
	});
	$(".subcategoria").click(function() {
		var sub = $(this).next();
		if(sub.attr("class") == "sub-categoria-ul") {
			sub.slideToggle("slow");
		}
	});



	// Categorias hover
	$(".categoria-li").hover(function() {
		$(this).css({"background-color":"#4a1568"});
	}, function() {
		$(this).css({"background-color":"#808080"});
	});
	$(".sub-categoria-li").hover(function() {
		$(this).css({"background-color":"#9273a3"});
		$($(this).children()).css({"color":"#FFFFFF"});
	}, function() {
		$(this).css({"background-color":"#f1f1f1"});
		$($(this).children()).css({"color":"#333333"});
	});

	
	



	// Username
	$("#username").focus(function() {
		var val = $(this).val();
		if(val == "utilizador") $(this).val("");
	});
	$("#username").blur(function() {
		var val = $(this).val();
		if(val == "") $(this).val("utilizador");
	});
	// Password
	$("#password").focus(function() {
		var val = $(this).val();
		if(val == "chave") $(this).val("");
	});
	$("#password").blur(function() {
		var val = $(this).val();
		if(val == "") $(this).val("chave");
	});
	
	// 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"});
	});

	// Procurar
	$("#pesquisar").focus(function() {
		var val = $(this).val();
		if(val == "pesquisa") $(this).val("");
	});
	$("#pesquisar").blur(function() {
		var val = $(this).val();
		if(val == "") $(this).val("pesquisa");
	});

	// 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"});
	});


	var noOut = null;
	$(".menu-hover").hover(function() {
		var img = $(this).attr("src");
		var len = img.length;
		if(img.substr(len-10,len) != "-hover.png") {
			var hover = img.replace(".png","-hover.png");
			$(this).attr({"src":hover});
		} else noOut = $(this).attr("id");
	}, function() {
		if(noOut != $(this).attr("id")) {
			var img = $(this).attr("src");
			var len = img.length;
			if(img.substr(len-10,len) == "-hover.png") {
				var hover = img.replace("-hover.png",".png");
				$(this).attr({"src":hover});
			}
		}
	});

	// 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});
	});

	// Faqs
	$(".faqs-categorias").click(function() {
		$(this).next().next().toggle("slow");
		var image_location = $(this).next().attr("src").split("/");
		var desporto = image_location[2];
		
		if($(this).next().attr("src") == "/images/"+desporto+"/bullet.png") $(this).next().attr({"src":"/images/"+desporto+"/arrow-down.png"});
		else $(this).next().attr({"src":"/images/"+desporto+"/bullet.png"});
	});




	// Contactos form
	$("#contactos-form-nome").focus(function() {
		var val = $(this).val();
		if(val == "nome") $(this).val("");
	});
	$("#contactos-form-nome").blur(function() {
		var val = $(this).val();
		if(val == "") $(this).val("nome");
	});
	
	$("#contactos-form-email").focus(function() {
		var value = $(this).attr("value");
		if(value == "e-mail") $(this).attr({"value":""});
	});
	$("#contactos-form-email").blur(function() {
		var email = $(this).attr("value");
		if(checkEmail(email) == false) $(this).attr({"value":"e-mail"});
	});


	$("#contactos-form-assunto").focus(function() {
		var val = $(this).val();
		if(val == "assunto") $(this).val("");
	});
	$("#contactos-form-assunto").blur(function() {
		var val = $(this).val();
		if(val == "") $(this).val("assunto");
	});
	
	$("#contactos-form-mensagem").focus(function() {
		var val = $(this).val();
		if(val == "mensagem") $(this).val("");
	});
	$("#contactos-form-mensagem").blur(function() {
		var val = $(this).val();
		if(val == "") $(this).val("mensagem");
	});




	
	
	// Login && Submit on enter
	submitLogin = function() {		
		var username = $("#username").val();
		var password = $("#password").val();
		var grupo = $("#grupo").val();
		$.ajax({
			type: "POST",
			url: "/requests/login.php",
			data: "username="+username+"&password="+password+"&grupo="+grupo,
			success: function(msg){
				if(msg == true) {
					$("#carrinho-cliente-wrapper").hide();
					$("#carrinho-cliente-wrapper").html("");
					$("#carrinho-cliente-wrapper").load('index.php?file=area-reservada').fadeIn("slow");
				}
				else alert("A autenticação falhou!");
			}
		});
	}
	$("#username, #password").keypress(function(e) {
		if(e.which == 13) submitLogin();
	});
	$("#login").click(function() {
			submitLogin();
	});








	// 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;
			}
		});
	});



	// List hover (servicos, etc)
	$(".list-items").hover(function() {
		$(this).removeClass("list-items");
		$(this).addClass("list-items-hover");
	}, function() {
		$(this).removeClass("list-items-hover");
		$(this).addClass("list-items");
	});
	
	



	// Select form small
	var okToToggle = true;
	$(".select-trigger").click(function() {
		okToToggle = false;
		$(this).next().slideToggle("fast", function(){
			okToToggle=true;
		});
	});
	$(document).click(function() {
		if(okToToggle) $(".select-items-wrapper").hide();	
	});
	$(".select-items-ul li").hover(function() {
		$(this).addClass("select-items-ul-li-item-hover");
		$(this).children("a").css({"color":"#FFFFFF", "text-decoration":"none"});
	}, function() {
		$(this).removeClass("select-items-ul-li-item-hover");
		$(this).children("a").css({"color":"#333333"});
	});







	// Select form small
	var okToToggleSmall = true;
	$(".select-small-trigger").click(function() {
		okToToggleSmall = false;
		$(this).next().slideToggle("fast", function(){
			okToToggleSmall = true;
		});
	});
	$(document).click(function() {
		if(okToToggleSmall) $(".select-small-items-wrapper").hide();	
	});
	$(".select-small-items-ul li").hover(function() {
		$(this).addClass("select-small-items-ul-li-item-hover");
		$(this).css({"color":"#FFFFFF", "text-decoration":"none"});
	}, function() {
		$(this).removeClass("select-small-items-ul-li-item-hover");
		$(this).css({"color":"#333333"});
	});
	// Select and change first label small
	$(".select-small-item").click(function() {
		var label = $(this).parent().parent().prev();
		var selected_id = $(this).attr('id').replace('grupo-','');
		var selected_label = $(this).html();
		label.html(selected_label);
		$("#grupo").val(selected_id);
	});






	// Load html area
	var popupID = 0;
	$(".popup-trigger").click(function() {
		var id = $(this).attr("id");

		if($('#popup-outside-wrapper').css("display") == "block"){
			$("#popup-outside-wrapper").hide();
			$("#popup-img-title").remove();
			if(popupID == id){
				return 1;
			}
		}
		if(id != "") {					
			//$("#popup-img-title").attr({"src":"/images/title-"+id+".png"});
			$.ajax({
				type: "POST",
				url: "/requests/html-area.php",
				data: "area="+id,
				success: function(msg){
					$("#popup-title-wrapper").append('<img id="popup-img-title" src="/images/title-'+id+'.png" class="middle content-img-title">');
					$("#popup-text-wrapper").html(msg);
					$("#popup-outside-wrapper").fadeIn('slow');
					popupID = id;
				}
			});
		}
	});
	$(document).click(function(event) {
		if($('#popup-outside-wrapper').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] == "popup-outside-wrapper") counter++;
			}
			if(counter == null) {
				$("#popup-img-title").remove();
				$("#popup-outside-wrapper").fadeOut("fast");
			}
		}
	});
	
	
	
	// Mudar bullet na listagem de categorias em /categoria
	$(".listagem-categorias-item").hover(function() {
		$(this).prev().attr({"src":"/images/bullet-listagem-categorias-hover.png"});
	}, function() {
		$(this).prev().attr({"src":"/images/bullet-listagem-categorias.png"});
	});








	// Hover listagem de produtos
	$(".moldura-produto-outside-wrapper").hover(function() {
		$(this).children(".moldura-produto-foto-wrapper").css({"border-color":"#4a1568"});
		$(this).children(".moldura-produto-foto-wrapper").next().css({"background":"url('/images/moldura-bottom-labels-hover.png') no-repeat", "color":"FFFFFF"});
		var img_detalhes = $(this).children(".moldura-produto-foto-wrapper").next().children(".moldura-produto-labels-detalhes").children().children();
		img_detalhes.attr({"src":"/images/btn-detalhes-hover.png"});
	}, function() {
		$(this).children(".moldura-produto-foto-wrapper").css({"border-color":"#acacac"});
		$(this).children(".moldura-produto-foto-wrapper").next().css({"background":"url('/images/moldura-bottom-labels.png') no-repeat", "color":"#333333"});
		var img_detalhes = $(this).children(".moldura-produto-foto-wrapper").next().children(".moldura-produto-labels-detalhes").children().children();
		img_detalhes.attr({"src":"/images/btn-detalhes.png"});
	});






	// Popup informações do produto.png
	var popupInfo = false;
	$("#info-trigger").click(function() {
		var height = $(window).height() + $(window).scrollTop()*2;
		var width = $(window).width();
		var popupHeight = $("#popup-info-outside-wrapper").height();
		var popupWidth = $("#popup-info-outside-wrapper").width();
		var top = parseInt((height/2)-(popupHeight/2));
		var left = parseInt((width/2)-(popupWidth/2));
		$("#popup-info-outside-wrapper").css({"top":top+"px", "left":left+"px", "z-index":"99"}).fadeIn("slow", 
			function() {
				popupInfo = true;
			}
		);
	});
	// Close popup
	$(document).click(function(event) {
		if(!popupInfo) return 0;
		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] == "popup-info-outside-wrapper") counter++;
		}
		if(counter == null) {
			$("#popup-info-outside-wrapper").fadeOut("fast");
			popupInfo = false;
		}
		
	});






	// request pedido de informações
	$("#info-btn").click(function() {
		data = new Object();
		$("#popup-info-text-wrapper input, textarea").each(function(obj,i) {
			data[$(i).attr("name")] = $(i).val();
		});
		//JSON Informações request aqui
		$.ajax({
			type: "GET",
			url: "/requests/info.php",
			data: data,
			contentType: "application/json; charset=utf-8",
			dataType: "json",
			success: function(msg){
				if(msg == null) {
					alert("Mensagem enviada com sucesso");
					$("#popup-info-outside-wrapper").fadeOut("fast");
					$("#popup-info-text-wrapper label").css({"color":"#333333"});
					$("#popup-info-text-wrapper input, textarea").val("");
					popupInfo = false;
				} else {
					var key;
					for(key in msg.error) {
						if(msg.error[key] == true) $("#popup-info-text-wrapper label[name='"+key+"']").css({"color":"red"});
						else $("#popup-info-text-wrapper label[name='"+key+"']").css({"color":"#333333"});
					}
				}
			}
		});
	});






	// Request recomendar a amigo
	$("#recomentarAmigo").click(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,
			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.");
				}
			}
		});		
	});






	// Hover moldura de novidades
	$(".listagem-novidades-moldura").hover(function() {
		$(this).css({"border":"1px solid #714988"});
	}, function() {
		$(this).css({"border":"1px solid #b3b3b3"});
	});
});

