
var gallscroll = function() {

	// set up variable
		var picList = new Array(
			//Array('gallery/thumb7505.jpg','gallery/IMGP7505.JPG','Automatic Stop Switch'),
			Array('gallery/thumb7508.jpg','gallery/IMGP7508.JPG','Fully-enclosed cage'),
			Array('gallery/thumb7512.jpg','gallery/IMGP7512.JPG','Access to Upper Deck'),
			Array('gallery/thumb7516.jpg','gallery/IMGP7516.JPG','Beach Butler Cargo Lift'),
			Array('gallery/DSC_0077thumbs.jpg','gallery/DSC_0077.jpg','Double-Mast with Inside Gate'),
			Array('gallery/DSC_0019thumb.jpg','gallery/DSC_0019.jpg','Two Motors Operate in Sync'),
			Array('gallery/thumb7489.jpg','gallery/IMGP7489.JPG','Easy Loading at Lower Level'),
			//Array('gallery/thumb7502.jpg','gallery/IMGP7502.JPG','Smart Stop Sealed Drive Unit'),
			//Array('gallery/thumb7503.jpg','gallery/IMGP7503.JPG','Automatic Stop Switch'),
			//Array('gallery/thumb7504.jpg','gallery/IMGP7504.JPG','Cable Winch'),
			//Array('gallery/thumb7486.jpg','gallery/IMGP7486.JPG','Secures to Upper Level'),
			Array('gallery/DSC_0002thumb.jpg','gallery/DSC_0002.jpg','Double-Mast Lift'),
			Array('gallery/DSC_0064thumb.jpg','gallery/DSC_0064.jpg','Double-Mast Lift Provides Safety')
		);

	/* state variable - what are we currently doing
		0 = not loaded
		1 = loaded thumbs
	*/
	var picsState = 0;
	var maxThumbs = 7;
	var currentPic = 0;
	var centerPicNum = 3;
	var thumbWidth = 90;
	var thumbHeight = 108;
	var movePicsCount = 0;
	var movePicsTimeout = 0;
	var slideInterval = 25;

	// set it up so the function is called after the page has completed loading
	function addLoadEvent(func) {
		var oldonload = window.onload;
		if (typeof window.onload != 'function') {
			window.onload = func;
		} else {
			window.onload = function() {
				if (oldonload) {
					oldonload();
				}
				func();
			}
		}
	}

	function loadThumbs() {
		// do something here
		document.getElementById('thumbs').innerHTML='';

		// go through the list of images
		for(i=0;i<maxThumbs;i++) {
			if ( i >= picList.length ) {
				break;
			}
			newAction=createThumbPic(i);
			document.getElementById('thumbs').appendChild(newAction);
		}

		// finished setting up thumbnails, set the big picture
		setTargetPic(centerPicNum);

		// tell the object that the thumbs are loaded and ready to go
		picsState = 1;
	}

	// create the thumbnail objects
	function createThumbPic(picNum) {
		pics = picList[picNum];
		newImg = document.createElement('img');
		newImg.src = pics[0];
		//newImg.width = thumbWidth;
		newAction = document.createElement('a');
		newAction.href = 'javascript:gallscroll.chooseThumb(\'' + picNum + '\');void(0);';
		newAction.appendChild(newImg);
		return newAction;	
	}

	// set the big picture in the target div
	function setTargetPic(picNum) {
		bigSrc = picList[picNum][1];
		document.getElementById('caption').innerHTML = picList[picNum][2];
		newImg = document.createElement('img');
		newImg.src = bigSrc;
		document.getElementById('target').innerHTML = '';
		document.getElementById('target').appendChild(newImg);

		// set the current pic so we remember it
		currentPic = picNum;
	}

	function movePicsLeft(count) {
		if ( movePicsTimeout ) {
			return;
		}
		if ( count > picList.length ) {
			count = count % picList.length;
		}
		movePicsCount = count;
		movePicsTimeout = setInterval("gallscroll.slideLeft()", slideInterval);
	}

	function slidePicsLeft() {
		if ( document.getElementById('thumbs').childNodes[0].childNodes[0].width == 0 ) {
			movePicsCount--;
			if ( movePicsCount <= 0 ) {
				clearTimeout(movePicsTimeout);
				movePicsTimeout = 0;
			}
			movePicsLeftOnce();
		} else {
			document.getElementById('thumbs').childNodes[0].childNodes[0].width -= 30;
			document.getElementById('thumbs').childNodes[0].childNodes[0].height = thumbHeight;
		}
	}

	function movePicsRight(count) {
		if ( movePicsTimeout ) {
			return;
		}
		if ( count > picList.length ) {
			count = count % picList.length;
		}
		movePicsCount = count;
		movePicsTimeout = setInterval("gallscroll.slideRight()", slideInterval);
	}

	function slidePicsRight() {
		if ( document.getElementById('thumbs').childNodes[0].childNodes[0].width >= thumbWidth ) {
			movePicsCount--;
			if ( movePicsCount <= -1 ) {
				clearTimeout(movePicsTimeout);
				movePicsTimeout = 0;
				return;
			}
			movePicsRightOnce();
			document.getElementById('thumbs').childNodes[0].childNodes[0].width = 0;
		} else {
			document.getElementById('thumbs').childNodes[0].childNodes[0].width += 30;
			document.getElementById('thumbs').childNodes[0].childNodes[0].height = thumbHeight;
		}
	}

	// move the pics to the left
	function OLDmovePicsLeft(count) {
		for(i=0;i<count;i++) {
			movePicsLeftOnce();
		}
	}

	function movePicsLeftOnce() {
		document.getElementById('thumbs').removeChild(document.getElementById('thumbs').childNodes[0]);
		setTargetPic((currentPic+1)%picList.length);
		newThumb = createThumbPic((currentPic+(maxThumbs-centerPicNum-1))%picList.length);
		document.getElementById('thumbs').appendChild(newThumb);
	}

	function OLDmovePicsRight(count) {
		for (i=0;i<count;i++) {
			movePicsRightOnce();
		}
	}

	function movePicsRightOnce() {
		document.getElementById('thumbs').removeChild(document.getElementById('thumbs').childNodes[document.getElementById('thumbs').childNodes.length-1]);
		setTargetPic((currentPic-1+picList.length)%picList.length);
		newThumb = createThumbPic((currentPic-(centerPicNum)+picList.length)%picList.length);
		document.getElementById('thumbs').insertBefore(newThumb, document.getElementById('thumbs').firstChild);
	}

	// this sets up the public-callable methods
	return {
		// init function
		init: function() {
			addLoadEvent(loadThumbs);
		},

		// change pictures
		chooseThumb: function(picNum) {

			if ( picNum == currentPic ) {
				setTargetPic(picNum);
			} else if ( (picNum > currentPic && picNum-currentPic <= maxThumbs-centerPicNum+1) || picNum < (currentPic-centerPicNum) ) {
				if ( picNum < currentPic-centerPicNum ) { picNum += picList.length; }
				movePicsLeft((picNum-currentPic));
			} else {
				movePicsRight(((currentPic+picList.length)-picNum)%picList.length);
			}
		},

		leftArrow: function() {
			movePicsRight(1);
		},

		rightArrow: function() {
			movePicsLeft(1);
		},

		slideLeft: function() {
			slidePicsLeft();
		},

		slideRight: function() {
			slidePicsRight();
		}

	}

}();

function thumbsSlideLeft() {
	gallscroll.slidePicsLeft();
}

gallscroll.init();

