
/*
 * Oog Photo-Gallery v3.1 beta
 * 2009 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() {

  document.write(html_lightbox);

  slideshow = 0;
  firsttitle = document.title;
  first_lightbox_left = document.getElementById('lightbox').style.left;
  first_lightbox_top = document.getElementById('lightbox').style.top;
  first_lightbox_height = document.getElementById('lightbox').style.height;
  first_lightbox_width = document.getElementById('lightbox').style.width;

}

function startlightbox(albumpath,photoname) {

  album=albumpath;

  document.getElementById('overlay').style.position='fixed';
  document.getElementById('lightbox').style.position='fixed';

  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=escape(albumpath) + escape(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=first_lightbox_left;

    if(windowheight<(document.getElementById('photo').height+90) || windowwidth<(document.getElementById('photo').width+2*(document.body.scrollWidth/100*parseInt(first_lightbox_left))))
      document.getElementById('lightbox').style.top=(document.documentElement.scrollTop+10) + 'px';

    if(slideshow==1) timeout=window.setTimeout('next()',timeout_slideshow);

  }

}

function resizelightbox() {

  windowheight = (window.innerHeight) ? window.innerHeight : window.document.documentElement.clientHeight;
  windowwidth = (window.innerWidth) ? window.innerWidth : window.document.documentElement.clientWidth;

  var heightspace = (Math.round(windowheight/100*parseInt(first_lightbox_height)))-55;
  var widthspace = (Math.round(windowwidth/100*parseInt(first_lightbox_width)))-25;

  if(resize_photos=='all') {

    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);
    }

  } else if(resize_photos=='fixed') {

    document.getElementById('photo').height = fixed_photo_height;
    document.getElementById('photo').width = fixed_photo_width;

  } else if(resize_photos=='only_to_bigs') {

    if(document.getElementById('photo').height>heightspace || document.getElementById('photo').width>widthspace) {
      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);
      }
    }

  } else if(resize_photos=='only_to_smalls') {

    if(document.getElementById('photo').height<heightspace || document.getElementById('photo').width<widthspace) {
      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);
      }
    }

  } else {}

  if(windowheight<(document.getElementById('photo').height+90) || windowwidth<(document.getElementById('photo').width+2*(document.body.scrollWidth/100*parseInt(first_lightbox_left)))) {

    document.getElementById('lightbox').style.position='absolute';
    document.getElementById('overlay').style.position='absolute';

    var body_height = (window.opera) ? document.body.scrollHeight : document.documentElement.scrollHeight;
    var body_width = (window.opera) ? (document.documentElement.scrollWidth-4999) : document.documentElement.scrollWidth;

    if(body_height>(document.getElementById('photo').height+90)) {
      document.getElementById('overlay').style.height=(body_height+50) + 'px';
    } else {
      document.getElementById('overlay').style.height=(document.getElementById('photo').height+100) + 'px';
    }
    if(body_width>(document.getElementById('photo').width+2*(body_width/100*parseInt(first_lightbox_left)))) {
      document.getElementById('overlay').style.width=(body_width) + 'px';
    } else {
      document.getElementById('overlay').style.width=(document.getElementById('photo').width+2*(body_width/100*parseInt(first_lightbox_left))) + 'px';
    }

  } else {

    document.getElementById('overlay').style.position='fixed';
    document.getElementById('lightbox').style.position='fixed';

    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;
    } else {
      document.getElementById('overlay').style.height = '100%';
      document.getElementById('overlay').style.width = '100%';
    }

  }

  document.getElementById('closeimg').style.marginLeft=((document.getElementById('photo').width)/2-90) + 'px';
  document.getElementById('infoimg').style.marginRight=((document.getElementById('photo').width)/2-90) + 'px';

}

function exitlightbox() {

  if(slideshow==1) slide('stop');

  document.title=firsttitle;

  document.getElementById('overlay').style.position='fixed';
  document.getElementById('lightbox').style.position='fixed';

  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()',timeout_slideshow);

  }

  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 one of the next versions');

}
