$(document).ready(function() {
    // load image
    var loadPhoto = function(path) {	
      container_wrapper = $('div#display_photo_wrapper');	
      container = $('div#display_photo', container_wrapper);

      container_wrapper.addClass('loading');

      container.fadeOut('fast', function() {			
	  i = $('<img />').attr('src', path);				
	  i.load(function() {
	      container_wrapper.removeClass('loading');
	      container.html(i).fadeIn('normal');
	    });			
	});								
    }
    //
	
    var setupPhotoNavs = function() {
      // set up nav
      $('div#photo_gallery_nav a').click(function() {	

	  switch($(this).attr('id')) {			
	  case 'photo_gallery_nav_left':
	    if(_display_photo_index > 0) {
	      --_display_photo_index;
	      a = $('div#photo_gallery_nav a#photo_gallery_nav_' + _display_photo_index);										
	    } else { return false; }
	    break;
			
	  case 'photo_gallery_nav_right':
	    if(_display_photo_index < _display_photo_count-1) {
	      ++_display_photo_index;
	      a = $('div#photo_gallery_nav a#photo_gallery_nav_' + _display_photo_index);
	    } else { return false; }
	    break;
			
	  default:			
	    a = $(this);
	    _display_photo_index = a.attr('rel');
	    break;
	  }

	  $('div#photo_gallery_nav a').removeClass('lit');
	  a.addClass('lit');
	  loadPhoto(a.attr('href'));
		
	  // hide/show arrows
	  if(_display_photo_index==0) {
	    $('div#photo_gallery_nav a#photo_gallery_nav_left').fadeOut('fast');
	  } else $('div#photo_gallery_nav a#photo_gallery_nav_left').fadeIn('fast');
		
	  if(_display_photo_index>=_display_photo_count-1) {
	    $('div#photo_gallery_nav a#photo_gallery_nav_right').fadeOut('fast');
	  } else $('div#photo_gallery_nav a#photo_gallery_nav_right').fadeIn('fast');
		
		
	  return false;
	});
	
      // keyboard nav
      $(document).keydown(function (e) {
	  key = e.keyCode;

	  switch(key) {
	  case 37: // left
	    if(_display_photo_index > 0) {
	      --_display_photo_index;
	      $('div#photo_gallery_nav a#photo_gallery_nav_' + _display_photo_index).click();
	    }
	    break;
			
	  case 39: // right
	    if(_display_photo_index < _display_photo_count) {
	      ++_display_photo_index;
	      $('div#photo_gallery_nav a#photo_gallery_nav_' + _display_photo_index).click();
	    }			
	    break;
	  }
	});
    }

	
    try {
      // load default photo
      if(_display_photo != undefined) {
	if(_display_photo.length) {
	  setupPhotoNavs();
	  loadPhoto(_display_photo);
	}	
      }	
    } catch(e) {
		
    }

    // format email addies
    // rel="photos|ignacy#net"
    $('a.email').each(function() {
	m = $(this).attr('rel').replace('|', '@').replace('#', '.');
	$(this).replaceWith('<a href="mailto:' + m + '">' + m + '</a>');
      });	
  });



