var fx = new Object;
fx['images'] = new Object;
fx['albums'] = new Object;
fx['navigationPosition'] = 0;
fx['currentPosition'] = 0;
fx['qeue'] = new Object;



function showImage (id)
{
  if (typeof fx['images'][id] == 'undefined' || ! fx['images'][id])
  {
    return false;
  }
  var imageProperties = fx['images'][id]['large'];
  showOverlay ();
  showNavigation (imageProperties);
  showImagebox (imageProperties);
  stretchOverlay();
  for (var i in fx['qeue'])
  {
    if (typeof fx['images'][i] != 'undefined')
    {
      fx['images'][i]['large']['preload'] = new Image;
      fx['images'][i]['large']['preload'].src = fx['images'][i]['large']['src'];
      delete fx['qeue'][i];
    }
  }
}
function stretchOverlay ()
{
  var dimensions = getPageDimensions();
  var imagebox = $('#imagebox').get(0);
  var pageHeight = Math.max(imagebox.offsetHeight + parseInt(imagebox.style.top), dimensions.height);
  $('#imageoverlay').css({position: 'absolute', top: '0px', left: '0px', right: '0px', height: pageHeight + 'px', width: '100%'});
}
function showOverlay ()
{
  var imageoverlay = document.getElementById('imageoverlay');
  if (typeof imageoverlay == 'undefined' || !imageoverlay)
  {
    imageoverlay = document.createElement('div');
    imageoverlay.id = 'imageoverlay';
    $(imageoverlay).click(function(e){ hideImageoverlay() });
    $('body').append(imageoverlay);
  }
  imageoverlay.style.position = 'absolute';
  $(imageoverlay).show();
}
function showImagebox (imageProperties)
{
  var imagebox = document.getElementById('imagebox');
  if (typeof imagebox == 'undefined' || !imagebox)
  {
    imagebox = document.createElement('div');
    imagebox.id = 'imagebox';
//    $(imagebox).click(function(e){ hideImageoverlay() });
    $('body').append(imagebox);
  }
  imagebox.style.position = 'absolute';
  var imageHTML = '<img src="' + imageProperties['src'] 
    + '" width="' + imageProperties['width'] + '" '
    + '" height="' + imageProperties['height'] + '" '
    + '" alt="" class="overlayimage" />';
  if (
    (typeof imageProperties['album'] != 'undefined')
    && (typeof fx['albums'][imageProperties['album']] != 'undefined')
    && (fx['albums'][imageProperties['album']]['images'].length > 1)
  )
  {
    if (imageProperties['num'] == fx['albums'][imageProperties['album']]['images'].length -1)
    {
      var nextId = fx['albums'][imageProperties['album']]['images'][0];
    }
    else
    {

      var nextId = fx['albums'][imageProperties['album']]['images'][imageProperties['num'] + 1];
    }
    var imageHTML = '<a href="'+ fx['link'] + '/showimage=' + nextId + '"'
      + ' onclick="showImage(\'' + nextId + '\'); return false">'
      + imageHTML + '</a>';
  }
  else
  {
    imageHTML = '<a href="'+ fx['link'] +'" onclick="hideImageoverlay(); return false">'
      + imageHTML + '</a>';
  }
  var imageOptions = document.createElement('div');
  imageOptions.id = 'image-options';
  imageOptions.innerHTML = closeButton();
  imagebox.innerHTML = imageHTML;
  imagebox.appendChild(imageOptions);
  imageOptions.style.width = (imageProperties['width'] + 20) + 'px';
  centerImageBox(imageProperties);
}
function centerImageBox (imageProperties)
{
  var windowDimensions = getWindowDimensions();
  if (fx['navigationPosition'] > 0)
  {
    var navigationPosition = Math.max(Math.round((windowDimensions['height'] - 850) / 2), 10);
    var space = Math.min(windowDimensions['height'] - navigationPosition - 40, 750);
    var boxY = Math.max(Math.round((space - imageProperties['height'] - 80) / 2), 0);
    boxY = boxY + 40 + fx['navigationPosition'];
  }
  else
  {
    var boxY = Math.max(Math.round((windowDimensions['height'] - imageProperties['height'] - 50) / 2), 10) + getScrollPosition();
  }
  var imagebox = $('#imagebox').get(0);
  imagebox.style.top = boxY + 'px';
  $(imagebox).show();
}
function hideImageoverlay ()
{
  $('#album-navigation').hide();
  fx['navigationPosition'] = 0;
  $('#imagebox').hide();
  $('#imageoverlay').hide();
}
function showNavigation (imageProperties)
{
  if (
    (typeof imageProperties['album'] == 'undefined')
    || (typeof fx['albums'][imageProperties['album']] == 'undefined')
    || (fx['albums'][imageProperties['album']]['images'].length < 2)
  )
  {
    fx['navigationPosition'] = 0;
    $('#album-navigation').hide();
    return;
  }
  var album = fx['albums'][imageProperties['album']];
  if (fx['navigationPosition'] == 0)
  {
    setNavigationPosition();
  }
  var albumNavigation = document.getElementById('album-navigation');
  if (typeof albumNavigation == 'undefined' || !albumNavigation)
  {
    albumNavigation = document.createElement('div');
    albumNavigation.id = 'album-navigation';
    $('body').append(albumNavigation);
  }
  albumNavigation.style.zIndex = 99;
  albumNavigation.style.top = fx['navigationPosition'] + 'px';
  
  var navHTML = '<div id="album-navigation-box">';
  if (imageProperties['num'] > 0)
  {
    navHTML += navigationLink(album['images'][0], 'first', 'first', true);
    navHTML += navigationLink(album['images'][imageProperties['num'] - 1], 'previous', 'previous', true);
  }
  else
  {
    navHTML += navigationLink(0, 'first', 'first', false);
    navHTML += navigationLink(0, 'previous', 'previous', false);
  }
  navHTML += ' &nbsp; ';
  if (imageProperties['num'] < album['images'].length - 1)
  {
    navHTML += navigationLink(album['images'][imageProperties['num'] + 1], 'next', 'next', true);
    navHTML += navigationLink(album['images'][album['images'].length - 1], 'last', 'last', true);
  }
  else
  {
    navHTML += navigationLink(0, 'next', 'next', false);
    navHTML += navigationLink(0, 'last', 'last', false);
  }
  navHTML += ' &nbsp; ';
  navHTML += closeButton();
  navHTML += '</div>';
  albumNavigation.innerHTML = navHTML;
  $(albumNavigation).show();
}

