

/*   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 fadeContainer = "";
	var fadeImg = "";
	var fadeCaption = "";		
	var nCycle = false;
	var nOpacity = 100;
	var IE = navigator.appName == "Microsoft Internet Explorer" ? true : false;	

	function fadeIn(){

		nOpacity < 100 ? nOpacity = nOpacity + 2 : nOpacity = 100;
		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)
					{
					 location.replace("http://signeffects.biz/gallery");
					}
			 	 return;
				}		 		 
			 setTimeout("fadeOut()", pause * 1000);																
			}
		else	{
			 setTimeout("fadeIn()",5);
			}
	}

	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 == 0)
			{
			 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 waitLoad(nLoadImg){

		IE ? nLoadImg.detachEvent('onload', waitLoad, false) : nLoadImg.removeEventListener('load', waitLoad, false);	
		if (nLoadImg.src == pagingImg[pagingImg.length-1].src && !nCycle)
			{
			 fadeCaption.innerHTML = imgSet[next].split("|")[1];
			 fadeImg.src = pagingImg[0].src;
			 setTimeout("fadeOut()", pause * 1000);				 
			 nCycle = true;				
			}		
	}

	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")
				{				
				 fadeContainer = nDiv[i]; 
				 nDiv[i].style.width = useWidth + "px";				 			 
				}
			 if (nDiv[i].className == "fade_img_container")
				{
				 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")
				{
				 fadeCaption = nDiv[i];					
				}					
			}	
		if (!placeholderImg){return nRule;}		
		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);		

