﻿(function($){
    $.fn.center = function(){
        var element = this;
        $(element).show();

        interval = window.setInterval(function(){
            changeCss();
        }, 100);
        $(window).bind("resize", function(){
            changeCss();
        });

        function changeCss(){
            window.clearInterval(interval);
            if (navigator.appName.indexOf("Netscape")!= -1) {
                myCurrentYPos = window.pageYOffset;
            } else {
                if (document.documentElement &&
                    document.documentElement.scrollTop
                ) {
                    myCurrentYPos = document.documentElement.scrollTop;
                } else {
                    myCurrentYPos = document.body.scrollTop;
                }
            }

            var imageHeight = $(element).height();
            var imageWidth = $(element).width();
            var windowWidth = $(window).width();
            var windowHeight = (window.innerHeight || (window.document.documentElement.clientHeight || window.document.body.clientHeight));

            $(element).css({
                "position" : "absolute",
                "left" : windowWidth / 2 - imageWidth / 2,
                "top" : windowHeight /2 - imageHeight / 2+ myCurrentYPos
            });
        };

    };

})(jQuery);
