//**************************************************************
// jQZoom allows you to realize a small magnifier window,close
// to the image or images on your web page easily.
//
// jqZoom version 2.2
// Author Doc. Ing. Renzi Marco(www.mind-projects.it)
// First Release on Dec 05 2007
// i'm looking for a job,pick me up!!!
// mail: renzi.mrc@gmail.com
//**************************************************************

(function($)
{
    $.fn.jqueryzoom = function(options)
    {
        var settings = {
            xzoom: 200,		//zoomed width default width
            yzoom: 200,		//zoomed div default width
            offset: 10,		//zoomed div default offset
            position: "right" ,//zoomed div default position,offset position is to the right of the image
            lens:1, //zooming lens over the image,by default is 1;
            preload: 1
        };

        if(options) {
            $.extend(settings, options);
        }

        var prevAlt = '';
        var isHovering = false;
        var imageLeft = 0;
        var imageTop = 0;
        var imageWidth = 0;
        var imageHeight = 0;
        var bigImageWidth = 0;
        var bigImageHeight = 0;
        var scaleX = 0.0;
        var scaleY = 0.0;
        var zoomBoxWidth = 0;
        var zoomBoxHeight = 0;
        var zoomBoxVisible = false;

        $(this).hover(function()
        {
            if (isHovering)
                return;

            isHovering = true;

            imageLeft = $(this).offset().left;
            imageTop = $(this).offset().top;
            imageWidth = $(this).children('img').get(0).offsetWidth;
            imageHeight = $(this).children('img').get(0).offsetHeight;

            prevAlt= $(this).children("img").attr("alt");
            $(this).children("img").attr("alt",'');

            if($("div.zoomdiv").get().length == 0)
            {
                var srcBigImage = $(this).children("img").attr("jqimg");

                $(this).after("<div class='zoomdiv'><img class='bigimg' src='"+srcBigImage+"'/></div>");
                $(this).append("<div class='jqZoomPup'>&nbsp;</div>");
            }

            if(settings.position == "right")
            {
                leftpos = imageLeft + imageWidth + settings.offset;

                if(leftpos + settings.xzoom > screen.width)
                    leftpos = imageLeft - settings.offset - settings.xzoom;
            }
            else
            {
                leftpos = imageLeft - settings.offset - settings.xzoom;

                if(leftpos < 0)
                    leftpos = imageLeft + imageWidth + settings.offset;
            }

            $("div.zoomdiv").show();

            bigImageWidth = $(".bigimg").get(0).offsetWidth;
            bigImageHeight = $(".bigimg").get(0).offsetHeight;

            $("div.zoomdiv").css({ top: imageTop,left: leftpos });
            $("div.zoomdiv").width(settings.xzoom < bigImageWidth? settings.xzoom : bigImageWidth);
            $("div.zoomdiv").height(settings.yzoom < bigImageHeight? settings.yzoom : bigImageHeight);
            //$("div.zoomdiv").width(settings.xzoom);
            //$("div.zoomdiv").height(settings.yzoom);

            scaleX = (bigImageWidth/imageWidth);
            scaleY = (bigImageHeight/imageHeight);
            zoomBoxWidth = (settings.xzoom)/scaleX;
            zoomBoxHeight = (settings.yzoom)/scaleY;

            $("div.jqZoomPup").width(zoomBoxWidth);
            $("div.jqZoomPup").height(zoomBoxHeight);

            if(!settings.lens) {
                $(this).css('cursor','crosshair');
            }

            if(settings.lens && zoomBoxWidth < imageWidth && zoomBoxHeight < imageHeight) {
                zoomBoxVisible = true;
                $("div.jqZoomPup").css('visibility','visible');
            }

            $(document.body).mousemove(function(e)
            {
                var mouse = new MouseEvent(e);
                /*$("div.jqZoomPup").hide();*/

                var xpos = mouse.x - zoomBoxWidth/2 - imageLeft;
                var ypos = mouse.y - zoomBoxHeight/2 - imageTop ;

                if(zoomBoxVisible)
                {
                    xpos = (mouse.x - zoomBoxWidth/2 < imageLeft ) ? 0 : (mouse.x + zoomBoxWidth/2 > imageWidth + imageLeft ) ?  (imageWidth -zoomBoxWidth -2)  : xpos;
                    ypos = (mouse.y - zoomBoxHeight/2 < imageTop ) ? 0 : (mouse.y + zoomBoxHeight/2  > imageHeight + imageTop ) ?  (imageHeight - zoomBoxHeight -2 ) : ypos;
                    $("div.jqZoomPup").css({ top: ypos,left: xpos });
                }

                $("div.zoomdiv").get(0).scrollLeft = xpos * scaleX ;
                $("div.zoomdiv").get(0).scrollTop = ypos * scaleY;
            });
        },
        function()
        {
            isHovering = false;
            zoomBoxVisible = false;
            $(this).children("img").attr("alt", prevAlt);
            $(document.body).unbind("mousemove");
            $("div.zoomdiv").remove();
            $("div.jqZoomPup").remove();
        });

        count = 0;

        if(settings.preload)
        {
            $('body').append("<div style='display:none;' class='jqPreload"+count+"'>sdsdssdsd</div>");

            $(this).each(function()
            {
                var imagetopreload= $(this).children("img").attr("jqimg");
                var content = jQuery('div.jqPreload'+count+'').html();

                jQuery('div.jqPreload'+count+'').html(content+'<img src=\"'+imagetopreload+'\">');
            });
        }
    }
})(jQuery);

function MouseEvent(e)
{
    this.x = e.pageX;
    this.y = e.pageY;
}


