(function($) {
    $.fn.fancyZoom2 = function(options) {

        var options = options || {};
        var directory = options && options.directory ? options.directory : '/Assets/images/zoom';
        var zooming = false;

        if ($('#zoom2').length == 0) {
            var ext = $.browser.msie ? 'gif' : 'png';
            var html = '<div id="zoom2_overlay" class="ajaxcontentoverlay" style="display:none; width:100%; height:100%;"></div> \
            <div id="zoom2" style="display:none; z-index:1000;"> \
                  <table id="zoom2_table" style="border-collapse:collapse; width:100%; height:100%; "> \
                    <tbody> \
                      <tr> \
                        <td class="tl" style="background:url(' + directory + '/tl.' + ext + ') 0 0 no-repeat; width:20px; height:20px; overflow:hidden;" /> \
                        <td class="tm" style="background:url(' + directory + '/tm.' + ext + ') 0 0 repeat-x; height:20px; overflow:hidden;" /> \
                        <td class="tr" style="background:url(' + directory + '/tr.' + ext + ') 100% 0 no-repeat; width:20px; height:20px; overflow:hidden;" /> \
                      </tr> \
                      <tr> \
                        <td class="ml" style="background:url(' + directory + '/ml.' + ext + ') 0 0 repeat-y; width:20px; overflow:hidden;" /> \
                        <td class="mm" style="background:#fff; vertical-align:top; padding:10px;"> \
                          <div id="zoom2_content"> \
                          </div> \
                        </td> \
                        <td class="mr" style="background:url(' + directory + '/mr.' + ext + ') 100% 0 repeat-y;  width:20px; overflow:hidden;" /> \
                      </tr> \
                      <tr> \
                        <td class="bl" style="background:url(' + directory + '/bl.' + ext + ') 0 100% no-repeat; width:20px; height:20px; overflow:hidden;" /> \
                        <td class="bm" style="background:url(' + directory + '/bm.' + ext + ') 0 100% repeat-x; height:20px; overflow:hidden;" /> \
                        <td class="br" style="background:url(' + directory + '/br.' + ext + ') 100% 100% no-repeat; width:20px; height:20px; overflow:hidden;" /> \
                      </tr> \
                    </tbody> \
                  </table> \
                </div>';

            $('#overlaycontent').append(html);
        }

        var zoom = $('#zoom2');
        var zoom2_table = $('#zoom2_table');
        var zoom2_content = $('#zoom2_content');
        var middle_row = $('td.ml,td.mm,td.mr');

        this.each(function(i) {

            var content_div = $(this);
            content_div.hide();
            if (zooming) return false;
            zooming = true;
            var zoom2_width = options.width;
            var zoom2_height = options.height;

            var width = window.innerWidth || (window.document.documentElement.clientWidth || window.document.body.clientWidth);
            var height = window.innerHeight || (window.document.documentElement.clientHeight || window.document.body.clientHeight);
            var x = window.pageXOffset || (window.document.documentElement.scrollLeft || window.document.body.scrollLeft);
            var y = window.pageYOffset || (window.document.documentElement.scrollTop || window.document.body.scrollTop);
            var window_size = { 'width': width, 'height': height, 'x': x, 'y': y }

            var width = (zoom2_width || content_div.width()) + 60;
            var height = (zoom2_height || content_div.height()) + 60;
            var d = window_size;

            // ensure that newTop is at least 0 so it doesn't hide close button
            var newTop = Math.max((d.height / 2) - (height / 2) + y, 0);
            var newLeft = (d.width / 2) - (width / 2);
            var curTop = height / 2;
            var curLeft = width / 2;

            $('#zoom2').hide().css({
                position: 'absolute',
                top: curTop + 'px',
                left: curLeft + 'px',
                width: '1px',
                height: '1px'
            });

            fixBackgroundsForIE();

            if (options.scaleImg) {
                zoom2_content.html(content_div.html());
                $('#zoom2_content img').css('width', '100%');
            } else {
                zoom2_content.html('');
            }
            $('#zoom2_overlay').css({ display: "block", opacity: .95 });
            $('#zoom2').animate({
                top: newTop + 'px',
                left: newLeft + 'px',
                opacity: "show",
                width: width,
                height: height
            }, 500, null, function() {
                if (options.scaleImg != true) {
                    zoom2_content.html(content_div.html());
                    content_div.html('');
                }
                unfixBackgroundsForIE();

                zooming = false;
            })
        });

        return this;


        function switchBackgroundImagesTo(to) {
            $('#zoom2_table td').each(function(i) {
                var bg = $(this).css('background-image').replace(/\.(png|gif|none)\"\)$/, '.' + to + '")');
                $(this).css('background-image', bg);
            });

        }

        function fixBackgroundsForIE() {
            if ($.browser.msie && parseFloat($.browser.version) >= 7) {
                switchBackgroundImagesTo('gif');
            }
        }

        function unfixBackgroundsForIE() {
            if ($.browser.msie && $.browser.version >= 7) {
                switchBackgroundImagesTo('png');
            }
        }
    }
})(jQuery);
