
// Some variables to autodetect browser type
var ns=(document.layers);
var ie=(document.all);
var w3=(document.getElementById && !ie);

var timeout=5000;
var help_isshow=0;

// Return an object
function get_object(id)
{
 if(!ns && !ie && !w3) 
   return null;
 if (ie)
   e=eval("document.all." + id);
 else if (ns)
   e=eval('document.links[id]');
 else if (w3)
   e=eval('document.getElementById(id)');
 return e;
}

// Change the current page to the id found in the page
function jumpto(id)
{
  e = get_object(id);
  if ((e != null) && (e.href != null)) {
    if (mytimeout) {
      location.href = e.href + "?slideshow=" + timeout;
    } else {
      location.href = e.href;
    }
  }
}

// Change the current page
function next_page()
{
  jumpto('next_link');
}

function previous_page()
{
  jumpto('previous_link');
}

function show_help_layer()
{
  if (ie)
    help_layer=eval('document.all.helpwindow.style');
  else if (ns)
    help_layer=eval('document.layers["helpwindow"]');
  else if (w3)
    help_layer=eval('document.getElementById("helpwindow").style');

  if (ie)
   {
     documentWidth  =document.body.offsetWidth/2+document.body.scrollLeft-20;
     documentHeight =document.body.offsetHeight/2+document.body.scrollTop-20;
     help_layer.visibility="visible";
   }    
  else if (ns)
   {
     documentWidth=window.innerWidth/2+window.pageXOffset-20;
     documentHeight=window.innerHeight/2+window.pageYOffset-20;
     help_layer.visibility ="show";
   }
  else if (w3)
   {
     documentWidth=self.innerWidth/2+window.pageXOffset-20;
     documentHeight=self.innerHeight/2+window.pageYOffset-20;
     help_layer.visibility="visible";
   }
  help_layer.left=documentWidth-250;
  help_layer.top =documentHeight-125;
  help_isshow=1;
}

function hide_help_layer()
{
  if (ie||w3)
    help_layer.visibility="hidden";
  else
    help_layer.visibility="hide";
  help_isshow=0;
  return false;
}

function toggle_help_layer()
{
  if (help_isshow)
   hide_help_layer();
  else
   show_help_layer();
  return false;
}


// Activate/Deactivate the slideshow
var mytimeout = 0;
function toggle_slideshow()
{
  if (!mytimeout)
   {
     mytimeout = setTimeout("next_page()",timeout);
     window.status='Slideshow set to ' + (timeout/1000) + ' seconds';
     e = get_object('slideicon');
     if ((e != null))
       e.src = "player_pause.png";
   }
  else
   {
     clearTimeout(mytimeout);
     mytimeout=0;
     window.status='Stopping Slideshow';
     e = get_object('slideicon');
     if ((e != null))
       e.src = "player_play.png";
   }
}

// Manage timeout for the slideshow
function modify_timeout(t)
{
  timeout+=t;
  if (timeout<1000)
    timeout=1000;
  if (mytimeout)
  { // If the counter is active, reactivate it !
    toggle_slideshow();
    toggle_slideshow();
  }
  else
  {
     window.status='Slideshow timeout set to ' + (timeout/1000) + ' seconds';
  }
}

// Event Handler that receive Key Event
function getkey(e)
{
  if (e == null)
   { // IE
     kcode = window.event.keyCode;
   } 
  else
   { // Mozilla
     kcode = e.which;
   }
  key = String.fromCharCode(kcode).toLowerCase();
  switch(key)
   {
     case "n":
     case " ":
       next_page();
       return false;
     case "p":
       previous_page();
       return false;
     case "s":
       toggle_slideshow();
       return false;
     case "+":
       modify_timeout(1000);
       return false;
     case "-":
       modify_timeout(-1000);
       return false;
     case "i":
       toggle_exif_window()
       return false;
     case "h":
     case "?":
       toggle_help_layer();
       return false;
   }
  switch(kcode)
   {
     case 8:
       previous_page();
       return false;
   }
  return true;
}

function common_init()
{
  if (typeof(exif_init) == "function")
    exif_init()
  // Test if the slideshow is active
  var argstr = location.search.substring(1, location.search.length)
  var args = argstr.split('&');
  for (var i = 0; i < args.length; i++)
  {
    var arg = unescape(args[i]).split('=');
    if (arg[0] == "slideshow") 
     { // ... and set timeout according to the last value
       timeout=parseInt(arg[1]);
       toggle_slideshow();
     }
  }
  // Some code for preloading the next image
  e = get_object('next_image');
  if ((e != null) && (e.href != null))
  {
    preload_image = new Image();
    preload_image.src = e.href;
  }
}

if(w3 || ie)
{
  document.onkeypress = getkey;
} 
else
{
  document.captureEvents(Event.KEYUP);
  document.onkeyup = getkey; 
  document.captureEvents(Event.KEYPRESS);
  document.onkeypress = getkey;
}


