/*
 * Oog Photo-Gallery v3.0
 * 2008 von Torben Rottbrand
 */

init();

window.onresize = function() {

  if(document.getElementById('lightbox').style.visibility == 'visible') resizelightbox();

}

window.document.onkeydown = function(event)  { 

  if(!event) event = window.event; 

  var keycode = event.which || event.keyCode; 

  if(keycode==37 && document.getElementById('lightbox').style.visibility=='visible') previous();
  if(keycode==39 && document.getElementById('lightbox').style.visibility=='visible') next();
  if(keycode==27) exitlightbox();
  if(keycode==73 && document.getElementById('lightbox').style.visibility=='visible') info();

}

function init() {

  slideshow = 0;

  firsttitle=document.title;

  document.writeln("<img src=\"../imgdata/stopslide.gif\" border=\"0\" height=\"0\" width=\"0\">");
  document.writeln("<div class=\"overlay\" id=\"overlay\"><\/div>");
  document.writeln("<div class=\"loading\" id=\"loading\">");
  document.writeln("<img src=\"../imgdata/loading.gif\" style=\"margin-top: 65px;\">");
  document.writeln("<p style=\"font: normal 12px Tahoma,sans-serif;margin-top:0px;\">Press ESC to cancel loading<\/p>");
  document.writeln("<\/div>");
  document.writeln("<div class=\"lightbox\" id=\"lightbox\">");
  document.writeln("<table height=\"100%\" width=\"100%\" border=\"0\">");
  document.writeln("<tr>");
  document.writeln("<th>");
  document.writeln("<img id=\"photo\">");
  document.writeln("<\/th>");
  document.writeln("<\/tr>");
  document.writeln("<tr width=\"100%\" height=\"35px\">");
  document.writeln("<th>");
  document.writeln("<a href=\"javascript: info();\"><img src=\"../imgdata/info.gif\" id=\"infoimg\" style=\"bottom: 5px;\" border=\"0\" title=\"Infos | i\"><\/a>");
  document.writeln("<a href=\"javascript: previous();\"><img src=\"../imgdata/left.gif\" style=\"margin:5px;\" border=\"0\" title=\"Previous Photo | Left Arrow Key\"><\/a>");
  document.writeln("<a href=\"javascript: slide('start');\" id=\"slidebutton\"><img src=\"../imgdata/startslide.gif\" id=\"slideimg\" border=\"0\" title=\"Start Slideshow\"><\/a>");
  document.writeln("<a href=\"javascript: next();\"><img src=\"../imgdata/right.gif\" style=\"margin:5px;\" border=\"0\" title=\"Next Photo | Right Arrow Key\"><\/a>");
  document.writeln("<a href=\"javascript: exitlightbox();\"><img src=\"../imgdata/close.gif\" id=\"closeimg\" style=\"bottom: 5px;\" border=\"0\" title=\"Close | Esc\"><\/a>");
  document.writeln("<\/th>");
  document.writeln("<\/tr>");
  document.writeln("<\/table>");
  document.writeln("<\/div>");
  document.writeln("<style type=\"text/css\">");
  document.writeln(".loading {");
  document.writeln("  text-align: center;");
  document.writeln("  position: fixed;");
  document.writeln("  z-index: 3;");
  document.writeln("  visibility: hidden;");
  document.writeln("  left: 50%;");
  document.writeln("  top: 50%;");
  document.writeln("  height: 150px;");
  document.writeln("  width: 300px;");
  document.writeln("  margin-left: -150px;");
  document.writeln("  margin-top: -75px;");
  document.writeln("  color: #FFF;");
  document.writeln("}");
  document.writeln(".overlay {");
  document.writeln("  position: fixed;");
  document.writeln("  z-index: 2;");
  document.writeln("  visibility: hidden;");
  document.writeln("  left: 0px;");
  document.writeln("  top: 0px;");
  document.writeln("  width: 0px;");
  document.writeln("  height: 0px;");
  document.writeln("  background-color: #000;");
  document.writeln("  -moz-opacity: 0.7;");
  document.writeln("  opacity:.70;");
  document.writeln("  filter: alpha(opacity=70);");
  document.writeln("}");
  document.writeln(".lightbox {");
  document.writeln("  position: fixed;");
  document.writeln("  z-index: 4;");
  document.writeln("  visibility: hidden;");
  document.writeln("  left: 6%;");
  document.writeln("  top: 4%;");
  document.writeln("  width: 88%;");
  document.writeln("  height: 94%;");
  document.writeln("}");
  document.writeln("</style>");

}

