﻿//Add-on jquery per spostare un controllo nella stessa posizione di selector
jQuery.fn.moveTo = function (selector) {    
    var position = $(selector).offset();
    $(this).css('left', position.left);
    $(this).css('top', position.top);
    return {
        left: position.left,
        top: position.top
    }
};
//Add-on jquery per confrontare la posizione di un controllo con selector
jQuery.fn.isAtPosition = function (selector) {
    var position = $(selector).offset();
    var thisPosition = $(this).offset();
    if (thisPosition.left == position.left && thisPosition.top == position.top) {
        return true;
    }
    else {
        return false;
    }
};
//Add-on jquery per controllare se this contiene el
jQuery.fn.isContainerOf = function (el) {
    while ($(el).get(0) != $(document.body).get(0)) {
        if (!el || $(el).get(0) == undefined) { return false; }
        if ($(el).get(0) == $(this).get(0)) {
            return true;
        }
        el = $(el).parent();
    }
    return false;
}
// Center an element on the screen
jQuery.fn.center = function(x,y) {
      
    if (x == undefined) {
        x = true;
    }
    if (y == undefined) {
        y = true;
    }
    var $this = $(this);
    var $window = $(window);
    $this.css({
        position: "absolute"
    });
    if (x) {
        var left = ($window.width() - $this.outerWidth())/2+$window.scrollLeft();
        $this.css('left',left)
    }
    if (!y == false) {
        var top = ($window.height() - $this.outerHeight())/2+$window.scrollTop();   
        $this.css('top',top);
    }
       
    return $(this);
}
jQuery.fn.toggleFade = function (settings, callBackFun) {
    if (settings == undefined) {
        settings = { speedIn: 'slow' };
    }

    settings = $.extend(
				{
				    speedIn: "normal",
				    speedOut: settings.speedIn
				}, settings
		);
    return this.each(function () {
        var isHidden = $(this).is(":hidden");
        $(this)[isHidden ? "fadeIn" : "fadeOut"](isHidden ? settings.speedIn : settings.speedOut, function () { callBackFun(); });
    });
};
jQuery.fn.scrollBody = function (settings, callBackFun) {
    var thisPosition = $(this).offset();
    $('html,body')
    .animate({ scrollTop: thisPosition.top }, 1000, function () {
        if (callBackFun != undefined) {
            callBackFun();
        }
    });
};

