//   **  javascript functions and variables  **
//------------  global variables  -----------
var imgsused=0;
var totalimgs=0;
var sources=0;
var rowct=0;
var colct=0;
var rowcount=0;
var columncount=0;
var imagecount=0;
var maximgct=0;
var thumbw=0;
var thumbh=0;
var consectflag=0;
var previmg=0;
var thisimg=0;
var imgnum=0;
var formatflag=0;
var orientflag=0;
var rotflag=0;
var zoomflag=0;
var zooming=0;
var millisecs=1500;
var imgwin=null;

//################################################################################################################################//

function makeimg()
{
  ajax(); 
  //initialize();
  //if (zoomflag!=0) showzoom();    
}
//---------------------------------------

function setupgridarray(rows,cols)
{ 
  imgsused=Math.floor((Math.min(rows,cols)+1)/2);   // # of different "rings" in grid
//  alert('imgsused: ' + imgsused + ' imagecount: ' + imagecount);
  gridarray=new Array(imgsused);
  previousimg=0;
  for (i=1; i <= imgsused; i++)      
    { 
    loadgridarray(i);
    }
    
}

//---------------------------------------
 
function loadgridarray(indx)
{   
  thisimg=Math.floor(Math.random()*imagecount)+1;   // choose a random image from those available

  if (consectflag==0)
    {
    while (thisimg==previousimg)
      {
      thisimg=Math.floor(Math.random()*imagecount)+1;   //  choose again if consecutives not ok
      } 
    }
//   alert('loading ' + indx + ' with ' + thisimg); 
  gridarray[indx]=thisimg;
  previousimg=thisimg;  
}    
    
//---------------------------------------
    
function edgedist(rowx,colx)
{

 min1=Math.min(Math.abs(rowx),Math.abs(colx));
 min2=Math.min(Math.abs(rowx-rowcount-1),Math.abs(colx-columncount-1));
//   alert('edge row col min: ' + rowx + ' ' + colx + ' ' + Math.min(min1,min2));
 return Math.min(min1,min2)
 
}
//---------------------------------------

function showzoom()
{
  switch (zoomflag) 
  {
    case 1: shiftin(); 
    break;
    case 2: shiftout(); 
    break;
  };

  increment();
  zooming=setTimeout("showzoom()",millisecs);
}

//---------------------------------------

function shiftin()
{
  for (i=1; i <= imgsused-1; i++) 
    {      
    gridarray[i]=gridarray[i+1];
    }

  loadgridarray(imgsused);
}
//---------------------------------------
function shiftout()
{
  for (i=1; i <= imgsused-1; i++) 
    {
    j=imgsused-i+1;    
    gridarray[j]=gridarray[j-1];
    };
    
  loadgridarray(1);
}
//---------------------------------------
function getradioindex(radioid) 
{
  var ret = -1;
  var pR = document.getElementsByName(radioid);
  for(var i=0; i<pR.length; i++) 
  {
    if(document.getElementsByName(radioid)[i].checked == true)
    {
     ret = i;
     break;
    }
  }
  return ret;
}

//################################################################################################################################//


//loop through each position in the grid and shows the first image
function initialize() {

	//grab the necessary variables from the HTML page
	columncount = document.getElementById('columncount').options[document.getElementById('columncount').selectedIndex].text;
	rowcount = document.getElementById('rowcount').options[document.getElementById('rowcount').selectedIndex].text;

	imagecount = document.getElementById('imagecount').options[document.getElementById('imagecount').selectedIndex].text;

  clearTimeout(zooming);
  zoomflag=document.imgform.zoomflag[getradioindex('zoomflag')].value*1; 

  consectflag=document.imgform.consectflag[getradioindex('consectflag')].value*1;  

  setupgridarray(rowcount,columncount);  //  set up initial state of grid output
    
	//do this for each row in the grid
	for (var row=1; row<=rowcount; row++) {

		//do this for each column in the row
		for (var col=1; col<=columncount; col++) {
			
			
			//calculate the row-col id of the current position
			var currentposition = row+"-"+col+"-";
			
			//calculate the first image to show
			
					//var show_image = currentposition+1;

		    imgtoshow=edgedist(row,col); 
		    var show_image = currentposition+gridarray[imgtoshow];
    			//alert(" show_image is " + show_image);
    			
			//and show it
			document.getElementById(show_image).setAttribute("class", "show"); 							
			
		}//for col

	}//for row

}//function initialize



//################################################################################################################################//


//loops through each position in the grid hides the currently showing image and shows the next image
function increment() {

	//grab the necessary variables from the HTML page
//	var columncount = document.getElementById('columncount').options[document.getElementById('columncount').selectedIndex].text;
//	var rowcount = document.getElementById('rowcount').options[document.getElementById('rowcount').selectedIndex].text;
//	var imagecount = document.getElementById('imagecount').options[document.getElementById('imagecount').selectedIndex].text;



	//do this for each row in the grid
	for (var row=1; row<=rowcount; row++) {

		//do this for each column in the row
		for (var col=1; col<=columncount; col++) {
			
			//figure out which image is currently showing
			for (var img=1; img<=imagecount; img++) {

				//calculate the row-col id of the current position
				var checkposition = row+"-"+col+"-";
				
				//determine which image is currently showing
				//alert("checkpos img is " + checkposition+img);
				if (document.getElementById(checkposition+img).className == "show") { 
				
					var currentimage = img; 
					
				}//if	
				
			}//for img
			
			
			//calculate the next image to "show"	
 
		    var nextimage = gridarray[edgedist(row,col)]; 
			

			//Hide the current image, and show the next image
			var hide_image = checkposition+currentimage;
			document.getElementById(hide_image).setAttribute("class", "hide"); 				

			var show_image = checkposition+nextimage;
			document.getElementById(show_image).setAttribute("class", "show"); 				

			
		}//for col

	}//for row

}//function increment()


//################################################################################################################################//
     



