//  
// Run gallery on the front page (top.html) 
//

var bp = 34;  // big pictures cnt (without zero)
var sp = 8;   // small pictures cnt (without zero)
var fa = 5;   // every x cycles a new big picture


var po = 4;   // offset into first gallery position..damn javascript junk
var cy = 0;   // cycles
var iv = 2001;// interval
var i,wi,lb;  //

var PL = new Array();
var BL = new Array();

// ---

function randomInt(min, max){
 if (min >= max) return;
   return Math.floor(((max+1-min) * Math.random())+min);
}


function stopshow(){ cy = 999999;  }


function runshow(){

 var a,b;

  if (cy > 777777){ // break sequence here
    window.clearInterval(wi);
    //  finalview();
     if (!isNS4)
	delete PL;
    return; // stop forever
  }
  // get a new pig picture: 

  if (cy % fa == 0){
    lb = lb + 1;
       if (lb > bp)
         lb = 1;
    document.images[po + 3].src = '../gallery/' + BL[lb] + '.jpg';
  }
  else {
    PL[0] = randomInt(0,sp);   
    do { PL[1] = randomInt(0,sp); } while (PL[0] == PL[1]);
    do { PL[2] = randomInt(0,sp); } while ((PL[0] == PL[2])||(PL[1] == PL[2]));
      for (a = 0; a < 3; a++)
        document.images[po + a].src = '../gallery/sw/' + PL[a] + '.jpg';
   } // make three different images
 cy++;
}



function nextbigpicture()

{
  window.clearInterval(wi);	
  cy = 0;
  runshow();
  wi = window.setInterval("runshow()",iv);
}



function nexticon(idx){
  PL[idx] = randomInt(0,sp);
  document.images[po + idx].src = '../gallery/sw/' + PL[idx] + '.jpg';
     if ((PL[0] == PL[1]) && (PL[1] == PL[2])){ // *lol*
       	document.images[po + 3].src = '../gallery/easteregg.jpg';
	window.clearInterval(wi);
	cy = 1;
	wi = window.setInterval("runshow()",iv);	
     }
}


function shake(){
  for (i = 0; i <= bp; i++)
    BL[i] = 0;
  for (i = 0; i <= bp; i++){
     while (1){
       j = randomInt(1,bp);
         if (BL[j] == 0){
           BL[j] = i;
           break;
         }
     }  
  }
}


// --- main()

 for (i = 0; i <= sp; i++)
  PL[i] = 0;

shake(); // einmal initial wrfeln und eine zufallsverteilung der bilder herstellen.

lb = randomInt(1,bp); // big picture index
runshow();
wi = window.setInterval("runshow()",iv);
