
jQuery(document).ready(function(){

	// Pdf preview download
	jQuery('#pdfPreview').click(function()
								{
									jQuery('#previewFrm').attr('src', '');
									jQuery('#previewFrm').attr('src', 'utils.php?func=DownloadPdfPreview&n=' + escape(jQuery(this).attr('rel')) + '&rand=' + Math.random());
								});
	
	// La mia libreria - paginazione
	var pagedOption = {rowsPerPage : 5};
	jQuery('#my-orders-table-paged').tablePagination(pagedOption);
	
	// Overlay aggiungi al carrello
	var options = {success: showResponse};

	jQuery("button[rel]").overlay(
					{
						expose: {color: '#333',loadSpeed: 20,opacity: 0.7},
						onBeforeLoad: function (e)
							{
								jQuery('#overlayResponse').html("");
								if (this.getTrigger().attr('name') != 'viaForm' && this.getTrigger().attr('name') != 'multiBook')
								{
                                                                        var url = this.getTrigger().attr('name');
                                                                        var split = url.split("/");
                                                                        var id = split[split.length-2]

                                                                        var peso = 0;
                                                                        if(jQuery('#pesopdf_'+id).length > 0 ){
                                                                         peso= parseInt(jQuery('#pesopdf_'+id).val());
                                                                        }
                                                                        if(peso > 40){
                                                                            if(navigator.userAgent.toLowerCase().match(/iPad/i)){
                                                                                jQuery('#warningOverlayBuy').addClass('ipad');
                                                                            }
                                                                            jQuery('#warningOverlayBuy').show();
                                                                        } else  {
                                                                            jQuery('#warningOverlayBuy').hide();
                                                                        }
									jQuery.get(this.getTrigger().attr('name'), function(data) {HandleResponse(data);});
									return;
								}
								else if (this.getTrigger().attr('name') == 'multiBook')	//multiBook
								{
                                                                        var peso = 0;
                                                                        if(this.getTrigger()[0].form.pesopdf){
                                                                         peso= parseInt(this.getTrigger()[0].form.pesopdf.value);
                                                                        }
                                                                        if(peso > 40){
                                                                            if(navigator.userAgent.toLowerCase().match(/iPad/i)){
                                                                                jQuery('#warningOverlayBuy').addClass('ipad');
                                                                            }
                                                                            jQuery('#warningOverlayBuy').show();
                                                                        } else  {
                                                                            jQuery('#warningOverlayBuy').hide();
                                                                        }
                                                                        
									jQuery('#product_addtocart_form').ajaxForm(options);
									jQuery('#product_addtocart_form').submit();
									return;
								}
								else
								{
                                                                        var peso = 0;
                                                                        if(this.getTrigger()[0].form.pesopdf){
                                                                         peso= parseInt(this.getTrigger()[0].form.pesopdf.value);
                                                                        }
                                                                        if(peso > 40){
                                                                            if(navigator.userAgent.toLowerCase().match(/iPad/i)){
                                                                                jQuery('#warningOverlayBuy').addClass('ipad');
                                                                            }
                                                                            jQuery('#warningOverlayBuy').show();
                                                                        } else  {
                                                                            jQuery('#warningOverlayBuy').hide();
                                                                        }
									jQuery(this.getTrigger().parent().parent()).ajaxForm(options);
									this.getTrigger().parent().parent().submit();
									//jQuery('#product_addtocart_form').submit();
								}
							},
						closeOnClick: true,
						api: true,
						close: '#overlayClose'
					});

/******************************** COMMENTATI TOOLTIP ************************************************************
	
      if(!navigator.userAgent.toLowerCase().match(/iPad/i) && !navigator.userAgent.toLowerCase().match(/Android/i)){
	jQuery("#booktip-target-1").ezpz_tooltip({contentPosition: 'rightStatic', stayOnContent: true, offset: 0});
	jQuery("#booktip-target-2").ezpz_tooltip({contentPosition: 'rightStatic', stayOnContent: true, offset: 0});
	jQuery("#booktip-target-3").ezpz_tooltip({contentPosition: 'rightStatic', stayOnContent: true, offset: 0});
	jQuery("#booktip-target-4").ezpz_tooltip({contentPosition: 'rightStatic', stayOnContent: true, offset: 0});
	jQuery("#booktip-target-5").ezpz_tooltip({contentPosition: 'rightStatic', stayOnContent: true, offset: 0});
	jQuery("#booktip-target-6").ezpz_tooltip({contentPosition: 'rightStatic', stayOnContent: true, offset: 0});
	
	jQuery("#booktip-target-7").ezpz_tooltip({contentPosition: 'rightStatic', stayOnContent: true, offset: 0});
	jQuery("#booktip-target-8").ezpz_tooltip({contentPosition: 'rightStatic', stayOnContent: true, offset: 0});
	jQuery("#booktip-target-9").ezpz_tooltip({contentPosition: 'rightStatic', stayOnContent: true, offset: 0});
	jQuery("#booktip-target-10").ezpz_tooltip({contentPosition: 'rightStatic', stayOnContent: true, offset: 0});
	jQuery("#booktip-target-11").ezpz_tooltip({contentPosition: 'rightStatic', stayOnContent: true, offset: 0});
	jQuery("#booktip-target-12").ezpz_tooltip({contentPosition: 'rightStatic', stayOnContent: true, offset: 0});
	
	jQuery("#booktip-target-13").ezpz_tooltip({contentPosition: 'rightStatic', stayOnContent: true, offset: 0});
	jQuery("#booktip-target-14").ezpz_tooltip({contentPosition: 'rightStatic', stayOnContent: true, offset: 0});
	jQuery("#booktip-target-15").ezpz_tooltip({contentPosition: 'rightStatic', stayOnContent: true, offset: 0});
	jQuery("#booktip-target-16").ezpz_tooltip({contentPosition: 'rightStatic', stayOnContent: true, offset: 0});
	jQuery("#booktip-target-17").ezpz_tooltip({contentPosition: 'rightStatic', stayOnContent: true, offset: 0});
	jQuery("#booktip-target-18").ezpz_tooltip({contentPosition: 'rightStatic', stayOnContent: true, offset: 0});
	
	jQuery("#booktip-target-19").ezpz_tooltip({contentPosition: 'rightStatic', stayOnContent: true, offset: 0});
	jQuery("#booktip-target-20").ezpz_tooltip({contentPosition: 'rightStatic', stayOnContent: true, offset: 0});
	jQuery("#booktip-target-21").ezpz_tooltip({contentPosition: 'rightStatic', stayOnContent: true, offset: 0});
	jQuery("#booktip-target-22").ezpz_tooltip({contentPosition: 'rightStatic', stayOnContent: true, offset: 0});
	jQuery("#booktip-target-23").ezpz_tooltip({contentPosition: 'rightStatic', stayOnContent: true, offset: 0});
	jQuery("#booktip-target-24").ezpz_tooltip({contentPosition: 'rightStatic', stayOnContent: true, offset: 0});	
	
	jQuery("#booktip-target-25").ezpz_tooltip({contentPosition: 'rightStatic', stayOnContent: true, offset: 0});
	jQuery("#booktip-target-26").ezpz_tooltip({contentPosition: 'rightStatic', stayOnContent: true, offset: 0});
	jQuery("#booktip-target-27").ezpz_tooltip({contentPosition: 'rightStatic', stayOnContent: true, offset: 0});
	jQuery("#booktip-target-28").ezpz_tooltip({contentPosition: 'rightStatic', stayOnContent: true, offset: 0});
	jQuery("#booktip-target-29").ezpz_tooltip({contentPosition: 'rightStatic', stayOnContent: true, offset: 0});
	jQuery("#booktip-target-30").ezpz_tooltip({contentPosition: 'rightStatic', stayOnContent: true, offset: 0});	
	
	jQuery("#booktip-target-31").ezpz_tooltip({contentPosition: 'rightStatic', stayOnContent: true, offset: 0});
	jQuery("#booktip-target-32").ezpz_tooltip({contentPosition: 'rightStatic', stayOnContent: true, offset: 0});	
	
        jQuery("#booktip-target-33").ezpz_tooltip({contentPosition: 'rightStatic', stayOnContent: true, offset: 0});
	jQuery("#booktip-target-34").ezpz_tooltip({contentPosition: 'rightStatic', stayOnContent: true, offset: 0});
	jQuery("#booktip-target-35").ezpz_tooltip({contentPosition: 'rightStatic', stayOnContent: true, offset: 0});
	jQuery("#booktip-target-36").ezpz_tooltip({contentPosition: 'rightStatic', stayOnContent: true, offset: 0});
	jQuery("#booktip-target-37").ezpz_tooltip({contentPosition: 'rightStatic', stayOnContent: true, offset: 0});
	jQuery("#booktip-target-38").ezpz_tooltip({contentPosition: 'rightStatic', stayOnContent: true, offset: 0});

	jQuery("#booktip-target-39").ezpz_tooltip({contentPosition: 'rightStatic', stayOnContent: true, offset: 0});
	jQuery("#booktip-target-40").ezpz_tooltip({contentPosition: 'rightStatic', stayOnContent: true, offset: 0});

        jQuery("#booktip-target-41").ezpz_tooltip({contentPosition: 'rightStatic', stayOnContent: true, offset: 0});
	jQuery("#booktip-target-42").ezpz_tooltip({contentPosition: 'rightStatic', stayOnContent: true, offset: 0});
	jQuery("#booktip-target-43").ezpz_tooltip({contentPosition: 'rightStatic', stayOnContent: true, offset: 0});
	jQuery("#booktip-target-44").ezpz_tooltip({contentPosition: 'rightStatic', stayOnContent: true, offset: 0});
	jQuery("#booktip-target-45").ezpz_tooltip({contentPosition: 'rightStatic', stayOnContent: true, offset: 0});
	jQuery("#booktip-target-46").ezpz_tooltip({contentPosition: 'rightStatic', stayOnContent: true, offset: 0});

	jQuery("#booktip-target-47").ezpz_tooltip({contentPosition: 'rightStatic', stayOnContent: true, offset: 0});


	//chiudi tip
	jQuery(".booktip-close").click(function(){jQuery(this).parent().hide();});
	jQuery(".booktip-close-right").click(function(){jQuery(this).parent().hide();});
      }

************************************************************************************************************************/




	//attiva tab slider	
	// jQuery('.jcarousel-control a').click(function()
	// {
	//  jQuery('.jcarousel-control a').removeClass('active');
	//  jQuery(this).addClass('active');
	// });



	//jQuery("#top_slider .jcarousel-prev").click( function(e) {
//		if (!jQuery("#navi-left").is('.jcarousel-prev-disabled')) {												  
//												  
//			var $active = jQuery(".active");
//			var $next = $active.prev().length ? $active.prev() : jQuery(".focusitem:last");
//	 		setTimeout( function() {		
//				$active.removeClass('active');
//				$active.addClass('notactive');
//				$next.addClass('active');
//				$next.removeClass('notactive');
//			}, 400);
//			
//		}
//	});
//	
//	jQuery("#top_slider .jcarousel-next").click( function(e) {
//		if (!jQuery("#navi-right").is('.jcarousel-next-disabled')) {											  
//		
//			var $active = jQuery(".active");
//			var $next = $active.next().length ? $active.next() : jQuery(".focusitem:first");
//	 		setTimeout( function() {
//				$active.removeClass('active');
//				$active.addClass('notactive');
//				$next.addClass('active');
//				$next.removeClass('notactive');
//			}, 400);			
//			
//		} 
//	});

});


