﻿$.fn.center = function()
{
     // For each element that is passed to the plug-in
     return this.each(function()
     {
          // Store "this"
          var t = $(this);

          // Set its position to absolute
          t.css({position: 'absolute'});

          // Calculate its new position based on the browser-window's dimensions, the element's dimensions and the scroll-position
          var leftPos = ($(window).width() - t.outerWidth()) / 2 + $(window).scrollLeft();
          var topPos = ($(window).height() - t.outerHeight()) / 2 + $(window).scrollTop();

          // Make sure it's not positioned outside the screen
          if(topPos < 0) topPos = 0;
          if(leftPos < 0) leftPos = 0;

          // Apply top and left values based on calculated new position
          t.css({left: '' + leftPos + 'px', top: '' + topPos + 'px', zIndex: '1000'});
     });
}
