;(function($) {
	
	$.centerInClient = {
		defaults: {
			forceAbsolute: false,
			container: window,    // selector of element to center in
			completeHandler: null
		}
	};
	
	$.fn.extend({
		centerInClient: function(options) {

			opt = $.extend({}, $.centerInClient.defaults, options);
			
			return this.each(function(i) {
				
				var size = sizes();
				var el = $(this);
				var jWin = $(opt.container);
				var isWin = opt.container == window;

				if (opt.forceAbsolute) {
					if (isWin)
						el.remove().appendTo("body");
					else
						el.remove().appendTo(jWin.get(0));
				}
		
				el.css("position", "absolute");
		
				var heightFudge = isWin ? 2.0 : 1.8;
		
				var x = (isWin ? jWin.width() : jWin.outerWidth()) / 2 - el.outerWidth() / 2;
				var y = (isWin ? jWin.height() : jWin.outerHeight()) / heightFudge - el.outerHeight() / 2;				
		
				el.css("left", x + jWin.scrollLeft());
				//el.css("top", y + jWin.scrollTop());
				
				el.css("top", (size.height - $(this).height()) / 2);
		
				if (opt.completeHandler)
					opt.completeHandler(this);
					
			
			});
		},
		
		sizes: function() {
			
			return sizes();
			
		}
	});
	
	function sizes() {
		
		if (window.innerHeight) {
			wh = window.innerHeight;
			ww = window.innerWidth;
		} else if (document.documentElement.clientHeight>0) {
			wh = document.documentElement.clientHeight;
			ww = document.documentElement.clientWidth;
		}else{
			wh = document.body.clientHeight;
			ww = document.body.clientWidth;
		}
		
		return { width: ww, height: wh };
	}
	
})(jQuery);
