/* Insight Web Design - Copyright 2008

image gallery navigation and image fade

*/
 
var fading = false;
var timeout = 2000;
var multiPause = 1500; //allows the fade of top image to catch up before fading the new one
var which=0;
var numberToFade = 0;
var gotFadeAmount = false;


// when the forwards navigation button is pressed
function forwards()
{
	if (fading == false)
	{
		if (fade[which] <= 0)
		{
			slideForwards();
			
			if (section != 'home')
			{
				autoForwards();
			}
		}
		else if (fade[which] >= 1)
		{
			calcFadeForwards();
		}		
	}	
}


// if gallery slides to an image that can fade then it will fade it
function autoForwards()
{
	if (fade[which] != 0)
	{	
		disableNavigation();
	
		window.setTimeout("calcFadeForwards();", timeout);
	}
	else
	{
		enableNavigation();
	}
}


// initiates the fading calculating how many images/times it needs to fade
function calcFadeForwards()
{	
	if (gotFadeAmount == false)
	{
		numberToFade = fade[which];			
						
		gotFadeAmount = true;
	}
	fadeForwards();	
						
	numberToFade --;
			
	if (numberToFade >= 1)
	{			
		window.setTimeout("calcFadeForwards();", timeout + multiPause); 	
	}
	else
	{
		gotFadeAmount = false;
	}
}


// when the backwards navigation button is pressed
function backwards()
{
	if (fading == false)
	{
		if (fade[which] >= 0)
		{
			slideBackwards();
			
			if (section != 'home')
			{
				autoBackwards();
			}
		}		
		else if (fade[which] <= -1)
		{
			calcFadeBackwards();
		}
	}	
}


// if gallery slides to an image that can fade then it will fade it
function autoBackwards()
{
	if (fade[which] != 0)
	{	
		disableNavigation();
	
		window.setTimeout("calcFadeBackwards();", timeout);
	}
	else
	{
		enableNavigation();
	}
}


// initiates the fading calculating how many images/times it needs to fade
function calcFadeBackwards()
{	
	if (gotFadeAmount == false)
	{
		numberToFade = fade[which];			
						
		gotFadeAmount = true;
	}
	
	fadeBackwards();	
						
	numberToFade ++;
				
	if (-1 >= numberToFade)
	{			
		window.setTimeout("calcFadeBackwards();", timeout + multiPause); 	
	}
	else
	{
		gotFadeAmount = false;
	}
}


// begins the fade process
function fadeForwards()
{	
	if (which < photos.length - 1)
	{	
		which ++;
		document.images.photoslider.src=photos[which];
			
		fadeImage();
		
		if (numberToFade >= 2)
		{
			window.setTimeout("document.images.bottom_photo.src=photos[which];", timeout);
		}
		else
		{						
			window.setTimeout("document.images.bottom_photo.src=photos[which]; enableNavigation();", timeout);
		}			
	}
	else
	{
		which = 0;
		document.images.photoslider.src=photos[which];
		fadeImage();
		window.setTimeout("document.images.bottom_photo.src=photos[which]", timeout);
	}
}


// begins the fade process
function fadeBackwards()
{	
	if (which > 0)
	{	
		which --;
		document.images.photoslider.src=photos[which];
			
		fadeImage();
		
		if (numberToFade <= -2)
		{
			window.setTimeout("document.images.bottom_photo.src=photos[which];", timeout);
		}
		else
		{
			window.setTimeout("document.images.bottom_photo.src=photos[which]; enableNavigation();", timeout);
		}
	}
	else
	{
		which = photos.length - 1;
		document.images.photoslider.src=photos[which];
			
		fadeImage();
			
		window.setTimeout("document.images.bottom_photo.src=photos[which]", timeout);
	}
}


// changes the images without fading
function slideForwards()
{
	
	if (which < photos.length - 1)
	{
		which ++;
		document.images.bottom_photo.src=photos[which];
		document.images.photoslider.src=photos[which];
	}
	else
	{
		which = 0;
		document.images.bottom_photo.src=photos[which];
		document.images.photoslider.src=photos[which];
	}
}


// changes the images without fading
function slideBackwards()
{	
	if (which > 0)
	{
		which --;
		document.images.bottom_photo.src=photos[which];
		document.images.photoslider.src=photos[which];
	}
	else
	{
		which = photos.length - 1;
		document.images.bottom_photo.src=photos[which];
		document.images.photoslider.src=photos[which];
	}
}


// when the image is clicked a large view of that image will open in a new window
function showLarge()
{
	var imageNumber = which + 1;

	var imagePath = path + imageNumber + '_large.jpg';
		
	window.open(imagePath);
}


// starts the fade process
function fadeImage()
{
	disableNavigation();
	
	imageId = 'photoslider';
	image = document.getElementById(imageId);
	setOpacity(image, 100);
	//image.style.visibility = 'visible';
	fadeIn(imageId,0);
}


// sets the opacity of the image to zero
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;
}


// starts fading the image opacity in from zero up in single units (i.e. by 1 each time to make it smooth)
function fadeIn(objId,opacity)
{
	if (document.getElementById)
	{
    	obj = document.getElementById(objId);
    
		if (opacity <= 100)
		{
      		setOpacity(obj, opacity);
      		opacity += 1;
      		window.setTimeout("fadeIn('"+objId+"',"+opacity+")", timeout / 100);
    	}
  }
}


// when mouse is over gallery navigation
function rollOver(button)
{
	if (fading == false)
	{
		if (button == 'prev')
		{
			document.images.nav_left.src='images/back_hover.gif';
			document.images.nav_left.setAttribute("alt", "Previous");
		}
		
		if (button == 'next')
		{		
			document.images.nav_right.src='images/next_hover.gif';
			document.images.nav_right.setAttribute("alt", "Next");
		}
	}
}


// when mouse leaves gallery navigation
function rollOut(button)
{
	if (fading == false)
	{
		if (button == 'prev')
		{
			document.images.nav_left.src='images/back.gif';
		}
		
		if (button == 'next')
		{		
			document.images.nav_right.src='images/next.gif';	
		}
	}
}


// disables gallery navigation while fading is in progress
function disableNavigation()
{	
	fading = true;
	
	//make navigation disappear
	document.images.nav_left.src='images/white_spacer.gif';
	document.images.nav_left.setAttribute("alt", "");	
	
	document.images.nav_right.src='images/white_spacer.gif';	
	document.images.nav_right.setAttribute("alt", "");
}


// enables gallery navigation
function enableNavigation()
{	
	fading = false;
	
	//make navigation disappear
	document.images.nav_left.src='images/back.gif';
	document.images.nav_left.setAttribute("alt", "Previous");	
	
	document.images.nav_right.src='images/next.gif';	
	document.images.nav_right.setAttribute("alt", "Next");
}
