
/*
 * Oog Photo-Gallery v3.1
 * 2009 von Torben Rottbrand
 */

init();

window.onresize = function() {

  if(document.getElementById('id_oog_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('id_oog_lightbox').style.visibility=='visible') previous();
  if(keycode==39 && document.getElementById('id_oog_lightbox').style.visibility=='visible') next();
  if(keycode==27) exitlightbox();
  if(keycode==73 && document.getElementById('id_oog_lightbox').style.visibility=='visible') info();

}

function init() {

  document.write(oog_lightbox_html);

  oog_overlay = document.getElementById('id_oog_overlay');
  oog_lightbox = document.getElementById('id_oog_lightbox');
  oog_loading = document.getElementById('id_oog_loading');
  oog_photo = document.getElementById('id_oog_photo');

  var first_windowheight = (window.innerHeight) ? window.innerHeight : window.document.documentElement.clientHeight;
  var first_windowwidth = (window.innerWidth) ? window.innerWidth : window.document.documentElement.clientWidth;

  lightbox_height_percent = oog_lightbox.offsetHeight/first_windowheight*100;
  lightbox_width_percent = oog_lightbox.offsetWidth/first_windowwidth*100;

  lightbox_top_percent = (100-lightbox_height_percent)/2;
  lightbox_left_percent = (100-lightbox_width_percent)/2;

  oog_lightbox.style.top = lightbox_top_percent + '%';
  oog_lightbox.style.left = lightbox_left_percent + '%';

  overlay_opacity = 0;
  oog_overlay.style.filter = "alpha(opacity=0)";
  oog_overlay.style.opacity = 0;
  oog_slideshow = 0;
  oog_firsttitle = document.title;

  oog_loading_height = oog_loading.offsetHeight;
  oog_loading_width = oog_loading.offsetWidth;

}

function overlay_change_opacity(opacity,close) {

  oog_overlay.style.filter = "alpha(opacity=" + opacity + ")";
  oog_overlay.style.opacity = (opacity / 100);

  if(opacity==0 && close==1) {
    oog_overlay.style.height='0';
    oog_overlay.style.width='0';
    oog_overlay.style.visibility='hidden';
  }

  if(opacity==70) load_photo();

}

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*lightbox_height_percent) - 75;
  var widthspace = Math.round(windowwidth/100*lightbox_width_percent) - 25;

  var new_photo_height;
  var new_photo_width;

  if(resize_photos=='all') {

    new_photo_height = heightspace;    
    new_photo_width = heightspace * (firstimgwidth/firstimgheight);

    if(new_photo_width>widthspace) {
      new_photo_width = widthspace;
      new_photo_height = widthspace * (firstimgheight/firstimgwidth);
    }

  } else if(resize_photos=='fixed') {

    new_photo_height = fixed_photo_height;
    new_photo_width = fixed_photo_width;

  } else if(resize_photos=='only_to_bigs') {

    if(firstimgheight>heightspace || firstimgwidth>widthspace) {
      new_photo_height = heightspace;    
      new_photo_width = heightspace * (firstimgwidth/firstimgheight);

      if(new_photo_width>widthspace) {
        new_photo_width = widthspace;
        new_photo_height = widthspace * (firstimgheight/firstimgwidth);
      }
    } else {
      new_photo_height = firstimgheight;    
      new_photo_width = firstimgwidth;
    }

  } else if(resize_photos=='only_to_smalls') {

    if(firstimgheight<heightspace && firstimgwidth<widthspace) {
      new_photo_height = heightspace;
      new_photo_width = heightspace * (firstimgwidth/firstimgheight);

      if(new_photo_width>widthspace) {
        new_photo_width = widthspace;
        new_photo_height = widthspace * (firstimgheight/firstimgwidth);
      }
    } else {
      new_photo_height = firstimgheight;    
      new_photo_width = firstimgwidth;
    }

  } else if(resize_photos=='never') {

    new_photo_height = firstimgheight;    
    new_photo_width = firstimgwidth;

  }

  oog_photo.height = new_photo_height;
  oog_photo.width = new_photo_width;

  var used_heightspace = new_photo_height+Math.round(windowheight/100*lightbox_top_percent)+50;
  var used_widthspace = new_photo_width+2*Math.round(windowwidth/100*lightbox_left_percent)+20;

  if(windowheight<used_heightspace || windowwidth<used_widthspace) {

    oog_lightbox.style.position = 'absolute';
    oog_overlay.style.position = 'absolute';

    var pageYOffset = 0;
    if( typeof( window.pageYOffset ) == 'number' ) {
      pageYOffset = window.pageYOffset;
    } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
      pageYOffset = document.body.scrollTop;
    } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
      pageYOffset = document.documentElement.scrollTop;
    }

    var new_top = pageYOffset - (new_photo_height + 65) + windowheight;
    oog_lightbox.style.top = (new_top<10) ? "10px" : new_top+"px";

    var body_height = 0;
    var body_width = 0;

    if( window.innerHeight && window.scrollMaxY ) {
      body_width = window.innerWidth + window.scrollMaxX;
      body_height = window.innerHeight + window.scrollMaxY;
    } else if( document.body.scrollHeight > document.body.offsetHeight ) {
      body_width = document.body.scrollWidth;
      body_height = document.body.scrollHeight;
    } else {
      body_width = document.body.offsetWidth + document.body.offsetLeft;
      body_height = document.body.offsetHeight + document.body.offsetTop;
    }

    oog_overlay.style.height = (used_heightspace<body_height) ? body_height+"px" : used_heightspace+"px";
    oog_overlay.style.width = (used_widthspace<body_width) ? body_width+"px" : used_widthspace+"px";

  } else {

    oog_lightbox.style.top = lightbox_top_percent + '%';
    oog_lightbox.style.left = lightbox_left_percent + '%';

    oog_overlay.style.position = 'fixed';
    oog_lightbox.style.position = 'fixed';

    oog_overlay.style.height = '100%';
    oog_overlay.style.width = '100%';

  }

  document.getElementById('oog_closebut').style.marginLeft = (new_photo_width/2-115) + 'px';
  document.getElementById('oog_infobut').style.marginRight = (new_photo_width/2-115) + 'px';

}

