﻿// JScript File

// ensure the 'push' method exists for an Array
if (!Array.prototype.push) Array.prototype.push = function() {
    for (var i=0; i<arguments.length; i++) this[this.length] = arguments[i];
    return this.length;
}

jQuery.fn.floatmiddle = function(){
    function FloatMiddleClass(objectToFloat)
    {
        // ************************************************************************************************************************
        // Routine to get the size and scroll position of the Window (regardless of browser)
        // ************************************************************************************************************************
        function GetWindowInformation() { 
            var scrOfX = 0, scrOfY = 0, myWidth = 0, myHeight = 0; 

            if( typeof(window.pageYOffset) == 'number' ) 
            { 
                with (window) //Netscape compliant 
                {
                    scrOfY = pageYOffset; scrOfX = pageXOffset; 
                    myWidth = innerWidth; myHeight = innerHeight; 
                }
            } 
            else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop || document.documentElement.clientWidth || document.documentElement.clientHeight) ) 
            { 
                with (document.documentElement) //IE6 standards compliant mode
                {
                    scrOfY = scrollTop; scrOfX = scrollLeft; 
                    myWidth = clientWidth; myHeight = clientHeight; 
                }
            } 
            else if( document.body && ( document.body.scrollLeft || document.body.scrollTop || document.body.clientWidth || document.body.clientHeight) ) 
            { 
                with (document.body) //DOM compliant
                { 
                    scrOfY = scrollTop; scrOfX = scrollLeft; 
                    myWidth = clientWidth; myHeight = clientHeight; 
                }
            } 
            return {left:scrOfX, top:scrOfY, width:myWidth, height:myHeight};
        }
          // ************************************************************************************************************************
          // Place the associated HTML object in the middle of the browser window
          // ************************************************************************************************************************
          this.Place = function()
          {
                var window = GetWindowInformation();
            var object = { width: floatObject.width(), height: floatObject.height() };
            var floatPosition = {
                top: object.height>window.height?0:(window.height-object.height)/2 + window.top,
                left: object.width>window.width?0:(window.width-object.width)/2 + window.left
            };
            
                if (!_isLoading) floatObject.stop(false, false).animate(floatPosition, 500);
                else floatObject.stop(false, false).css(floatPosition);
          }

          var floatObject = $(objectToFloat).css({"position": "absolute", "margin": "0"});
          this.Place();
    }

    function PositionAll() { for (var i=0; i<_floating.length; i++) _floating[i].Place(); }

    var _isLoading = true;
    $(window).load(function() { PositionAll(); _isLoading = false; }).scroll(PositionAll).resize(PositionAll);

    var _floating = new Array();
      return this.each(function(i) { _floating.push(new FloatMiddleClass(this)); });
}

