/**
 * @author Alexandre Magno
 * @desc Center a element with jQuery
 * @version 1.0
 * @example
 * $("element").center({
 *
 * 		vertical: true,
 *      horizontal: true
 *
 * });
 * @obs With no arguments, the default is above
 * @license free
 * @param bool vertical, bool horizontal
 * @contribution Paulo Radichi
 *
 */
jQuery.fn.center = function(params) {

		var options = {

			vertical: true,
			horizontal: true,
			position: 'absolute'

		}
		op = jQuery.extend(options, params);

   return this.each(function(){

		//initializing variables
		var $self = jQuery(this);
		//get the dimensions using dimensions plugin
		var width = $self.width();
		var height = $self.height();

		//get the paddings
		var paddingTop = parseInt($.curCSS(this, 'paddingTop', true));
		var paddingBottom = parseInt($.curCSS(this, 'paddingBottom', true));
		var paddingLeft = parseInt($.curCSS(this, 'paddingLeft', true));
		var paddingRight = parseInt($.curCSS(this, 'paddingRight', true));

		//get the borders
        var borderTop = parseInt($.curCSS(this, 'borderTopWidth', true));
        var borderBottom = parseInt($.curCSS(this, 'borderBottomWidth', true));
        var borderLeft = parseInt($.curCSS(this, 'borderLeftWidth', true));
        var borderRight = parseInt($.curCSS(this, 'borderRightWidth', true));

        borderTop = isNaN(borderTop) ? 0 : borderTop;
        borderBottom = isNaN(borderBottom) ? 0 : borderBottom;
        borderLeft = isNaN(borderLeft) ? 0 : borderLeft;
        borderRight = isNaN(borderRight) ? 0 : borderRight;

		//get the media of padding and borders
		var mediaBorder = (borderTop+borderBottom)/2;
		var mediaPadding = (paddingTop+paddingBottom)/2;
		var mediaBorderLR = (borderLeft+borderRight)/2;
		var mediaPaddingLR = (paddingLeft+paddingRight)/2;
		//get the type of positioning
		var positionType = $self.parent().css("position");
		// get the half minus of width and height
		var halfWidth = Math.floor((width/2)*(-1)-mediaPaddingLR-mediaBorderLR);
		var halfHeight = Math.floor(((height/2)*(-1))-mediaPadding-mediaBorder);
		// initializing the css properties
		var cssProp = {
			position: op.position
		};

		if(op.vertical) {
			cssProp.height = height;
			cssProp.top = '50%';
			cssProp.marginTop = halfHeight;
		}
		if(op.horizontal) {
			cssProp.width = width;
			cssProp.left = '50%';
			cssProp.marginLeft = halfWidth;
		}
		//check the current position
		if(positionType == 'static') {
			$self.parent().css("position","relative");
		}
		//aplying the css
		$self.css(cssProp);


   });

};
