	
	var immagine_width = 1680;
	var immagine_height = 1100;
	var immagine_ratio = 1.05;
	
	var current_page = "";	
	
(function($) {
	var imgList = [];
	$.extend({
		preload: function(imgArr, option) {
			var setting = $.extend({
				init: function(loaded, total) {},
				loaded: function(img, loaded, total) {},
				loaded_all: function(loaded, total) {}
			}, option);
			var total = imgArr.length;
			var loaded = 0;
			
			setting.init(0, total);
			for(var i in imgArr) {
				imgList.push($("<img />")
					.attr("src", imgArr[i])
					.load(function() {
						loaded++;
						setting.loaded(this, loaded, total);
						if(loaded == total) {
							setting.loaded_all(loaded, total);
						}
					}).error(function(){
						//alert('Check image ' + loaded + '!');
						loaded++
						setting.loaded(this, loaded, total);
						if(loaded == total) {
							setting.loaded_all(loaded, total);}
						})
				);
			}
			
		}
	});
})(jQuery);

$.fn.qtip.styles.spare = { // Last part is the name of the style
	'font-size': '14px',
	width: 250,
    padding: 10,
    background: '#cecece',
	textAlign: 'left',
	border: {
		width: 3,
		radius: 3,
		color: '#cecece'
      	},
	tip: { // Now an object instead of a string
         corner: 'bottomMiddle', // We declare our corner within the object using the corner sub-option
         color: '#cecece',
         size: {x: 20,y : 8}
		}
}

/*
$.fn.qtip.styles.spare = { // Last part is the name of the style
	'font-size': '14px',
	width: 250,
    padding: 5,
    background: '#ccc',
	textAlign: 'left',
	border: {
		width: 3,
		radius: 3,
		color: '#ccc'
      	},
	tip: { // Now an object instead of a string
         corner: 'bottomMiddle', // We declare our corner within the object using the corner sub-option
         color: '#ccc',
         size: {x: 20,y : 8}
		}
}

*/

