﻿(function() {
    $.fn.verticalCenter = function(settings) {

        /***
        * 
        * 	Vertically center a container.
        * 
        *  @example 
        $("#content").verticalCenter({
        minH:580,
        maxH:680,
        offsetTop:60,
        offsetBot:60,
        container:"#wrapper"
        });
        *  @result - makes calculations based on #content and apples the margins to
        *  		  the element in the json "#wrapper"	
        * 
        *  
        *  @param (Integer)	minH - default 0 - min height allowed for content
        *  @param (Integer) 	maxH - default 0 - max height allowed for content
        *  @param (Integer) 	offsetTop - default 0 - minimum amount of padding to be 
        *  					applied at the top of the centering element
        *  @param (Integer) 	offsetBot - default 0 - minimum amount of padding to be 
        *  					applied at the bottom of the centering element
        *  @param (String)  	container - defaults to form#aspnetForm - you can pass
        *  					either a class or id
        *  					
        */


        settings = jQuery.extend({
            minH: 0,
            maxH: 0,
            offsetTop: 0,
            offsetBot: 0,
            container: "form#aspnetForm"
        }, settings);


        // vertical align
        var docH = $(this).height();
        //console.log(docH);
        var windowH = $(window).height();
        docH = (docH < settings["minH"]) ? settings["minH"] : docH;

        var _offset = { top: 0, bottom: 0 };
       
        _offset["top"] = _offset["bottom"] = (docH >= settings["maxH"]) ? (docH>windowH)?0 :(windowH - settings["maxH"]) / 2 :
                                                 (docH > windowH) ? settings["offsetTop"] : (windowH - docH) / 2;
        
        _offset["top"] = (_offset["top"] <= settings["offsetTop"]) ? 0 : _offset["top"] - settings["offsetTop"];
        _offset["bottom"] = (_offset["bottom"] <= settings["offsetBot"]) ? settings["offsetBot"] : _offset["bottom"] - settings["offsetBot"];

        $(settings["container"]).css("margin-top", _offset["top"]);
        $(settings["container"]).css("margin-bottom", _offset["bottom"]);

        return this;
    }; //Close function

})(jQuery);


