/***
@title:
Center

@version:
2.0

@author:
Andreas Lagerkvist

@date:
2008-09-17

@url:
http://andreaslagerkvist.com/jquery/center/

@license:
http://creativecommons.org/licenses/by/3.0/

@copyright:
2008 Andreas Lagerkvist (andreaslagerkvist.com)

@requires:
jquery

@does:
This little pluggy centers an element on the screen using either fixed or absolute positioning. Can be used to display messages, pop up images etc.

@howto:
jQuery('#my-element').center(true); would center the element with ID 'my-element' using absolute position (leave empty for fixed).

@exampleHTML:
<p>I should be fixed centered</p>

<p>The paragraph above and the paragraph beneath this one are centered. They should be in the middle of the viewport.</p>

<p>I should be absolutely centered</p>

@exampleJS:
jQuery('#jquery-center-example p:first-child').center({ vertical: true, horizontal: true});
***/
jQuery.fn.center = function (params) {

	var options = {
					absolute: true,
					vertical: true,
					horizontal: true
				}
				op = jQuery.extend(options, params);
	
	return this.each(function () {
							   
		var self = jQuery(this);
		var posType = self.parent().css("position");
		
		self.css({
					position:	op.absolute ? 'absolute' : 'fixed',  
					zIndex:		'99',
		});
		
		if(op.vertical) {
				self.css({
					top:		'50%'
				}).css({
					marginTop:	'-' + (self.outerHeight() / 2) + 'px'
				});
		
				if (op.absolute) {
					self.css({
						marginTop:	parseInt(self.css('marginTop'), 10) + jQuery(window).scrollTop()
					});
				}
		}
		
		if(op.horizontal) {
			self.css({
				left:		'50%'
			}).css({
				marginLeft:	'-' + (self.outerWidth() / 2) + 'px'
			});
	
			if (op.absolute) {
				self.css({
					marginLeft:	parseInt(self.css('marginLeft'), 10) + jQuery(window).scrollLeft()
				});
			}
		}
	});
};


