var imagesAmount = 6;
var currentImage = 1;
var selectedImage = 1;
var selectedRelImage = 1;

(function($) {
  var cache = [];
  // Arguments are image paths relative to the current page.
  $.preLoadImages = function() {
    var args_len = arguments.length;
    for (var i = args_len; i--;) {
      var cacheImage = document.createElement('img');
      cacheImage.src = arguments[i];
      cache.push(cacheImage);
    }
  }
})(jQuery)


jQuery(function() {
	
	$('.thumbnails > img').fadeTo(1000, 0.25, function() {	});
	$('.thumbnails > img#foto1').fadeTo(1000, 1, function() {	});
	
	
	$('.thumbnails > img').click(function () {
		
	  var rel = $(this).attr('rel');
	  $('#foto'+selectedImage).fadeTo(500, 0.25, function() {	});
	  selectedImage = $(this).attr('id').substring(4);
	  $('#foto'+selectedImage).fadeTo(500, 1, function() {	});
		  
	  
	  
      $('#bigFoto'+selectedRelImage).fadeTo(500, 0, function() {
		  
		  $('#bigFoto'+selectedRelImage).css('display', 'none');
		  selectedRelImage = rel;
		  $('#bigFoto'+rel).css('opacity', '0');
		  $('#bigFoto'+rel).css('display', '');
		  
		  
		  
//		  $('#bigFoto'+rel').attr('src', base_url+'catalog/pageFoto'+rel+'/big.jpg');
		  
		  $('#ajaxLoader').css('display', '');
		  
		  $('#bigFoto'+rel+' > img').attr('src', base_url+'catalog/pageFoto'+rel+'/big.jpg').load(function(){
			  $('#ajaxLoader').css('display', 'none');
			  $('#bigFoto'+rel).fadeTo(500, 1, function() {	});
		  });;
		  
//		  $('.mainFoto a').lightBox({fixedNavigation:true});
		  
		  
	  });
    });
	
	$(".thumbnails > img").hover(
	  function () {
		  $(this).fadeTo(500, 1, function() {	});
	  },
	  function () {
		  if($(this).attr('id') != 'foto'+selectedImage) {
			  $(this).fadeTo(500, 0.25, function() {	});
		  }
		  
	  }
	);
});

function nextImage() {
	
	
	if(currentImage < imagesAmount - 3) {
		currentImage += 1;
		p = $('#foto'+currentImage);
		var position = p.position();
		
		$('.thumbnails').css('left', -position.left);
		
//		$('.thumbnails').animate({
//			left: -position.left,
//		}, 1000, function() {});
		
	}
}
function prevImage() {
	if(currentImage != 1) {
		currentImage -= 1;
		p = $('#foto'+currentImage);
		var position = p.position();
		
		$('.thumbnails').css('left', -position.left);
		
//		$('.thumbnails').animate({
//			left: -position.left,
//		}, 1000, function() {});
		
	}
}
