var heightPar = new Array();
$(document).ready(function () {
 	$(".openLogin").click(function(){
		openModal();				   
		return false;		   
	});
	var navActive;
	var idprod;
	var url = String(document.location).toLowerCase().split("#/");
	var urlSection = url[0].split("/");
	var section = urlSection[urlSection.length - 1].split('.');

	if(section[0] == 'products'){
		var product = url[1];
		navActive = 'prodotti';
		switch(product)
		{
			case 'zero-virgola': idprod = "zero-virgola";
				break;
			case 'briantea-pilsner': idprod = "briantea-pilsner";
				break;
			case 'briantea-weiner': idprod = "briantea-weiner";
				break;
			case 'brughe-monaco': idprod = "brughe-monaco";
				break;
			case 'saraceno': idprod = "saraceno";
				break;
			default: idprod = "";
		}
		setProducts(idprod);
	}
	if(section[0] == 'chisiamo'){
		navActive = 'chi-siamo';	
	}
	if(section[0] == 'contact'){
		navActive = 'contatti';	
	}
	if(section[0] == 'ordini'){
		navActive = 'ordini';
		setHelp();
	}
	if(section[0] == 'riepilogo'){
		setCondition();
	}
	if(section[0] == 'mission'){
		navActive = 'mission';	
	}
	if(section[0] == 'registrazione' || section[0] == 'modifica'){
		setRegistration();
	}
	if(section[0] == 'registrazionegas'){
		setRegistrationGas();
	}
	if(section[0] == 'indirizzospedizione'){
		setAddress1();	
	}
	if(section[0] == 'indirizzofatturazione'){
		setAddress2();
	}
	if(section[0] == 'mieiordini'){
		setOrders();
	}
	if(section[0] == 'carrello'){
		setCart();
	}
	if(section[0] == 'default'|| section[0] == ""){
		setZoomLabel();
	}
	if(section[0] == 'help'){
		setHelp();	
	}
	setNavigation(navActive);
});

function setNavigation(id){
	$("#" + id).removeClass('noactived').addClass('active');
	$("#" + id).css( {backgroundPosition: "0 -58px"} );	
	$('.noactived').css( {backgroundPosition: "0 0"} )
	$('.noactived').mouseover(function(){
		$(this).stop().animate({marginTop:"-58px"}, {duration:500})
	});
	$('.noactived').mouseout(function(){
		$(this).stop().animate({marginTop:"0"}, {duration:500})
	});
}

function openModalfeedBack(title , message , type){
  	var winH = $(document).height();
	var winW = $(window).width();
	$('#ModalOverlay').css({"height":winH + "px", "width": winW + "px", "top":0, "left": 0, "opacity":0});
	$('#ModalOverlay').animate({"opacity":0.5}, 500, "linear", function(){openFeedBack(title , message , type)});
}
function openFeedBack(title , message , type){
  $("#title-feedback").text(title);
  $(".feedback-data p").html(message);
  $(".feedback-data p").removeClass();
  if(type == 1){
    $(".feedback-data p").removeClass().addClass("feedBack-error");
  }
  $('#ModalOverlay').bind('click', function() {
    closeFeedBack();
  });
	var centerH = Math.floor(($(window).height() - $("#Boxfeedback").height())/2);
	var centerW = Math.floor(($(window).width() - $("#Boxfeedback").width())/2);
	$("#Boxfeedback").css({ "left": centerW + "px" , "top": centerH + "px", "opacity":0 })
	$('#Boxfeedback').animate({"opacity":1}, 500, "linear");
	//resize trigger
	$(window).resize(function() {
		var newW = $(this).width();
		$('#ModalOverlay').css({"width": newW + "px"});
		var centerH = Math.floor(($(window).height() - $("#Boxfeedback").height())/2);
		var centerW = Math.floor(($(window).width() - $("#Boxfeedback").width())/2);
		$("#Boxfeedback").css({ "left": centerW + "px" , "top": centerH + "px"})
	});
	$("#close-feedback span").click(function(){
		closeFeedBack();								 
	});
	$("#close-feedback span").mouseover(function(){
			$(this).css({ "margin":"-26px 0 0 0" });
	});
	$("#close-feedback span").mouseout(function(){
			$(this).css({ "margin":"0"});
	});
}

