
/*
 * Oog Photo-Gallery v3.1.1
 * http://www.oog-gallery.de/ 
 * Copyright (C) 2009 Torben Rottbrand
 * 
 * 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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

init();

window.onresize = function() {

  if(document.getElementById('oog_lightbox_main').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('oog_lightbox_main').style.visibility=='visible') previous();
  if(keycode==39 && document.getElementById('oog_lightbox_main').style.visibility=='visible') next();
  if(keycode==27) exitlightbox();

}

document.getElementById('oog_lightbox_next_radius').onmouseover = function() {
  document.getElementById('oog_lightbox_next_but').style.backgroundPosition='-54px 0px';
}
document.getElementById('oog_lightbox_next_radius').onmouseout = function() {
  document.getElementById('oog_lightbox_next_but').style.backgroundPosition='0px 0px';
}
document.getElementById('oog_lightbox_previous_radius').onmouseover = function() {
  document.getElementById('oog_lightbox_previous_but').style.backgroundPosition='-54px 0px';
}
document.getElementById('oog_lightbox_previous_radius').onmouseout = function() {
  document.getElementById('oog_lightbox_previous_but').style.backgroundPosition='0px 0px';
}

function init() {

  document.write(oog_lightbox_xhtml);

  lightbox_overlay = document.getElementById('oog_lightbox_overlay');
  lightbox_main = document.getElementById('oog_lightbox_main');
  lightbox_loading = document.getElementById('oog_lightbox_loading');
  lightbox_photo = document.getElementById('oog_lightbox_photo');

  var first_windowheight = (window.innerHeight) ? window.innerHeight : window.document.documentElement.clientHeight;
  var first_windowwidth = (window.innerWidth) ? window.innerWidth : window.document.documentElement.clientWidth;

  lightbox_main_height_percent = lightbox_main.offsetHeight/first_windowheight*100;
  lightbox_main_width_percent = lightbox_main.offsetWidth/first_windowwidth*100;

  lightbox_main_top_percent = (100-lightbox_main_height_percent)/2;
  lightbox_main_left_percent = (100-lightbox_main_width_percent)/2;

  lightbox_main.style.top = lightbox_main_top_percent + '%';
  lightbox_main.style.left = lightbox_main_left_percent + '%';

  overlay_opacity = 0;
  lightbox_overlay.style.filter = "alpha(opacity=0)";
  lightbox_overlay.style.opacity = 0;
  lightbox_slideshow = 0;
  lightbox_info = 0; 
  lightbox_firsttitle = document.title;

  lightbox_loading_height = lightbox_loading.offsetHeight;
  lightbox_loading_width = lightbox_loading.offsetWidth;

}

function overlay_change_opacity(opacity,close) {

  lightbox_overlay.style.filter = "alpha(opacity=" + opacity + ")";
  lightbox_overlay.style.opacity = (opacity / 100);

  if(opacity==0 && close==1) {
    lightbox_overlay.style.height='0';
    lightbox_overlay.style.width='0';
    lightbox_overlay.style.visibility='hidden';
  }

  if(opacity==26) 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_main_height_percent) - 56;
  var widthspace = Math.round(windowwidth/100*lightbox_main_width_percent) - 46;

  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(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;
    }
    
  }

  lightbox_photo.height = new_photo_height;
  lightbox_photo.width = new_photo_width;

  lightbox_main.style.top = Math.round(windowheight/100*lightbox_main_top_percent) + 'px';
  lightbox_main.style.left = lightbox_main_left_percent + '%';

  lightbox_overlay.style.position = 'fixed';
  lightbox_main.style.position = 'fixed';

  lightbox_overlay.style.height = '100%';
  lightbox_overlay.style.width = '100%';

  document.getElementById('oog_lightbox_photo_box').style.marginLeft = ((lightbox_main.offsetWidth-(new_photo_width+46))/2) + 'px';
  document.getElementById('oog_lightbox_photo_box').style.marginTop = ((lightbox_main.offsetHeight-(new_photo_height+56))/2) + 'px';

  document.getElementById('oog_lightbox_close_but').style.top = ((lightbox_main.offsetHeight-(new_photo_height+56))/2)-3 + 'px';
  document.getElementById('oog_lightbox_close_but').style.right = ((lightbox_main.offsetWidth-(new_photo_width+46))/2)-6 + 'px';

  document.getElementById('oog_lightbox_next_radius').style.height = (new_photo_height/100*85) + 'px';
  document.getElementById('oog_lightbox_next_radius').style.width = (new_photo_width/100*45) + 'px';
  document.getElementById('oog_lightbox_next_radius').style.bottom = ((lightbox_main.offsetHeight-(new_photo_height+56))/2)+29 + 'px';
  document.getElementById('oog_lightbox_next_radius').style.right = ((lightbox_main.offsetWidth-(new_photo_width+46))/2)-6 + 'px';

  document.getElementById('oog_lightbox_previous_radius').style.height = (new_photo_height/100*85) + 'px';
  document.getElementById('oog_lightbox_previous_radius').style.width = (new_photo_width/100*45) + 'px';
  document.getElementById('oog_lightbox_previous_radius').style.bottom = ((lightbox_main.offsetHeight-(new_photo_height+56))/2)+29 + 'px';
  document.getElementById('oog_lightbox_previous_radius').style.left = ((lightbox_main.offsetWidth-(new_photo_width+46))/2)-6 + 'px';

  document.getElementById('oog_lightbox_shad_top').style.width = new_photo_width + 20 + 'px';
  document.getElementById('oog_lightbox_shad_bottom').style.width = new_photo_width + 20 + 'px';
  document.getElementById('oog_lightbox_shad_left').style.height = new_photo_height + 20 + 'px';
  document.getElementById('oog_lightbox_shad_right').style.height = new_photo_height + 20 + 'px';
  
}

function load_photo() {

  lightbox_photo.name = oog_photoname;
  lightbox_img = new Image();
  lightbox_loading.style.visibility = 'visible';
  lightbox_img.onload = function() {
    lightbox_loading.style.visibility = 'hidden';
    firstimgheight = lightbox_img.height;
    firstimgwidth = lightbox_img.width;
    resizelightbox();
    lightbox_photo.src = escape(oog_album) + escape(oog_photoname);
    lightbox_main.style.visibility = 'visible';
    document.getElementById('oog_lightbox_photo_box').style.display = 'block'; //for IE7
    if(lightbox_info == 1) {
      document.getElementById('oog_lightbox_comments_info_box').style.visibility = 'visible';

    }
    if(lightbox_slideshow == 1) lightbox_timeout = window.setTimeout('next()',timeout_slideshow);
  }
  lightbox_img.src = escape(oog_album) + escape(oog_photoname);

}

function startlightbox(albumpath,photoname) {

  oog_album = albumpath;
  oog_photoname = photoname;

  document.title = lightbox_firsttitle + ' - ' + photoname;

  lightbox_overlay.style.visibility = 'visible';

  lightbox_main.style.position = 'fixed';
  lightbox_overlay.style.position = 'fixed';

  lightbox_overlay.style.height = '100%';
  lightbox_overlay.style.width = '100%';

  lightbox_loading.style.visibility='hidden';

  if(overlay_opacity==0) {

    var timer = 0;

    while(overlay_opacity<=25) {
      overlay_opacity = overlay_opacity + 1;
      setTimeout("overlay_change_opacity(" + overlay_opacity + ",0)",timer);
      if(navigator.appVersion.substring(0, 25) == "4.0 (compatible; MSIE 7.0" || navigator.appVersion.substring(0, 25) == "4.0 (compatible; MSIE 8.0") {
        timer = timer + 5;
      } else {
        timer = timer + 13;
      }
    }
  } else {

    load_photo();

  }

}

function exitlightbox() {

  lightbox_loading.style.visibility='hidden';

  document.title = lightbox_firsttitle;

  lightbox_overlay.style.position='fixed';
  lightbox_main.style.position='fixed';

  lightbox_main.style.visibility = 'hidden';
  document.getElementById('oog_lightbox_photo_box').style.display = 'none'; //for IE7
  
  lightbox_photo.removeAttribute('name');
  lightbox_photo.removeAttribute('src');
  lightbox_photo.removeAttribute('height');
  lightbox_photo.removeAttribute('width');

  var timer = 0;

  while(overlay_opacity>=1) {
    overlay_opacity = overlay_opacity - 1;
    setTimeout("overlay_change_opacity(" + overlay_opacity + ",1)",timer);
      if(navigator.appVersion.substring(0, 25) == "4.0 (compatible; MSIE 7.0" || navigator.appVersion.substring(0, 25) == "4.0 (compatible; MSIE 8.0") {
        timer = timer + 4;
      } else {
        timer = timer + 7;
      }

  }

}

function previous() {

  if(lightbox_slideshow==1) window.clearTimeout(lightbox_timeout);

  var count=document.getElementsByName('thumb').length;
  var i=0;

  while(i<count) {

    if(document.getElementsByName('thumb')[i].title==lightbox_photo.name) {
      var previousphoto = (i==0) ? document.getElementsByName('thumb')[count-1].title : document.getElementsByName('thumb')[i-1].title;
      break;
    }
    i++;

  }
  
  lightbox_main.style.visibility='hidden';

  lightbox_photo.removeAttribute('name');
  lightbox_photo.removeAttribute('src');
  lightbox_photo.removeAttribute('height');
  lightbox_photo.removeAttribute('width');

  startlightbox(oog_album,previousphoto);

}

function next() {

  if(lightbox_slideshow==1) window.clearTimeout(lightbox_timeout);

  var count=document.getElementsByName('thumb').length;
  var i=0;

  while(i<count) {

    if(document.getElementsByName('thumb')[i].title==lightbox_photo.name) {
      var nextphoto = (i==count-1) ? document.getElementsByName('thumb')[0].title : document.getElementsByName('thumb')[i+1].title;
      break;
    }
    i++;

  }

  lightbox_main.style.visibility='hidden';

  lightbox_photo.removeAttribute('name');
  lightbox_photo.removeAttribute('src');
  lightbox_photo.removeAttribute('height');
  lightbox_photo.removeAttribute('width');

  startlightbox(oog_album,nextphoto);

}
