$(document).ready(function(){
	var colore_bordo = "1px solid #ff0000";

	$("#keyword").focusin(function(){
		if($(this).val() == 'Cerca articolo...')
			$(this).val('');
	});

	$("#keyword").focusout(function(){
		if($(this).val() == '')
			$(this).val('Cerca articolo...');
	});

	$("a[rel=foto_art]").fancybox({
		'transitionIn'		: 'none',
		'transitionOut'		: 'none',
		'titleFormat'     : function(title, currentArray, currentIndex, currentOpts){ return ''; }
	});

	$("#email_nl").bind("keyup",function(){
		$(this).removeClass("bg_convalida");
	});

	//iscri newsletter
	$("#invia_nl").click(function(){
		var email = $("#email_nl").val();
		var mail_reg_exp = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-]{2,})+\.)+([a-zA-Z0-9]{2,})+$/;
		
		if(!mail_reg_exp.test(email) || email == "" || email == "undefined"){
			$("#email_nl").addClass("bg_convalida");
			$("#email_nl").focus();
			return false;
		}
		else if($("#privacy_nl").attr("checked") == false){
			$(".privacy_nlConv").show();
			$("#privacy_nl").css({"border":colore_bordo});
			$("#privacy_nl").focus();
			return false;
		}
		else{
			$(".loader_nl").show();

			$.ajax({
				type    : 'get',
				data    : 'azione=iscri_nl&email='+email,
				url     : '../../includes/action/action_get.php',
				success : function(sText,sStatus){
					var res = sText.split("###");
					
					if(res[0] == "002"){
						$.fancybox(res[1], { onClosed : function(){ $(".loader_nl").fadeOut(); } });
					}
					else{
						$.fancybox(res[1], { onClosed : function(){ $(".loader_nl").fadeOut(); $("#email_nl").val(''); $("#privacy_nl").attr("checked",false); } });
					}
				},
				error   : function(sText,sStatus){
					$.fancybox(sText, { onClosed : function(){ $(".loader_nl").fadeOut(); } });
				}
			});
		}
	});

	//CANCELLAZIONE newsletter
	$("#invia_nl_canc").click(function(){
		var email = $("#email_nl_canc").val();
		var mail_reg_exp = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-]{2,})+\.)+([a-zA-Z0-9]{2,})+$/;
		
		if(!mail_reg_exp.test(email) || email == "" || email == "undefined"){
			$(".email_nl_cancConv").show();
			$("#email_nl_canc").css({"border":colore_bordo});
			$("#email_nl_canc").focus();
			return false;
		}
		else{
			$(".loader_nl_canc").show();

			$.ajax({
				type    : 'get',
				data    : 'azione=canc_nl&email='+email,
				url     : '../../includes/action/action_get.php',
				success : function(sText,sStatus){
					var res = sText.split("###");
					
					if(res[0] == "002"){
						$.fancybox(res[1], { onClosed : function(){ $(".loader_nl_canc").fadeOut(); } });
					}
					else{
						$.fancybox(res[1], { onClosed : function(){ $(".loader_nl_canc").fadeOut(); $("#email_nl_canc").val(''); } });					
					}
				},
				error   : function(sText,sStatus){
					$.fancybox(sText);
				}
			});
		}
	});

	//ricorda password
	$("#ricorda_pass").click(function(){
		var cont_box = '<div id="ricorda_pass_box" style="width:300px;">'+
									 '<h1>Inserisci il tuo indirizzo email</h1>'+
									 'E-mail:<br />'+
									 '<input type="text" name="email_ric_pass" id="email_ric_pass" size="30" /> '+
									 '<a href="javascript:goRicordaPass();" class="bottone_medium" id="">Vai</a> '+
									 '<img src="immagini/loader.gif" id="loader_ric_pass" alt="" style="display:none" />'+
									 '</div>';
		$.fancybox(cont_box);
	});

	//apro iframe condizioni privacy
	$("#normativa_priv").click(function(){
		$.ajax({
			type    : 'get',
			data    : 'azione=privacy',
			url     : '../../includes/action/action_get.php',
			success : function(sText,sStatus){
				$.fancybox('<div style="width:700px; height:400px; text-align:left">'+sText+'</div>');
			},
			error   : function(sText,sStatus){
				$.fancybox(sText);
			}
		});
	});

});