function closeFeedBack(){
  $('#ModalOverlay').unbind('click');
	$('#ModalOverlay').animate({"opacity":0}, 500, "linear");
	$('#Boxfeedback').animate({"opacity":0}, 500, "linear",function(){
		$('#ModalOverlay').css({"height":"20px","top":"-200px","width":"20px","left":"-200px"});
		$('#Boxfeedback').css({"height":"-600px","top":"-600px"});														  
	});
}
////////////////////////
function openModal(){
	var winH = $(document).height();
	var winW = $(window).width();
	$('#ModalOverlay').css({"height":winH + "px", "width": winW + "px", "top":0, "left": 0, "opacity":0});
	$('#ModalOverlay').animate({"opacity":0.5}, 500, "linear", function(){openLogin()});
}

function openLogin(){
  $('#ModalOverlay').bind('click', function() {
    closeLogin();
  });
  
  $("#recuperaPwd").bind('click', function() {
	$('#ModalOverlay').unbind('click');
    openPwdModal();
  });
  
	var centerH = Math.floor(($(window).height() - $("#BoxLogin").height())/2);
	var centerW = Math.floor(($(window).width() - $("#BoxLogin").width())/2);
	$("#BoxLogin").css({ "left": centerW + "px" , "top": centerH + "px", "opacity":0 })
	$('#BoxLogin').animate({"opacity":1}, 500, "linear");
	//resize trigger
	$(window).resize(function() {
		var newW = $(this).width();
		$('#ModalOverlay').css({"width": newW + "px"});
		var centerH = Math.floor(($(window).height() - $("#BoxLogin").height())/2);
		var centerW = Math.floor(($(window).width() - $("#BoxLogin").width())/2);
		$("#BoxLogin").css({ "left": centerW + "px" , "top": centerH + "px"})
	});
	
	$("#BoxLogin input").blur(function() {
	  $(this).css({ backgroundColor: "#fbfbfb" , border:"1px solid #e9e9e9" });
	});
	$("#BoxLogin input").focus(function() {
	  $(this).css({ backgroundColor: "#e8e8e8" , border:"1px solid black" });
	});
	
	$("#close-login span").click(function(){
		closeLogin();								 
	});
	$("#close-login span").mouseover(function(){
			$(this).css({ "margin":"-26px 0 0 0" });
	});
	$("#close-login span").mouseout(function(){
			$(this).css({ "margin":"0"});
	});
}

function closeLogin(){
  $('#ModalOverlay').unbind('click');
	$('#ModalOverlay').animate({"opacity":0}, 500, "linear");
	$('#BoxLogin').animate({"opacity":0}, 500, "linear",function(){
		$('#ModalOverlay').css({"height":"20px","top":"-200px","width":"20px","left":"-200px"});
		$('#BoxLogin').css({"height":"-600px","top":"-600px"});														  
	});
}
//////////////////////////
function openPwdModal(){
	$('.error-psw').wrapAll('<div id="pswWrap" />');
	$('#BoxLogin').animate({"opacity":0}, 500, "linear",function(){
			$('#BoxLogin').css({"height":"-600px","top":"-600px"});														  
		});
  $('#ModalOverlay').bind('click', function() {
    closePwd();
  });
  
  if($('#ModalOverlay').css("top") == "-200px"){
	  var winH = $(document).height();
		var winW = $(window).width();
		$('#ModalOverlay').css({"height":winH + "px", "width": winW + "px", "top":0, "left": 0, "opacity":0});
	  $('#ModalOverlay').animate({"opacity":0.5}, 500, "linear");
	  $(window).resize(function() {
			var newW = $(this).width();
			$('#ModalOverlay').css({"width": newW + "px"});
			var centerH = Math.floor(($(window).height() - $("#BoxPWD").height())/2);
			var centerW = Math.floor(($(window).width() - $("#BoxPWD").width())/2);
			$("#BoxPWD").css({ "left": centerW + "px" , "top": centerH + "px"})
		});
	}
  
  
	var centerH = Math.floor(($(window).height() - $("#BoxPWD").height())/2);
	var centerW = Math.floor(($(window).width() - $("#BoxPWD").width())/2);
	$("#BoxPWD").css({ "left": centerW + "px" , "top": centerH + "px", "opacity":0 })
	$('#BoxPWD').delay(500).animate({"opacity":1}, 500, "linear");
	//resize trigger
	$(window).resize(function() {
		var newW = $(this).width();
		$('#ModalOverlay').css({"width": newW + "px"});
		var centerH = Math.floor(($(window).height() - $("#BoxPWD").height())/2);
		var centerW = Math.floor(($(window).width() - $("#BoxPWD").width())/2);
		$("#BoxPWD").css({ "left": centerW + "px" , "top": centerH + "px"})
	});
	
	$("#BoxPWD input").blur(function() {
	  $(this).css({ backgroundColor: "#fbfbfb" , border:"1px solid #e9e9e9" });
	});
	$("#BoxPWD input").focus(function() {
	  $(this).css({ backgroundColor: "#e8e8e8" , border:"1px solid black" });
	});
	
	$("#close-login span").click(function(){
		closePwd();								 
	});
	$("#close-login span").mouseover(function(){
		$(this).css({ "margin":"-26px 0 0 0" });
	});
	$("#close-login span").mouseout(function(){
		$(this).css({ "margin":"0"});
	});
}

