var loadPhoto, setupPhotoNavs;
var _photosPerLine = 3; // for up/down calculations

function analytics_track(type, action, path) {
  if(typeof(pageTracker) != "undefined") { // google
    pageTracker._trackEvent(type, action, path);
  }
}

$(document).ready(function() {

    // load image
    loadPhoto = function(path) {	
      container_wrapper = $('div#display_photo_wrapper');	
      container = $('div#display_photo a', 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');
		  container.append($('<span />'));
	    });			
	});	

      p = path.split('#');
      track = path;
      if(p.length>1) {
	t = p[1].split('=');
	if(t[0] == 'track') {
	  track = t[1];
	}
      }
      analytics_track('image', 'view', track);
    }
    //

    photoNavPrev = function() {
      --_display_photo_index;
      if(_display_photo_index<0) _display_photo_index = _display_photo_count-1;
      return $('div#photo_strip a#photo_' + _display_photo_index);
    }

    photoNavNext = function() {
      _display_photo_index = (Number(_display_photo_index)+1) % _display_photo_count;
      return $('div#photo_strip a#photo_' + _display_photo_index);
    }

    photoNavJumpUp = function() {     
      if((Number(_display_photo_index) - Number(_photosPerLine))<0) {
	_display_photo_index = (_display_photo_count - 1) + ((_display_photo_index - (_display_photo_count - 1))%_photosPerLine);
      } else {
	_display_photo_index = Number(_display_photo_index) - Number(_photosPerLine);
      }
      return $('div#photo_strip a#photo_' + _display_photo_index);
    }

    photoNavJumpDown = function() {      
      _display_photo_index = Number(_display_photo_index) + Number(_photosPerLine);
      if(_display_photo_index >= _display_photo_count) {
	_display_photo_index = _display_photo_index%_photosPerLine;
      }
      return $('div#photo_strip a#photo_' + _display_photo_index);
    }


    doThumbLoad = function(a) {
      $('div#photo_strip .thumb_wrapper').removeClass('lit');
      a.parent().addClass('lit');
      loadPhoto(a.attr('href'));
    }
	
    setupPhotoNavs = function() {      
      // set up nav
      $('div#photo_strip a').click(function() {	

	  switch($(this).attr('id')) {			
	  case 'photo_gallery_nav_left':
	    a = photoNavPrev();
	    break;
			
	  case 'photo_gallery_nav_right':	      
	    a = photoNavNext();
	    break;
			
	  default:			
	    a = $(this);
	    _display_photo_index = a.attr('id').split('_')[1];
	    break;
	  }

	  doThumbLoad(a);
		
	  return false;
	});
	
      // keyboard nav
      $(document).keydown(function (e) {
	  key = e.keyCode;

	  switch(key) {
	  case 37: // left
	    photoNavPrev().click();
	    e.preventDefault();
	    break;
			
	  case 39: // right
	      photoNavNext().click();
	      e.preventDefault();
	    break;

	  case 38: // up
	    photoNavJumpUp().click();
	    e.preventDefault();
	    break;

	  case 40: // down
	    photoNavJumpDown().click();
	    e.preventDefault();
	    break;
	  }
	});
    }       

    // 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>');
      });	
  });