$.fn.qtip.styles.installations = { // Last part is the name of the style width: {min : 100},
	'font-size': '14px',
	width: {min : 100},
    padding: 3,
    background: '#ccc',
	textAlign: 'center',
	border: {
		width: 3,
		radius: 3,
		color: '#ccc'
      	},
	tip: { // Now an object instead of a string
         corner: 'bottomMiddle', // We declare our corner within the object using the corner sub-option
         color: '#ccc',
         size: {x: 20,y : 8}
		}
}

	
	function goMenu(){
		resizing("immagine",immagine_width,immagine_height,immagine_ratio);
		//$('#page-background').fadeIn(500,function(){$('#box1').show('slide',{ direction: "up" , easing: "easeOutCubic"}, 200,function (){ $('#box2').show('slide',{ direction: "up" }, 150,function (){ $('#box3').show('slide',{ direction: "up" }, 300, function (){$('#footer-animation').show("slide", { direction: "down" }, 400, function(){goBody();}); });});});});
		
		goBody();
		return false;
	}
	
	function goMenuHome(){
		resizing("immagine",immagine_width,immagine_height,immagine_ratio);
		
		if ($.browser.msie) {
		$('#page-background').fadeIn(500,function(){$('#box1').show('slide',{ direction: "up" , easing: "easeOutCubic"}, 200,function (){ $('#box2').show('slide',{ direction: "up" }, 150,function (){ $('#box3').show('slide',{ direction: "up" }, 300, function (){$('#footer-animation').show("slide", { direction: "down" }, 400, function(){$('#footer-home1').show();$('#footer-home2').delay(100).show(); $('#footer-home3').delay(200).show(); goBody();}); });});});}); 
		}
		else {
		$('#page-background').fadeIn(500,function(){$('#box1').show('slide',{ direction: "up" , easing: "easeOutCubic"}, 200,function (){ $('#box2').show('slide',{ direction: "up" }, 150,function (){ $('#box3').show('slide',{ direction: "up" }, 300, function (){$('#footer-animation').show("slide", { direction: "down" }, 400, function(){$('#footer-home1').fadeIn(200);$('#footer-home2').delay(50).fadeIn(200); $('#footer-home3').delay(100).fadeIn(300,function(){goBody();});}); });});});}); 
		}
		return false;
	}
	
	function isFilled(str){
		if (str == "" || str == null || typeof(str)=="undefined") {return false}
		else {return true}
	}
	
	function goBody(){
			if (current_page == 'home' && !$.browser.msie) {
				$("#body").fadeIn(600,"easeInOutExpo");
				$('.cloud-zoom, .cloud-zoom-gallery').CloudZoom();
				$('.jcarousel-skin-ata').jcarousel({
        			easing: 'easeInOutExpo',
					scroll: 4,
       				animation: 500
    			});
			}
			else {
				$("#body").slideDown(300,function(){goUrlAnchor();});
				//$("#body").slideDown(600,"easeOutQuint",function(){alert('test')});
				$('.cloud-zoom, .cloud-zoom-gallery').CloudZoom();
				$('.jcarousel-skin-ata').jcarousel({
        			easing: 'easeInOutExpo',
					scroll: 4,
       				animation: 500
    			});
			}
			
			return false;
	}
	
	function goGallery(elm) {
			gallery_elm = elm.parent().parent().parent();
			image1 = gallery_elm.children(".image").children(".wrapper").children(".image1").children("img");
			image2 = gallery_elm.children(".image").children(".wrapper").children(".image2").children("img");
			image1.attr('src',image2.attr('src'));
			image2.fadeOut(0);
			loader = gallery_elm.children(".image").children(".wrapper").children(".loader");
			
			next_src = elm.attr('src').replace('.jpg','-large.jpg');
			
			image2.attr('src',next_src);loader.fadeOut(0);image2.fadeIn(300);
			
			/*
			$.preload([next_src],
            	{
            	init: function(loaded, total) {loader.css({opacity:0.0}).show().fadeTo(150, 0.3);},
            	loaded: function(img, loaded, total) {},
            	loaded_all: function(loaded, total) {image2.attr('src',next_src);loader.fadeOut(0);image2.fadeIn(300)}
       			 });
			
			*/
			
			/*
			alert(next_src);
			var immagini = new Array();
			
			immagini[0] = next_src;
				
			
			var gt = immagini.length;
			$.each(immagini,function(e) {
				$(new Image()).load(function() {
				if (--gt < 1) function() {image2.attr('src',next_src);loader.fadeOut(0);image2.fadeIn(300)};		}).attr('src',this);
			});
			*/
			return false;
}
	
	function addToCart(elm){
		code = $('#' + elm).children("h1").text();
		cookie_exists = getCookie("cart_" + code);		
		if ( cookie_exists == null || cookie_exists == '') {
		  descr = $('#' + elm).children("h2").text();
		  quantity = 1;
		  price = $('#' + elm).children("h3").children(".price").text();
		  price = price.replace(/,/g,".");
		  if (isNaN(price)) {price = "0";}
		  days = 5;
		  str_to_store = descr + '[]' + quantity + '[]' + price;
		  setCookie("cart_" + code,str_to_store,days);
		  active_raws += 1;
		  total_amount += getVal(quantity, price);
		  createCartRaw(code,descr,quantity,price);
		  updateShowEmpty();
		  alert(labels["msg_add"]);
		}
		else {alert(labels["msg_exists"]);}
	
	}
	
	// For products
	function addToCartProduct(elm){
		code = $('#product-code').text();
		cookie_exists = getCookie("cart_" + code);		
		if ( cookie_exists == null || cookie_exists == '') {
		  descr = $('#product-description').text();
		  descr = descr.substr(0,70).toUpperCase() + '...';
		  quantity = 1;
		  price = $('#product-price').text();
		  price = price.replace(/,/g,".");
		  if (isNaN(price)) {price = "0";}
		  days = 5;
		  str_to_store = descr + '[]' + quantity + '[]' + price;
		  setCookie("cart_" + code,str_to_store,days);
		  //active_raws += 1;
		  //total_amount += getVal(quantity, price);
		  //createCartRaw(code,descr,quantity,price);
		  //updateShowEmpty();
		  alert(labels["msg_add"]);
		}
		else {alert(labels["msg_exists"]);}
	
	}
	
	// For accessories
	function addToCartAcs(elm){
		code = $('#' + elm).children("h1").text();
		cookie_exists = getCookie("cart_" + code);		
		if ( cookie_exists == null || cookie_exists == '') {
		  descr = $('#' + elm).children("h2").text();
		  quantity = 1;
		  price = $('#' + elm).children("h3").children(".price").text();
		  price = price.replace(/,/g,".");
		  if (isNaN(price)) {price = "0";}
		  days = 5;
		  str_to_store = descr + '[]' + quantity + '[]' + price;
		  setCookie("cart_" + code,str_to_store,days);
		  //active_raws += 1;
		  //total_amount += getVal(quantity, price);
		  //createCartRaw(code,descr,quantity,price);
		  //updateShowEmpty();
		  alert(labels["msg_add"]);
		}
		else {alert(labels["msg_exists"]);}
	
	}
	
	// For reservedarea-spareparts-detail-zoom.php
	function addToCartBig(elm){	
		code = $('#' + elm).children("h1").text();
		cookie_exists = getCookie("cart_" + code);		
		if ( cookie_exists == null || cookie_exists == '') {
		  descr = $('#' + elm).children("h2").text();
		  quantity = 1;
		  price = $('#' + elm).children("h3").children(".price").text();
		  price = price.replace(/,/g,".");
		  if (isNaN(price)) {price = "0";}
		  days = 5;
		  str_to_store = descr + '[]' + quantity + '[]' + price;
		  setCookie("cart_" + code,str_to_store,days);
		  window.parent.active_raws += 1;
		  window.parent.total_amount += getVal(quantity, price);
		  window.parent.createCartRaw(code,descr,quantity,price);
		  window.parent.updateShowEmpty();
		  alert(window.parent.labels["msg_add"]);
		}
		else {alert(window.parent.labels["msg_exists"]);}
	}
	
	
	function updateCart(elm) {
		code = $(elm).parent().parent().children('.code').text();
		value = getCookie("cart_" + code);
		str_recover = value.split("[]");
		descr =  str_recover[0];
		quantity_ex = str_recover[1];
		quantity = $(elm).val();
		if (quantity == "" || quantity == "0") {quantity = 1}
		price = str_recover[2];
		str_to_store = descr + '[]' + quantity + '[]' + price;
		days = 5;
		total_amount += getVal(quantity, price) - getVal(quantity_ex, price);
		setCookie("cart_" + code,str_to_store,days);
		updateShowEmpty();
	}
	
	function checkKeyCode()
{
  if( (event.keyCode == 189 || event.keyCode == 109) || event.keyCode == 8 || event.keyCode == 24 || event.keyCode == 39 ||  event.keyCode == 37 || 
      (event.keyCode >= 48 && event.keyCode <= 57) || 
      (event.keyCode >= 96 && event.keyCode <= 105) ) {
    return true; }
  else {
    return false;
  }
}

	function createCartRaw(code,descr,quantity,price){
			out = '<tr id="tr_' + code +'"><td align="center" class="code">' + code + '</td><td>' + descr + '</td><td align="center"><input size="4" maxlength="4" onkeydown="return checkKeyCode()" onkeyup="updateCart(this)" type="text" value="' + quantity + '" /></td>';
			out += '<td align="right">' + showNumber(price) + '</td><td align="center"><a href="javascript:void(0)" onclick="removeCart(this)">' + labels["elimina"] + '</a></td></tr>';
			$('#cart-table tbody').append(out);
			//$('#tr_' + code).css('opacity',0.1);
			//$('#tr_' + code).animate({opacity : 1}, 'slow');
			return false;
	}
	
	function isOdd(x) { return !isEven(x); }
	function isEven(x) { return (x%2)?false:true; }
	
	function setCookie(sNome, sValore, iGiorni) {
 		 var dtOggi = new Date();
 		 var dtExpires = new Date();
 		 dtExpires.setTime(dtOggi.getTime() + 24 * iGiorni * 3600000);
  		//document.cookie = sNome + "=" + escape(sValore) + "; expires=" + dtExpires.toGMTString() + "; path=/";
 		 document.cookie = sNome + "=" + escape(sValore) + "; expires=" + dtExpires.toGMTString(); // + "; path=/";
  		//document.cookie = sNome + "=" + escape(sValore) + ";expires=Sat, 20-Jan-2018 00:00:01 GMT";
		}

	// restituisce il valore del cookie sNome
	function createSpareTable() {
		var items = new Array();
 	 // genera un array di coppie "Nome = Valore"
  		// NOTA: i cookies sono separati da ';'
  	var asCookies = document.cookie.split("; ");
  	// ciclo su tutti i cookies
	
	var num_cookies = asCookies.length;
  		for (var iCnt = 0; iCnt < num_cookies; iCnt++)
  	{
    	// leggo singolo cookie "Nome = Valore"
    	var asCookie = asCookies[iCnt].split("=");
    	items[asCookie[0]] = unescape(asCookie[1]);
 	 }
  	// SE non esiste il cookie richiesto

 if (num_cookies <= 0) {}
 else {
  for (x in items) {
	  if (x.indexOf("cart_") != -1) {
		code = x.replace("cart_","");
		str_recover = items[x].split("[]");
		descr =  str_recover[0];
		quantity = str_recover[1];
		price = str_recover[2];
		active_raws += 1;
		total_amount += getVal(quantity, price);	
		createCartRaw(code,descr,quantity,price);
	  }
	}
 }
  updateShowEmpty();
}


