/**************************************************************************
 *   Copyright (C) 2008, Paul Lutus                                        *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/

// function to add an event listener

function add_Event(o,e,f) {
  if (o.addEventListener) {
    o.addEventListener(e,f,false);
    return true;
  }
  else if (o.attachEvent) {
    return o.attachEvent("on"+e,f);
  }
  else {
    return false;
  }
}

add_Event(window,"load",first);
// addEvent(window,"unload",finish);
// addEvent(document,"click",on_click);

var image_index = 0;
var next_image_index = 0;
var image_max = 0;
var gallery_timer;
var fade_timer = null;
var image_loader = null;
var image_path = "/home_page_related/images/";
var banner_image = null;
var banner_background = null;

function first() {
  banner_image = document.getElementById("banner_image");
  banner_background = document.getElementById("banner_background");
  image_loader = new Image();
  image_max = file_list.length;
  image_index = parseInt(Math.random() * image_max);
  show_image(0);
}

function image_click() {
  clearTimeout(gallery_timer);
  clearTimeout(fade_timer);
  show_image(1);
}

function show_image(n) {
  image_index += n;
  next_image_index = image_index + 1;
  image_index %= image_max;
  next_image_index %= image_max;
  image_loader.src = image_path + file_list[next_image_index];
  show_image2();
}

function show_image2() {
  var bg_image = document.getElementById("banner_image").src;
  banner_background.style.backgroundImage = "url(" + banner_image.src + ")";
  banner_image.src = image_path + file_list[image_index];
  var op = (bg_image.length > 0)?0:100;
  fade_image(op);
}

function fade_image(op) {
  var os = banner_image.style;
  set_transparency(os,op);
  if(op < 100) {
    fade_timer=setTimeout("fade_image(" + (op+4) + ");",30); // milliseconds
  }
  else {
    gallery_timer=setTimeout("show_image(1);",8000); // milliseconds
  }
}

function set_transparency(os,op) {
  var op_pct = op / 100.0;
  os.opacity = (op_pct);
  os.MozOpacity = (op_pct);
  os.KhtmlOpacity = (op_pct);
  os.filter = "alpha(opacity=" + op + ")";
}

