(function($) {
    $.fn.extend({
        center: function(options) {
			var defaults = {
				zIndex: '2100'
			};
			var options = jQuery.extend(defaults, options);

		    // Always return each...
			return this.each(function() {
		        var t = jQuery(this);
		        jQuery(t).appendTo(jQuery('body')); // make sure to be on top of dom and the last in line
				if (jQuery.browser.msie && parseInt(jQuery.browser.version) < 9) {
		            var leftPos = (jQuery(window).width() - jQuery(this).outerWidth()) / 2 + jQuery(window).scrollLeft(),
		                 topPos = (jQuery(window).height() - jQuery(this).outerHeight()) / 2 + jQuery(window).scrollTop();

		            // Make sure element is not out of bounds
		            leftPos = (leftPos < 0) ? 0 : leftPos;
		            topPos = (topPos < 0) ? 0 : topPos;
		            jQuery(this).css({left: leftPos +'px', top: topPos +'px', zIndex: options.zIndex, position: 'absolute'});
				} else {
		            // Set position to other than 'static' so element shrink-wraps and width/height is calculated properly
		            t.css({position: 'fixed'});
		            // Why are there no jQuery.fn.outerWidth/Height:s?
		            var w = t.width(),
		                    h = t.height(),
		                    lrPadding = parseInt(t.css('paddingLeft'), 10) + parseInt(t.css('paddingRight'), 10),
		                    lrBorder = parseInt(t.css('borderLeftWidth'), 10) + parseInt(t.css('borderRightWidth'), 10),
		                    tbPadding = parseInt(t.css('paddingTop'), 10) + parseInt(t.css('paddingBottom'), 10),
		                    tbBorder = parseInt(t.css('borderTopWidth'), 10) + parseInt(t.css('borderBottomWidth'), 10),
		                    leftMargin = (w + lrPadding + lrBorder) / 2;
		                    topMargin = (h + tbPadding + tbBorder) / 2;
		            t.css({
		                    position: 'fixed',
		                    left: '50%',
		                    top: '50%',
		                    marginLeft: '-' +leftMargin +'px',
		                    marginTop: '-' +topMargin +'px',
		                    zIndex: options.zIndex
		            });
		            // scrollable hack
		            	var position = t.position();
			            var top = position.top + parseInt(t.css('margin-top'));
			            var left = position.left;
						t.css('position', 'absolute').css('top', top+'px').css('left', left+'px').css('margin', '0');

					// the really center hack
					var arrPageSize = getPageSize();
					var centertop = top + (arrPageSize[3] - h)/2;
					if(centertop < 0)
						centertop = 0;
					if(centertop < top)
						centertop = top;
					var centerleft = (arrPageSize[2] - w)/2;
					if(centerleft < 0)
						centerleft = 0;
					t.css('top', centertop +'px').css('left', centerleft + 'px');
				}
		    });
        }
    });
})(jQuery);



function getPageSize(){
	var xScroll, yScroll;
	if (window.innerHeight && window.scrollMaxY) {
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	}
	else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	 }
	 else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
	 	 xScroll = document.body.offsetWidth;
	 	 yScroll = document.body.offsetHeight;
	  }

	var windowWidth, windowHeight;
	if (self.innerHeight) { // all except Explorer
		 windowWidth = self.innerWidth;
		 windowHeight = self.innerHeight;
	 }
	 else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
	 	windowWidth = document.documentElement.clientWidth;
	 	windowHeight = document.documentElement.clientHeight;
	 }
	 else if (document.body) { // other Explorers
	 	windowWidth = document.body.clientWidth;
	 	windowHeight = document.body.clientHeight;
	 }

	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	}
	else {
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){
		pageWidth = windowWidth;
	}
	else {
		pageWidth = xScroll;
	 }

	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight);
	return arrayPageSize;
}