function navigationLink (id, type, text, active)
{
  switch (type)
  {
    case 'first':
    case 'last':
      var imageWidth = 9;
      break;
    default:
      var imageWidth = 6;
  }
  var imageSrc = active ? type + '.gif' : type + '-inactive.gif';
  var output = '<img src="/images/common/' + imageSrc + '" id="album-navigation-' + type + '"'
    + ' width="' + imageWidth + '" height="11" title="'+ text +'" alt="' + text + '" />';
  if (!active)
  {
    return output;
  }
  if (typeof fx['images'][id]['large']['preload'] == 'undefined')
  {
    fx['qeue'][id] = true;
  }
  return '<a href="'+ fx['link'] + '/showimage=' + id + '"'
   + ' onclick="showImage(\'' + id + '\'); return false">'
   + output + '</a>';
}

function closeButton ()
{
  return '<a href="'+ fx['link'] +'" onclick="hideImageoverlay(); return false">'
    + '<img title="close" src="/images/common/close.gif" class="image-close-button" width="11" height="11"  alt="close" /></a>';
}
function setNavigationPosition ()
{
  var windowDimensions = getWindowDimensions();
  var position = Math.max(Math.round((windowDimensions['height'] - 850) / 2), 10);
  fx['navigationPosition'] = position + getScrollPosition();
}

function dropshadow (image)
{
    var dropshadowbox = document.createElement('div');
    dropshadowbox.className = 'dropshadowbox';
    if (image.align)
    {
      $(dropshadowbox).css({float: image.getAttribute('align')}).addClass('image-' + image.getAttribute('align'));
      image.align = "";
    }
    if (image.parentNode.tagName == 'A')
    {
      var origNode = image.parentNode;
      var clone = image.parentNode.cloneNode(true);
      var parentNode = image.parentNode.parentNode;
    }
    else
    {
      var origNode = image;
      var clone = image.cloneNode(true);
      var parentNode = image.parentNode;
    }
    var dropshadowcorner = document.createElement('div');
    dropshadowcorner.className = 'dropshadowcorner';
    var dropshadowdrop = document.createElement('div');
    dropshadowdrop.className = 'dropshadowdrop';
    var dropshadowinnerbox = document.createElement('div');
    dropshadowinnerbox.className = 'dropshadowinnerbox';
    dropshadowinnerbox.appendChild(clone);
    dropshadowdrop.appendChild(dropshadowinnerbox);
    dropshadowcorner.appendChild(dropshadowdrop);
    dropshadowbox.appendChild(dropshadowcorner);
    parentNode.replaceChild(dropshadowbox, origNode);

}

$(document).ready(function(){
  $('img.picture').each(function (){
    dropshadow (this)    
  })
});


