

/*   Bidirectional Horizontal Scrolling Images with 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 imageWidth = 1000;
	
	var playing = true;	
	var imagesLoaded = 0;
	var barWidthToGetTo = 0;
	var barWidth = 0;
	var preloaderBar;
	
	var imageScrollTimer = null;
	var imageScrollTimerRunning = true;
	var lastScrollTime;
	
	var IE = navigator.appName == "Microsoft Internet Explorer" ? true : false;

	function animateCaption(nContainer,brush){
	}

	function initScrollPrev(){

	}

	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;
//			 conveyorCaption.innerHTML = imgSet[curr].split("|")[1];
//			 animateCaption(conveyorCaption,'in');				
			}							
	}

	function initConveyorImgs(conveyorImg,direction){

//		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();
			}
		prevDirection = direction;
	}

	function waitLoad(nLoadImg,conveyorImg){
		if (nLoadImg.src == pagingImg[pagingImg.length-1].src)
			{
//			 setTimeout("animateCaption(conveyorCaption,'in')", 750);
//			 conveyorCaption.innerHTML = imgSet[0].split("|")[1];	
/*
  var newdiv = document.createElement('div');
  var divIdName = 'myDiv';
  newdiv.setAttribute('id',divIdName);
  newdiv.setAttribute('style', 'width: 1000px; height: 399px; background-color: #FFF000;');
  newdiv.innerHTML = 'Element Number 1 has been added!';
  conveyorImg.appendChild(newdiv);
*/

			 conveyorImg.appendChild(pagingImg[0]);				 
			}
	}

	function buildConveyor(){

		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";
					}
				}
			}
		useWidth = 1000;
		var nDiv = document.getElementsByTagName('div');
		for (i=0; i<nDiv.length; i++)
			{		
			//getting rid of this got rid of the need for the copyright notice
//			 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")
				{
				 conveyorCaption = nDiv[i];
				 if (IE)
					{
				 	 conveyorCaption.style.paddingTop = "2px";
					 conveyorCaption.style.height = "20px";
					}	
				 else	{
					 conveyorCaption.style.paddingBottom = "2px";
					}		 
				}
			 if (nDiv[i].className == "navigation_imgs")
				{
				 var nBtn = nDiv[i].getElementsByTagName('div');
				 nBtn[0].appendChild(document.createElement('img'));
				 nBtn[0].firstChild.src = "images/pause_gray.png";
				 nBtn[0].firstChild.id = "pauseButton";	
				 nBtn[0].firstChild.style.visibility = "hidden";
				 nBtn[0].onclick = function()
					{
						if (playing == true) {
							playing = false;
							nBtn[0].firstChild.src = "images/play_gray.png";
						} else if (playing == false) {
							playing = true;
							lastScrollTime = new Date().getTime() - 5000;							
							incrementImage();
							nBtn[0].firstChild.src = "images/pause_gray.png"
						}
//					 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);
				 this.width = "1000";
				}	
//			 pagingImg[i].src =  imgPath + imgSet[i].split("|")[0];
			 pagingImg[i].src =  imgPath + imgSet[i];
			}		
		IE ? conveyorCaption.style.filter = "alpha(opacity = 0)" : conveyorCaption.style.opacity = 0;
		
		setTimeout("startIncrementingImages()", 0);
	
	}
	
	function startIncrementingImages() {
		
		var pauseButton = document.getElementById("pauseButton");
		
		pauseButton.style.visibility = "visible";
		conveyorImgContainer.style.visibility = "visible";
		
		imageScrollTimeout = setTimeout("incrementImage()", 4000);
		lastScrollTime = new Date().getTime();

		fadeIn("conveyor_container", 0);	
		fadeIn("pauseButton", 0);		
	}
	
	function incrementImage() {
		
		if(playing == true && new Date().getTime() - lastScrollTime >= 3850) {
			initConveyorImgs(conveyorImg,'next');
			setTimeout("incrementImage()", 4000);
			lastScrollTime = new Date().getTime();
		}
	}
	
	function incrementLoadedImages() {
		imagesLoaded++;		
		barWidthToGetTo = imageWidth / imgSet.length * imagesLoaded;		
//		alert(barWidthToGetTo);
//		alert("image " + imagesLoaded + "loaded sdf" + preloaderBar);
		/*
		if (imagesLoaded == imgSet.length) {
			setTimeout("buildConveyor()", 200);			
		}
		*/
	}
	
	function animatePreloader() {

		if (barWidth >= imageWidth) {
//		if (barWidth == refImg.width && barWidth != 0) {
			preloaderBar.style.background = '#FFFFFF';
			buildConveyor();
//			setTimeout("buildConveyor()", 200);
		} else {
			if (barWidth < barWidthToGetTo) {
				barWidth += 5;
			}
			preloaderBar.style.background = "#000000";			
			preloaderBar.style.width = barWidth + 'px';
			setTimeout("animatePreloader()", 20);
		}
	}

	function init(){

		refImg.src = imgPath + imgSet[0]; //use this "reference image" later to get the width of the images so we can lay them out correctly
		for (i=0; i<imgSet.length; i++) {
			var image = new Image();
			image.onLoad = incrementLoadedImages();
			image.src = imgPath + imgSet[i];
				
//			image.width = "1000px";
//			image.height = "299px";
		}
		
		barWidth = 2;
		preloaderBar = document.getElementById("conveyor_preloader");
		preloaderBar.style.background = "#FF0000";		
		animatePreloader();
	}

//	IE ? attachEvent('onload', init, false) : addEventListener('load', init, false);
	
	function setOpacity(obj, opacity) {
		  opacity = (opacity == 100)?99.999:opacity;
		
		  // IE/Win
		  obj.style.filter = "alpha(opacity:"+opacity+")";
		
		  // Safari<1.2, Konqueror
		  obj.style.KHTMLOpacity = opacity/100;
		
		  // Older Mozilla and Firefox
		  obj.style.MozOpacity = opacity/100;
		
		  // Safari 1.2, newer Firefox and Mozilla, CSS3
		  obj.style.opacity = opacity/100;

		}

		function fadeIn(objId,opacity) {
//		  if (document.getElementById) {
			obj = document.getElementById(objId);
			if (opacity <= 100) {
			  setOpacity(obj, opacity);
			  opacity += 10;
			  window.setTimeout("fadeIn('"+objId+"',"+opacity+")", 50);
			}
//		  }
		}