function closePwd(){
	$('#ModalOverlay').unbind('click');
	$('#ModalOverlay').animate({"opacity":0}, 500, "linear");
	$('#BoxPWD').animate({"opacity":0}, 500, "linear",function(){
		$('#ModalOverlay').css({"height":"20px","top":"-200px","width":"20px","left":"-200px"});
		$('#BoxPWD').css({"height":"-600px","top":"-600px"});
		$("#pswWrap").html('<p class="error-psw"></p>');
	});
}


/////////////////

function setProducts(id){
	$('.accordion-head').each(function(){
		$(this).css({ cursor:"pointer" });
		$(this).attr('mode', 'noclicked');
		$(this).attr('basecolor' ,$(this).children('h3').css('color') );
	});
	
	$('.accordion-head').mouseover(function(){
		if($(this).attr('mode') !== 'clicked'){
			$(this).children('.accordion-icon').css({ "background-position": "-30px 27px" });
			$(this).children('h3').children('span.name-product').css({ color: "white", "background-color": $(this).attr('basecolor'), padding: "0 10px" });
		}
	});
	
	$('.accordion-head').mouseout(function(){
		if($(this).attr('mode') !== 'clicked'){
			$(this).children('.accordion-icon').css({ "background-position": "0 27px" });
			$(this).children('h3').children('span.name-product').css({ color: $(this).attr('basecolor'), "background-color": "white", padding: 0 });
		}
	});
	
	$('.accordion-head').click(function(){
		if($(this).attr('mode') !== 'clicked'){
			var idOpener = $(this).parent().attr("id");
			openProduct(idOpener);
		}
	});
	openProduct(id);
}

function openProduct(id){
	
	rewriteUrl(id);
	
	$(".accordion-content").each(function(index){
		var checkid = $(this).attr('id');
		if( $(this).attr('id') == id){
			$("#" + id).children('.accordion-head').attr('mode', 'clicked');
			$("#" + id).children('.accordion-head').children('.accordion-icon').css({ "background-position": "-60px 27px" });
			$("#" + id).children('.accordion-head').children('.action-product').css({"padding-right":"50px", "opacity": 0, "margin-top": "0"});
			$("#" + id).children('.accordion-head').children('.action-product').animate({"padding-right":"0", "opacity":1}, { duration:1000 });
			
			$("#" + id).children('.accordion-bottle').css({"opacity":"0","background-position":"0px 0px"});
			$("#" + id).children('.accordion-bottle').animate({"opacity":1},{duration:1000});
			$("#" + id).children('.accordion-head').children('h3').children('span.name-product').css({ color: "white", "background-color": $(this).children('.accordion-head').attr('basecolor'), "padding": "0 10px" });
			var gap = 122 + $(this).children('.accordion-text').height();
			if(gap < 559){
				gap = 559;	
			}
			$(this).animate({"height": gap + "px"}, { duration:1000 });
			$("#" + id).children('.accordion-head').children('h3').click(function(){
				closeProduct(id);
				return false;
			});
			var scrollPage = $('#header').height() + 20 + (index * 110);
			$('html, body').animate({scrollTop: scrollPage + "px"}, 1000);
		}else{
			$("#" + checkid).children('.accordion-head').attr('mode', 'noclicked');
			$("#" + checkid).children('.accordion-head').children('.accordion-icon').css({ "background-position": "0px 27px" });
			$("#" + checkid).children('.accordion-head').children('.action-product').animate({"padding-right":"50px", "opacity": 0});
			$("#" + checkid).children('.accordion-bottle').animate({"opacity":0},{duration:1000});
			$("#" + checkid).children('.accordion-head').children('h3').children('span.name-product').css({ color: $(this).children('.accordion-head').attr('basecolor'), "background-color": "white", "padding": "0px 0px" });
			var gap = 110;
			$("#" + checkid).animate({"height": gap + "px"}, { duration:1000 });
			$("#" + checkid).children('.accordion-head').children('h3').unbind('click');
		}
	});
}

