﻿(function($) {
 
    $.fn.jqueryzoom = function(options) {
        var settings = {
            offset: 10, 	
            position: "right"  
        };
 
        if (options) {
            $.extend(settings, options);
        }
        $bigimagediv = $("div.zoomdiv");
        $bigimage = $(".bigimg");
 
        $(this).hover(function() {
            var $img = $(this);
            var position = $img.offset();
            var imageLeft = position.left;
            var imageTop = position.top;
            var imageWidth = $img.width();
            var imageHeight = $img.height();
 
            $bigimage.attr('src',"/images/products/" + $img.attr("bgimage"));
            
            if (settings.position == "right")
                leftpos = imageLeft + imageWidth + settings.offset;
            else
                leftpos = imageLeft - $("div.zoomdiv").width() - settings.offset;
 
            $bigimagediv.css({ top: imageTop, left: leftpos });
            $bigimagediv.show();
 
 
            $(document.body).mousemove(function(e) {
                var bigwidth = $bigimage.get(0).offsetWidth;
                var bigheight = $bigimage.get(0).offsetHeight;
                var scaley = 'x';
                var scalex = 'y';
                if (isNaN(scalex) | isNaN(scaley)) {
                    var scalex = Math.round(bigwidth / imageWidth);
                    var scaley = Math.round(bigheight / imageHeight);
                }
                scrolly = e.pageY - imageTop - ($("div.zoomdiv").height() * 1 / scaley) / 2;
                $("div.zoomdiv").get(0).scrollTop = scrolly * scaley;
                scrollx = e.pageX - imageLeft - ($("div.zoomdiv").width() * 1 / scalex) / 2;
                $("div.zoomdiv").get(0).scrollLeft = (scrollx) * scalex;
 
 
            });
        }, function() {
            $bigimagediv.hide();
            $(document.body).unbind("mousemove");
        });
 
    }
})(jQuery);
 
 
