/**
 * @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.centerIE = function(params) {

		var options = {

			vertical: true,
			horizontal: true

		}
		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($self.css("padding-top"));
		var paddingBottom = parseInt($self.css("padding-bottom"));
		//get the borders
		var borderTop = parseInt($self.css("border-top-width"));
		var borderBottom = parseInt($self.css("border-bottom-width"));
		//get the media of padding and borders
		var mediaBorder = (borderTop+borderBottom)/2;
		var mediaPadding = (paddingTop+paddingBottom)/2;
		//get the type of positioning
		var positionType = $self.parent().css("position");
		// get the half minus of width and height
		var halfWidth = (width/2)*(-1);
		var halfHeight = ((height/2)*(-1))-mediaPadding-mediaBorder;
		// initializing the css properties
		var cssProp = {
			position: 'absolute'
		};

		if(op.vertical) {
			var doc_height = $(document).height();
		
			$self.css({'position':'absolute', 'top':doc_height/2 - height/2});
/*
			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);

   });

};

/**
 * ColourPicker 1.0
 *
 * Centers all matched elements using position: fixed (no IE)
 *
 * Usage: jQuery('div.img-overlay').center();
 *
 * @class center
 *
 * Copyright (c) 2008 Andreas Lagerkvist (andreaslagerkvist.com)
 * Released under a GNU General Public License v3 (http://creativecommons.org/licenses/by/3.0/)
 */
jQuery.fn.center = function() {

var userAgent = navigator.userAgent.toLowerCase();
		$.browser.chrome = /chrome/.test(navigator.userAgent.toLowerCase()); 
		
		// Is this a version of IE?
		if($.browser.msie){
			this.centerIE();
			return;
		}
		
        // Always return each...
        return this.each(function() {
                var t = jQuery(this);

                // Set position to other than 'static' so element shrink-wraps and width/height is calculated properly
                t.css({position: 'fixed'});

                // Why are there no jQuery.fn.outerWidth/Height:s?
                var w = t.width(), 
                        h = t.height(), 
                        lrPadding = parseInt(t.css('paddingLeft'), 10) + parseInt(t.css('paddingRight'), 10), 
                        lrBorder = parseInt(t.css('borderLeftWidth'), 10) + parseInt(t.css('borderRightWidth'), 10), 
                        tbPadding = parseInt(t.css('paddingTop'), 10) + parseInt(t.css('paddingBottom'), 10), 
                        tbBorder = parseInt(t.css('borderTopWidth'), 10) + parseInt(t.css('borderBottomWidth'), 10), 
                        leftMargin = (w + lrPadding + lrBorder) / 2;
                        topMargin = (h + tbPadding + tbBorder) / 2;

                t.css({
                        position: 'fixed', 
                        left: '50%', 
                        top: '50%', 
                        marginLeft: '-' +leftMargin +'px', 
                        marginTop: '-' +topMargin +'px', 
                        zIndex: '99'
                });

                /* Use this code if you care about IE<7, this requires the dimensions plug-in tho
                // Calculate left and top pos values
                var leftPos = (jQuery(window).width() - jQuery(this).outerWidth()) / 2 + jQuery(window).scrollLeft(), 
                        topPos = (jQuery(window).height() - jQuery(this).outerHeight()) / 2 + jQuery(window).scrollTop();

                // Make sure element is not out of bounds
                leftPos = (leftPos < 0) ? 0 : leftPos;
                topPos = (topPos < 0) ? 0 : topPos;

                jQuery(this).css({left: leftPos +'px', top: topPos +'px', zIndex: '1000'});
                */
        });
};
/*
// If dimensions plug-in isn't available
// Why is there no jQuery.fn.outerWidth bundled with jQuery?
if(!jQuery.fn.outerWidth) {
        jQuery.fn.outerWidth = function() {
                var t = jQuery(this[0]), 
                        w = t.width(), 
                        lrPadding = parseInt(t.css('paddingLeft'), 10) + parseInt(t.css('paddingRight'), 10), 
                        lrBorder = parseInt(t.css('borderLeftWidth'), 10) + parseInt(t.css('borderRightWidth'), 10);

                return w + lrPadding + lrBorder;
        };
}
if(!jQuery.fn.outerHeight) {
        jQuery.fn.outerHeight = function() {
                return this.each(function() {
                        var t = jQuery(this)[0], 
                                h = t.height(), 
                                tbPadding = parseInt(t.css('paddingTop'), 10) + parseInt(t.css('paddingBottom'), 10), 
                                tbBorder = parseInt(t.css('borderTopWidth'), 10) + parseInt(t.css('borderBottomWidth'), 10);

                        return h + tbPadding + tbBorder;
                });
        };
}
*/