var opacitySpeed = 2;	// Speed of opacity - switching between large images - Lower = faster
var opacitySteps = 10; 	// Also speed of opacity - Higher = faster
var slideSpeed = 5;	// Speed of thumbnail slide - Lower = faster
var slideSteps = 8;	// Also speed of thumbnail slide - Higher = faster
var columnsOfThumbnails = 4;	// Hardcoded number of thumbnail columns, use false if you want the script to figure it out dynamically.
	
var gallery_imageToShow = false;
var gallery_currentOpacity = 100;
var gallery_slideWidth = false;
var gallery_thumbTotalWidth = false;
var gallery_viewableWidth = false;
	
var currentUnqiueOpacityId = false;
var gallery_currentActiveImage = false;
var gallery_thumbDiv = false;
var gallery_thumbSlideInProgress = false;
	
var browserIsOpera = navigator.userAgent.indexOf('Opera')>=0?true:false;
var leftArrowObj;
var rightArrowObj;
var thumbsColIndex = 1;
var thumbsLeftPos = false;
	
function initGalleryScript() {
  leftArrowObj = document.getElementById('gallery_leftArrow');		
  leftArrowObj.style.visibility='hidden';
  rightArrowObj = document.getElementById('gallery_rightArrow');	
  leftArrowObj.style.cursor = 'pointer';	
  rightArrowObj.style.cursor = 'pointer';	
  leftArrowObj.onclick = moveThumbnails;
  rightArrowObj.onclick = moveThumbnails;
  var innerDiv = document.getElementById('gallery_thumbs_inner');
  gallery_slideWidth = innerDiv.getElementsByTagName('DIV')[0].offsetWidth;
  gallery_thumbDiv = document.getElementById('gallery_thumbs_inner');
  gallery_thumbDiv.style.left = '0px';
		
  var subDivs = gallery_thumbDiv.getElementsByTagName('DIV');
  gallery_thumbTotalWidth = 0;
  var tmpLeft = 0;
  for (var no=0; no<subDivs.length; no++) {
	if (subDivs[no].className=='strip_of_thumbnails') {
	  gallery_thumbTotalWidth = gallery_thumbTotalWidth + gallery_slideWidth;
	  subDivs[no].style.left = tmpLeft + 'px';
	  subDivs[no].style.top = '0px';
	  tmpLeft = tmpLeft + subDivs[no].offsetWidth;
	}
  }

  gallery_viewableWidth = document.getElementById('gallery_thumbs').offsetWidth;
		
  gallery_currentActiveImage = gallery_thumbDiv.getElementsByTagName('A')[0].getElementsByTagName('IMG')[0];
  gallery_currentActiveImage.className='activeImage';
}
	
function moveThumbnails() {
  if (gallery_thumbSlideInProgress) return;
  gallery_thumbSlideInProgress = true;
  if (this.id=='gallery_leftArrow') {
    thumbsColIndex--;
	rightArrowObj.style.visibility='visible';
	if (gallery_thumbDiv.style.left.replace('px','')/1>=0) {
	  leftArrowObj.style.visibility='hidden';
	  gallery_thumbSlideInProgress = false;
	  return;
	}
		
	slideThumbs(slideSteps,0);
  } else {
	thumbsColIndex++;
	leftArrowObj.style.visibility='visible';
 	var left = gallery_thumbDiv.style.left.replace('px','')/1;	
	var showArrow = true;
	if (gallery_thumbTotalWidth + left - gallery_slideWidth <= gallery_viewableWidth)showArrow = false;
	if (columnsOfThumbnails) showArrow = true;
				
	if (!showArrow) {
	  rightArrowObj.style.visibility='hidden';
	  gallery_thumbSlideInProgress = false;
	  return;
	}	
			
	slideThumbs((slideSteps*-1),0);
  }	
}
	
function slideThumbs(speed,currentPos) {
  var leftPos;
  if (thumbsLeftPos) {
    leftPos= thumbsLeftPos;
  } else {
	var leftPos = gallery_thumbDiv.style.left.replace('px','')/1;
	thumbsLeftPos = leftPos;
  }
  currentPos = currentPos + Math.abs(speed);		
  var tmpLeftPos = leftPos;
  leftPos = leftPos + speed;
  thumbsLeftPos = leftPos;
  gallery_thumbDiv.style.left = leftPos + 'px';
  if (currentPos<gallery_slideWidth)setTimeout('slideThumbs(' + speed + ',' + currentPos + ')',slideSpeed);
  else {
    if (tmpLeftPos>=0 || (columnsOfThumbnails && thumbsColIndex==1)) {
	  document.getElementById('gallery_leftArrow').style.visibility='hidden';
	}	
	var left = tmpLeftPos;		
	var showArrow = true;
	if (gallery_thumbTotalWidth + left - gallery_slideWidth <= gallery_viewableWidth) showArrow=false;
	if (columnsOfThumbnails) {
	  if ((thumbsColIndex+1)<columnsOfThumbnails) showArrow=true; else showArrow = false;				
	}			
	if (!showArrow) {
	  document.getElementById('gallery_rightArrow').style.visibility='hidden';
	}					
	gallery_thumbSlideInProgress = false;
  }
}
	
function showPreview(imagePath,inputObj) {		
  if (gallery_currentActiveImage) {
	if (gallery_currentActiveImage==inputObj.getElementsByTagName('IMG')[0]) return;
    gallery_currentActiveImage.className='';
  }
  gallery_currentActiveImage = inputObj.getElementsByTagName('IMG')[0];
  gallery_currentActiveImage.className='activeImage';
	
  gallery_imageToShow = imagePath;
  var tmpImage = new Image();
  tmpImage.src = imagePath;
  currentUnqiueOpacityId = Math.random();
  moveOpacity(opacitySteps*-1,currentUnqiueOpacityId);
}

function moveOpacity(speed,uniqueId) {
  gallery_currentOpacity = gallery_currentOpacity + speed;
  if (gallery_currentOpacity<=5 && speed<0) {
		
    alert(gallery_imageToShow);
		
    speed=opacitySteps;
  }
  if (gallery_currentOpacity>=99 && speed>0)gallery_currentOpacity=99;		
  if (gallery_currentOpacity>=99 && speed>0)return;		
  if (uniqueId==currentUnqiueOpacityId)setTimeout('moveOpacity(' + speed + ',' + uniqueId + ')',opacitySpeed);		
}
