function constrainWH(width,maxWidth,height,maxHeight) {
   var divider;
  
   if (width>maxWidth&&width!=0) {
      divider=width/maxWidth;
      width/=divider;
      height/=divider;
	  
	  
   }
   if (height>maxHeight&&height!=0) {
      divider=height/maxHeight;
      width/=divider;
      height/=divider;
   }
   return [Math.round(width),Math.round(height)];
}

function createImg(id,id_container,image_index,nr_items) 
{
	var width = 620; var height = 500;
	
	var src = 'http://85.9.22.242/game_x_media/images/'+id+'.jpg'	
	
	container = document.getElementById('image_container')
	while (container.firstChild) {
		container.removeChild(container.firstChild);
	}
		
	var img = document.createElement("img");
    img.src=src;
	
	img.onload = function () {
		old_width = img.width;
	
	    var constraints = constrainWH(img.width,width,img.height,height);
	    img.width =constraints[0];
	    img.height=constraints[1];
		
		if(img.width==0) {
			img.width = 620; img.height = 500;
		}
		
		/*
		
		document.getElementById('image_container').appendChild(img);
		
		if(old_width>620) {	
			var	link = document.createElement("a");
			link.id = 'full_size';
			link.innerHTML = 'full size';	
			link.href = src;
			link.rel="lightbox["+image_index+"]";		
			
			document.getElementById('image_container').appendChild(link);
			
			myLightbox = new Lightbox(); 
			
		}
		*/
		
		
		
		if(old_width>620) {	
			var	link = document.createElement("a");
			link.id = 'full_size';
			//link.innerHTML = 'full size';	
			link.href = src;
			link.rel="lightbox["+image_index+"]";		
			
			document.getElementById('image_container').appendChild(link);
			document.getElementById('full_size').appendChild(img);			
			
			
			myLightbox = new Lightbox(); 
			
		}
		else
			document.getElementById('image_container').appendChild(img);
		
		
		
		if(image_index>0)
			hCarousel.scrollTo(image_index-1);			
		else
			hCarousel.scrollTo(0);
		
		for(i=0;i<nr_items;i++)
			$('li_'+i).className = '';
		
		$('li_'+image_index).addClassName('spot');
	}
	
	
	
	  
}

/*
function createImg(id,id_container,image_index,nr_items) 
{
	var width = 620; var height = 500;
	var src = 'http://85.9.22.242/game_x_media/images/'+id+'.jpg'	
	
	container = document.getElementById('image_container')
	while (container.firstChild) {
		container.removeChild(container.firstChild);
	}
	
	
	var img = document.createElement("img");
    img.src=src;
	
	old_width = img.width;
	//alert(img.width)
    var constraints = constrainWH(img.width,width,img.height,height);
    img.width =constraints[0];
    img.height=constraints[1];
	
	if(img.width==0) {
		img.width = 620; img.height = 500;
	}
	
	document.getElementById('image_container').appendChild(img);
	
	if(old_width>620) {	
		var	link = document.createElement("a");
		link.id = 'full_size';
		link.innerHTML = 'full size';	
		link.href = "#";
		document.getElementById('image_container').appendChild(link);
	}
	
	if(image_index>0)
		hCarousel.scrollTo(image_index-1);			
	else
		hCarousel.scrollTo(0);
	
	for(i=0;i<nr_items;i++)
		$('li_'+i).className = '';
	
	$('li_'+image_index).addClassName('spot');
	  
}
*/