

/*   Bidirectional Horizontal Scrolling Images with Optional Caption */
/*   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 curr = 0;
	var throttle = 1;
	var useWidth = 0;
	var placeholderImg = false;	
	var toggle = true;
	var decelerate = false;
	var conveyorImgContainer = "";
	var conveyorImg = "";
	var conveyorCaption = "";	
	var prevDirection = "";	
	var nPace = IE ? 3 : 30;	
	
	var IE = navigator.appName == "Microsoft Internet Explorer" ? true : false;

	function animateCaption(nContainer,brush){
	
		var nOpacity = 0;	
		var nInterval = "";
		if(brush == "out") 
			{
			 clearInterval(nInterval);
			 nOpacity = 100;
			 nInterval = setInterval(function()
				{
				 nOpacity > 0 ? nOpacity = nOpacity - 5 : clearInterval(nInterval);
				 IE ? nContainer.style.filter = "alpha(opacity = "+nOpacity+")"
				    : nContainer.style.opacity = (nOpacity / 100); 
				}, 2);			
			} 
		if (brush == "in")
			{
			 clearInterval(nInterval);
			 nOpacity = 0;
			 nInterval = setInterval(function()
				{
				 nOpacity < 100 ? nOpacity = nOpacity + 5 : clearInterval(nInterval);
				 IE ? nContainer.style.filter = "alpha(opacity = "+nOpacity+")"
				    : nContainer.style.opacity = (nOpacity / 100); 
				}, 2);						
			}		
	}

	function initScrollPrev(){

		var currPos = conveyorImgContainer.scrollLeft;
		if (currPos > 0)
			{				
			 conveyorImgContainer.scrollLeft = currPos - throttle;									
			 if (conveyorImgContainer.scrollLeft > Math.round(useWidth / 2))
				{				 
				  throttle = throttle + Math.round(throttle * .6);	
				  if (conveyorImgContainer.scrollLeft - throttle <= Math.round(useWidth * .25))
					{
				 	 throttle = throttle - Math.round(throttle * .15);					 
					}					
				 decelerate = true;		 
				}
			 else 	{				 
				 if (decelerate)
					{
					 throttle = Math.round(conveyorImgContainer.scrollLeft * .33);					 
					 decelerate = false;					 
					}
				 throttle = throttle - Math.round(throttle * .25);				 	 					 			 		 
				}
			 setTimeout("initScrollPrev()", nPace); 
			}	
		else	{			 		
			 throttle = 1;				 
			 if (useCaptions)
				{
				 conveyorCaption.innerHTML = imgSet[curr].split("|")[1];
				 animateCaption(conveyorCaption,'in');	
				}			 
			}
	}

	function initScrollNext(){

		var  currPos = conveyorImgContainer.scrollLeft;
		if (currPos < useWidth)
			{				
			 conveyorImgContainer.scrollLeft = currPos + throttle;									
			 if (conveyorImgContainer.scrollLeft < Math.round(useWidth / 2))
				{
				 throttle = throttle + Math.round(throttle * .6);
				 if (conveyorImgContainer.scrollLeft + throttle >= Math.round(useWidth * .75))
					{
				 	 throttle = throttle - Math.round(throttle * .33);					 
					}	
				 decelerate = true;			 		 
				}
			 else 	{
				 if (decelerate)
					{
					 throttle = Math.round((useWidth - conveyorImgContainer.scrollLeft) * .333)
					 decelerate = false;
					}
				 throttle = throttle - Math.round(throttle * .25);				 	 					 			 		 
				}
			 setTimeout("initScrollNext()", nPace); 
			}	
		else	{
			 throttle = 1;
			 if (useCaptions)
				{
			 	 conveyorCaption.innerHTML = imgSet[curr].split("|")[1];			 
			 	 animateCaption(conveyorCaption,'in');		
				}		
			}							
	}

	function initConveyorImgs(conveyorImg,direction){

		if (useCaptions)
			{
			 animateCaption(conveyorCaption,'out');
			}
		if (conveyorImg.childNodes.length == 2)
			{
			 if (prevDirection == "next" && direction == "next")
				{
		 	 	 conveyorImg.removeChild(conveyorImg.firstChild);    
				}	
			 if (prevDirection == "prev" && direction == "prev")	
				{
		 	 	 conveyorImg.removeChild(conveyorImg.lastChild);    
				} 	 		
			}			
		if (direction == "next")
			{			 		 
			 if (curr < imgSet.length-1)
				{				
		 	 	 curr++;				 	
				}
		 	 else 	{			 	 			
			 	 curr = 0;				 					 
				}	
			 conveyorImg.appendChild(pagingImg[curr]);			
			 conveyorImgContainer.scrollLeft = 0;	
			 initScrollNext();			 
			}
		if (direction == "prev")
			{			 	
			 if (curr > 0)
				{			 					
		 	 	 curr--;				 				 			 	 	 	
				}
			 else 	{			 	 			
			 	 curr = imgSet.length-1;							 		 					 
				}				
			 conveyorImg.insertBefore(pagingImg[curr],conveyorImg.firstChild);			 		
			 conveyorImgContainer.scrollLeft = useWidth;				 		 
			 initScrollPrev();			 
			}	
		prevDirection = direction;					
	}	

	function waitLoad(nLoadImg,conveyorImg){

		if (nLoadImg.src == pagingImg[pagingImg.length-1].src)
			{
			 if (useCaptions)
				{
			 	 setTimeout("animateCaption(conveyorCaption,'in')", 750);
			 	 conveyorCaption.innerHTML = imgSet[0].split("|")[1];				 	 
				}	
			 conveyorImg.appendChild(pagingImg[0]);	
			 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 == ".conveyor_container")
						{
				 	 	 nRule[i].style.display = "block";
						}
					}
				}				 
			}		
	}

	function buildConveyor(){

		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 == "conveyor_container")
				{				 
				 nDiv[i].style.width = useWidth + "px";				 			 
				}
			 if (nDiv[i].className == "conveyor_img_container")
				{
				 nDiv[i].style.width = useWidth + "px";	
				 conveyorImgContainer = nDiv[i];				 
				}
			 if (nDiv[i].className == "conveyor_img")
				{
				 conveyorImg = nDiv[i];				 		 
				}			
			 if (nDiv[i].className == "conveyor_caption")
				{
				 if (useCaptions)
					{
				 	 conveyorCaption = nDiv[i];	
				 	 if (IE)
						{
				 	 	 conveyorCaption.style.paddingTop = "2px";
					 	 conveyorCaption.style.height = "20px";
						}	
				 	else	{
					 	 conveyorCaption.style.paddingBottom = "2px";
						}	
					}
				 else	{
					 nDiv[i].parentNode.removeChild(nDiv[i]);
					 if (!IE)
						{
						 conveyorImgContainer.style.marginBottom = "-4px";
						}
					}	 
				}
			 if (nDiv[i].className == "navigation_imgs")
				{
				 var nBtn = nDiv[i].getElementsByTagName('div');
				 nBtn[0].appendChild(document.createElement('img'));
				 nBtn[0].firstChild.src = imgPath + "prev.gif";
				 nBtn[1].appendChild(document.createElement('img'));
				 nBtn[1].firstChild.src = imgPath + "next.gif";	
				 nBtn[0].onclick = function()
					{
					 initConveyorImgs(conveyorImg,'prev');
					}
				 nBtn[1].onclick = function()
					{
					 initConveyorImgs(conveyorImg,'next');
					}
				}					
			}	
		if (!placeholderImg){return toggle;}		
		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,conveyorImg);
				}	
			 pagingImg[i].src =  imgPath + imgSet[i].split("|")[0];				 			 			 			 	 	
			}		
		if (useCaptions)
			{
			 IE ? conveyorCaption.style.filter = "alpha(opacity = 0)" : conveyorCaption.style.opacity = 0;
			}	
	}

	function init(){

		refImg.src = imgPath + imgSet[0].split("|")[0];			
		setTimeout("buildConveyor()", 200);			
	}

	IE ? attachEvent('onload', init, false) : addEventListener('load', init, false);
	