function load_photo() {

  oog_photo.name = oog_photoname;
  oog_img = new Image();
  oog_loading.style.height = oog_loading_height + "px";
  oog_loading.style.width = oog_loading_width + "px";
  oog_loading.style.visibility = 'visible';
  oog_img.onload = function() {
    oog_loading.style.visibility = 'hidden';
    firstimgheight = oog_img.height;
    firstimgwidth = oog_img.width;
    resizelightbox();
    oog_photo.src = escape(oog_album) + escape(oog_photoname);
    oog_lightbox.style.visibility = 'visible';
    if(oog_slideshow == 1) oog_timeout = window.setTimeout('next()',timeout_slideshow);
  }
  oog_img.src = escape(oog_album) + escape(oog_photoname);

}

function startlightbox(albumpath,photoname) {

  oog_album = albumpath;
  oog_photoname = photoname;

  document.title = oog_firsttitle + ' - ' + photoname;

  oog_overlay.style.visibility = 'visible';

  oog_lightbox.style.position = 'fixed';
  oog_overlay.style.position = 'fixed';

  oog_overlay.style.height = '100%';
  oog_overlay.style.width = '100%';

  oog_loading.style.height = '0px';
  oog_loading.style.width = '0px';

  if(overlay_opacity==0) {

    var timer = 0;

    while(overlay_opacity<=69) {
      overlay_opacity = overlay_opacity + 5;
      setTimeout("overlay_change_opacity(" + overlay_opacity + ",0)",timer);
      timer = timer + 18;
    }

  } else {

    load_photo();

  }

}

function exitlightbox() {

  oog_loading.style.height = '0px';
  oog_loading.style.width = '0px';
  oog_loading.style.visibility='hidden';

  if(oog_slideshow==1) slide();

  document.title=oog_firsttitle;

  oog_overlay.style.position='fixed';
  oog_lightbox.style.position='fixed';

  oog_lightbox.style.visibility='hidden';

  oog_photo.removeAttribute('name');
  oog_photo.removeAttribute('src');
  oog_photo.removeAttribute('height');
  oog_photo.removeAttribute('width');

  var timer = 0;

  while(overlay_opacity>=1) {
    overlay_opacity = overlay_opacity - 5;
    setTimeout("overlay_change_opacity(" + overlay_opacity + ",1)",timer);
    timer = timer + 18;
  }

}

function previous() {

  if(oog_slideshow==1) window.clearTimeout(oog_timeout);

  var count=document.getElementsByName('thumb').length;
  var i=0;

  while(i<count) {

    if(document.getElementsByName('thumb')[i].title==oog_photo.name) {
      var previousphoto = (i==0) ? document.getElementsByName('thumb')[count-1].title : document.getElementsByName('thumb')[i-1].title;
      break;
    }
    i++;

  }

  oog_lightbox.style.visibility='hidden';

  oog_photo.removeAttribute('name');
  oog_photo.removeAttribute('src');
  oog_photo.removeAttribute('height');
  oog_photo.removeAttribute('width');

  startlightbox(oog_album,previousphoto);

}

function next() {

  if(oog_slideshow==1) window.clearTimeout(oog_timeout);

  var count=document.getElementsByName('thumb').length;
  var i=0;

  while(i<count) {

    if(document.getElementsByName('thumb')[i].title==oog_photo.name) {
      var nextphoto = (i==count-1) ? document.getElementsByName('thumb')[0].title : document.getElementsByName('thumb')[i+1].title;
      break;
    }
    i++;

  }

  oog_lightbox.style.visibility='hidden';

  oog_photo.removeAttribute('name');
  oog_photo.removeAttribute('src');
  oog_photo.removeAttribute('height');
  oog_photo.removeAttribute('width');

  startlightbox(oog_album,nextphoto);

}

function slide() {

  if(oog_slideshow==0) {

    oog_slideshow=1;

    document.getElementById('oog_slidebut').style.backgroundPosition='-40px 0px';
    document.getElementById('oog_slidebut').title="Stop Slideshow";

    oog_timeout=window.setTimeout('next()',timeout_slideshow);

  } else {

    oog_slideshow=0;

    document.getElementById('oog_slidebut').style.backgroundPosition='0px 0px';
    document.getElementById('oog_slidebut').title="Start Slideshow";

    window.clearTimeout(oog_timeout);

  }

}

function info() {

  alert('This function will be added in one of the next versions');

}