var aId;

function findPos(obj) 
{
 var curleft = curtop = 0;
 if (obj.offsetParent) 
 {
  curleft = obj.offsetLeft
  curtop = obj.offsetTop
  while (obj = obj.offsetParent) 
  {
   curleft += obj.offsetLeft
   curtop += obj.offsetTop
  }
 }
 return [curleft,curtop];
}

function changeProduct(value)
{
 var imgContainer = document.getElementById('imgContainer');
 var existingImage = imgContainer.firstChild;
 var pos = findPos(existingImage);

 if (existingImage.style.position != 'absolute')
 {
  existingImage.style.position = 'absolute';
  existingImage.style.left = pos[0]+'px';
 }

 var existingImage_opacity = 1;
 
 function opacitySwitch()
 {
  function opacityS()
  {
   if (existingImage_opacity <= 0)
   {
	window.clearInterval(fId);
	if (existingImage && existingImage.parentNode)
 	 existingImage.parentNode.removeChild(existingImage);
   }
   else
   {
	existingImage_opacity -= 0.02;
	existingImage.style.opacity = existingImage_opacity;
	existingImage.style.filter = 'alpha(opacity='+(existingImage_opacity*100)+')'
    existingImage.style.MozOpacity = existingImage_opacity;
   }
  }
	
  var fId = window.setInterval(opacityS,1);   	
 }
	
 var img = document.createElement('img');
 img.setAttribute('src',value+'?id='+Math.random());
 img.style.position = 'absolute';
 img.style.left = pos[0]+"px";
 imgContainer.insertBefore(img,imgContainer.firstChild);   
  
 var fun = function()
  {
   if (img.complete)
   {
    window.clearInterval(oId);
    opacitySwitch();  
   }
  }

 var oId = window.setInterval(fun,100) 
}

function animation(id,ammount)
{
 var i=1;
 var interval = 4000;
 
 function animate()
 {
  i++;
  if (i==ammount) i=1;  
  changeProduct('animation'+id+'/'+i+'.jpg'); 
 }

 aId = window.setInterval(animate,interval);
}