// restituisce il valore del cookie sNome
	function deleteCartOrder() {
		var items = new Array();
 	 // genera un array di coppie "Nome = Valore"
  		// NOTA: i cookies sono separati da ';'
  	var asCookies = document.cookie.split("; ");
  	// ciclo su tutti i cookies
	
	var num_cookies = asCookies.length;
  		for (var iCnt = 0; iCnt < num_cookies; iCnt++)
  	{
    	// leggo singolo cookie "Nome = Valore"
    	var asCookie = asCookies[iCnt].split("=");
    	items[asCookie[0]] = unescape(asCookie[1]);
 	 }
  	// SE non esiste il cookie richiesto

 if (num_cookies <= 0) {}
 else {
  for (x in items) {
	  if (x.indexOf("cart_") != -1) {
		eraseCookie(x);
		//code = x.replace("cart_","");
	  }
	}
 }
}



function formatNum(num_to_format){
	return (convertNumber(num_to_format).toFixed(2));
	
}

function getVal(quantity, price){
	newprice = convertNumber(price);
	newquantity = convertNumber(quantity);
	return (newprice * newquantity);
}
	
function getCookie(sNome) {
  // genera un array di coppie "Nome = Valore"
  // NOTA: i cookies sono separati da ';'
  var asCookies = document.cookie.split("; ");
  // ciclo su tutti i cookies
  for (var iCnt = 0; iCnt < asCookies.length; iCnt++)
  {
    // leggo singolo cookie "Nome = Valore"
    var asCookie = asCookies[iCnt].split("=");
    if (sNome == asCookie[0]) {
      return (unescape(asCookie[1]));
    }
  }
  // SE non esiste il cookie richiesto
  return("");
}
	
	function convertNumber(num) {
		newnum = num + '';
		newnum = newnum.replace(",",".");
		newnum = parseFloat(newnum);
		return newnum;
	}
	
	function showNumber(num) {
		newnum = formatNum(num);
		newnum = newnum + '';
		newnum = newnum.replace(".",",");
		return newnum;
	}
	
	function eraseCookie(name) {
		setCookie(name,"",-1);
	}
	
	function updateShowEmpty() {
		if (active_raws == 0) {
			$('#empty-msg').show();
			$('#amount_raw').hide();
			$('#checkout_box').hide();
			total_amount = 0;
		}
		else {
				$('#empty-msg').hide();
				$('#amount_raw').show();
				$('#checkout_box').show();
		}
		
		$('#amount_sum').text(showNumber(total_amount));
		
		
	}
	
	function removeCart(elm){
		code = $(elm).parent().parent().children('.code').text();
		active_raws -= 1;
		
		value = getCookie("cart_" + code);
		str_recover = value.split("[]");
		descr =  str_recover[0];
		quantity = str_recover[1];
		price = str_recover[2];
		
		total_amount -= getVal(quantity, price);
		eraseCookie("cart_" + code);
		//$(elm).parent().parent().fadeOut('slow', function() {$(elm).parent().parent().remove();});
		
		//$(elm).parent().parent().hide('slide',{ direction: "up"},300,function() {$(elm).parent().parent().remove();});
		//$(elm).parent().parent().fadeTo(300, 0).slideUp(300, function() {$(elm).parent().parent().remove();})
		
		//$(elm).parent().parent().fadeTo("slow", 0.1, function(){$(this).slideUp("slow", function() {$(this).remove();});});
		//$(elm).parent().parent().find('td').wrapInner('<div style="display: block;" />').parent().find('td > div').slideUp(700, function(){$(this).parent().parent().remove();});
 
 		$(elm).parent().parent().find('td').wrapInner('<div style="display: block;" />').parent().find('td > div').fadeTo(300, 0.1).slideUp(300, function(){$(this).parent().parent().remove();});
		
		//$(elm).parent().parent().remove();
		
		updateShowEmpty();
	}
	
	function maximize(){
		var footer_y = $('#footer').offset().top;
		var window_y = $(window).height();
		var footer_h = $('#footer').height();
		var top_h = $('#top').height();
		var body_obj = $('#body');
		var body_y = body_obj.offset().top;
		var add_h = footer_y - 2 - body_y;
		var footer_bottom = footer_y + footer_h;
		
		
		if (footer_bottom == window_y) {
			
			if ( $.browser.msie ) {
				if (parseInt($.browser.version) < 7.0){body_obj.css({'height' :  add_h});}
			}
			else {
				body_obj.css({'min-height' :  add_h, 'height' :  add_h, 'height' : 'auto !important'}); 
			}
		}

		// if (footer_bottom == window_y) {body_obj.css({'min-height' :  add_h, 'height' :  add_h, 'height' : 'auto !important;'});  body_obj.text(add_h)} 

	}
	
	function resizing(image_id,image_w,image_h,image_zoom_factor){
		var image_obj = $('#' + image_id);	
		var parent_node = image_obj.parent();	
		var w_actual = parent_node.width();
		var h_actual = parent_node.height();
		var zoom_factor = image_zoom_factor;
		var ratio = image_w / image_h;
		var w_zoom = Math.round(w_actual * zoom_factor);
		var h_zoom = Math.round(h_actual * zoom_factor);
		var ratio_new = w_zoom/h_zoom ;
		var w,h,x,y;
		
		if (ratio_new > ratio) {
		
			w = w_zoom;
			x = Math.round((w_zoom - w_actual) / 2);
			h = Math.round(w / ratio);
			y = Math.round((h - h_actual) / 2);		
		}
		else {
			h = h_zoom;
			w = Math.round(ratio * h);	
			x = Math.round((w - w_actual) / 2);
			y = Math.round((h_zoom - h_actual) / 2);
						
		}
	
		image_obj.css("width",w);
		image_obj.css("height",h);
		//image_obj.css("margin-top","-" + y + "px");
		//image_obj.css("margin-left","-" + x + "px");
		
	return false
}
  
