(function($) {
    $.fn.grViewer = function(options) {

        var defaults = {
            zoomLimit: 2, // 1x , 2x, ...
            minLimit: 700,
            renderScale: 0.5
        };
        var options = $.extend(defaults, options);

        return this.each(function() {

            var imageZoom = $(this);
            imageZoom.width(2500);
            var originalWidth = imageZoom.width();

            imageZoom.width(originalWidth * options.renderScale);

            imageZoom.parent().css({ 'overflow': 'hidden' });
            $(this).hide().fadeIn(600);

            $('#zoomerNav, .lnkdownloadrecord, #printrecord').fadeIn('slow');
            if ($.browser.msie) {
                $('.invert').fadeIn('slow');
            }

            function widthError() { } //alert('zooming more than this will cause pixelation'); }
            function minWidthError() { } //alert('the record can not get smaller than this'); }

            var move = function(event) {

                if ($(this).data('mousemove')) {

                    var changeX = event.clientX - $(this).data('mouseX');
                    var changeY = event.clientY - $(this).data('mouseY');

                    var position = $(this).position();

                    var newX = position.left + changeX;
                    var newY = position.top + changeY;

                    $(this).css({ left: newX, top: newY });

                    $(this).data('mouseX', event.clientX);
                    $(this).data('mouseY', event.clientY);

                    event.preventDefault();
                }
            }



            $(this).dblclick(function(event) {

                var parentPosition = $(this).parent().position();

                var distancefromCentreX = ($(this).parent().width() / 2 + parentPosition.left) - $(this).data('mouseX');
                var distancefromCentreY = ($(this).parent().height() / 2 + parentPosition.top) - $(this).data('mouseY');


                if (distancefromCentreX < 0 && distancefromCentreY < 0) {

                    if (imageZoom.width() <= originalWidth * options.zoomLimit) {
                        $(this).animate({
                            top: parseInt($(this).css('top')) - Math.abs(distancefromCentreY),
                            left: parseInt($(this).css('left')) - Math.abs(distancefromCentreX),
                            width: $(this).width() + 200
                        }, 300);
                    } else {
                        $(this).animate({
                            top: parseInt($(this).css('top')) - Math.abs(distancefromCentreY),
                            left: parseInt($(this).css('left')) - Math.abs(distancefromCentreX)
                        }, 300);
                    }

                }
                if (distancefromCentreX > 0 && distancefromCentreY > 0) {
                    if (imageZoom.width() <= originalWidth * options.zoomLimit) {
                        $(this).animate({
                            top: parseInt($(this).css('top')) + Math.abs(distancefromCentreY),
                            left: parseInt($(this).css('left')) + Math.abs(distancefromCentreX),
                            width: $(this).width() + 200
                        }, 300);
                    } else {
                        $(this).animate({
                            top: parseInt($(this).css('top')) + Math.abs(distancefromCentreY),
                            left: parseInt($(this).css('left')) + Math.abs(distancefromCentreX)
                        }, 300);
                    }
                }
                if (distancefromCentreX < 0 && distancefromCentreY > 0) {
                    if (imageZoom.width() <= originalWidth * options.zoomLimit) {
                        $(this).animate({
                            top: parseInt($(this).css('top')) + Math.abs(distancefromCentreY),
                            left: parseInt($(this).css('left')) - Math.abs(distancefromCentreX),
                            width: $(this).width() + 200
                        }, 300);
                    } else {
                        $(this).animate({
                            top: parseInt($(this).css('top')) + Math.abs(distancefromCentreY),
                            left: parseInt($(this).css('left')) - Math.abs(distancefromCentreX)
                        }, 300);
                    }
                }
                if (distancefromCentreX > 0 && distancefromCentreY < 0) {
                    if (imageZoom.width() <= originalWidth * options.zoomLimit) {
                        $(this).animate({
                            top: parseInt($(this).css('top')) - Math.abs(distancefromCentreY),
                            left: parseInt($(this).css('left')) + Math.abs(distancefromCentreX),
                            width: $(this).width() + 200
                        }, 300);
                    } else {
                        $(this).animate({
                            top: parseInt($(this).css('top')) - Math.abs(distancefromCentreY),
                            left: parseInt($(this).css('left')) + Math.abs(distancefromCentreX)
                        }, 300);
                    }
                }

                distancefromCentreX = 0;
                distancefromCentreY = 0;
            });


            $(this).mousedown(function(event) {
                $(this).data('mousemove', true);
                if ($.browser.msie) {
                    $(this).css({ 'cursor': 'url(../../../../_resources/images/cursors/grabbing.cur)' });
                }
                if ($.browser.mozilla) {
                    $(this).css({ 'cursor': '-moz-grabbing' });
                }
                else {
                    $(this).css({ 'cursor': 'url(../../../../_resources/images/cursors/grabbing.cur)' });
                }

                $(this).data('mouseX', event.clientX);
                $(this).data('mouseY', event.clientY);
                event.preventDefault();
            });

            $(this).mouseup(function() {
                $(this).data('mousemove', false);
                if ($.browser.msie) {
                    $(this).css({ 'cursor': 'url(../../../../_resources/images/cursors/grab.cur)' });
                }
                if ($.browser.mozilla) {
                    $(this).css({ 'cursor': '-moz-grab' });
                }
                else {
                    $(this).css({ 'cursor': 'url(../../../../_resources/images/cursors/grab.cur)' });
                }
            });
            $(this).mouseout(function() {
                $(this).data('mousemove', false);
                if ($.browser.msie) {
                    $(this).css({ 'cursor': 'url(../../../../_resources/images/cursors/grab.cur)' });
                }
                if ($.browser.mozilla) {
                    $(this).css({ 'cursor': '-moz-grab' });
                }
                else {
                    $(this).css({ 'cursor': 'url(../../../../_resources/images/cursors/grab.cur)' });
                }
            });

            $(this).mouseout(move);
            $(this).mousemove(move);


            // Nav Control events

            function increaseSize() {
                if (imageZoom.width() <= originalWidth * options.zoomLimit) {
                    imageZoom.stop(false, true).animate({ width: imageZoom.width() + 300, left: parseInt(imageZoom.css('left')) - 150, top: parseInt(imageZoom.css('top')) - 150 }, 500);
                    return false
                }
                else {
                    imageZoom.stop();
                    widthError();
                    return false
                }
            }

            function decreaseSize() {
                if (imageZoom.width() >= options.minLimit) {
                    imageZoom.stop(false, true).animate({ width: imageZoom.width() - 300, left: parseInt(imageZoom.css('left')) + 150, top: parseInt(imageZoom.css('top')) + 150 }, 500);

                    // Limit for not allowing the image to get lost in the bottom
                    if (parseInt(imageZoom.css('top')) > 400) {
                        imageZoom.stop(false, false).css({ top: parseInt(imageZoom.css('top')) });
                    }

                    return false
                }
                else {
                    imageZoom.stop();
                    minWidthError();
                    return false
                }
            }

            $('#btn_plus').click(function() { increaseSize(); return false });
            $('#btn_minus').click(function() { decreaseSize(); return false });

            imageZoom.bind('mousewheel', function(event, delta) {
                if (delta > 0) {
                    if (imageZoom.width() <= originalWidth * options.zoomLimit) {
                        imageZoom.stop(false, true).animate({ width: imageZoom.width() + 200, left: parseInt(imageZoom.css('left')) - 100, top: parseInt(imageZoom.css('top')) - 100 }, 200);
                        return false
                    }
                    else {
                        imageZoom.stop();
                        widthError();
                        return false
                    }

                }
                if (delta < 0) {
                    if (imageZoom.width() >= options.minLimit) {
                        imageZoom.stop(false, true).animate({ width: imageZoom.width() - 200, left: parseInt(imageZoom.css('left')) + 100, top: parseInt(imageZoom.css('top')) + 100 }, 200);


                        // Limit for not allowing the image to get lost in the bottom
                        if (parseInt(imageZoom.css('top')) > 400) {
                            imageZoom.stop(false, false).css({ top: parseInt(imageZoom.css('top')) });
                        }

                        return false
                    }
                    else {
                        imageZoom.stop();
                        minWidthError();
                        return false
                    }
                }
            })

            // the random numbers are just offset which should be left from the image at least!

            function dnBoundryIsValid() {
                if (parseInt(imageZoom.css('top')) + Math.abs(imageZoom.height() - imageZoom.parent().height() + 150) < 0) { return false; }
                else { return true; }
            }

            function upBoundryIsValid() {
                if (parseInt(imageZoom.css('top')) > (imageZoom.parent().height() - 450)) { return false; }
                else { return true; }
            }
            function leftBoundryIsValid() {
                if (parseInt(imageZoom.css('left')) > (imageZoom.parent().width() - 450)) { return false; }
                else { return true; }
            }
            function rightBoundryIsValid() {
                if (parseInt(imageZoom.css('left')) + Math.abs(imageZoom.width() - imageZoom.parent().width() + 400) < 0) { return false; }
                else { return true; }
            }



            $('#btn_up').click(function() {
                if (upBoundryIsValid()) {
                    imageZoom.stop(false, true).animate({ top: parseInt(imageZoom.css('top')) + 200 }, 500);
                    return false
                }
                else {
                    return false;
                }

            });
            $('#btn_dn').click(function() {
                if (dnBoundryIsValid()) {
                    imageZoom.stop(false, true).animate({ top: parseInt(imageZoom.css('top')) - 200 }, 500);
                    return false
                }
                else {
                    return false;
                }

            });
            $('#btn_left').click(function() {
                if (leftBoundryIsValid()) {
                    imageZoom.stop(false, true).animate({ left: parseInt(imageZoom.css('left')) + 200 }, 500);
                    return false;
                }
                else {
                    return false;
                }

            });
            $('#btn_right').click(function() {
                if (rightBoundryIsValid()) {
                    imageZoom.stop(false, true).animate({ left: parseInt(imageZoom.css('left')) - 200 }, 500);
                    return false
                }
                else {
                    return false
                }

            });
            $('#btn_home').click(function() {
                imageZoom.stop(false, true).animate({ width: originalWidth * options.renderScale, left: 0, top: 0 }, 800);
                return false
            });

            if ($.browser.msie) {
                $('#invertimage').toggle(function() {
                    imageZoom.css({ 'filter': 'progid:DXImageTransform.Microsoft.BasicImage(invert=1)' });
                    return false
                }, function() {
                    imageZoom.css({ 'filter': 'none' });
                    return false
                });
            }
            else {
                return false
            }



        });
    };
})(jQuery);