// Handle dell'errore di "oggetto gi� presente nel carrello"
function HandleResponse(p_data)
{
	if (p_data != undefined && p_data != "")
	{
		var patt1 = /<li class="error-msg"><ul><li>(.*)<\/li><\/ul>/;
		
		t_response = p_data.match(patt1);
		if (t_response != undefined && t_response != null)
		{
			if (t_response.length > 0)
			{
				if (t_response[1] != undefined && t_response[1] != "")
				{
					jQuery('#overlayResponse').html("");
					jQuery('#overlayResponse').html(t_response[1]);
				}
			}
		}
	}
	
	// Valorizza il numero degli items presenti nel carrello.
	CountItemsInCart();
}

function CountItemsInCart()
{
	jQuery.get("/utils.php?func=CountItemsInCart&rand=" + Math.random(),
		function(data)
		{
			jQuery('.top-link-cart').html("Carrello (" + data + " items)");
		});
}

// infomail newsletter
function check_fields_newsletter(){
	firstName=document.subscribe.firstName.value
	lastName=document.subscribe.lastName.value
	email=document.subscribe.email.value
	
	if(firstName == "" || firstName == "Nome" || firstName == null || !isNaN(firstName) || firstName.charAt(0) == ' ') {
	    alert('Nome obbligatorio');
		return false;
	}
	else if(lastName == "" || lastName == "Cognome" || lastName == null || !isNaN(lastName) || lastName.charAt(0) == ' '){
		alert('Cognome obbligatorio');
		return false;
	}
	else if(!indirizzoEmailValido(email)){
		alert('Indirizzo email non valido');
		return false;
	}
	else if(document.subscribe.privacy.checked==false){
		alert('Per procedere con l\'invio del modulo e\' necessario accettare le condizioni presenti nell\'informativa.');
		return false;
	}
	else{
		return true;
	}
}
function indirizzoEmailValido(indirizzo) {
  if (window.RegExp) {
    var nonvalido = "(@.*@)|(\\.\\.)|(@\\.)|(\\.@)|(^\\.)";
    var valido = "^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,4}|[0-9]{1,3})(\\]?)$";
    var regnv = new RegExp(nonvalido);
    var regv = new RegExp(valido);
    if (!regnv.test(indirizzo) && regv.test(indirizzo))
      return true;
    return false;
	}
  else {
    if(indirizzo.indexOf("@") >= 0)
      return true;
    return false;
  	}
};


jQuery(document).ready(function(){
								
});







// rollover listato scuolabook v1
/*
function boxOffHover(box) {
	box.className ='product-shop';
}

function boxOnHover(box) {
	box.className='product-shop-Over';
}
*/


// homepage tab scuolabook v1
/*
function tab_show_tab(N,T) {
	var i=0;
	for(i=0; i<T; i++) {
		if(i==N) {
			//show it
			if (document.getElementById){ 
				obj = document.getElementById('tab_div'+i); 
				obj.style.display = ''; 
				document.getElementById('tab_tab'+i).className='self'; 
			} 						
		} else {
			if (document.getElementById){ 
				obj = document.getElementById('tab_div'+i); 
				obj.style.display = 'none'; 
				document.getElementById('tab_tab'+i).className=''; 
			} 						
		}
	}
}
*/

function showResponse(responseText, statusText, xhr, $form)
{
	CountItemsInCart();
}
