/**************************************************************************
 *   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.             *
 ***************************************************************************/

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

// function to add an event listener

function addEvent(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;
  }
}

var img_index=0;
var gallery_timer = null;
var fade_timer = null;
var show_counter = -1;
var auto_show_flag = false;
var pic_time_delay = 10; // seconds
var image_preloader = null;

function move_abs(n) {
  if(!isNaN(n)) {
    my_max = filelist.length;
    n = (1*n); // force numeric context
    if(n < 0) n = 0;
    if(n >=my_max) n = my_max-1;
    img_index = n;
    image_preloader = new Image(630,420);
    image_preloader.src = basepath + "/" + filelist[img_index];
    if(image_preloader.onLoad) {
        image_preloader.onLoad=move_abs2();
    }
    else {
        move_abs2();
    }
  }
}

function move_abs2() {
  var bg_image = document.getElementById("image").src;
  document.getElementById("bg_image").style.backgroundImage = "url(" + document.getElementById("image").src + ")";
  document.getElementById("image").src = image_preloader.src;
  var sn = (1 + img_index);
  var str = sn + " of " + my_max;
  document.getElementById("imagenum").innerHTML = str;
  document.getElementById("caption").innerHTML = commentlist[img_index];
  var op = (bg_image.length > 0)?0:100;
  fade_image(op);
}

function fade_image(op) {
  var os = document.getElementById("image").style;
  set_transparency(os,op);
  if(op < 100) {
    fade_timer=setTimeout("fade_image(" + (op+4) + ");",30); // 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 + ")";
}


function move_rel(n) {
  nn = img_index + (1*n);
  move_abs(nn);
  return(false);
}

function first() {
  start_auto_show(false);
  move_abs(0);
}

function start_auto_show(arg) {
  if(arg) {
    document.getElementById("auto_start").innerHTML = "Stop auto show";
  }
  else {
    document.getElementById("auto_start").innerHTML = "Start Auto show";
  }
  auto_show_flag = arg;
  show_auto();
}

function show_auto() {
  if(auto_show_flag) {
    show_counter -= 1;
    if(show_counter < 1) {
      show_counter = pic_time_delay;
      img_index += 1;
      img_index %= filelist.length;
      move_abs(img_index);
    }
    document.getElementById("countdown").innerHTML = show_counter;
    gallery_timer=setTimeout("show_auto();",1000); // milliseconds
  }
  else {
    clearTimeout(gallery_timer);
    show_counter = 0;
    document.getElementById("countdown").innerHTML = "";
  }
}

function on_click(e) {
  clearTimeout(gallery_timer);
  clearTimeout(fade_timer);
  var evt=e?e:event;
  var target=evt.target?evt.target:evt.srcElement;
  tid = target.id;
  switch(tid) {
    case "first":
      start_auto_show(false);
    move_abs(0);
    break;
    case "prior":
      start_auto_show(false);
    move_rel(-1);
    break;
    case "next":
      start_auto_show(false);
    move_rel(1);
    break;
    case "last":
      start_auto_show(false);
    move_abs(filelist.length-1);
    break;
    case "auto_start":
      start_auto_show(!auto_show_flag);
    break;
    default:
    if(tid.match(/\d+/)) {
      start_auto_show(false);
      n = (1*tid);
      move_abs(n);
    }
    break;
  }
  return true;
}


