var slideShow=function(){
  var ia,fs,ci,tar,tarl;
  ia=document.getElementById(imgid);
  fs=5;
  return{
    init:function(){
      tarl=imgs.length;
      tar=[];
      for(i=0;i<tarl;i++){
        var id=i+1; tar[i]=id;
      }
      this.getimg(1);
    },
    getimg:function(id){
      if(auto){clearTimeout(ia.timer)}
      if(ci!=null){
        var ts,tsl,x;
        ts=ia.getElementsByTagName('img'); tsl=ts.length;x=0;
        for(x;x<tsl;x++){
          if(ci.id!=id){var o=ts[x]; clearInterval(o.timer); o.timer=setInterval(function(){slideShow.fdout(o)},fs)}
        }
      }
      if(!document.getElementById(id)){
        var i=document.createElement('img');
        ia.appendChild(i);
        i.id=id; i.av=0; i.style.opacity=0;
	i.style.position = "absolute";
	i.style.left = "1px";
	i.style.top = "1px";
	//i.style.margin = "auto";
        i.style.filter='alpha(opacity=0)';
        i.src=imgs[id-1];
	// my resize code goes here.
	//alert(i.id + "\n" + i.height + " " + i.width);
	//i.timer=setInterval(function(){slideShow.resize(i),fs);
      }else{
        i=document.getElementById(id); clearInterval(i.timer);
      }
      
      i.timer=setInterval(function(){slideShow.resize(i)},fs);
    },
    nav:function(d){
      var c=0;
      for(key in tar){if(tar[key]==ci.id){c=key}}
      if(tar[parseInt(c)+d]){
        this.getimg(tar[parseInt(c)+d]);
      }else{
        if(d==1){
          this.getimg(d);
        }else{this.getimg(tarl)}
      }
    },
    resize:function(i){
      if(i.complete){
	// insert math for width & height here, once image has loaded
	// if image is bigger than frame size
	if (i.width > 396 || i.height > 297)
	{
	  var ratio = i.height/i.width;
	  if (ratio > 0.75) {i.height=297; i.width=i.height/ratio;}
	  else if (ratio < 0.75) {i.width=396; i.height=i.width*ratio;}
	  else {i.width=396; i.height=297;}
	}

	// centering code
	if (i.width < 396) {i.style.left = Math.round((396-i.width)/2) + "px";}
	if (i.height < 297) {i.style.top = Math.round((297-i.height)/2) + "px";}

	// go to the fade in function
	clearInterval(i.timer);
	i.timer=setInterval(function(){slideShow.fdin(i)},fs);}
    },
    auto:function(){ia.timer=setInterval(function(){slideShow.nav(1)},autodelay*1000)},
    fdin:function(i){
      if(i.complete){i.av=i.av+fs; i.style.opacity=i.av/100; i.style.filter='alpha(opacity='+i.av+')'}
      if(i.av>=100){if(auto){this.auto()}; clearInterval(i.timer); ci=i}
    },
    fdout:function(i){
      i.av=i.av-fs; i.style.opacity=i.av/100;
      i.style.filter='alpha(opacity='+i.av+')';
      if(i.av<=0){clearInterval(i.timer); if(i.parentNode){i.parentNode.removeChild(i)}}
    }
  };
}();

window.onload=function(){slideShow.init();startList();};