function closeProduct(id)	{
	$("#" + id).children('.accordion-head').attr('mode', 'noclicked');
	$("#" + id).children('.accordion-head').children('.accordion-icon').css({ "background-position": "0px 27px" });
	$("#" + id).children('.accordion-head').children('.action-product').animate({"padding-right":"50px", "opacity": 0});
	$("#" + id).children('.accordion-bottle').animate({"opacity":0},{duration:1000});
	$("#" + id).children('.accordion-head').children('h3').children('span.name-product').css({ color: $(this).children('.accordion-head').attr('basecolor'), "background-color": "white", "padding": "0px 0px" });
	var gap = 110;
	$("#" + id).animate({"height": gap + "px"}, { duration:1000 });
	$("#" + id).children('.accordion-head').children('h3').unbind('click');
	$('html, body').animate({scrollTop: 0}, 1000);
	rewriteUrl("");
}

function rewriteUrl(id){
	var url = String(document.location).toLowerCase().split("#/");
	document.location = url[0] + "#/" + id;	
}

function setRegistration(){
	$(".BtnInviaReg").mouseover(function(){
		$(this).css({ "background-position":"0 -32px"});									 
	});
	$(".BtnInviaReg").mouseout(function(){
		$(this).css({ "background-position":"0 0"});									 
	});
	$("#user-data input").blur(function() {
	  $(this).css({ backgroundColor: "#fbfbfb" , border:"1px solid #e9e9e9" });
	});
	$("#user-data input").focus(function() {
	  $(this).css({ backgroundColor: "#e8e8e8" , border:"1px solid black" });
	});
	$("#NewsletterChoice").buttonset();
	$("#PrivacyChoice").buttonset();
}


function setRegistrationGas(){
	$(".BtnInviaReg").mouseover(function(){
		$(this).css({ "background-position":"0 -32px"});									 
	});
	$(".BtnInviaReg").mouseout(function(){
		$(this).css({ "background-position":"0 0"});									 
	});
	
	
	$("#user-data input").blur(function() {
	  $(this).css({ backgroundColor: "#fbfbfb" , border:"1px solid #e9e9e9" });
	});
	$("#user-data input").focus(function() {
	  $(this).css({ backgroundColor: "#e8e8e8" , border:"1px solid black" });
	});
	$("#NewsletterChoice").buttonset();
	$("#PrivacyChoice").buttonset();
}



function setAddress1(){
	$("#user-address input").blur(function() {
	  $(this).css({ backgroundColor: "#fbfbfb" , border:"1px solid #e9e9e9" });
	});
	$("#user-address input").focus(function() {
	  $(this).css({ backgroundColor: "#e8e8e8" , border:"1px solid black" });
	});
	
	$('.address-note').blur(function() {
	  $(this).css({ backgroundColor: "#fbfbfb" , border:"1px solid #e9e9e9" });
	});
	
	$('.address-note').focus(function(){
		$(this).animate({width: "402px"}, 800, "linear", function(){setResizeNote()});
		$(this).css({ backgroundColor: "#e8e8e8" , border:"1px solid black" });
	});
}

function setResizeNote(){
	var row = 0;
	$('.address-note').autoResize({
		onResize : function() {
			row++;
			$(this).css({opacity:0.8});
			var h = $(this).height() + 50;
			if(row > 1) {
				$(this).parent("div").animate({"height": h + "px"}, { duration:300 });
			}
		},
		animateCallback : function() {
			$(this).css({opacity:1});
		},
		animateDuration : 300,
		extraSpace : 0
	});	
}

 /*
 * jQuery autoResize (textarea auto-resizer)
 * @copyright James Padolsey http://james.padolsey.com
 * @version 1.04
 */


