/*
function changeImg( imgId, img ) 
{ 
  document.getElementById( imgId ).src = img;
} 
*/

function newImage( arg )
{
	if ( document.images )
	{
		rslt = new Image();
		rslt.src = arg;
		return rslt;
	}
}


function changeImages()
{
	if ( document.images && ( preloadFlag == true ) )
	{
    document.getElementById( 'imgScreen' ).src = imgarray[changeImages.arguments[0]].src;
	}
}


function changeSLImages()
{
	if ( document.images )
	{/*
	  if ( changeSLImages.arguments[0] >= imgPerPage )
	  {
	    imgNum = changeSLImages.arguments[0] - ( Math.floor( 16 / imgPerPage ) * 12 );
	  }
	  if ( allImgArray[imgNum].src != '' )
	  {
	    document.getElementById( 'imgScreen' ).src = allImgArray[imgNum].src;
	  }
	  else
	  {
  	  document.getElementById( 'imgScreen' ).src = allImgArray[changeSLImages.arguments[0]].src;
	  }*/
	  document.getElementById( 'imgScreen' ).src = allImgArray[changeSLImages.arguments[0]].src;
	}
}


var preloadFlag = false;
var imgarray = new Array();
function preloadImages()
{
	if (document.images)
	{
	  for ( var i = 0; i < newImages.length; i++ )
	  {
	    imgarray[i] = newImage( newImages[i] );
	  }
		preloadFlag = true;
		preloadAllImages();
	}
}


var preloadAllFlag = false;
var allImgArray = new Array();
function preloadAllImages()
{
	if (document.images)
	{
	  for ( var i = 0; i < allImages.length; i++ )
	  {
	    allImgArray[i] = newImage( imgPath + allImages[i] );
	  }
		preloadAllFlag = true;
	}
}

function pagelink( link )
{
  document.location = link;
}

function stoperror()
{
  return true;
}

function loadImages()
{ 
  for (i = 0; i < imgLen; i++)
  { 
    preImages[i]     = new Image(); 
    preImages[i].src = newImages[i]; 
    loaded[i]        = 0; 
    cover[i]         = Math.floor(num/imgLen)*(i+1) 
  } 
  cover[cover.length-1] += num%imgLen 
  checkLoad(); 
} 

function checkLoad()
{ 
  if (pending)
  { 
    changeto();
    return;
  } 
  if (currCount == imgLen)
  {
//    location.replace(locationAfterPreload);
    return;
  } 
  for (i = 0; i < imgLen; i++)
  { 
    if (!loaded[i] && preImages[i].complete)
    { 
      loaded[i] = 1; pending++; currCount++; 
      checkLoad(); 
      return; 
    } 
  } 
  setTimeout("checkLoad()",10); 
} 

var lastColor = '';

function changeto()
{ 
  if (h+1 > cover[currCount-1])
  { 
    var percent = Math.round(100/imgLen)*currCount; 
    
    if (percent > 100)
    {
      while (percent != 100)
      {
        percent--;
      }
    }
    if (currCount == imgLen && percent < 100)
    {
      percent = 100;
    }
    defaultStatus = "Loaded " + currCount + " out of " + imgLen + " images [" + percent + "%]."; 
    pending--; 
    checkLoad(); 
    return; 
  }
  /*
  reg = new RegExp( 'undefined', 'i' );
  
  if ( reg.test( hilite[h] ) != true )
  {
    lastColor = hilite[h];
  }
  else
  {
    hilite[h] = lastColor;
  }
  */
  
  eval("document.getElementById('cell" + (h+1) + "').style.backgroundColor = hilite[0]");
//  eval("document.getElementById('cell" + (h+1) + "').style.backgroundColor = hilite[h]");
  h++; 
  setTimeout("changeto()",1); 
}


function in_array( needle, haystack )
{
  for ( var x = 0; x < haystack.length; x++ )
  {
    if ( needle == urlPath + imgPath + haystack[x] )
    {
      return x;
    }
  }
  return -1;
}

function startSL()
{
//  preloadAllImages();
  current_img        = document.getElementById( 'imgScreen' ).src;
  var current_img_id = in_array( current_img, allImages );
  slPlay             = true;
  
  current_img_id++;
  setTimeout("nextSLimg( "+current_img_id+" )", SLTimer);
}

function nextSLimg( num )
{
  if ( num < allImages.length && slPlay == true )
  {
//    changeImages( num );
    changeSLImages( num );
    num++;
    setTimeout("nextSLimg( "+num+" )", SLTimer);
  }
  else if ( slPlay == true )
  {
    num = 0;
    changeSLImages( num );
    num++;
    setTimeout("nextSLimg( "+num+" )", SLTimer);
  }
  else if ( slPlay == false )
  {
    slPlay = true;
  }
}

function stopSL()
{
  slPlay = false;
}

function nextImg()
{
  current_img        = document.getElementById( 'imgScreen' ).src;
  var current_img_id = in_array( current_img, allImages );
  slPlay             = false;
  current_img_id++;
  
  if ( current_img_id < allImages.length )
  {
    changeSLImages( current_img_id );
  }
  else
  {
    current_img_id = 0;
    changeSLImages( current_img_id );
  }
}

function priorImg()
{
  current_img        = document.getElementById( 'imgScreen' ).src;
  var current_img_id = in_array( current_img, allImages );
  slPlay             = false;
  current_img_id--;
  
  if ( current_img_id < 0 )
  {
    changeSLImages( allImages.length - 1 );
  }
  else
  {
    changeSLImages( current_img_id );
  }
}
