
function changeCommandsDisplay(i,show){
	if(!isMobileDevice()){
		if(show){
			jQuery(".commands" + i).show();
		}else{
			jQuery(".commands" + i).hide();
		}
	}
}

//adds .naturalWidth() and .naturalHeight() methods to jQuery
//for retreaving a normalized naturalWidth and naturalHeight.
(function($){
	  var
	  props = ['Width', 'Height'],
	  prop;
	 
	  while (prop = props.pop()) {
	    (function (natural, prop) {
	      $.fn[natural] = (natural in new Image()) ? 
	      function () {
	        return this[0][natural];
	      } : 
	      function () {
	        var 
	        node = this[0],
	        img,
	        value;
	 
	        if (node.tagName.toLowerCase() === 'img') {
	          img = new Image();
	          img.src = node.src,
	          value = img[prop];
	        }
	        return value;
	      };
	    }('natural' + prop, prop.toLowerCase()));
	  }
	}(jQuery));

function createGallery(){
	/* This code is executed after the DOM has been completely loaded */
	
	//per ipad rendo sempre visibili le frecce
	if(isMobileDevice()){
		jQuery(".commands1").show();
		jQuery(".commands2").show();
	}
	
	var totWidth=0;
	var totThumbWidth = 0;
	var positions = new Array();
	var thumbPositions = new Array();
	var widthArray = new Array();
	var slideWidthArray = new Array();
	var currentItem = 0;
	var thumbPos = 0;
	
	jQuery("#galleryMain").width(panelWidth);
	jQuery("#gallery").width(panelWidth);
		
	//se vedo una sola immagine per volta (step == 1) la metto centrata rispetto al contenitore
	if(steps == 1){
		jQuery(".slide").css("width", panelWidth);
		jQuery(".slide").css("text-align", "center");
	}
	
	//quando le immagini grandi si caricano calcolo la lunghezza totale del div contenitore ("#slides");
	var slideCounter = 0;
	jQuery('#slides .slide img').load(function(){
		slideCounter++;
		var imgIdx = jQuery(this).parent().prevAll().length;
		imgW = jQuery(this).naturalWidth();//this.naturalWidth || this.width;
		imagesArray[imgIdx] = [this.src,imgW];
		jQuery(this).click(function(){
			showZoomPanel(this.src);
		});
		if(slideCounter == jQuery(".slide").length){
			jQuery('#slides .slide').each(function(i,item){
				positions[i]= totWidth;
				totWidth += jQuery(item).width();
				if(BrowserDetect.browser == "Explorer")
					totWidth += 1; //aggiungo un px di margine altrimenti explorer mi taglia l'immagine
			});
			jQuery('#slides').width(totWidth);
		}
	});
	
	//quando le immagini thumb si caricano calcolo la lunghezza totale del div contenitore ("#thumbList");
	var imgCounter = 0;
	jQuery('#thumbList .thumbItem a img').load(function(){
		imgIndex = jQuery(this).parent().parent().prevAll().length;
		widthArray[imgIndex] = jQuery(this).width();
		imgCounter++;
		if(imgCounter == jQuery(".thumbItem").length){
			jQuery('#thumbList .thumbItem a img').each(function(i){
				thumbPositions[i]= totThumbWidth;
				totThumbWidth = totThumbWidth + parseInt(widthArray[i]) + 8;//8px di margine tra una thumb e l'altra
			});
			jQuery('#thumbList').width(totThumbWidth);
			jQuery('#thumbList').css("overflow","hidden");
			jQuery(".modalBackground").hide();
			jQuery("#zoomContainer").show();
		    jQuery(".waitImg").hide();
		      
		}
	});
	
	//click su una delle thumb
	jQuery('#thumbList .thumbItem a').click(function(e,keepScroll,move,jumpSteps){

			jQuery('.thumbItem').removeClass('act').addClass('inact');
			jQuery(this).parent().addClass('act');
			
			var pos = jQuery(this).parent().prevAll('.thumbItem').length;
			if((move == 'right' || currentItem < pos) && (panelWidth - thumbPositions[pos]) < (panelWidth / 2))
				jQuery(".thumbsNext").trigger('click',[true,jumpSteps]);//slitto a destra
			else if((move == 'left' || currentItem > pos) && (panelWidth + thumbPositions[pos]) > (panelWidth / 2)) 
				jQuery(".thumbsPrev").trigger('click',[true,jumpSteps]);//slitto a sinistra
			currentItem = pos;
			jQuery('#slides').stop().animate({marginLeft:-positions[pos]+'px'},transitionSpeed);
			e.preventDefault();
			
			//if(!keepScroll) clearInterval(itvl);
	});
	
	//attivo la prima thumb
	jQuery('#thumbList .thumbItem:first').addClass('act').siblings().addClass('inact');
	
	//gestione azioni frecce delle thumb
	jQuery('[class *= thumbs]').click(function(e,keepScroll,jumpSteps){
		if(jumpSteps == null)
			jumpSteps = 1;
		if(jQuery(this).attr("class") == "thumbsNext"){
			if(thumbPos < jQuery('.thumbItem').length - (jumpSteps - 1)){
				if(thumbPositions[thumbPos + jumpSteps] <= (jQuery("#thumbList").width() - panelWidth)){
					thumbPos += jumpSteps;
					jQuery('#thumbList').stop().animate({marginLeft:-thumbPositions[thumbPos]+'px'},transitionSpeed);
				}
			}
			//else thumbPos = jQuery('.thumbItem').length - (jumpSteps - 1); 
		}
		else {
			if(thumbPos >= jumpSteps)
				thumbPos -= jumpSteps;
			else thumbPos = 0;
			jQuery('#thumbList').stop().animate({marginLeft:-thumbPositions[thumbPos]+'px'},transitionSpeed);
		}
	});
	
	//gestione azioni frecce dello slider
	jQuery('[class *= arrow]').click(function(e,keepScroll, jumpSteps){
			if(jumpSteps == null)
				jumpSteps = steps;
			if(jQuery(this).attr("class") == "arrowNext"){
				if(currentItem < jQuery('.thumbItem').length - jumpSteps)
					currentItem = currentItem + jumpSteps;
				else if(currentItem != jQuery('.thumbItem').length - 1){
					currentItem = jQuery('.thumbItem').length - 1;
				}else return;//se sono già arrivata all'ultimo elemento non devo scorrere avanti
				jQuery(jQuery('.thumbItem a').get(currentItem)).trigger('click',[true,'right',jumpSteps]);
				jQuery('#slides').stop().animate({marginLeft:-positions[currentItem]+'px'},transitionSpeed);
			}
			else {
				if(currentItem >= jumpSteps)
					currentItem = currentItem - jumpSteps;
				else if(currentItem != 0){
					currentItem = 0;
				}else return;//se sono già arrivata al primo elemento non devo scorrere indietro
				jQuery(jQuery('.thumbItem a').get(currentItem)).trigger('click',[true,'left',jumpSteps]);
				jQuery('#slides').stop().animate({marginLeft:-positions[currentItem]+'px'},transitionSpeed);
			}
			
			e.preventDefault();
			
			// Stopping the auto-advance if an icon has been clicked:
			//if(!keepScroll) clearInterval(itvl);
	});
	
	jQuery('[class *= zoomArr]').click(function(e,keepScroll){
		var direction = jQuery(this).attr("class").replace("zoomArr","");
		jQuery(".arrow" + direction).trigger('click',[true,1]);
	});
	
	/*****
	 *
	 *	Enabling auto-advance.
	 *
	 ***
	 
	var current=1;
	function autoAdvance()
	{
		if(current==-1) return false;
		
		jQuery('#thumbList .thumbItem a').eq(current%jQuery('#thumbList .thumbItem a').length).trigger('click',[true]);	// [true] will be passed as the keepScroll parameter of the click function on line 28
		current++;
	}

	// The number of seconds that the slider will auto-advance in:
	
	var changeEvery = 10;

	var itvl = setInterval(function(){autoAdvance()},changeEvery*1000);*/

	/* End of customizations */
	}

