

/*   Copyright 2010, Michael J. Hill.  All rights reserved. Used with permission.  www.javascript-demos.com */
/*   Free use of the code, so long as the above notice is kept intact */

	var refImg = document.createElement('img');
	var pagingImg = [];
	var next = 0;
	var useWidth = 0;
	var placeholderImg = "";
	var outterContainer = "";
	var fadeContainer = "";
	var fadeImg = "";
	var fadeCaption = "";		
	var nOpacity = 100;
	var done = false;
	var IE = navigator.appName == "Microsoft Internet Explorer" ? true : false;	

	function fadeIn(){

		nOpacity < 100 ? nOpacity = nOpacity + 2 : null;
		IE ? fadeImg.style.filter = "alpha(opacity = "+nOpacity+")"
		   : fadeImg.style.opacity = (nOpacity / 100); 
		IE ? fadeCaption.style.filter = "alpha(opacity = "+nOpacity+")"
		   : fadeCaption.style.opacity = (nOpacity / 100); 
		if (nOpacity == 100)
			{		
			 if (!nonStop && next == pagingImg.length-1)
				{
				 if (hideWhenDone)
					{				
					 var nTop = 0;	 
					 doneMsg = document.createElement('div');
					 doneMsg.appendChild(document.createTextNode("Done"));	
					 doneMsg.style.display = "none";		 
			 		 doneMsg.style.position = "absolute";	
					 IE ? nTop = outterContainer.parentNode.offsetTop : nTop = outterContainer.offsetTop;
					 doneMsg.style.top = nTop + 
							   + Math.round(refImg.height / 2) - 10 + "px";
					 doneMsg.style.left = outterContainer.offsetLeft + Math.round(useWidth /2)
							    -  30 + "px";
					 doneMsg.className = "done_message";					 
			 		 document.getElementsByTagName('body')[0].appendChild(doneMsg);
					 done = true;
					 setTimeout("fadeOut()", pause * 1500);
					 setTimeout("doneMsg.style.display = ''", pause * 1250);					
					}
			 	 return;
				}						 
			 setTimeout("fadeOut()", pause * 1000);																
			}
		else	{
			 setTimeout("fadeIn()", 15);
			}
	}

	function fadeOut(){

		nOpacity > 0 ? nOpacity = nOpacity - 2 : null;
		IE ? fadeImg.style.filter = "alpha(opacity = "+nOpacity+")"
		   : fadeImg.style.opacity = (nOpacity / 100); 
		IE ? fadeCaption.style.filter = "alpha(opacity = "+nOpacity+")"
		   : fadeCaption.style.opacity = (nOpacity / 100); 
		if (nOpacity == 10 && !done)
			{
			 if (next < imgSet.length-1)
				{						
			 	 next++;			 		 		 	
				}
			 else 	{	
			 	 next = 0;				   		 		 	 		 	 	  					 
				}	
			fadeImg.src = pagingImg[next].src;
			fadeCaption.innerHTML = imgSet[next].split("|")[1];
			fadeIn();						
			}
		else 	{
			 setTimeout("fadeOut()", 5);			
			}
	}	

	function shuffle(nSet){

		for (r=0; r<nSet.length; r++)
			{
			 var tmp1 = parseInt(Math.random()*nSet.length);
			 var tmp2 = parseInt(Math.random()*nSet.length);
			 var tmp3 = nSet[tmp1];
			 nSet[tmp1] = nSet[tmp2];
			 nSet[tmp2] = tmp3;
			}		
	}
	
	function waitLoad(nLoadImg){

		IE ? nLoadImg.detachEvent('onload', waitLoad, false) : nLoadImg.removeEventListener('load', waitLoad, false);	
		if (nLoadImg.src == pagingImg[pagingImg.length-1].src)
			{
			 fadeCaption.innerHTML = imgSet[next].split("|")[1];
			 fadeImg.src = pagingImg[0].src;
			 setTimeout("fadeOut()", pause * 1000);				 			
			}		
	}

	function buildfade(){

		var nRule = "";	
		var nSheet = document.styleSheets;
		for (n=0; n<nSheet.length; n++)
			{
			 IE ? nRule = document.styleSheets[n].rules : nRule = document.styleSheets[n].cssRules;
			 for (i=0; i<nRule.length; i++)
				{
			  	 if (nRule[i].selectorText == ".fade_container")
					{
				 	 nRule[i].style.display = "block";
					}
				}
			}	
		useWidth = refImg.width;
		var nDiv = document.getElementsByTagName('div');
		for (i=0; i<nDiv.length; i++)
			{		
			 if (/r[a-z]{3}t/.test(nDiv[i].className) && /j\.\sh/i.test(nDiv[i].firstChild.data))
				{
				 placeholderImg = true;
				}			
			 if (nDiv[i].className == "fade_container")
				{				
				 outterContainer = nDiv[i]; 
				 nDiv[i].style.width = useWidth + "px";				 			 
				}
			 if (nDiv[i].className == "fade_img_container")
				{
				 fadeContainer = nDiv[i]; 
				 nDiv[i].style.width = useWidth + "px";					 		 
				}		
			 if (nDiv[i].className == "fade_img")
				{
				 fadeImg = nDiv[i];
				 fadeImg.appendChild(document.createElement('img'));	
				 fadeImg = fadeImg.firstChild;			 
				}	
			 if (nDiv[i].className == "fade_caption")
				{
				 if (!useCaptions)
					{
					 nDiv[i].style.display = "none";
					}
				 fadeCaption = nDiv[i];					
				}					
			}	
		if (!placeholderImg){return nRule;}	
		if (randomize)
			{
			 shuffle(imgSet);
			}		
		for (i=0; i<imgSet.length; i++)
			{
			 pagingImg[pagingImg.length] = document.createElement('img');							
			}		
		for (i=0; i<imgSet.length; i++)
			{
		 	 pagingImg[i].onload = function()
				{
			 	 waitLoad(this);
				}	
			 pagingImg[i].src =  imgPath + imgSet[i].split("|")[0];				 		 			 			 			 	 	
			}					
	}

	function init(){

		refImg.src = imgPath + imgSet[0].split("|")[0];			
		setTimeout("buildfade()", 200);		
	}

	IE ? attachEvent('onload', init, false) : addEventListener('load', init, false);		

