// JavaScript Document
window.onload = InitAll;

function InitAll()
{
	document.getElementById("nextLink").onclick = showNextPic;
	document.getElementById("prevLink").onclick = showPrevPic;
}
function showNextPic(){
thisPic++;
	if(thisPic == tnPic.length){thisPic=0;}

	if(document.getElementById("tn_1"))
	{			   
	document.getElementById("bigImage").src = Pic[thisPic];		
	var i,j,k;
	if(thisPic==0){i=tnPic.length-1;}else{i=thisPic-1;}
	document.getElementById("tn_1").src = tnPic[i];
	document.getElementById("tn_2").src = tnPic[thisPic];
	if(thisPic==tnPic.length-1){j=0;}else{j=thisPic+1;}
	document.getElementById("tn_3").src = tnPic[j];
	if(thisPic==tnPic.length-2){k=0;}else if(thisPic==tnPic.length-1){k=1;}else{k=thisPic+2;}
	document.getElementById("tn_4").src = tnPic[k];	
	}
	else
	{
	document.getElementById("randImage").src = tnPic[thisPic];		
	}
	return false;
}
function showPrevPic()
{
	if(thisPic == 0){thisPic=tnPic.length;}
thisPic--;

	if(document.getElementById("tn_1"))
	{//Shows Previous picture(s) for tn and original pictures
	document.getElementById("bigImage").src = Pic[thisPic];
	var i,j,k;
	if(thisPic==0){i=tnPic.length-1;}else{i=thisPic-1;}
	document.getElementById("tn_1").src = tnPic[i];
	document.getElementById("tn_2").src = tnPic[thisPic];
	if(thisPic==tnPic.length-1){j=0;}else{j=thisPic+1;}
	document.getElementById("tn_3").src = tnPic[j];
	if(thisPic==tnPic.length-2){k=0;}else if(thisPic==tnPic.length-1){k=1;}else{k=thisPic+2;}
	document.getElementById("tn_4").src = tnPic[k];	
	}
	else
	{
	document.getElementById("randImage").src = tnPic[thisPic];		
	}
	return false;
}

//===This is the slideshow functions part===
var FadeDurationMS=1000;
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 msNow = (new Date()).getTime();
  var opacity = fromO + (toO - fromO) * (msNow - msStart) / msDuration;
  if (opacity>=100)
  {
    SetOpacity(element,100);
    element.timer = undefined;
  }
  else if (opacity<=0)
  {
    SetOpacity(element,0);
    element.timer = undefined;
  }
  else 
  {
    SetOpacity(element,opacity);
    element.timer = window.setTimeout("ChangeOpacity('" + id + "'," + msDuration + "," + msStart + "," + fromO + "," + toO + ")",10);
  }
}
function FadeInImage(foregroundID,newImage,backgroundID)
{
  var foreground=document.getElementById(foregroundID);
  if (foreground.timer) window.clearTimeout(foreground.timer);
  if (backgroundID)
  {
    var background=document.getElementById(backgroundID);
    if (background)
    {
      if (background.src)
      {
        foreground.src = background.src; 
        SetOpacity(foreground,100);
      }
      background.src = newImage;
	  background.style.border = 1;
	  background.style.width = 200;
	  background.style.heigth = 150;
      background.style.backgroundImage = 'url(' + newImage + ')';
 //     background.style.backgroundRepeat = 'no-repeat';
      var startMS = (new Date()).getTime();
      foreground.timer = window.setTimeout("ChangeOpacity('" + foregroundID + "'," + FadeDurationMS + "," + startMS + ",100,0)",10);
    }
  } else {
    foreground.src = newImage;
  }
}
var futureImage;
function RunSlideShow(pictureID,displaySecs)
{ //Pic[] comes from menu.php and its global holding the array of image sources as strings
//thisPic comes from menu.php and is global holding the # of the shown image in Pic[]
  var nextImage;
  if((thisPic)<tnPic.length-1){thisPic++;}
  else{thisPic=0;}
  if(futureImage){nextImage = futureImage;}
  else{nextImage = tnPic[thisPic];}  
  
  document.getElementById(pictureID).src = nextImage;
  setTimeout("RunSlideShow('"+pictureID+"',"+displaySecs+")",displaySecs*1000);

  if((thisPic)<tnPic.length-1)
  {
	futureImage = tnPic[thisPic+1];
  }
  else
  {
	futureImage = tnPic[0];
  }
//  FadeInImage(pictureID,nextImage,backgroundID);
}

