/**
 * @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
	}
	
	op = jQuery.extend(options, params);

	return this.each(function() {

		//initializing variables
		var $self = jQuery(this);

		// initializing the css properties
		var cssProp = {
			position: 'absolute'
		};

		if (op.vertical) {
			cssProp.top = getPageScroll()[1] + (getPageHeight() / 2) - ($self.height() / 2);
		}
		if (op.horizontal) {
			cssProp.left = $(window).width() / 2 - ($self.width() / 2);
		}
		//check the current position
		cssProp.position = "absolute";
		
		//aplying the css
		$self.css(cssProp);
	});

  function getPageScroll() {
    var xScroll, yScroll;
    if (self.pageYOffset) {
      yScroll = self.pageYOffset;
      xScroll = self.pageXOffset;
    } else if (document.documentElement && document.documentElement.scrollTop) {	 // Explorer 6 Strict
      yScroll = document.documentElement.scrollTop;
      xScroll = document.documentElement.scrollLeft;
    } else if (document.body) {// all other Explorers
      yScroll = document.body.scrollTop;
      xScroll = document.body.scrollLeft;	
    }
    return new Array(xScroll,yScroll) 
  }

  // Adapted from getPageSize() by quirksmode.com
  function getPageHeight() {
    var windowHeight
    if (self.innerHeight) {	// all except Explorer
      windowHeight = self.innerHeight;
    } else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
      windowHeight = document.documentElement.clientHeight;
    } else if (document.body) { // other Explorers
      windowHeight = document.body.clientHeight;
    }	
    return windowHeight
  }

};
