	/************************************************************************************************************
	(C) www.dhtmlgoodies.com, June 2006
	
	This is a script from www.dhtmlgoodies.com. You will find this and a lot of other scripts at our website.	
	
	Terms of use:
	You are free to use this script as long as the copyright message is kept intact. However, you may not
	redistribute, sell or repost it without our permission.
	
	Thank you!
	
	www.dhtmlgoodies.com
	Alf Magne Kalleland
	
	************************************************************************************************************/		
	
	var opacitySpeed = 2;	// Speed of opacity - switching between large images - Lower = faster
	var opacitySteps = 20; 	// Also speed of opacity - Higher = faster
	var slideSpeed = 5;	// Speed of thumbnail slide - Lower = faster
	var slideSteps = 10;	// 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.
	//This is defined in the actual page itself now....
	//var columnsOfThumbnails = 11;	//Aisling: number of images + 1 ---> makes the Next button visible for all the images
	
	var numberOfImagesPerSlide = 1; //3; //Aisling - how many images the Previous and Next arrows will scroll at one time
    	var imageCaption = false; //Aisling - the caption for the current image
	var imageCount = false; //Aisling - the current image's count out of total images
	var thumbnailWidth = 125; //Aisling - default image thumbnail width - IE 7 doesn't seem to be able to resolve the div's offsetWidth

	/* Don't change anything below here */
	var DHTMLgoodies_largeImage = false;
	var DHTMLgoodies_imageToShow = false;
	var DHTMLgoodies_currentOpacity = 100;
	var DHTMLgoodies_slideWidth = false;
	var DHTMLgoodies_thumbTotalWidth = false;
	var DHTMLgoodies_viewableWidth = false;
	
	var currentUnqiueOpacityId = false;
	var DHTMLgoodies_currentActiveImage = false;
	var DHTMLgoodies_thumbDiv = false;
	var DHTMLgoodies_thumbSlideInProgress = false;
	
	var browserIsOpera = navigator.userAgent.indexOf('Opera')>=0?true:false;
	var leftArrowObj;
	var rightArrowObj;
	var thumbsColIndex = 1;
	var thumbsLeftPos = false;
	var imgAltText = '';
	
	function initGalleryScript()
	{
		leftArrowObj = document.getElementById('DHTMLgoodies_leftArrow');
		leftArrowObj.style.visibility='hidden';
		rightArrowObj = document.getElementById('DHTMLgoodies_rightArrow');	
		leftArrowObj.style.cursor = 'pointer';
		rightArrowObj.style.cursor = 'pointer';	
		leftArrowObj.onclick = moveThumbnails;
		rightArrowObj.onclick = moveThumbnails;
		DHTMLgoodies_largeImage = document.getElementById('DHTMLgoodies_largeImage').getElementsByTagName('IMG')[0];
		//imageCaption = document.getElementById('DHTMLgoodies_largeImage').getElementsByTagName('SPAN')[1];
		imageCaption = document.getElementById('imageCaption');
		//imageCount = document.getElementById('DHTMLgoodies_largeImage').getElementsByTagName('SPAN')[0];
		imageCount = document.getElementById('imageCount');
		var innerDiv = document.getElementById('DHTMLgoodies_thumbs_inner');
		DHTMLgoodies_slideWidth = innerDiv.getElementsByTagName('DIV')[0].offsetWidth;
		if (DHTMLgoodies_slideWidth == 0) 
		{
			DHTMLgoodies_slideWidth = thumbnailWidth;
		}		
		DHTMLgoodies_thumbDiv = document.getElementById('DHTMLgoodies_thumbs_inner');
		DHTMLgoodies_thumbDiv.style.left = '0px';
		
		var subDivs = DHTMLgoodies_thumbDiv.getElementsByTagName('DIV');
		DHTMLgoodies_thumbTotalWidth = 0;
		var tmpLeft = 0;
		for(var no=0;no<subDivs.length;no++){
			if(subDivs[no].className=='strip_of_thumbnails'){
				DHTMLgoodies_thumbTotalWidth = DHTMLgoodies_thumbTotalWidth + DHTMLgoodies_slideWidth;
				subDivs[no].style.left = tmpLeft + 'px';
				subDivs[no].style.top = '0px';
				if (subDivs[no].offsetWidth > 0) {
					tmpLeft = tmpLeft + subDivs[no].offsetWidth;
				} else {
					tmpLeft = tmpLeft + thumbnailWidth;
				}
			}
		}

		DHTMLgoodies_viewableWidth = document.getElementById('DHTMLgoodies_thumbs').offsetWidth;
		
		DHTMLgoodies_currentActiveImage = DHTMLgoodies_thumbDiv.getElementsByTagName('A')[0].getElementsByTagName('IMG')[0];
		DHTMLgoodies_currentActiveImage.className='activeImage';
		
		//Aisling: scale the thumbnails in proportion
		//scaleThumbnails();
	}
	
	function moveThumbnails()
	{
		if(DHTMLgoodies_thumbSlideInProgress)return;
		DHTMLgoodies_thumbSlideInProgress = true;
		if(this.id=='DHTMLgoodies_leftArrow'){			
			thumbsColIndex = thumbsColIndex - numberOfImagesPerSlide; //thumbsColIndex--;
			rightArrowObj.style.visibility='visible';
			if(DHTMLgoodies_thumbDiv.style.left.replace('px','')/1>=0){
				leftArrowObj.style.visibility='hidden';
				DHTMLgoodies_thumbSlideInProgress = false;
				return;
			}

			slideThumbs(slideSteps,0);

		}else{			
			thumbsColIndex = thumbsColIndex + numberOfImagesPerSlide; //thumbsColIndex++;
			leftArrowObj.style.visibility='visible';
			var left = DHTMLgoodies_thumbDiv.style.left.replace('px','')/1;
			var showArrow = true;
			if(DHTMLgoodies_thumbTotalWidth + left - DHTMLgoodies_slideWidth <= DHTMLgoodies_viewableWidth)showArrow = false;
			if(columnsOfThumbnails)showArrow = true;
			if(!showArrow)
			{
				rightArrowObj.style.visibility='hidden';
				DHTMLgoodies_thumbSlideInProgress = false;
				return;
			}

			slideThumbs((slideSteps*-1),0);
		}

	}

	function slideThumbs(speed,currentPos)
	{
		var leftPos;
		if(thumbsLeftPos){
			leftPos= thumbsLeftPos;
		}else{
			var leftPos = DHTMLgoodies_thumbDiv.style.left.replace('px','')/1;
			thumbsLeftPos = leftPos;
		}
		currentPos = currentPos + Math.abs(speed);
		var tmpLeftPos = leftPos;
		leftPos = leftPos + speed;
		thumbsLeftPos = leftPos;
		DHTMLgoodies_thumbDiv.style.left = leftPos + 'px';
		//if(currentPos<DHTMLgoodies_slideWidth)setTimeout('slideThumbs(' + speed + ',' + currentPos + ')',slideSpeed);else{
         if(currentPos<DHTMLgoodies_slideWidth*numberOfImagesPerSlide)setTimeout('slideThumbs(' + speed + ',' + currentPos + ')',slideSpeed);else{
			if(tmpLeftPos>=0 || (columnsOfThumbnails && thumbsColIndex==1)){
				document.getElementById('DHTMLgoodies_leftArrow').style.visibility='hidden';
			}
			var left = tmpLeftPos;
			var showArrow = true;
			if(DHTMLgoodies_thumbTotalWidth + left - DHTMLgoodies_slideWidth <= DHTMLgoodies_viewableWidth)showArrow=false;
			if(columnsOfThumbnails){
				if((thumbsColIndex+1)<columnsOfThumbnails)showArrow=true; else showArrow = false;
				//alert('thumbsColIndex:'+(thumbsColIndex+1)+' columnsOfThumbnails:'+columnsOfThumbnails+' showArrow:'+showArrow);
			}
			if(!showArrow){
				document.getElementById('DHTMLgoodies_rightArrow').style.visibility='hidden';
			}					
			DHTMLgoodies_thumbSlideInProgress = false;
			
			//Aisling: show picture in the main section
			var linkDiv = document.getElementById('DHTMLgoodies_thumbs_inner');
			var imageLink = linkDiv.getElementsByTagName('a')[thumbsColIndex-1];						
			imageLink.onclick();
		}
	
	}
	
	function showPreview(imagePath, inputObj, caption, count, imgAlt)
	{
		if (caption) {
		    imageCaption.innerHTML = caption;
        } else {
            imageCaption.innerHTML = '';
        }
	    
		if (count) {
		    imageCount.innerHTML = 'Image ' + count + ' of '+ (columnsOfThumbnails - 1);
        } else {
            imageCount.innerHTML = '';
        }
		
		if(imgAlt) {
			imgAltText = imgAlt;
		}
		
		if(DHTMLgoodies_currentActiveImage){
			if(DHTMLgoodies_currentActiveImage==inputObj.getElementsByTagName('IMG')[0])return;
			DHTMLgoodies_currentActiveImage.className='';
		}
		DHTMLgoodies_currentActiveImage = inputObj.getElementsByTagName('IMG')[0];
		DHTMLgoodies_currentActiveImage.className='activeImage';

		DHTMLgoodies_imageToShow = imagePath;
		var tmpImage = new Image();
		tmpImage.src = imagePath;

		currentUnqiueOpacityId = Math.random();
		moveOpacity(opacitySteps*-1,currentUnqiueOpacityId, caption);
	}
	
	function setOpacity()
	{
		if(document.all)
		{
			DHTMLgoodies_largeImage.style.filter = 'alpha(opacity=' + DHTMLgoodies_currentOpacity + ')';
		}else{
			DHTMLgoodies_largeImage.style.opacity = DHTMLgoodies_currentOpacity/100;
		}
	}
	function moveOpacity(speed,uniqueId, caption)
	{
		if(browserIsOpera){
			DHTMLgoodies_largeImage.src = DHTMLgoodies_imageToShow;
			return;
		}

		DHTMLgoodies_currentOpacity = DHTMLgoodies_currentOpacity + speed;
		if(DHTMLgoodies_currentOpacity<=5 && speed<0){
		
			var tmpParent = DHTMLgoodies_largeImage.parentNode; 
			DHTMLgoodies_largeImage.parentNode.removeChild(DHTMLgoodies_largeImage);
			DHTMLgoodies_largeImage = document.createElement('IMG');
			tmpParent.appendChild(DHTMLgoodies_largeImage);
			setOpacity();
			DHTMLgoodies_largeImage.src = DHTMLgoodies_imageToShow;

			speed=opacitySteps;
		}
		
		if (imgAltText) {
			DHTMLgoodies_largeImage.alt = imgAltText;
		}
		
		if(DHTMLgoodies_currentOpacity>=99 && speed>0)DHTMLgoodies_currentOpacity=99;		
		setOpacity();	
		if(DHTMLgoodies_currentOpacity>=99 && speed>0)return;		
		if(uniqueId==currentUnqiueOpacityId)setTimeout('moveOpacity(' + speed + ',' + uniqueId + ')',opacitySpeed);	


	}
	
	//Function to scale the thumbnails in proportion
	function scaleThumbnails()
    {
         var box;
         var img;
         var innerDivs = document.getElementById('DHTMLgoodies_thumbs_inner').getElementsByTagName('DIV');
         for (var x = 0; x < innerDivs.length; x++)
         {
			box = innerDivs[x];
			if (box.className == 'strip_of_thumbnails')
			{
				img = box.getElementsByTagName('IMG')[0];				
		         // Grab the image's dimensions
		         var imgH = img.clientHeight > 0 ? img.clientHeight : img.offsetHeight;		         				 
				 var imgW = img.clientWidth > 0 ? img.clientWidth : img.offsetWidth;  
				 //alert('imgH:'+imgH+' imgW:'+imgW);				 
				 //Get the box dimensions
				 var boxH = box.clientHeight > 0 ? box.clientHeight : box.offsetHeight;		         				 
				 var boxW = box.clientWidth > 0 ? box.clientWidth : box.offsetWidth;				 
				 //Add padding around the image     
				 boxH = boxH - 10;
				 boxW = boxW - 10; 		         
				 //alert('boxH:'+boxH+' boxW:'+boxW);
		         // Find which dimension is scaled the most		         
		         var scaleH = boxH / imgH;
		         var scaleW = boxW / imgW;
		         // Scale the image
		         if (scaleH < scaleW) {
		         	img.style.height = boxH + "px";
		         	img.style.width = Math.round(imgW * scaleH) + "px";
		         } else {
		         	img.style.width = boxW + "px";
		         	img.style.height = Math.round(imgH * scaleW) + "px";
		         }
         		//alert('width:'+img.style.width+' height:'+img.style.height);
         	} //end if
         } //end for
     }
	