(function(a){a.fn.autoResize=function(j){var b=a.extend({onResize:function(){},animate:true,animateDuration:150,animateCallback:function(){},extraSpace:20,limit:1000},j);this.filter('textarea').each(function(){var c=a(this).css({resize:'none','overflow-y':'hidden'}),k=c.height(),f=(function(){var l=['height','width','lineHeight','textDecoration','letterSpacing'],h={};a.each(l,function(d,e){h[e]=c.css(e)});return c.clone().removeAttr('id').removeAttr('name').css({position:'absolute',top:0,left:-9999}).css(h).attr('tabIndex','-1').insertBefore(c)})(),i=null,g=function(){f.height(0).val(a(this).val()).scrollTop(10000);var d=Math.max(f.scrollTop(),k)+b.extraSpace,e=a(this).add(f);if(i===d){return}i=d;if(d>=b.limit){a(this).css('overflow-y','');return}b.onResize.call(this);b.animate&&c.css('display')==='block'?e.stop().animate({height:d},b.animateDuration,b.animateCallback):e.height(d)};c.unbind('.dynSiz').bind('keyup.dynSiz',g).bind('keydown.dynSiz',g).bind('change.dynSiz',g)});return this}})(jQuery);

function setAddress2(){
	$("#user-address input").blur(function() {
	  $(this).css({ backgroundColor: "#fbfbfb" , border:"1px solid #e9e9e9" });
	});
	$("#user-address input").focus(function() {
	  $(this).css({ backgroundColor: "#e8e8e8" , border:"1px solid black" });
	});
	var listOption = '<ul>';
	$(".replace-select option").each(function(index){
		listOption += '<li choice="' + $(this).attr("value") + '"><a href="javascript:;" mode="' + $(this).attr("selected") + '">' + $(this).text() +'</a></li>';
	});
	listOption += '</ul>';
	
	$("#headSelect").attr("isOver", "false");
	$("#headSelect").attr("mode", "close");
	$("#optionSelect").append(listOption);
	$("#headSelect").text('Seleziona');
	$("#headSelect").bind('mouseover', overHeadSelect);
	$("#headSelect").bind('mouseout', outHeadSelect);
	$("#optionSelect ul li a").bind('click', selectChoice);
	$("#headSelect").bind('click', showListOption);
	$(".replace-select").bind('focusout',focusOutSelect);
	
	
	
	$("#optionSelect ul li a").each(function(index){
		if($(this).attr("mode") == "true"){
			setChoice($(this));
		}								  
	});
}

function overHeadSelect(){
	$("#headSelect").attr("isOver", "true");
	if($("#headSelect").attr("mode") == "close"){
		$(this).stop().css({"border":"1px solid black","background-color":"#fbfbfb","background-position":"161px -37px","color":"#2a2a2a"});	
	}
}

function outHeadSelect(){
	$("#headSelect").attr("isOver", "false");
	if($("#headSelect").attr("mode") == "close"){
		$(this).css({"border":"1px solid #e9e9e9", "background-color":"#fbfbfb","background-position":"161px 0"});
	}
}

function showListOption(){	
	$(".replace-select").focus();
	if($('#optionSelect').css("display") == "none"){
		$("#headSelect").attr("mode", "open");
		$("#headSelect").css({"border":"1px solid #e8e8e8","cursor":"default"});
		$("#optionSelect").attr("mode","opened");
		$("#optionSelect").css({"top":"47px","opacity":0,"display":"block"});
		$("#optionSelect").animate({"top":"37px","opacity":1},{duration:300});
	}else{
		$(".replace-select").blur();
	}
}

