function fadeInOutImages(container, fadeSpeed, changeSpeed, borderSize, dir, srcArray) { // images must be pre-loaded in head for FF

var p, _w, _h, _src, _top, _left, x, pct;
var photos = [];
var containerSize=container.offsetWidth - borderSize; // minus the borders

for (var i=0, j=0; i<srcArray.length; i++) {
	
	p = document.createElement("img");
	p.src=dir + srcArray[i];
	
	_w = p.width;
	_h = p.height; 
	x = (_w>_h)? _w : _h; 
	 _src = p.src;
		
	pct = (x / containerSize); // proportion to the square container's size

	if (pct>0.99){ // too big
	  		  _w =  Math.ceil(_w / pct);
			  _h =  Math.ceil(_h / pct);
		    }
	
	_top = Math.ceil((containerSize - _h) * 0.5);
	_left = Math.ceil((containerSize - _w) * 0.5);
	photos[i]={src:_src,w:_w,h:_h,t:_top,l:_left};
	
}	

	
var imgs=container.getElementsByTagName("IMG");
var img1=imgs[0], img2=imgs[1];  // anticipates two img tags in container
img1.src=photos[0].src;
img1.style.top=photos[0].t + 'px';
img1.style.left=photos[0].l + 'px';
img1.style.height=photos[0].h + 'px';
img1.style.width=photos[0].w + 'px';
img2.className += ' opacity0';

		
var x = setInterval(rotate,changeSpeed);
	
	function rotate(){
		if (typeof rotate.no == 'undefined')
		   rotate.no = Math.floor(Math.random() * photos.length);
		var startOver = photos.length;
		if (++rotate.no==startOver)
		   rotate.no=0;
		   	 
		var hid = (img1.className.indexOf(' opacity0')!=-1)? img1 : img2; // currently hidden
		var vis = (img1.className.indexOf(' opacity0')==-1)? img1 : img2; // currently visible
		hid.src=photos[rotate.no].src;
		hid.style.top=photos[rotate.no].t + 'px';
		hid.style.left=photos[rotate.no].l + 'px';
		hid.style.height=photos[rotate.no].h + 'px';
		hid.style.width=photos[rotate.no].w + 'px';
		fadeOut(vis,fadeSpeed);
		fadeIn(hid,fadeSpeed);
		
	}
	
	function fadeOut(el,speed /*1=fast & 200=very slow */)  {     
  			 el.className += ' opacity9';
  			 var i = 9;  
  			 var x = setInterval(
  			 function(){  // iterates at 10% increments
			   var theClass = ' opacity' + i;
			   el.className = el.className.replace(new RegExp(theClass+'\\b'), "");
			   theClass = ' opacity' + --i;
			   el.className += theClass;
			   if (i<1) { 
			 	  clearInterval(x);
			   }	
		    },
  			speed);
	}


	function fadeIn(el,speed /*1=fast & 200=very slow */)  {    
  			 var i = 0;  
  			 var x = setInterval(
  			 function(){  // iterates at 10% increments
			   var theClass = ' opacity' + i;
			   el.className = el.className.replace(new RegExp(theClass+'\\b'), "");
			   theClass = ' opacity' + ++i;
			   el.className += theClass;
			   if (i>9) { 
			 	  clearInterval(x);
				  el.className = el.className.replace(new RegExp(theClass+'\\b'), "");
			   }	
		    },
  			speed);
	}


} // end of fadeInOutImages();

