﻿jQuery.fn.center = function() {
    this.css("position", "absolute");
    if (!($(window).height() < this.height()) && !($(window).width() < this.width())) {
        // più alto e più largo
        this.css("top", ($(window).height() - this.height()) / 2 + $(window).scrollTop() + "px");
        this.css("left", ($(window).width() - this.width()) / 2 + $(window).scrollLeft() + "px");
    } else if (!($(window).height() > this.height()) && !($(window).width() < this.width())) {
        // meno alto ma più largo
        this.css("top", "0px");
        this.css("left", ($(window).width() - this.width()) / 2 + $(window).scrollLeft() + "px");
    } else if (!($(window).height() < this.height()) && !($(window).width() > this.width())) {
        // più alto ma meno largo
        this.css("top", ($(window).height() - this.height()) / 2 + $(window).scrollTop() + "px");
        this.css("left", "0px");
    } else {
        // meno alto e meno largo
        this.css("top", "0px");
        this.css("left", "0px");
    }
    return this;
}