function startlightbox(albumpath,photoname) {

  album=albumpath;

  document.getElementById('lightbox').style.left='-4999';
  document.getElementById('lightbox').style.top='-4999';

  document.getElementById('loading').style.visibility='visible';
  document.getElementById('overlay').style.height = '100%';
  document.getElementById('overlay').style.width = '100%';

  if(navigator.appVersion.indexOf("MSIE 6")>-1) {
    document.getElementById('overlay').style.height = window.document.documentElement.clientHeight;
    document.getElementById('overlay').style.width = window.document.documentElement.clientWidth;
  }

  document.getElementById('overlay').style.visibility='visible';
  document.getElementById('photo').src=albumpath + photoname;
  document.getElementById('photo').name=photoname;

  document.title=firsttitle + ' - ' + photoname;

  document.getElementById('photo').onload=function() {

    document.getElementById('loading').style.visibility='hidden';
    document.getElementById('lightbox').style.visibility='visible';

    firstimgheight = document.getElementById('photo').height;
    firstimgwidth = document.getElementById('photo').width;

    resizelightbox();

    document.getElementById('lightbox').style.left='6%';
    document.getElementById('lightbox').style.top='4%';

    if(slideshow==1) timeout=window.setTimeout('next()',5000);

  }

}

function resizelightbox() {

  var windowheight = (window.innerHeight) ? window.innerHeight : window.document.documentElement.clientHeight;
  var windowwidth = (window.innerWidth) ? window.innerWidth : window.document.documentElement.clientWidth;

  var heightspace = (Math.round(windowheight/100*94))-45;
  var widthspace = (Math.round(windowwidth/100*88))-10;

  document.getElementById('photo').height = heightspace;    
  document.getElementById('photo').width = document.getElementById('photo').height * (firstimgwidth/firstimgheight);

  if(document.getElementById('photo').width > widthspace) {

    document.getElementById('photo').width = widthspace;
    document.getElementById('photo').height = document.getElementById('photo').width * (firstimgheight/firstimgwidth);

  }

  document.getElementById('closeimg').style.marginLeft=((document.getElementById('photo').width)/2-90) + 'px';
  document.getElementById('infoimg').style.marginRight=((document.getElementById('photo').width)/2-90) + 'px';

  if(navigator.appVersion.indexOf("MSIE 6")>-1) {
    document.getElementById('overlay').style.height = window.document.documentElement.clientHeight;
    document.getElementById('overlay').style.width = window.document.documentElement.clientWidth;
  }

}

function exitlightbox() {

  if(slideshow==1) slide('stop');

  document.title=firsttitle;

  document.getElementById('lightbox').style.visibility='hidden';

  document.getElementById('loading').style.visibility='hidden';

  document.getElementById('overlay').style.visibility='hidden';
  document.getElementById('overlay').style.height='0';
  document.getElementById('overlay').style.width='0';

  document.getElementById('photo').removeAttribute('name');
  document.getElementById('photo').removeAttribute('src');
  document.getElementById('photo').removeAttribute('height');
  document.getElementById('photo').removeAttribute('width');

}

//Dont wonder about this strange code. It tricks the onload-bug in Opera and IE6.
function showphoto(albumpath,photo) {

  startlightbox(albumpath,photo);
  exitlightbox();

  document.getElementById('lightbox').style.visibility='hidden';

  document.getElementById('photo').removeAttribute('name');
  document.getElementById('photo').removeAttribute('src');
  document.getElementById('photo').removeAttribute('height');
  document.getElementById('photo').removeAttribute('width');

  startlightbox(albumpath,photo);

}

function previous() {

  if(slideshow==1) window.clearTimeout(timeout);

  var count=document.getElementsByName('thumb').length;
  var i=0;

  while(i<count) {

    if(document.getElementsByName('thumb')[i].title==document.getElementById('photo').name) {

      var previousphoto = (i==0) ? document.getElementsByName('thumb')[count-1].title : document.getElementsByName('thumb')[i-1].title;
      break;

    }

    i++;

  }

  document.getElementById('lightbox').style.visibility='hidden';

  document.getElementById('photo').removeAttribute('name');
  document.getElementById('photo').removeAttribute('src');
  document.getElementById('photo').removeAttribute('height');
  document.getElementById('photo').removeAttribute('width');

  startlightbox(album,previousphoto);

}

function next() {

  if(slideshow==1) window.clearTimeout(timeout);

  var count=document.getElementsByName('thumb').length;
  var i=0;

  while(i<count) {

    if(document.getElementsByName('thumb')[i].title==document.getElementById('photo').name) {

      var nextphoto = (i==count-1) ? document.getElementsByName('thumb')[0].title : document.getElementsByName('thumb')[i+1].title;
      break;

    }

    i++;

  }

  document.getElementById('lightbox').style.visibility='hidden';

  document.getElementById('photo').removeAttribute('name');
  document.getElementById('photo').removeAttribute('src');
  document.getElementById('photo').removeAttribute('height');
  document.getElementById('photo').removeAttribute('width');

  startlightbox(album,nextphoto);

}

function slide(action) {

  if(action=='start') {

    slideshow=1;

    document.getElementById('slideimg').src='../imgdata/stopslide.gif';
    document.getElementById('slidebutton').href="javascript: slide('stop');";
    document.getElementById('slideimg').title="Stop Slideshow";

    timeout=window.setTimeout('next()',5000);

  }

  if(action=='stop') {

    slideshow=0;

    document.getElementById('slideimg').src='../imgdata/startslide.gif';
    document.getElementById('slidebutton').href="javascript: slide('start');";
    document.getElementById('slideimg').title="Start Slideshow";

    window.clearTimeout(timeout);

  }

}

function info() {

  alert('This function will be added in the next version');

}