function trimSpaces(str) {
	return str.replace(/^([\s\t\n]|\&nbsp\;)+|([\s\t\n]|\&nbsp\;)+$/g, '');

}

function removeSpaces(str) {
	return str.replace(/ /g,"");
}

function searchTable(table_obj,item_to_search,col_to_search) {
	if (item_to_search.length > 0) {
	  $obj = $(table_obj);
	  
	  if (!isFilled(col_to_search)) {
		  $.uiTableFilter($obj, item_to_search);
		}
	  else {
		  $.uiTableFilter($obj , item_to_search, col_to_search);
	  }
	}
	else {
		$.uiTableFilter($obj, '');
	}
	
}

function goPrint(urlprint,nameprint) {
	p = new Array();
	p["file"] = urlprint;
	p["name"] = "print";
	openWindow(p);
}


function openWindow(params) {
	$(this).popupWindow({ 
	centerScreen:1,
	//centerBrowser:1,
	height:600, 
	width:850,
	scrollbars:1,
	
	windowURL: params['file'], 
	windowName: params['name'],
	fullSize:1
	}); 
}

function readMore(obj) {
	$(obj).hide();
	moretext = $(obj).parent().parent().parent().find('.textmore').slideDown('fast');	
}
function readLess(obj) {
	$(obj).parent().parent().slideUp('fast');
	$(obj).parent().parent().parent().parent().find('.readmore').show();
}

function isAnchor(str) {
	if (str.indexOf('#') > -1) {return true;}
	else {return false;}
}

function goUrlAnchor() {
if(isAnchor(document.location.toString())) {
	var myAnchor = '#' + document.location.toString().split('#')[1];
	
	$('html, body').animate({ scrollTop: $(myAnchor).offset().top - 30 }, 500);
	//document.location.href="'" + myAnchor + "'";

}
}
