resizeImage = function(which, max) {
  var elem = document.getElementById(which);
  if (elem == undefined || elem == null) return false;
  if (max == undefined) max = 110;
  if (elem.width > elem.height) {
    if (elem.width > max) elem.width = max;
  } else {
    if (elem.height > max) elem.height = max;
  }
}
resizeProduitImage = function(which,container,max) {
	  
	  var elem = document.getElementById(which);
	  var container = document.getElementById(container);
	  
	  var posX = 0;
	  var posY = 0;
	  
	  
	  if (elem == undefined || elem == null) return false;
	  else
	  {
		  if (max == undefined) max = 140;
		  if (elem.width > elem.height) {
		    if (elem.width > max) elem.width = max;
		  } else {
		    if (elem.height > max) elem.height = max;
		  }
		  /*
		  if (elem) {
			 
				curleft += elem.offsetLeft;
				console.log("offsetLeft"+curleft);
				curtop += elem.offsetTop;
				console.log( elem.style.position);
				
			return [curleft,curtop];
		  }*/
		   
		  posY = container.offsetHeight / 2 - elem.height / 2 ;
		  posX = container.offsetWidth / 2 - elem.width / 2 ;
	
		  elem.style.position = 'absolute';
		  elem.style.top = posY+'px';
		  elem.style.left = posX+'px';
	  }

}
changeImage = function(image_id) {
	  
	  
	  var thumbnail = document.getElementById(image_id);
	  var img = document.getElementById('main_image');
	  var id = document.getElementById('main_image_id');
	  var name = document.getElementById('image_name');
	  
	  img.src = thumbnail.src;
	  id.innerHTML= image_id;
	  name.innerHTML= thumbnail.name;
	  
	  resizeImageMain(thumbnail.width, thumbnail.height);
	  
	
}
resizeImageMain = function(w, h) {
	var img_main = document.getElementById('main_image');
	var maxheight = 400;
	var maxwidth = 400;
	var ratio = 0;	
	
	if((!w) && (!h))
	{ 
		w = img_main.width;
		h = img_main.height;
	}
		if(w > h)
		{
			ratio = w/h;
			img_main.height= maxwidth/ratio;
			img_main.width=maxwidth;
		}
		else
		{
			ratio = h/w;
			img_main.height= maxheight;
			img_main.width=  maxheight/ratio;
		}
}
getPrev = function(limit) {
	  
	  var img = document.getElementById('main_image');
	  var main = document.getElementById('main_image_id');
	  var image_container = document.getElementById('image_container');
	  var id = main.innerHTML;
	  var name = document.getElementById('image_name');
	  var prev_id = 0;
	  if(id > 1){		  
		  prev_id = id - 1;
	  }
	  else{
		  if(id = 1){
			  prev_id = limit;
		  }	 
		  else{		 
			  prev_id = 1;			  
		  }
	  }	  
	  var thumbnail = document.getElementById("image"+prev_id);
	
	  var url =  thumbnail.src;
	  image_container.href = url;
	  img.src = url;
	  main.innerHTML= prev_id;
	  name.innerHTML= thumbnail.name;
	  resizeImageMain(thumbnail.width,thumbnail.height);
}
getNext = function(l) {
	  
	  var limit = Number(l);
	  var img = document.getElementById('main_image');
	  var main = document.getElementById('main_image_id');
	  var image_container = document.getElementById('image_container');
	  var id = main.innerHTML;
	  var name = document.getElementById('image_name');
	  var next_id = 0;
	
	  if(id < limit){		  		 		 
		  next_id = ++id; 	
	  }
	  else{
		  next_id = 1;
	  }	  

	  var thumbnail = document.getElementById("image"+next_id);	
	  var url =  thumbnail.src;
	  image_container.href = url;
	  img.src = url;
	  main.innerHTML= next_id;
	  name.innerHTML= thumbnail.name;
	  resizeImageMain(thumbnail.width,thumbnail.height);
}
