function ChangePic(SID,SN,PID,TID,AText)
 {
 $('#'+TID+' img').remove();
 
 var PlacePhotoOffset = $('#SID'+SID+'_SN'+SN+'_PID'+PID).offset();
 var FlowOffset = $('#Flow').offset();
 
 var BigPhoto = new Image();
 var PhotoHeight;
 var PhotoWidth;
 
 $(BigPhoto).load(function() {
  $('#'+TID).append(this);
  PhotoHeight = $(this).height();
  PhotoWidth = $(this).width();
  var PhotoOffsetTop = PlacePhotoOffset.top - ((PhotoHeight+34) / 2) + (116/2);
  var PhotoOffsetLeft = FlowOffset.left + 320 - ((PhotoWidth+34) / 2)
  $('#'+TID).parent().parent().offset({top : PhotoOffsetTop, left: PhotoOffsetLeft});
 });
 
 $(BigPhoto).attr('src','./Gallery/'+SID+'-'+SN+'-Large-'+PID+'.jpg');
 $(BigPhoto).attr('alt',AText);
 $('#'+TID).parent().parent().show();
 
 // IE8 decides under certain conditions not to load images correctly (without sending errors), so we need to babysit and reprompt it.
 if ($.browser.msie && PhotoHeight == 0) {
  ChangePic(SID,SN,PID,TID,AText);}
 
 $('#PGLV_CloseButton').click(function(){
  $('#'+TID).parent().parent().hide();
  });
  
 }
 
function ScrollToTopOnFooterNavClick() {
 $('.FooterText .CHE_'+PageID).click(function(){
  $(window).scrollTop(0);
  });
 }

function PageNavigated(HashTarget) {
 if (HashTarget != '') {
  $('.CHE').removeClass('CHE_FO');
  $('.CHE_'+HashTarget).addClass('CHE_FO');
  }
 else {
  $('.CHE').removeClass('CHE_FO');
  $('.CHE').eq(0).addClass('CHE_FO');
  }
 
 // IE8 can't seem to manage to redraw the screen correctly by this point so we give it a nudge.
 if ($.browser.msie) {
  ReDraw();}
 }
 
function ReDraw() {
 $('body').addClass('Redraw');
 $('body').removeClass('Redraw');}
 
function SetupFancyNav() {
 var HashTarget = window.location.hash.replace("#", "");
 if (HashTarget == false)
  {$('.CHE').eq(0).addClass('CHE_FO');}
  
 $.historyInit(PageNavigated);
 $('.CHE_'+PageID).click(function(){
  $('.BigImageContainer').hide();
  var hash = this.href;
  hash = hash.replace(/^.*#/, '');
  // moves to a new page. 
  // pageload is called at once. 
  // hash don't contain "#", "?"
  $.historyLoad(hash);
  return false;
  });
 }
 

$(document).ready(function() {

 PageID = $('#Container').attr("class");

 ScrollToTopOnFooterNavClick();
 $("a.NavTarget").remove();
 SetupFancyNav();

});
