(function($){  
	$.fn.center = function(cssPosition) {
		var element = this;

		if(typeof document.body.style.maxHeight === "undefined")
			return;

		applyCss();

		$(window).bind("resize", function(){
			applyCss();
		});

		$(element).bind("change", function() {
			applyCss();
		});


		function applyCss() {

			var elementTop = $(window).height() / 2 - $(element).height() / 2;
			var elementLeft = $(window).width() / 2 - $(element).width() / 2;

			if (elementTop < 0) elementTop = 0;
			if (elementLeft < 0) elementLeft = 0;

			$(element).css({
				"position": cssPosition,
				"left": elementLeft,
				"top": elementTop
			});
		};
	};
})(jQuery);
