function transitionElements(el, action) {
  $w(el).each(function(el) {
    if (document.getElementById(el)) {
      switch(action) {
        case 'hide': $(el).hide(); break;
        case 'appear': $(el).appear({duration: 0.5}); break;
        case 'fade': $(el).fade({duration: 0.5}); break;
      }
    }
  });
}

function fadeAndLoad(event, target) {
  var element = event.element();
  saveLastUrl(stripPageParams(window.location));
  if (compareUrls(element.href) == false) {
    transitionElements(target, 'fade');
    window.setTimeout(function() { window.location = element.href; }, 500);
  } else {
    window.location = element.href;
  }
  event.stop();
}

function stripPageParams(url) {
  var url = String(url);
  return url.sub(/(\/?\?page=\d)$/, '');
}

function splitUrl(url) {
  var url = String(url);
  url = url.sub(/^(http:\/\/)/, '');
  return url.split('/');
}

function differentGallery() {
  var last_url = readCookie('ge_lasturl');
  var current_url = stripPageParams(window.location);
  var last = splitUrl(last_url);
  var current = splitUrl(current_url);
  return (current[2] == last[2]) ? false : true;
}

function compareUrls(url) {
  var current_url = (url) ? stripPageParams(url) : stripPageParams(window.location);
  var last_url = readCookie('ge_lasturl');
  if (current_url == last_url) {
    return true;
  } else {
    return false;
  }
}

function showLoading() {
  if (document.getElementById('photo')) {
    var loadingText = '<p id="loading">Loading &hellip;</p>';
    $('pagebody').insert(loadingText);
  }
}

function hideLoading() {
  if(!document.getElementById('loading')) return false;
  $('loading').hide();
}

function saveLastUrl(url) {
  createCookie('ge_lasturl', url);
}

function preloadThumbs() {
  if (document.getElementById('thumbs')) {
    var galleries = new Object;
    galleries.food = 20;
    galleries.people = 38;
    galleries.places = 34;
    galleries.books = 14;
    
    var gallery = document.body.className;
    var img = [];
    for (i=1; i<=galleries[gallery]; i++) {
      img[i] = new Image();
      var photo = (String(i).length == 2) ? i : '0' + String(i);
      img[i].src = '/images/photos/th_' + gallery + photo + '.jpg';
    }
  }
}

// cookie functions from http://www.quirksmode.org/js/cookies.html
function createCookie(name,value,days) {
  if (days) {
    var date = new Date();
    date.setTime(date.getTime()+(days*24*60*60*1000));
    var expires = "; expires="+date.toGMTString();
  }
  else var expires = "";
  document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
  var nameEQ = name + "=";
  var ca = document.cookie.split(';');
  for(var i=0;i < ca.length;i++) {
    var c = ca[i];
    while (c.charAt(0)==' ') c = c.substring(1,c.length);
    if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
  }
  return null;
}

function prepareLinks() {
  $$('#thumbs li a').invoke('observe', 'click', fadeAndLoad.bindAsEventListener(this, 'photo text'));
  $$('#nav li a').invoke('observe', 'click', fadeAndLoad.bindAsEventListener(this, 'photo text thumbs'));
}

var elements = '';

document.observe("dom:loaded", function(){
  if (compareUrls() == false) {
    elements += 'photo text';
  }
  if (differentGallery() == true) {
    elements += ' thumbs';
  }
  transitionElements(elements, 'hide');
  prepareLinks();
});

// looks oldskool but seems to be more consistent than using Event.observe
window.onload = function() {
  transitionElements(elements, 'appear');
  preloadThumbs();
}

// solves onload events not firing on use of back or forward buttons
window.onunload = function(){};
