// This file contains JS routines to load and fade-in the filmstrip images
// at the top and bottom of screen


//Declarations
var num_pics_home = 8;
var num_loaded_home = 3;
var first_stage = 3;
var filenames_home = new Array(num_pics_home);
var text_donations = new Array(num_pics_home);
var check_loaded_ti_home;
var image_cache_home = new Array(num_pics_home);
var current_file_num_home = 0;
var max_pics = 0;
var display_pics_ti_home;
var isMoz = 0;
var interval = 3000;


// Extra bits for Mozilla
if (document.all) {
  isMoz = 0;
} else {
  isMoz = 1;
}


// If this is Mozilla, use our Load function
if (isMoz) {
  Image.prototype.readyState = "0";
  Image.prototype.__load__ = Image.prototype.load;
  Image.prototype.load = _Image_load;
}

//This is called when it starts loading
function _Image_load(strURL) {
    //change the readyState
    this.readyState = 1;

    //call the original load method
    this.__load__(strURL);
}

// This is called when it's finished loading
function _Image_onload() {
    //change the readyState
    this.readyState = 4;
}


function load_first_three_home()
{
  // Loads first 3 images into image_cache_home array

  filenames_home[0] = './images/donation_scouts.jpg';
  filenames_home[1] = './images/donation_hospital.jpg';
  filenames_home[2] = './images/donation_barung.jpg';
  filenames_home[3] = './images/donation_highschool.jpg';
  filenames_home[4] = './images/donation_chap.jpg';
  filenames_home[5] = './images/donation_primary.jpg';
  filenames_home[6] = './images/donation_fire.jpg';
  filenames_home[7] = './images/donation_robandsam.jpg';

//  text_donations[0] = 'Maleny Scouts $1234';
 // text_donations[1] = 'Chappy $999';
//  text_donations[2] = 'Barung Landcare $2345';
//  text_donations[3] = '120 COMMUNITY GROUPS';
//  text_donations[4] = 'OVER $140,000 IN TOTAL!';


  // Load each picture into Cache array
  for (i=0 ; i < first_stage ; i++) {
    image_cache_home[i] = new Image;
    image_cache_home[i].src = filenames_home[i];
    //add the event listener for the load event - then call our onload function
    if (isMoz) {
      image_cache_home[i].addEventListener("load", _Image_onload, false);
    }
  }
}



function load_next_three_home()
{
  // Load each picture into Cache array
  for (i=first_stage ; i < num_pics_home ; i++) {
    image_cache_home[i] = new Image;
    image_cache_home[i].src = filenames_home[i];
    if (isMoz) {
      image_cache_home[i].addEventListener("load", _Image_onload, false);
    }
  }
}


function setOpacity_home(obj, opacity) {
  opacity = (opacity >= 100)?99.999:opacity;

  // IE/Win
  obj.style.filter = "alpha(opacity:"+opacity+")";

  // Safari<1.2, Konqueror
  obj.style.KHTMLOpacity = opacity/100;

  // Older Mozilla and Firefox
  obj.style.MozOpacity = opacity/100;

  // Safari 1.2, newer Firefox and Mozilla, CSS3
  obj.style.opacity = opacity/100;
}


function fadeIn_home(objId,opacity) {
  if (document.getElementById) {
    obj = document.getElementById(objId);
    if (opacity <= 100) {
      setOpacity_home(obj, opacity);
      opacity += 8;
      window.setTimeout("fadeIn_home('"+objId+"',"+opacity+")", 70);
    }
  }
}


function show_pic_home()
{

  // Get image object
  pic_id = "donation-pic";
  if (document.getElementById) {
    pic_obj = document.getElementById(pic_id);
  }
  else {
    pic_obj = document[pic_id];
  }


  if (isMoz) {
    pic_obj.src=filenames_home[current_file_num_home];
    setOpacity_home(pic_obj,10);
    fadeIn_home(pic_id,10);
  } else {
    pic_obj.filters.blendTrans.apply();
    pic_obj.src=filenames_home[current_file_num_home];
    pic_obj.filters.blendTrans.play();
  }

  // Set TEXT as well
//  text_id = "donations-bottom";
//  if (document.getElementById) {
//    text_obj = document.getElementById(text_id);
//  }
//  else {
//    text_obj = document[text_id];
//  }

//  text_obj.innerHTML = text_donations[current_file_num_home];



  current_file_num_home = current_file_num_home + 1;
  if (current_file_num_home >= num_loaded_home)
    current_file_num_home = 0;

}


function display_film_images_home()
{
  // This displays each one in turn and fades it in
//  setTimeout('show_pic_home();',1500);
  if (isMoz) {
    display_pics_ti_home = window.setInterval('show_pic_home();',interval);
  }
  else {
    display_pics_ti_home = window.setInterval('show_pic_home();',interval);
  }
}


function check_next_three_loaded_home()
{
  // Look for ONE image that has not finished loading yet

  i =first_stage;
  found_one = "N";
  while (i < num_pics_home && found_one=="N" ) {
    cachepic_obj = image_cache_home[i];
   if ( (cachepic_obj.readyState != "complete") && (cachepic_obj.readyState != 4) ){
      found_one = "Y";
    } else {
      i++;
    }
  }

  if (found_one=="N") {
    // ...then they have all finished loading
    // call function to display the images
//    window.clearInterval(display_pics_ti_home);
    window.clearInterval(check_loaded_ti_home);
    num_loaded_home = num_pics_home;
//    display_film_images_home();
  }

}


function check_first_three_loaded_home()
{
  // Look for ONE image that has not finished loading yet
  i = 0;
  found_one = "N";
  while (i < first_stage && found_one=="N" ) {
    cachepic_obj = image_cache_home[i];
    if ( (cachepic_obj.readyState != "complete") && (cachepic_obj.readyState != 4) ){
      found_one = "Y";
    } else {
      i++;
    }
  }

  if (found_one=="N") {
    // ...then they have all finished loading
    // call function to display the images
    num_loaded_home = first_stage;
    window.clearInterval(check_loaded_ti_home);
    load_next_three_home();
    check_loaded_ti_home = window.setInterval("check_next_three_loaded_home()",200);
    display_film_images_home();
  }

}



function run_film_strip_donations()
{
  // First load all images into image cache
  load_first_three_home();

  // Now loop every 200 ms - keep checking whether they've all loaded
  // When they have all loaded, the check_all_loaded function
  // calls display_film_images which displays them one at a time
  check_loaded_ti_home = window.setInterval("check_first_three_loaded_home()",50);
}
