// vertical positioning in the viewport

(function($) {
    $.fn.vCenter = function(options) {
        var pos = {
            sTop: function() {
                return window.pageYOffset || document.documentElement && document.documentElement.scrollTop || document.body.scrollTop;
            },
            wHeight: function() {
                return window.innerHeight || document.documentElement && document.documentElement.clientHeight || document.body.clientHeight;
            }
        };
        return this.each(function(index) {
            if (index == 0) {
                var $this = $(this);
                var elHeight = $this.height();
                var elTop = pos.sTop() + (pos.wHeight() / 2) - (elHeight / 2);

                if (elTop < pos.sTop())
                    elTop = pos.sTop();
                    
                $this.css({
                    position: 'absolute',
                    marginTop: '0',
                    top: elTop
                });
            }
        });
    };

})(jQuery);

(function($) {
    $.fn.hCenter = function(options) {
        var pos = {
            sLeft: function() {
                return window.pageXOffset || document.documentElement && document.documentElement.scrollLeft || document.body.scrollLeft;
            },
            wWidth: function() {
                return window.innerWidth || document.documentElement && document.documentElement.clientWidth || document.body.clientWidth;
            }
        };
        return this.each(function(index) {
            if (index == 0) {
                var $this = $(this);
                var elWidth = $this.width();
                var elLeft = pos.sLeft() + (pos.wWidth() / 2) - (elWidth / 2);

                if (elLeft < pos.sLeft())
                    elLeft = pos.sLeft();

                $this.css({
                    position: 'absolute',
                    marginLeft: '0',
                    left: elLeft
                });
            }
        });
    };

})(jQuery);

(function($) {
    $.fn.center = function(options) {
        $(this).vCenter(options);
        $(this).hCenter(options);  
    };
})(jQuery);


