function FullSlideShow(IdFullSlideshow) {
  if(!IdFullSlideshow) return;
  
  var $active = $('#'+IdFullSlideshow+' > div.active');      
  if ( $active.length == 0 ) $active = $('#'+IdFullSlideshow+' > div:last');

  var $next =  $active.next('div').length ? $active.next('div')
      : $('#'+IdFullSlideshow+' > div:first');
  
  $active.addClass('last-active');
  
  $active.animate({opacity: 0.0}, 500);
  $next.css({
    opacity: 0.0
    })
    .addClass('active')
    .animate({opacity: 1.0}, timeEffectSlideshow, function() {
      if($('#'+IdFullSlideshow+' > div').size() > 1) $active.removeClass('active last-active');            
    });
}

function setPositionAltImage(IdFullSlideshow){
  
  var theAlt = $('#'+IdFullSlideshow+' span.alt_image');
  var gain = 10;
  var gainMore = 20;
  
  var WindowHeight = $(window).height();
	var WindowWidth = $(window).width();
  
  $(theAlt).each(function(){
    var theSingleAlt = $(this);
    
    switch(altImagePosition){
      case 'top':
        var topPosition = parseInt(theSingleAlt.height()) + gain + 'px';
        var rightPosition = '';
        var bottomPosition = '';
        var leftPosition = WindowWidth / 2;
        var marginLeftPosition = '-' + parseInt((theSingleAlt.width() / 2)) + 'px';
      break;
      case 'top-right':
        var topPosition = parseInt(theSingleAlt.height()) + gain + 'px';
        var rightPosition = '';
        var bottomPosition = '';
        var leftPosition = WindowWidth / 2;
        var marginLeftPosition = parseInt((blueprintWidth / 2) - (theSingleAlt.width())) + 'px';
      break;
      case 'right':
        var topPosition = '50%';
        var rightPosition = '';
        var bottomPosition = '';
        var leftPosition = WindowWidth / 2;
        var marginLeftPosition = parseInt((blueprintWidth / 2) - (theSingleAlt.width())) + 'px';
      break;
      case 'bottom-right':
        var topPosition = '100%';
        var rightPosition = '';
        var bottomPosition = '';
        var leftPosition = WindowWidth / 2;
        var marginLeftPosition = parseInt((blueprintWidth / 2) - (theSingleAlt.width())) + 'px';
        var marginTopPosition = '-' + parseInt(theSingleAlt.height() + gainMore) + 'px';
      break;
      case 'bottom':
        var topPosition = '100%';
        var rightPosition = '';
        var bottomPosition = '';
        var marginLeftPosition = '-' + parseInt((theSingleAlt.width() / 2)) + 'px';
        var marginTopPosition = '-' + parseInt(theSingleAlt.height() + gainMore) + 'px';
      break;
      case 'bottom-left':
        var topPosition = '100%';
        var rightPosition = '';
        var bottomPosition = '';
        var leftPosition = '';
        var marginLeftPosition = '-' + parseInt(blueprintWidth / 2) + 'px';
        var marginTopPosition = '-' + parseInt(theSingleAlt.height() + gainMore) + 'px';
      break;
      case 'left':
        var topPosition = '50%';
        var rightPosition = '';
        var bottomPosition = '';
        var leftPosition = WindowWidth / 2;
        var marginLeftPosition = '-' + parseInt(blueprintWidth / 2) + 'px';     
      break;
      case 'top-left':
        var topPosition = parseInt(theSingleAlt.height()) + gain + 'px';
        var rightPosition = '';
        var bottomPosition = '';
        var leftPosition = WindowWidth / 2;
        var marginLeftPosition = '-' + parseInt(blueprintWidth / 2) + 'px';      
      break;
      case 'center':
        var topPosition = '50%';
        var rightPosition = '';
        var bottomPosition = '';
        var leftPosition = WindowWidth / 2;
        var marginLeftPosition = '-' + parseInt((theSingleAlt.width() / 2)) + 'px';
      break;
    }
    
    $(theSingleAlt).css({
      'top' : topPosition,
      'right' : rightPosition,
      'bottom' : bottomPosition,
      'left' : leftPosition,
      'margin-left' : marginLeftPosition,
      'margin-top' : marginTopPosition
    });    
  });  
}

function getPositionImageSlide(instance, marginType){
  if(marginType == 'margin-top'){
    var heightNextImage = instance.height();
    var marginTopNextImage = parseInt(heightNextImage / 2);
    return marginTopNextImage;  
  } else if(marginType == 'margin-left'){
    var widthNextImage = instance.width();
    var marginLeftNextImage = parseInt(widthNextImage / 2);
    return marginLeftNextImage;
  } else return null;
}

function setAllPositionImageSlide(IdFullSlideshow){	
	var larg = $(window).width();
	
	$('#'+IdFullSlideshow+' > div').each(function(){
		/*
		var marginTopNextImage = getPositionImageSlide($(this), 'margin-top');
	  var marginLeftNextImage = getPositionImageSlide($(this), 'margin-left');
	
	  $(this).css({
	    'margin-top': '-' + marginTopNextImage + 'px',
	    'visibility': 'visible',
	    'left': '0',
	    'position': 'absolute'
	   'visibility': 'visible'
	  });	 
	  */
	 
	 	var instanceImg = $(this).children('img');
	 	var altImg = instanceImg.height();
    var largImg = instanceImg.width();
 		var ratio = largImg/altImg;	 	
    var newHeight = larg/ratio;
	  
	  //console.log("1. NEW HEIGHT | " + newHeight + '|  larg schermo' + larg + ' |  ratio ' + ratio + ' IdFullSlideshow ' +IdFullSlideshow);
	  
	  $(this).css({
	  	'height': newHeight + 'px',
      'marginTop': '-' + (newHeight / 2) + 'px',
 			'left': '0',
	    'position': 'absolute',
	    'visibility': 'visible',
	    'width': '100%'
	  });
	  	 	     
	  instanceImg.css({
	  	'height': newHeight + 'px'
	  });	 
	});	
}

function uniqIdFullSlideshow(){
  var newDate = new Date;
  return newDate.getTime();
}

$(document).ready(function() {
  if(timeFullSlideshow) timeFullSlideshow *= 1000;
  else timeFullSlideshow = 5000;
  if(timeEffectSlideshow) timeEffectSlideshow *= 1000;
  else timeEffectSlideshow = 1000;  
  if(!altImagePosition) altImagePosition = 'bottom-right'; 
  
  if($('.full_slideshow').length != 0){
    $('.full_slideshow').each(function(){      
      var IdFullSlideshow = this.id;
      if(!IdFullSlideshow) {
        var IdFullSlideshow = 'full_slideshow_' + uniqIdFullSlideshow();
        $(this).attr('id',IdFullSlideshow);
      }
      
      $('#'+IdFullSlideshow+' > div').each(function(){
        $(this).css({ visibility: "visible"});
        $(this).css({ opacity: 0.0 });
      });
      
      //$(theLastImg).load(function(){
      //var theLastImg = $('#'+IdFullSlideshow+' div:last img');  
      $(window).load(function(){      
        setPositionAltImage(IdFullSlideshow);
        setAllPositionImageSlide(IdFullSlideshow);
        FullSlideShow(IdFullSlideshow);        
        if($('#'+IdFullSlideshow+' > div').size() > 1) {
          setInterval( "FullSlideShow('"+IdFullSlideshow+"')", timeFullSlideshow );
        }
        
        jQuery(window).resize(function(){
        	setPositionAltImage(IdFullSlideshow);
					setAllPositionImageSlide(IdFullSlideshow);
				});        
      });
    }); 
  }
});


