//=======================
//
// Merging Image Slideshow
//
// http://www.astral-consultancy.co.uk/cgi-bin/hunbug/doco.cgi?11530
//
//=======================

var slideshowMergeAnimate = new Array();
var slideshowMergeTimer   = new Array();
var slideshowMergeCount   = new Array();
var slideshowMergeImages  = new Array();

//======================

function slideshowMerge(id,cl,imageArray,fadeInterval,holdTime)
{

  for(i=0;i<imageArray.length;i++)
  {
    var imgLoad = new Image();
    imgLoad.src = imageArray[i];
  }

  if(cl)
    cl = ' class="'+cl+'"';

  document.write('<div id="'+id+'"'+cl+' class="slideshow">');
  // document.write('<img id="'+id+'img1" style="position:absolute; top:5px; left:5px;" onload="slideshowMergeRun(\''+id+'\')"/>');
  // document.write('<img id="'+id+'img2" style="position:absolute; top:5px; left:5px;display:none;"/></div>');
  document.write('<img id="'+id+'img1" onload="slideshowMergeRun(\''+id+'\')"/>');
  document.write('<img id="'+id+'img2" style="display:none;"/></div>');
  
  slideshowMergeCount[id]   = 0;
  slideshowMergeImages[id]  = imageArray;
  slideshowMergeAnimate[id] = 'run';
  slideshowMergeTimer[id]   = setInterval('slideshowMergeAnimation(\''+id+'\',\''+holdTime+'\');',fadeInterval);

}

//======================

function slideshowMergeAnimation(id,holdTime)
{
  if(slideshowMergeAnimate[id]=='run')
  {
    var obj1 = document.getElementById(id+'img1');
    var obj2 = document.getElementById(id+'img2');

    var opa  = slideshowMergeCount[id]%100;

    if(opa==0)
    {  
      if(obj1.src)
      {
        slideshowMergeAnimate[id] = 'hold';
        setTimeout('slideshowMergeRun(\''+id+'\')',holdTime);
        obj2.src = obj1.src;
		obj2.style.top = obj1.style.top;
		obj2.style.left = obj1.style.left;
        obj2.style.display = 'block';
      }
    }
    else if(opa==1)
    {
      slideshowMergeAnimate[id] = 'load';
      obj1.src = slideshowMergeImages[id][Math.floor(slideshowMergeCount[id]/100)%slideshowMergeImages[id].length];
    }
	else if(opa==2) {
	  setTopLeft(obj1);
	}
      
    obj1.style.opacity = (opa/100).toString();
    obj1.style.filter  = "alpha(opacity="+opa.toString()+")";
    obj2.style.opacity = ((100-opa)/100).toString();
    obj2.style.filter  = "alpha(opacity="+(100-opa).toString()+")";

    slideshowMergeCount[id]++;
    
    if(slideshowMergeCount[id]==(slideshowMergeImages[id].length*100))
      slideshowMergeCount[id]=0;
  }
}

//======================

function slideshowMergeRun(id)
{
  slideshowMergeAnimate[id] = 'run';
}

//======================

function setTopLeft(obj) {
	// (1110 - 337) % 2
	if (obj.parentNode != null) {
		var left = Math.round((obj.parentNode.clientWidth - obj.clientWidth) / 2);
		obj.style.left = left + 'px';
		var top = Math.round((obj.parentNode.clientHeight - obj.clientHeight) / 2);
		obj.style.top = top + 'px';
		//alert('src' + obj.src + '\n parent width: ' + obj.parentNode.clientWidth + '\n client width: ' + obj.clientWidth + '\n natural width: ' + obj.naturalWidth + '\n left: ' + obj.style.left);
	}
}
