function swapImg( numNewImg ) {
	stopShow();
	

	x = eval('imgPhoto' + numNewImg + '.width');
	//alert(x);
	if (x >= 570) {
	    FadeInImage('imgMain', 'imgPhoto' + numNewImg + '.src', 'backgrounddiv')
	} else {
	    FadeInImage2('imgMain', 'imgPhoto' + numNewImg + '.src', 'backgrounddiv')
	}
	eval("document.imgMain.src = imgPhoto" + numNewImg + ".src");
	if (document.getElementById('imgLabel'))
		document.getElementById('imgLabel').innerHTML = numNewImg;
	numCurrentImg = numNewImg;
}

function startShow() {
	if (timer == null)
	    swapTimeout();
}

function stopShow() {
	if (timer != null){
		window.clearTimeout(timer);
		timer = null;				
	}
}
function toggleShow() {
	if (timer == null) {
	    startShow();
	} else {
		stopShow();				
	}
}

function nextImg() {
	numNewImg = numCurrentImg+1;
	if (numNewImg > numPhotos)
		numNewImg = 1
    
    
    x = eval('imgPhoto' + numNewImg + '.width');
    //alert(x);
    if (x >= 570) {
        FadeInImage('imgMain', 'imgPhoto' + numNewImg + '.src', 'backgrounddiv')
    } else {
        FadeInImage2('imgMain', 'imgPhoto' + numNewImg + '.src', 'backgrounddiv')
    }
    
    eval("document.imgMain.src = imgPhoto" + numNewImg + ".src");    
	if (document.getElementById('imgLabel'))
		document.getElementById('imgLabel').innerHTML = numNewImg;
	numCurrentImg = numNewImg;
}

function prevImg() {
	numNewImg = numCurrentImg-1;
	if (numNewImg < 1)
		numNewImg = numPhotos

    x = eval('imgPhoto' + numNewImg + '.width');
    //alert(x);
    if (x >= 570) {
        FadeInImage('imgMain', 'imgPhoto' + numNewImg + '.src', 'backgrounddiv')
    } else {
        FadeInImage2('imgMain', 'imgPhoto' + numNewImg + '.src', 'backgrounddiv')
    }
    
    
	eval("document.imgMain.src = imgPhoto" + numNewImg + ".src");
	if (document.getElementById('imgLabel'))
		document.getElementById('imgLabel').innerHTML = numNewImg;
	numCurrentImg = numNewImg;
}

function swapTimeout() {

	if ( numCurrentImg == numPhotos ) { // We've just displayed the last img.
		numCurrentImg = 1;
	} else {
		numCurrentImg++;
	}

	swapImg(numCurrentImg);
	timer = window.setTimeout(swapTimeout, numSeconds);
}



// Opacity and Fade in script.
// Script copyright (C) 2008 http://www.cryer.co.uk/.
// Script is free to use provided this copyright header is included.
function SetOpacity(object,opacityPct)
{
  // IE.
  object.style.filter = 'alpha(opacity=' + opacityPct + ')';
  // Old mozilla and firefox
  object.style.MozOpacity = opacityPct/100;
  // Everything else.
  object.style.opacity = opacityPct/100;
}
function ChangeOpacity(id,msDuration,msStart,fromO,toO)
{
  var element=document.getElementById(id);
  var opacity = element.style.opacity * 100;
  var msNow = (new Date()).getTime();
  opacity = fromO + (toO - fromO) * (msNow - msStart) / msDuration;
  if (opacity<0) 
    SetOpacity(element,0)
  else if (opacity>100)
    SetOpacity(element,100)
  else
  {
    SetOpacity(element,opacity);
    element.timer = window.setTimeout("ChangeOpacity('" + id + "'," + msDuration + "," + msStart + "," + fromO + "," + toO + ")",1);
  }
}
function FadeIn(id)
{
  var element=document.getElementById(id);
  if (element.timer) window.clearTimeout(element.timer); 
  var startMS = (new Date()).getTime();
  element.timer = window.setTimeout("ChangeOpacity('" + id + "',1000," + startMS + ",0,100)",1);
}
function FadeOut(id)
{
  var element=document.getElementById(id);
  if (element.timer) window.clearTimeout(element.timer); 
  var startMS = (new Date()).getTime();
  element.timer = window.setTimeout("ChangeOpacity('" + id + "',1000," + startMS + ",100,0)",1);
}
function FadeInImage(foregroundID,newImage,backgroundID)
{
  var foreground=document.getElementById(foregroundID);
  if (backgroundID)
  {
    var background=document.getElementById(backgroundID);
    if (background)
    {
      background.style.backgroundImage = 'url(' + foreground.src + ')';
      background.style.backgroundRepeat = 'no-repeat';
      background.style.backgroundColor = '#FFFFFF';
      background.style.backgroundPosition = 'top';
      background.style.width ='570px';
    }
}

SetOpacity(foreground, 0);

foreground.src = newImage;
  
  if (foreground.timer) window.clearTimeout(foreground.timer); 
  var startMS = (new Date()).getTime();
  foreground.timer = window.setTimeout("ChangeOpacity('" + foregroundID + "',1000," + startMS + ",0,100)",10);
}
function FadeInImage2(foregroundID, newImage, backgroundID) {//imgMain, newImg, prevImg
    var foreground = document.getElementById(foregroundID);
    if (backgroundID) {
        var background = document.getElementById(backgroundID);
        if (background) {
            background.style.backgroundImage = 'url(images/white_bg.gif)';
            background.style.backgroundRepeat = 'no-repeat';
            background.style.backgroundColor = '#FFFFFF';
            background.style.backgroundPosition = 'top';
            background.style.width = '570px';
        }
    }
    foreground.style.width = '570px';
    SetOpacity(foreground, 0);
    
    foreground.src = newImage;
    
    if (foreground.timer) window.clearTimeout(foreground.timer);
    var startMS = (new Date()).getTime();
    foreground.timer = window.setTimeout("ChangeOpacity('" + foregroundID + "',1000," + startMS + ",0,100)", 10);
}
//to get image width
function getImageWidth(myImage) {
    var x, obj;
    if (document.layers) {
        var img = getImage(myImage);
        return img.width;
    } else {
        return getElementWidth(myImage);
    }
    return -1;
}