function number_format( number, decimals, dec_point, thousands_sep ) {
    var n = number, c = isNaN(decimals = Math.abs(decimals)) ? 2 : decimals;
    var d = dec_point == undefined ? "," : dec_point;
    var t = thousands_sep == undefined ? "." : thousands_sep, s = n < 0 ? "-" : "";
    var i = parseInt(n = Math.abs(+n || 0).toFixed(c)) + "", j = (j = i.length) > 3 ? j % 3 : 0;
    
    return  s + (j ? i.substr(0, j) + t : "") + i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + t) + (c ? d + Math.abs(n - i).toFixed(c).slice(2) : "");
}

//prendo la nazione
function nazione_sped(nazione){
	$("#loader_top").slideDown();

	$.ajax({
		type   : "get",
		data   : "azione=prendi_spedizioni&nazione="+nazione+"&continente="+$("#continente_sped").val(),
		url    : "../../includes/action/action_get.php",
		success : function(sText){
			$("#loader_top").slideUp();
			$("#cont_spedizione").html(sText);
		},
		error  : function(sText){
			$("#loader_top").slideUp();
			alert(sText);
		}
	});
}


//funzione per ricorda password
function goRicordaPass(){
	var colore_bordo = "2px solid #ff0000";
	var email = $("#email_ric_pass").val();
	
	if(email == ""){
		$("#email_ric_pass").css({"border":colore_bordo});
		$("#email_ric_pass").focus();
	}
	else{
		$("#loader_ric_pass").show();

		$.ajax({
			type    : 'get',
			data    : 'email='+$("#email_ric_pass").val()+'&azione=ricorda_pass',
			url     : '../../includes/action/action_get.php',
			success : function(sText,sStatus){
				var res = sText.split("###");
					
				if(res[0] == "002"){
					$.fancybox(res[1]);
					$("#loader_ric_pass").fadeOut();
				}
				else{
					$.fancybox(res[1]);
					$("#loader_ric_pass").fadeOut();
					$("#email_nl_canc").val('');
				}
				
			},
			error   : function(sText,sStatus){
				$.fancybox(sText);
			}
		});
	}
}


/*****news ticker*****/
(function(a){a.fn.vTicker=function(b){var c={speed:700,pause:4000,showItems:3,animation:"",mousePause:true,isPaused:false,direction:"up",height:0};var b=a.extend(c,b);moveUp=function(g,d,e){if(e.isPaused){return}var f=g.children("ul");var h=f.children("li:first").clone(true);if(e.height>0){d=f.children("li:first").height()}f.animate({top:"-="+d+"px"},e.speed,function(){a(this).children("li:first").remove();a(this).css("top","0px")});if(e.animation=="fade"){f.children("li:first").fadeOut(e.speed);if(e.height==0){f.children("li:eq("+e.showItems+")").hide().fadeIn(e.speed)}}h.appendTo(f)};moveDown=function(g,d,e){if(e.isPaused){return}var f=g.children("ul");var h=f.children("li:last").clone(true);if(e.height>0){d=f.children("li:first").height()}f.css("top","-"+d+"px").prepend(h);f.animate({top:0},e.speed,function(){a(this).children("li:last").remove()});if(e.animation=="fade"){if(e.height==0){f.children("li:eq("+e.showItems+")").fadeOut(e.speed)}f.children("li:first").hide().fadeIn(e.speed)}};return this.each(function(){var f=a(this);var e=0;f.css({overflow:"hidden",position:"relative"}).children("ul").css({position:"absolute",margin:0,padding:0}).children("li").css({margin:0,padding:0});if(b.height==0){f.children("ul").children("li").each(function(){if(a(this).height()>e){e=a(this).height()}});f.children("ul").children("li").each(function(){a(this).height(e)});f.height(e*b.showItems)}else{f.height(b.height)}var d=setInterval(function(){if(b.direction=="up"){moveUp(f,e,b)}else{moveDown(f,e,b)}},b.pause);if(b.mousePause){f.bind("mouseenter",function(){b.isPaused=true}).bind("mouseleave",function(){b.isPaused=false})}})}})(jQuery);

//aggiungi ai wishlist
function add_wish(idarticolo,idcliente){
	if(idcliente == 0){
		$.fancybox('Solo per utenti registrati.');
		return false;
	}
	else{
		$.ajax({
			type    : "get",
			url     : "../../includes/action/action_get.php?azione=add_wish&idarticolo="+idarticolo,
			success : function(sText,sStatus){
				var res = sText.split("###");

				$.fancybox(res[1]);
			},
			error   : function(sText,sStatus){
				$.fancybox(sText);
			}
		});
	}
}
