$(window).load(function(){


function getPictureSrc(picture) {
return 'images/' + picture.src;
}

function getPictureTitle(picture) {
return picture.title;
}

function rand () {
//        Algorithm AS 183  Appl. Statist. (1982) Vol.31, No.2 
//        as amended by 
//        Remark AS R58 Appl. Statist. (1985) Vol.34, No.2 

 
        x = (x * 171) % 30269;
        y = (y * 172) % 30307;
        z = (z * 170) % 30323;
 
        return (x / 30269 + y / 30307 + z/ 30323)%1;
       
}
    


//  Generate seed from current time in seconds

    date = new Date();
    z = 1 + (date.getSeconds() + date.getMinutes()*60 
              + date.getHours()*3600) % 29998;


    for (i = 0; i < 3; i++)
	nextIndex [i] = new Array(pic[i].length);


    for (i = 0; i < 3; i++)
    for (j = 0; j < pic[i].length; j++) {
    nextIndex[i][j] = j;
    }


    for (i = 0; i < 3; i++) {
    for (j=0; j<pic[i].length; j++) {
        k = Math.floor((j+1)*rand());   
    
        dummy = nextIndex[i][j];
        nextIndex[i][j] = nextIndex[i][k];
        nextIndex[i][k] = dummy;

        }

    }

// Get 0 index pictures to front of queue

    for (i = 0; i < 3; i++)
       for (j = 0; j < pic[i].length; j++) 
       if (nextIndex[i][j] == 0 )
       {
            dummy = nextIndex[i][j];
            nextIndex[i][j] = nextIndex[i][0];
            nextIndex[i][0] = dummy;
       }
            

    window.setInterval(function() {

    for (i = 0; i < 3; i++) {
            
       
    $('#pic' + (2*i + 1)).attr({
                 src: getPictureSrc(pic[i][nextIndex[i][indexCycle[i]]]),
                 title: getPictureTitle(pic[i][nextIndex[i][indexCycle[i]]]),
                 alt: getPictureTitle(pic[i][nextIndex[i][indexCycle[i]]])}).show().fadeOut(5000);

    if (++indexCycle[i] == pic[i].length) indexCycle[i]=0;
                   
    $('#pic' + (2*i + 2)).attr({
                 src: getPictureSrc(pic[i][nextIndex[i][indexCycle[i]]]),
                 title: getPictureTitle(pic[i][nextIndex[i][indexCycle[i]]]),
                 alt: getPictureTitle(pic[i][nextIndex[i][indexCycle[i]]])}).hide().fadeIn(5000);

 
    }        
         },8000);


    $('html.js').css("visibility","visible");
});

function picture(pictureSrc,pictureTitle) {
    this.src = pictureSrc;
    this.title = pictureTitle;
    this.alt = pictureTitle;
}



var pic = new Array(3);
pic[0] = [new picture("DSCN0683.jpg","The Autumn Produce Show Secretary 09"), new picture("DSC03831.jpg","Magical Mystery Tour to Highgrove Gardens!"), new picture("DSCN0680.jpg","Autumn Produce Show 09"), 
          new picture("DSCN0670.jpg","Autumn Produce Show 09"), new picture("ydgsparty1.jpg","A YDGS Family Party!"), 
          new picture("DSCN0674.jpg","Autumn Produce Show 09"), new picture("timberlea1.jpg","Timberlea Evening Visit"), new picture("johnnegussb.jpg", "John Negus giving a lively talk on Summers Bounty!"),
          new picture("DSCN0669.jpg","Autumn Produce Show 09"), new picture("timberlea4.jpg","Timberlea Evening Visit"),new picture("ydgsparty2.jpg","A YDGS Family Party!"), 
          new picture("yateleybloom2.jpg","Yateley in Bloom Winners!"), new picture("DSC01137.jpg","Autumn Produce Show 09"), new picture("flowerarrangement.jpg","Flower Arrangements")];
pic[1] = [new picture("davidandcherie2.jpg","David and Cherie at Yateley May Fair 2011 - image courtesy of Surrey Advertiser"),new picture("mayfair2.jpg","Yateley May Fair"), new picture("timberlea2.jpg","Timberlea Evening Visit"),new picture("hangbasketworkshop1.jpg","Hanging Basket Workshop"), new picture("waggy.jpg","The Dog with the Waggiest Tail!"),
          new picture("DSC01143.jpg","Chairman's entry for Autumn Produce Show 09 - The Full Monty!!"), new picture("smallteam.jpg","Westfields Infants Courtyard Garden"), 
        new picture("Mottisfont.jpg","Mottisfont Abbey"), new picture("ydgsparty4.jpg","A YDGS Family Party!"), new picture("DSC01145.jpg","Autumn Produce Show 09"), new picture("yateleybloom1.jpg","Yateley in Bloom"), new picture("matthewbiggs.jpg","Matthew Biggs and friends!"), 
        new picture("bonsai.jpg","Bonsai Class Section"), new picture("DSCN0671.jpg","Autumn Produce Show 09"), new picture("DSCN0689.jpg","Autumn Produce Show 09"), new picture("DSCN0681.jpg","Autumn Produce Show 09"), new picture("firstprizecake.jpg","Autumn Produce Show 09")];
pic[2] = [new picture("Mottisfont2.jpg","Mottisfont Abbey"), new picture("mayfair1.jpg","Yateley May Fair"), new picture("hangbasketworkshop2.jpg","Hanging Basket Workshop"), new picture("DSC01139.jpg","The Autumn Produce Show Secretary and fans!!"), new picture("DSCN0676.jpg","Autumn Produce Show 09"), new picture("quiznighthenrystreet.jpg","Quiz Night at the Henry Street Garden Centre"), new picture("DSCN0679.jpg","Autumn Produce Show 09"), new picture("mayfair3.jpg","Yateley May Fair"), new picture("DSCN0684.jpg","Autumn Produce Show 09"), 
        new picture("Mottisfont3.jpg","Mottisfont Abbey"), new picture("timberlea3.jpg","Timberlea Evening Visit"),new picture("ydgsparty3.jpg","A YDGS Family Party!"), new picture("Mottisfont Abbey.jpg","Mottisfont Abbey"), new picture("mayfair4.jpg","Yateley May Fair"), new picture("DSC01138.jpg","Autumn Produce Show 09"),new picture("timberlea5.jpg","Timberlea Evening Visit")];
var nextIndex = new Array(3);
var i,j,k;
var x = 10000, y = 20000, z;
var dummy,date;
var indexCycle =  new Array(0,0,0);