function selectChoice(){
	var newOption = $(this).parent().attr("choice");
	var newTextOption = $(this).text();
	$("#headSelect").text(newTextOption);
	$("#optionSelect ul li").each(function(index){
		if($(this).attr("choice") == newOption){
			$(this).css({"background-color":"#ffffcc"});
		}else{
			$(this).css({"background-color":"white"});
		}
	});
	$(".replace-select").attr("value",newOption);
	$("#headSelect").css(
		{"color":"#ff8400",borderBottomColor:"#ff8400",borderLeftColor:"#ff8400",borderRightColor:"#ff8400",borderTopColor:"#ff8400"}
	);
	$("#headSelect").animate(
		{"color":"#2a2a2a",borderBottomColor:"#e9e9e9",borderLeftColor:"#e9e9e9",borderRightColor:"#e9e9e9",borderTopColor:"#e9e9e9"}, 
		{duration:2000}
	);
	$(".replace-select").blur();
}

function setChoice(elem){
	var newOption = elem.parent().attr("choice");
	var newTextOption = elem.text();
	$("#headSelect").text(newTextOption);
	$("#optionSelect ul li").each(function(index){
		if($(this).attr("choice") == newOption){
			$(this).css({"background-color":"#ffffcc"});
		}else{
			$(this).css({"background-color":"white"});
		}
	});
	$(".replace-select").attr("value",newOption);
	$("#headSelect").css(
		{"color":"#ff8400",borderBottomColor:"#ff8400",borderLeftColor:"#ff8400",borderRightColor:"#ff8400",borderTopColor:"#ff8400"}
	);
	$("#headSelect").animate(
		{"color":"#2a2a2a",borderBottomColor:"#e9e9e9",borderLeftColor:"#e9e9e9",borderRightColor:"#e9e9e9",borderTopColor:"#e9e9e9"}, 
		{duration:2000}
	);
	$(".replace-select").blur();
}



function focusOutSelect(){
	$("#headSelect").attr("mode", "close");
	$("#headSelect").bind('mouseover', overHeadSelect);
	$("#headSelect").bind('mouseout', outHeadSelect);
	$('#optionSelect').animate({"top":"47px","opacity":0}, 300, "linear", function(){$(this).css({"display":"none"})});
	if($("#headSelect").attr("isOver") == "true"){
		 $("#headSelect").css({"border":"1px solid black", "background-color":"#fbfbfb","background-position":"161px -37px","cursor":"pointer"});
	}else{
		$("#headSelect").css({"border":"1px solid #e9e9e9e", "background-color":"#fbfbfb","background-position":"161px 0","cursor":"pointer"});
	}
}


function setOrders(){
	$("table.user-orders tbody tr").mouseenter(function(){
		$(this).stop().animate(
			{"background-color":"#fdfce8"}, 
			{duration:1000}
		);
		$(this).children("td").stop().animate(
			{"color":"#2a2a2a",borderLeftColor:"#f9e9b1"}, 
			{duration:1000}						   
		);
	});
	
	$("table.user-orders tbody tr").mouseleave(function(){
		$(this).stop().animate(
			{"background-color":"#ffffff"},
			{duration:1000}
		);	
		$(this).children("td").stop().animate(
			{"color":"#999999",borderLeftColor:"#e9e9e9"}, 
			{duration:1000}						   
		);
	});
	$(".status-order a").mouseenter(function(){
		showTip($(this).parent().children('span').attr('class') , $(this));									 
	});
	$(".status-order a").mouseleave(function(){
		clearTip();								 
	});
}

function showTip(typeClass, elem){
	var txt;
	switch(typeClass)
		{
			case 'status-1': txt = "Il tuo ordine &egrave; stato annullato dall'amministratore del sistema su tua richiesta, oppure per incoerenze nei dati.";
				break;
			case 'status-2': txt = "Il tuo ordine &egrave; in preparazione e sta per essere spedito. Riceverai una e-mail al momento della spedizione.";
				break;
			case 'status-3': txt = "Il tuo ordine non &egrave; stato completato.";
				break;
			case 'status-4': txt = "Il pagamento con Paypal ha dato esito negativo.";
				break;
			case 'status-5': txt = "Il tuo ordine &egrave; stato spedito, lo riceverai a breve.";
				break;
			case 'status-6': txt = "Il processo di acquisto non &egrave; stato terminato.";
				break;
			case 'status-7': txt = "Il pagamento non ci è ancora stato confermato. A pagamento ricevuto provvederemo a preparare la spedizione.";
				break;
			default: txt = "";
		}
	var offset = elem.offset();
	$("#tooltip").css({"display":"none",top: offset.top + "px" , left: (offset.left - 260) + "px"});
	$("#tooltip").html(txt);
	$("#tooltip").fadeIn("slow");
}

