﻿var $windowHeight;
var $windowWidth;
var $windowscrollLeft;
var $windowscrollTop;

(function($){ 

    //###################################################################################################################
    // My custom jQuery extension method to disable an element (selector)
    jQuery.fn.disable = function() {
        this.attr('disabled', 'disabled');
        return this;
    };
    //###################################################################################################################
    // My custom jQuery extension method to enable an element (selector)
    jQuery.fn.enable = function() {
        this.removeAttr('disabled');
        return this;
    };
    //###################################################################################################################
    $.fn.centerInClient = function(objectToCenter) {
        /// <summary>Centers the selected items in the browser window. Takes into account scroll position.
        /// Ideally the selected set should only match a single element.
        /// </summary>    
        /// <param name="fn" type="Function">Optional function called when centering is complete. Passed DOM element as parameter</param>    
        /// <param name="forceAbsolute" type="Boolean">if true forces the element to be removed from the document flow 
        ///  and attached to the body element to ensure proper absolute positioning. 
        /// Be aware that this may cause ID hierachy for CSS styles to be affected.
        /// </param>
        /// <returns type="jQuery" />
        /// Source: Rich Strahl @ http://www.west-wind.com/Weblog/posts/459873.aspx
        var opt = { forceAbsolute: true,
            container: window,    // selector of element to center in
            completeHandler: null
        };
        $.extend(opt, objectToCenter);

        return this.each(function(i) {
            var el = $(this);
            var jWin = $(opt.container);
            var isWin = opt.container == window;
            var topOffset = -200;  //This is the height of teh div that contains teh school search results

            // force to the top of document to ENSURE that document absolute positioning is available
            if (opt.forceAbsolute) {
                if (isWin)
                    el.remove().appendTo("body");
                else
                    el.remove().appendTo(jWin.get(0));
            }

            // have to make absolute
            el.css("position", "absolute");

            // height is off a bit so fudge it
            var heightFudge = isWin ? 2.0 : 1.8;

            var x = (isWin ? jWin.width() : jWin.outerWidth()) / 2 - el.outerWidth() / 2;
            var y = (isWin ? jWin.height() : jWin.outerHeight()) / heightFudge - el.outerHeight() / 2;

            el.css("left", x + jWin.scrollLeft());
            el.css("top", y + jWin.scrollTop() + topOffset);

            // if specified make callback and pass element
            if (opt.completeHandler)
                opt.completeHandler(this);
        });
    }
    //##################################################################################################################
    // Custom jQuery extension method to center an element like a modal dialog (note position:absolute)
//    jQuery.fn.center = function($windowHeight, $win   dowWidth, $windowscrollLeft, $windowscrollTop) {
    jQuery.fn.center = function() {

        $windowHeight = $(window).height();
        $windowWidth = $(window).width();
        $windowscrollTop = $(window).scrollTop();
        $windowscrollLeft = $(window).scrollLeft();
    
        this.css("position", "absolute");
        this.css("top", ($windowHeight - this.height()) / 2 + $windowscrollTop + "px");
        this.css("left", ($windowWidth - this.width()) / 2 + $windowscrollLeft - 30 + "px");
        
        return this;
    };
    //###################################################################################################################
    jQuery.fn.centerLeftRight = function() {

        $windowWidth = $(window).width();
        $windowscrollLeft = $(window).scrollLeft();

        this.css("position", "absolute");
        this.css("left", ($windowWidth - this.width()) / 2 + $windowscrollLeft - 30 + "px");

        return this;
    };
    //###################################################################################################################
    // My custom jQuery extension method to add zebra striping to a table
    jQuery.fn.zebraStripe = function() {
        this.find("tbody tr:even").addClass("zebraStripe");
        return this;
    };
})(jQuery); 
