// This file contains JS routines to load and fade-in the filmstrip images
// at the top and bottom of screen


//Declarations
var num_pics = 6;
var num_loaded = 3;
var filenames = new Array(num_pics);
var check_loaded_ti;
var image_cache = new Array(num_pics);
var current_file_num = 0;
var max_pics = 0;
var display_pics_ti;
var isMoz = 0;
var obj_name = "picplace1";
var action = "fade";
var interval = 3750;
var doing_slide = false;
var loading_shown = true;


// 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()
{
  // Loads first 3 images into image_cache array

  filenames[0] = './fade_pics/bedroom2.jpg';
  filenames[1] = './fade_pics/kitchen2.jpg';
  filenames[2] = './fade_pics/lounge.jpg';
  filenames[3] = './fade_pics/washstand.jpg';
  filenames[4] = './fade_pics/fireplace.jpg';
  filenames[5] = './fade_pics/fronthouse.jpg';
  filenames[6] = './fade_pics/bench.jpg';
  filenames[7] = './fade_pics/bath.jpg';

  obj_name = "picplace1";
  action = "fade";
  num_pics = 8;



  // Load each picture into Cache array
  for (i=0 ; i < 3 ; i++) {
    image_cache[i] = new Image;
    image_cache[i].src = filenames[i];
    //add the event listener for the load event - then call our onload function
    if (isMoz) {
      image_cache[i].addEventListener("load", _Image_onload, false);
    }
  }
}

function load_first_three_facilities()
{
  // Loads first 3 images into image_cache array

  filenames[0] = './fade_pics/deck.jpg';
  filenames[1] = './fade_pics/garden.jpg';
  filenames[2] = './fade_pics/kids_bedroom.jpg';
  filenames[3] = './fade_pics/creek.jpg';
  filenames[4] = './fade_pics/kitchen.jpg';
  filenames[5] = './fade_pics/deck2.jpg';
  filenames[6] = './fade_pics/deck-from-below.jpg';
  filenames[7] = './fade_pics/outside.jpg';

  obj_name = "picplace1";
  action = "fade";
  num_pics = 8;



 // Load each picture into Cache array
  for (i=0 ; i < 3 ; i++) {
    image_cache[i] = new Image;
    image_cache[i].src = filenames[i];
    //add the event listener for the load event - then call our onload function
    if (isMoz) {
      image_cache[i].addEventListener("load", _Image_onload, false);
    }
  }
}


function load_first_three_testimonials()
{
  // Loads first 3 images into image_cache array

  filenames[0] = './images/testimonial6.gif';
  filenames[1] = './images/testimonial2.gif';
  filenames[2] = './images/testimonial3.gif';
  filenames[3] = './images/testimonial4.gif';
  filenames[4] = './images/testimonial5.gif';
  filenames[5] = './images/testimonial1.gif';
  filenames[6] = './images/testimonial7.gif';
  filenames[7] = './images/testimonial8.gif';

  obj_name = "testimonial_pic";
  action = "slide";
  interval =4500;
  num_pics = 8;

  // Load each picture into Cache array
  for (i=0 ; i < 3 ; i++) {
    image_cache[i] = new Image;
    image_cache[i].src = filenames[i];
    //add the event listener for the load event - then call our onload function
    if (isMoz) {
      image_cache[i].addEventListener("load", _Image_onload, false);
    }
  }
}

function load_next_three()
{
  // Load each picture into Cache array
  for (i=3 ; i < num_pics ; i++) {
    image_cache[i] = new Image;
    image_cache[i].src = filenames[i];
    if (isMoz) {
      image_cache[i].addEventListener("load", _Image_onload, false);
    }
  }
}


function setOpacity(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(objId,opacity) {
  if (document.getElementById) {
    obj = document.getElementById(objId);
    if (opacity <= 100) {
      setOpacity(obj, opacity);
      opacity += 8;
      window.setTimeout("fadeIn('"+objId+"',"+opacity+")", 70);
    }
  }
}


function slide_in(objId,new_width) {
  doing_slide = true;
  if (document.getElementById) {
    obj = document.getElementById(objId);
    if (new_width < 500) {
      obj.style.width=new_width;
      new_width += 10;
      window.setTimeout("slide_in('"+objId+"',"+new_width+")", 30);
    }
    else {
      doing_slide = false;
      //document.body.style.cursor=url(./images/frog.cur);
    }
  }

}

function show_pic()
{

  if (loading_shown) {
    // Get loading img
    if (document.getElementById) {
      pic_obj = document.getElementById("loading");
    }
    else {
      pic_obj = document["loading"];
    }

    if (pic_obj) {
      pic_obj.style.visibility='hidden';
    }

    loading_shown = false;
  }

  // Get image object
  pic_id = obj_name;
  if (document.getElementById) {
    pic_obj = document.getElementById(pic_id);
  }
  else {
    pic_obj = document[pic_id];
  }


  if (action=="fade") {
  if (isMoz) {
    pic_obj.src=filenames[current_file_num];
    setOpacity(pic_obj,10);
    fadeIn(pic_id,10);
  } else {
    pic_obj.filters.blendTrans.apply();
    pic_obj.src=filenames[current_file_num];
    pic_obj.filters.blendTrans.play();
  }

  }
  else {
    if (doing_slide == false) {
      pic_obj.style.background = 'url('+filenames[current_file_num]+')';
      slide_in(pic_id,10);
    }
  }

  current_file_num = current_file_num + 1;
  if (current_file_num >= num_loaded)
    current_file_num = 0;


}


function display_film_images()
{
  // This displays each one in turn and fades it in
  if (isMoz) {
//    if (action!="slide") {window.setTimeOut('show_pic();',1500);}
        display_pics_ti = window.setInterval('show_pic();',interval+500);
  }
   else {
//  if (action!="slide") {window.setTimeout('show_pic();',1500);}
    display_pics_ti = window.setInterval('show_pic();',interval);
  }

// interval = interval + 3000;

}


function check_next_three_loaded()
{
  // Look for ONE image that has not finished loading yet

  i =3;
  found_one = "N";
  while (i < num_pics && found_one=="N" ) {
    cachepic_obj = image_cache[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);
    window.clearInterval(check_loaded_ti);
    num_loaded = num_pics;
    display_film_images();
  }

}


function check_first_three_loaded()
{

  // Look for ONE image that has not finished loading yet
  i = 0;
  found_one = "N";
  while (i < 3 && found_one=="N" ) {
    cachepic_obj = image_cache[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 = 3;
    window.clearInterval(check_loaded_ti);
    window.setTimeout('show_pic();',1000);
    load_next_three();
    check_loaded_ti = window.setInterval("check_next_three_loaded()",200);
    display_film_images();
  }

}


function run_film_strip()
{

  // First load all images into image cache
  load_first_three();

  // 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 = window.setInterval("check_first_three_loaded()",200);

}



function run_film_strip_facilities()
{

  // First load all images into image cache
  load_first_three_facilities();

  // 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 = window.setInterval("check_first_three_loaded()",200);

}

function run_film_strip_testimonials()
{

  // First load all images into image cache
  load_first_three_testimonials();

  // 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 = window.setInterval("check_first_three_loaded()",50);
}