$(document).ready(function () {
  //IE7-fix
  $("#leftframe").height($("#rightframe").height()-17);
});

function showLargeImage(sender, largeImgWidth, largeImgHeight, largeImgURL)
{
  var pageWidth = 478;
  var parentImg = $(sender);
  var largeImg = $("#largeImage");
  var largeImgCanvas = $("#largeImageCanvas");

    //check if the photo is already open - then close the first
  if (largeImg.is(":visible"))
  {
    if (largeImgCanvas.attr("src") != largeImgURL)
    {
      largeImg.hide();
      //$("#mainframe").css({ opacity: 0.1 });
    }
    else
    {
      //$("#mainframe").css({ opacity: 1.0 });
    }
  }
  else
  {
    //$("#mainframe").css({ opacity: 0.1 });
  }
  
  largeImgCanvas.attr("src", largeImgURL);
  largeImgCanvas.css("width", largeImgWidth);
  largeImgCanvas.css("height", largeImgHeight);
  largeImg.css("width", largeImgWidth);
  largeImg.css("height", largeImgHeight);
  
  var newTop = (($("#mainframe").height() - largeImg.height() + $("#mainframe").offset().top) / 2) + $(window).scrollTop();
  if (newTop < $("#mainframe").offset().top)
  {
      newTop = $("#mainframe").offset().top;
  }
  
  if ((largeImgHeight + parentImg.offset().top) < $("#mainframe").height())
  {
    newTop = parentImg.offset().top;
  }
  
    else
  {
      if ((parentImg.height() +parentImg.offset().top - largeImgHeight + 8) > ($("#mainframe").scrollTop()))
      {
        newTop = parentImg.height() +parentImg.offset().top - largeImgHeight + 8;
      }
  }

    if (((parentImg.height() +parentImg.offset().top) < parseInt($(window)[0].scrollHeight)) && (parentImg.scrollTop() > $(window).scrollTop()))
      {
        newTop = parentImg.height() + parentImg.offset().top - largeImgHeight;
      }  

  
  if (newTop < $("#mainframe").offset().top)
  {
    newTop = $("#mainframe").offset().top
  }
    
  largeImg.css("top", newTop + "px"); 
  
  if (largeImgWidth<$("#rightframe").width())
  {
      if (parentImg.css("float") == "right")
      {
        largeImg.css("left", parentImg.offset().left - largeImg.width() + parentImg.width() + "px");
      }
    else
    {
      largeImg.css("left", parentImg.offset().left + "px");
      //largeImg.css("left", $("#rightframe").offset().left + "px"); 
    }
  }
  else
  {
    var luftTilKant = $("#mainframe").width() - parentImg.offset().left - parentImg.width() - 8;
    if (largeImgWidth<($("#mainframe").width()-luftTilKant))
    {
       largeImg.css("left",($("#mainframe").width()-luftTilKant-largeImgWidth - 8) + "px");
    }
    else
    {
      largeImg.css("left", (($(window).width() - largeImg.outerWidth()) / 2) + $(window).scrollLeft() + "px"); 
    }
  }

  
  largeImg.slideToggle('fast', function() {});
}

