var ImageScroller = function (_imageScrollerParentDivID, _imageScrollerChildDivID) {

   _imageScrollerParentDivID = getElem(_imageScrollerParentDivID);
   _imageScrollerChildDivID  = getElem(_imageScrollerChildDivID);

   var prImagePaths = new Array();
   var prImageAltText = new Array();
   var prImageClicks = new Array();
   var prImageDescription = new Array();
   var prOnClicks = new Array();

   var iNumOfThumbsShown = 0;
   var iNumOfImages = 0;

   var bAutoScroll = 0;
   //0=false, 1=true
   var iAutoScrollDelay = 2000;
   var bAutoScrollDirection = 1;
   //0=back, 1=forward
   var bAutoReverse = 1;
   //0=false, 1=true
   var iScrollType = 1;
   //0=horizontal, 1=vertical
	var bEnableThumbBorder = 0;		// 0=no, 1=yes
   var bEnableCurrentCount = 0;
   //0=no, 1=yes
   var bEnableThumbDescription = 0;
   //0=no, 1=yes
   var bClickOpenType = 0;
   //0=same window, 1=new window
   var iImageScrollAmount = 1;
   //number of images to scroll

   var objCounterDiv = "";
   var objDescriptionDiv = "";
   var iSmoothSlideInterval = 3;
   var iSmoothSlideAmount = 7;
   var moveTimer;

	// Thumbnail image will be inside an <A> tag,
	// set these to wrap <A> tag e.g. inside a <span> or <div>:
	var wrapperElement = null;
	var wrapperClass   = null;

	var iThumbWidth   = 80;
	var iThumbHeight  = 80;
	var iThumbPadding = 4;

	// Set these if width/height cannot be calculated
	// from thumbnail image width/height plus padding,
	// e.g. if affected by styles or by wrapper elements:
	var iItemWidth  = 0;
	var iItemHeight = 0;

	var iScrollWidthBy  = 0;
	var iScrollHeightBy = 0;

   var CURRENT_THUMB_INDEX = 1;
   var NEW_REVERSE_OFFSET = 0;
   var MAX_REVERSE_OFFSET = 0;
   var NEW_FORWARD_OFFSET = 0;
   var IS_SCROLLING = false;

   //* BEGIN FUNCTIONS *//

	this.setThumbnailWrapperElement = function (text) {
		wrapperElement = text;
	}
	this.setThumbnailWrapperClass = function (text) {
		wrapperClass = text;
	}

	this.setItemWidth = function (numpx) {
		iItemWidth = numpx;
	}

	this.setItemHeight = function (numpx) {
		iItemHeight = numpx;
	}

   this.setNumOfImageToScroll = function(_NumOfImagesToScroll) {
        iImageScrollAmount = parseInt(_NumOfImagesToScroll);
   };
   this.enableThumbnailDescription = function(_descriptionDivID) {
      bEnableThumbDescription = 1;
      objDescriptionDiv = _descriptionDivID;
      };
   this.setScrollType = function(_iType) {
      if (_iType == 0) {
         iScrollType = 0;
         }
      else {
         iScrollType = 1;
         }
      };
   this.setScrollSpeed = function(_iSpeed) {
      if (_iSpeed > 0 || _iSpeed < 1000) {
         iSmoothSlideInterval = _iSpeed;
         }
      else {
         iSmoothSlideInterval = 7;
         }
      };
   this.setScrollAmount = function(_iAmount) {
      if (_iAmount > 0 || _iAmount < 1000) {
         iSmoothSlideAmount = _iAmount;
         }
      else {
         iSmoothSlideAmount = 7;
         }
      };
   this.setClickOpenType = function(_openType) {
      if (_openType == 0 || _openType == 1) {
         bClickOpenType = _openType;
         }
      };
   this.enableCurrentCount = function(_counterDivID) {
      bEnableCurrentCount = 1;
      objCounterDiv = _counterDivID;
      };

	this.enableThumbBorder = function(_boolean) {
		bEnableThumbBorder = _boolean;
	};

	this.setThumbsShown = function(_newNumOfThumbsShown) {
		iNumOfThumbsShown = parseInt(_newNumOfThumbsShown);
	};

	this.addThumbnail = function (_thumbnailURL, _fullClickURL, _thumbnailAlt, _thumbnailDescription, _onClickURL) {
		prImagePaths[iNumOfImages]       = _thumbnailURL;
		prImageClicks[iNumOfImages]      = _fullClickURL;
		prImageAltText[iNumOfImages]     = _thumbnailAlt;
		prImageDescription[iNumOfImages] = _thumbnailDescription;
		prOnClicks[iNumOfImages]         = _onClickURL;
		iNumOfImages++;
	};

	this.setThumbnailHeight = function(_newThumbHeight) {
		iThumbHeight = _newThumbHeight;
	};
	this.getThumbnailHeight = function() {
		return iThumbHeight;
	};

	this.setThumbnailWidth = function(_newThumbWidth) {
		iThumbWidth = _newThumbWidth;
	};
	this.getThumbnailWidth = function() {
		return iThumbWidth;
	};

	this.setThumbnailPadding = function(_newThumbPadding) {
		iThumbPadding = _newThumbPadding;
	};
	this.getThumbnailPadding = function() {
		return iThumbPadding;
	};

	this.getCurrentThumbIndex = function() {
		return CURRENT_THUMB_INDEX;
	};

	this.getThumbnailCount = function() {
		return iNumOfImages;
	};

	this.renderScroller = function () {

		if (iNumOfThumbsShown > iNumOfImages ||
		    iNumOfThumbsShown < 1) {
			iNumOfThumbsShown = iNumOfImages;
		}

		if (iItemWidth < 1) {
			iItemWidth  = iThumbWidth  + 2 * iThumbPadding;
			if (bEnableThumbBorder) {
				iItemWidth += 4;
			}
		}
		if (iItemHeight < 1) {
			iItemHeight = iThumbHeight + 2 * iThumbPadding;
			if (bEnableThumbBorder) {
				iItemHeight += 4;
			}
		}

		MAX_REVERSE_OFFSET = 0 - (iNumOfImages - iNumOfThumbsShown) * iItemWidth;

		iScrollWidthBy  = iItemWidth  * iImageScrollAmount;
		iScrollHeightBy = iItemHeight * iImageScrollAmount;

		var width;
		var height;
		if (iScrollType == 0) {
			width  = iItemWidth * iNumOfThumbsShown;
			height = iItemHeight;
			_imageScrollerParentDivID.style.width  = width  + 'px';
			_imageScrollerParentDivID.style.height = height + 'px';

			width  = iItemWidth * iNumOfImages;
			_imageScrollerChildDivID.style.width  = width  + 'px';
			_imageScrollerChildDivID.style.height = height + 'px';
		}
		else if (iScrollType == 1) {
			width  = iItemWidth;
			height = iItemHeight * iNumOfThumbsShown;
			_imageScrollerParentDivID.style.width  = width  + 'px';
			_imageScrollerParentDivID.style.height = height + 'px';

			height = iItemHeight * iNumOfImages;
			_imageScrollerChildDivID.style.width  = width  + 'px';
			_imageScrollerChildDivID.style.height = height + 'px';
		}

		var oHref;
		var oImage;

		for (i = 0; i < iNumOfImages; i++) {
			oHref = document.createElement('a');
			oHref.href = prImageClicks[i];
			oHref.title = prImageAltText[i];
			if (bClickOpenType == 1) {
				oHref.target = "_blank";
			}
			if (prOnClicks[i]) {
				oHref.onclick = prOnClicks[i];
			}

			oImage = document.createElement('img');
			oImage.src = prImagePaths[i];
			oImage.alt = prImageAltText[i];
			oImage.border = bEnableThumbBorder ? 1 : 0;
			oImage.width = iThumbWidth;
			oImage.height = iThumbHeight;
			oImage.style.padding = iThumbPadding;

			oHref.appendChild(oImage);

			if (wrapperElement) {
				oWrapper = document.createElement(wrapperElement);
				if (wrapperClass) {
					oWrapper.className= wrapperClass;
				}
				oWrapper.appendChild(oHref);
				_imageScrollerChildDivID.appendChild(oWrapper);
			}
			else {
				_imageScrollerChildDivID.appendChild(oHref);
			}
		}

		if (bEnableCurrentCount == 1) {
			addAnEvent(window, "load", this.updateCurrentCount);
		}
		if (bEnableThumbDescription == 1) {
			addAnEvent(window, "load", this.updateCurrentDescription);
		}
	};

   this.scrollUp = function() {
      _origOffset = parseInt(_imageScrollerChildDivID.style.top);
      _currentOffset = parseInt(_imageScrollerChildDivID.style.top);
      var newOffset = _currentOffset - (iThumbHeight * iImageScrollAmount);
      if (iThumbPadding > 0) {
         newOffset = newOffset - (2 * iThumbPadding);
         }
      if (bEnableThumbBorder == 1) {
         newOffset = newOffset - 4;
         }
      if (IS_SCROLLING == false && newOffset >= MAX_REVERSE_OFFSET) {
         NEW_FORWARD_OFFSET = newOffset;
         moveScrollerUp();
         }
      };
   this.scrollDown = function() {
      _origOffset = parseInt(_imageScrollerChildDivID.style.top);
      _currentOffset = parseInt(_imageScrollerChildDivID.style.top);
      newOffset = _currentOffset + (iThumbHeight * iImageScrollAmount);
      if (iThumbPadding > 0) {
         newOffset = newOffset + (2 * iThumbPadding);
         }
      if (bEnableThumbBorder == 1) {
         newOffset = newOffset + 4;
         }
      if (newOffset <= 0) {
         if(_currentOffset > (_origOffset - iThumbHeight)) {
            if (IS_SCROLLING == false && newOffset >= MAX_REVERSE_OFFSET) {
               NEW_REVERSE_OFFSET = newOffset;
               moveScrollerDown();
               }
            }
         }
      };
   this.scrollTop = function() {
      if (IS_SCROLLING == false) {
         NEW_FORWARD_OFFSET = ( - 1 * (iNumOfImages - iNumOfThumbsShown) * iThumbHeight);
         CURRENT_THUMB_INDEX = iNumOfImages - iNumOfThumbsShown;
         moveScrollerUp();
         }
      };
   this.scrollBottom = function() {
      if (IS_SCROLLING == false) {
         NEW_REVERSE_OFFSET = 0;
         CURRENT_THUMB_INDEX = iNumOfImages - iNumOfThumbsShown;
         moveScrollerDown();
         }
      };

	this.scrollReverse = function() {
		var currentOffset = getLeft(_imageScrollerChildDivID);
		var newOffset = currentOffset + iScrollWidthBy;
		if (newOffset > 0) {
			newOffset = 0;
		}
		if (IS_SCROLLING == false) {
			NEW_REVERSE_OFFSET = newOffset;
			moveScrollerRight();
		}
	};

	this.scrollForward = function() {
		var currentOffset = getLeft(_imageScrollerChildDivID);
		var newOffset = currentOffset - iScrollWidthBy;
		if (newOffset < MAX_REVERSE_OFFSET) {
			newOffset = MAX_REVERSE_OFFSET;
		}
		if (IS_SCROLLING == false) {
			NEW_FORWARD_OFFSET = newOffset;
			moveScrollerLeft();
		}
	};

	this.scrollBegin = function() {
		if (IS_SCROLLING == false) {
			NEW_REVERSE_OFFSET = 0;
			CURRENT_THUMB_INDEX = 2;
			moveScrollerRight();
		}
	};

	this.scrollEnd = function() {
		if (IS_SCROLLING == false) {
			NEW_FORWARD_OFFSET = MAX_REVERSE_OFFSET;
			CURRENT_THUMB_INDEX = iNumOfImages - iNumOfThumbsShown;
			moveScrollerLeft();
		}
	};

   this.updateCurrentDescription = function() {
      getElem(objDescriptionDiv).innerHTML = prImageDescription[CURRENT_THUMB_INDEX - 1];
      };
   this.updateCurrentCount = function() {
      getElem(objCounterDiv).innerHTML = CURRENT_THUMB_INDEX + "/" + iNumOfImages;
      };

   function moveScrollerUp() {
      _ElementObj = _imageScrollerChildDivID;
      _currentOffset = parseInt(_ElementObj.style.top);
      if (_currentOffset > NEW_FORWARD_OFFSET && (_currentOffset - iSmoothSlideAmount) >= NEW_FORWARD_OFFSET) {
         _ElementObj.style.top = _currentOffset - iSmoothSlideAmount + "px";
         IS_SCROLLING = true;
         moveTimer = window.setTimeout(moveScrollerUp, iSmoothSlideInterval);
         }
      else if (_currentOffset > NEW_FORWARD_OFFSET) {
         _ElementObj.style.top = _currentOffset - 1 + "px";
         IS_SCROLLING = true;
         moveTimer = window.setTimeout(moveScrollerUp, iSmoothSlideInterval);
         }
      else {
         IS_SCROLLING = false;
         CURRENT_THUMB_INDEX++;
         window.clearTimeout(moveTimer);
         if (bEnableThumbDescription == 1) {
            getElem(objDescriptionDiv).innerHTML = prImageDescription[CURRENT_THUMB_INDEX - 1];
            }
         if (bEnableCurrentCount == 1) {
            getElem(objCounterDiv).innerHTML = CURRENT_THUMB_INDEX + "/" + iNumOfImages;
            }
         }
      };
   function moveScrollerDown() {
      _ElementObj = _imageScrollerChildDivID;
      _currentOffset = parseInt(_ElementObj.style.top);
      if (_currentOffset < NEW_REVERSE_OFFSET && (_currentOffset + iSmoothSlideAmount) <= NEW_REVERSE_OFFSET) {
         _ElementObj.style.top = _currentOffset + iSmoothSlideAmount + "px";
         IS_SCROLLING = true;
         moveTimer = window.setTimeout(moveScrollerDown, iSmoothSlideInterval);
         }
      else if (_currentOffset < NEW_REVERSE_OFFSET) {
         _ElementObj.style.top = _currentOffset + 1 + "px";
         IS_SCROLLING = true;
         moveTimer = window.setTimeout(moveScrollerDown, iSmoothSlideInterval);
         }
      else {
         IS_SCROLLING = false;
         CURRENT_THUMB_INDEX--;
         window.clearTimeout(moveTimer);
         if (bEnableThumbDescription == 1) {
            getElem(objDescriptionDiv).innerHTML = prImageDescription[CURRENT_THUMB_INDEX - 1];
            }
         if (bEnableCurrentCount == 1) {
            getElem(objCounterDiv).innerHTML = CURRENT_THUMB_INDEX + "/" + iNumOfImages;
            }
         }
      };
   function moveScrollerRight() {
      _ElementObj = _imageScrollerChildDivID;
      _currentOffset = getLeft(_ElementObj);
      if (_currentOffset < NEW_REVERSE_OFFSET && (_currentOffset + iSmoothSlideAmount) <= NEW_REVERSE_OFFSET) {
         _ElementObj.style.left = _currentOffset + iSmoothSlideAmount + "px";
         IS_SCROLLING = true;
         moveTimer = window.setTimeout(moveScrollerRight, iSmoothSlideInterval);
         }
      else if (_currentOffset < NEW_REVERSE_OFFSET) {
         _ElementObj.style.left = _currentOffset + 1 + "px";
         IS_SCROLLING = true;
         moveTimer = window.setTimeout(moveScrollerRight, iSmoothSlideInterval);
         }
      else {
         IS_SCROLLING = false;
         CURRENT_THUMB_INDEX--;
         window.clearTimeout(moveTimer);
         if (bEnableThumbDescription == 1) {
            getElem(objDescriptionDiv).innerHTML = prImageDescription[CURRENT_THUMB_INDEX - 1];
            }
         if (bEnableCurrentCount == 1) {
            getElem(objCounterDiv).innerHTML = CURRENT_THUMB_INDEX + "/" + iNumOfImages;
            }
         }
      };
   function moveScrollerLeft() {
      _ElementObj = _imageScrollerChildDivID;
      _currentOffset = getLeft(_ElementObj);
      if (_currentOffset > NEW_FORWARD_OFFSET && (_currentOffset - iSmoothSlideAmount) >= NEW_FORWARD_OFFSET) {
         _ElementObj.style.left = _currentOffset - iSmoothSlideAmount + "px";
         IS_SCROLLING = true;
         moveTimer = window.setTimeout(moveScrollerLeft, iSmoothSlideInterval);
         }
      else if (_currentOffset > NEW_FORWARD_OFFSET) {
         _ElementObj.style.left = _currentOffset - 1 + "px";
         IS_SCROLLING = true;
         moveTimer = window.setTimeout(moveScrollerLeft, iSmoothSlideInterval);
         }
      else {
         IS_SCROLLING = false;
         CURRENT_THUMB_INDEX++;
         window.clearTimeout(moveTimer);
         if (bEnableThumbDescription == 1) {
            getElem(objDescriptionDiv).innerHTML = prImageDescription[CURRENT_THUMB_INDEX - 1];
            }
         if (bEnableCurrentCount == 1) {
            getElem(objCounterDiv).innerHTML = CURRENT_THUMB_INDEX + "/" + iNumOfImages;
            }
         }
      };

	function addAnEvent (_obj, _eventName, _functionName) {
		if (window.addEventListener) {
			_obj.addEventListener(_eventName, _functionName, false);
		}
		else {
			_obj.attachEvent("on" + _eventName, _functionName);
		}
	};

	function getElem (_elemID) {
		return document.getElementById(_elemID);
	};

	function getLeft (elem) {
		return parseInt(elem.style.left);
	}
	function getRight (elem) {
		return parseInt(elem.style.right);
	}
};

