﻿///This is a place to put your general purpose, jQuery specific functions that are NOT SITE SPECIFIC.
///Think of this as a jQuery toolbelt that can be used on any site.
///functions that are plain JavaScript should go in general.js and other frameworks should go in general-[framework].js

(function($) {

    ///Aligns the element vertically within its parent.
    ///Usage - $(element).vAlign();
    ///Author - BWM
    $.fn.vAlign = function() {
        return this.each(function() {
            var h = $(this).outerHeight();
            if (h < 1) return false;
            var pih = $(this).parent().innerHeight();
            var top = (pih - h) / 2;
            $(this).css("position", "absolute");
            $(this).css("top", top + "px");
        });
    };

    ///Aligns the element vertically within its parent with animation.
    ///Default animation is 500ms
    ///Usage - $(element).vAlignAnimate(); or $(element).vAlignAnimate(milliseconds);
    ///Author - BWM
    $.fn.vAlignAnimate = function(speed) {
        if (!speed) speed = 500;
        return this.each(function() {
            var h = $(this).outerHeight();
            if (h < 1) return false;
            var pih = $(this).parent().innerHeight();
            var top = (pih - h) / 2;
            $(this).css("position", "absolute");
            $(this).animate({ top: top }, speed);
        });
    };

    ///Aligns the element vertically and/or horizontally within the specified container element.
    ///container defaults to window.
    ///vertical and horizontal default to true.
    ///Usage - $(element).align(); or $(element).align(otherElement); or $(element).align(otherElement, false); etc.
    ///Author - BWM
    $.fn.align = function(container, vertical, horizontal) {
        return this.each(function() {
            if (container === undefined) container = window;
            if (vertical === undefined) vertical = true;
            if (horizontal === undefined) horizontal = true;
            var h = $(this).outerHeight();
            var w = $(this).outerWidth();
            if (h < 1 || w < 1) return false; //exit if height or width is 0
            var ch; //container height
            var cw; //container width
            var top;
            var left;
            if (container == window || container == document) {
                ch = $(container).height();
                cw = $(container).width();
                top = (ch - h) / 2;
                left = (cw - w) / 2;
            }
            else {
                var offset = $(container).offset();
                ch = $(container).innerHeight();
                cw = $(container).innerWidth();
                top = ((ch - h) / 2) + offset.top;
                left = ((cw - w) / 2) + offset.left;
            }
            $(this).css("position", "absolute");
            if (vertical) $(this).css("top", top + "px");
            if (horizontal) $(this).css("left", left + "px");
        });
    };

})(jQuery);