function clearTip(){
	$("#tooltip").fadeOut("slow");
}


jQuery.fn.ForceNumericOnly = function()
{
    return this.each(function()
    {
        $(this).keydown(function(e)
        {
            var key = e.charCode || e.keyCode || 0;
            // allow backspace, tab, delete, arrows, numbers and keypad numbers ONLY
            return (
                key == 8 || 
                key == 9 ||
                key == 46 ||
                (key >= 37 && key <= 40) ||
                (key >= 48 && key <= 57) ||
                (key >= 96 && key <= 105));
        })
    })
};

function setCart(){
	$(".quantity input").blur(function() {
	  $(this).css({ backgroundColor: "#fbfbfb" , border:"1px solid #e9e9e9" });
	});
	$(".quantity input").focus(function() {
	  $(this).css({ backgroundColor: "#e8e8e8" , border:"1px solid black" });
	});
	
	$(".quantity input").keypress(function(event) {
      if (event.keyCode != 46 && event.keyCode != 8) {
          if (!parseInt(String.fromCharCode(event.which))) {
              event.preventDefault();
          }
      }
  });

	
	$(".quantity input").change(function() {
	   $(".quantity input").ForceNumericOnly();
  		var Qty = Number($(this).attr("value"));
  		var MaxQty = Number($(this).attr("maxqty"));
  		if(Qty > 0 && Qty < MaxQty){$(this).attr("value", Qty);}
  		if(Qty < 0){ $(this).attr("value" , 1);}
  		if(Qty > MaxQty){$(this).attr("value" , MaxQty);}
	});
	
	
	$(".quantity a.quantity-minus").click(function(){
		var elem = 	$(this).parent().children("input");
		var getQty = Number(elem.attr("value"));
		getQty--;
		if(getQty > 0){
			elem.attr("value", getQty);
		}else{
			elem.attr("value", 0);	
		}
		return false;
	});
	
	$(".quantity a.quantity-plus").click(function(){
		var elem = 	$(this).parent().children("input");
		var getQty = Number(elem.attr("value"));
		var getMaxQty = elem.attr("maxqty");
		getQty++;
		if(getMaxQty > getQty){
			elem.attr("value", getQty);
		}else{
			elem.attr("value", getMaxQty);	
		}
		return false;							   
	});
}

function setZoomLabel(){
	$(".image-product a").mouseover(function(e){
		var id = $(this).parent().parent().attr("id");
		showZoom(id);
		checkZoomCoo($(this))
	});	
	$(".image-product a").mouseout(function(){
		closeZoom();										  
	});	
	/*
	$(".image-product a").mousemove(function(e) {
		$("#zoomLabel").css({ "top":e.pageY , "left":e.pageX })
	});
	*/
}

function showZoom(id){
	$("#zoomLabel p").each(function(){
		if($(this).attr("id")  == (id + "-label")){
			$(this).css({"display":"block"});
		}else{
			$(this).css({"display":"none"});
		}						   
	});
	$("#zoomLabel").fadeIn("normal");
}

function closeZoom(){
	$("#zoomLabel").fadeOut("fast");
}

function checkZoomCoo(elem){
	elem.mousemove(function(e){
		var top = e.pageY - $("#zoomLabel").height()/2;
		var left = e.pageX + 50;
		$("#zoomLabel").css({"top":top, "left":left});
	});
}

function setHelp(){
	$("#intro-faq a").click(function(){
		var faq = String($(this).attr("href")).toLowerCase().split("#")
		scrollToFaq(faq[1]);
		return false;		   
	});
}

function scrollToFaq(id){
	var offset = $('#' + id).offset();
	$('html, body').animate({scrollTop: offset.top + "px"}, 1000);
	$('#' + id + " strong").delay(1000).effect("pulsate", { times:3 }, 400);
	$('#' + id).effect("highlight", {}, 3000);

}

function setCondition(){
	if($("#condizioni:checked")){
		return true;
	}else{
		openModalfeedBack("Attenzione" , "&Egrave; necessario prendere visione delle condizioni di vendita ed accettarle per effettuare l'ordine" , 0)
		return false;
	}									 
}
