(function($) {
    $.fn.zoom = function(params) {
        /*
        Zoom jQuery Plugin
        Copyright: (C) 2009 - Francesco Paggin
        Parameters:
            (int) duration: the duration of the zooming animaton
            (int) small_width, small_height: the dimensions of the small thumbnails
            (int) big_width, big_height: the dimensions of the zoomed images
        */
        var defaults = {
            duration: 200,
            small_width:940,
            small_height:470,
            big_width:2820,
            big_height:1410
        };
        var options = $.extend(defaults, params);

        return this.each(function() {
            //caching...
            var $container = $(this);
            var $link = $container.find('a');
            var $image = $link.find('img');

            //reading some parameters...
            var small_path = $image.attr('src');
            var big_path   = $link.attr('href');

            //status variables...
            var dragging = false;
            var zoomed   = false;

            //calculate correct positioning...
            var a_width  = (options.big_width  - options.small_width)  * 2 + options.small_width;
            var a_height = (options.big_height - options.small_height) * 2 + options.small_height;

            var a_top  = options.big_height - options.small_height;
            var a_left = options.big_width  - options.small_width;

            /*
                INITIALIZATION...
            */
            $link.css({
                display:'block',
                position:'absolute',
                width: a_width   + 'px',
                height: a_height + 'px',
                top:  '-' + a_top  + 'px',
                left: '-' + a_left + 'px'
            });

            /* Removing default link behaviour... */
            $link.removeAttr('href');

            $image.css({
                'cursor' : 'url(/assets/templates/votec/images/icons/magnifier.cur), -moz-zoom-in',
                display:'block',
                position:'absolute',
                width:  options.small_width + 'px',
                height: options.small_height + 'px',
                top: a_top + 'px',
                left: a_left + 'px'
            });

            /*
                HANDLE EVENTS...
            */
            $image.mouseup(function() {
                if (!dragging) {
                    if (zoomed) {
                        zoomOut();
                    } else {
                        zoomIn();
                    }
                }
            });

            /*
                METHODS...
            */
            function zoomOut() {
                $image.animate({
                    width:options.small_width + 'px',
                    height:options.small_height + 'px',
                    top:  a_top  + 'px',
                    left: a_left + 'px'
                }, options.duration, swapWithSmall);
                $image.css({
                    'cursor' : 'url(/assets/templates/votec/images/icons/magnifier.cur), -moz-zoom-in',
                    'z-index' : '1'
                })
                $("div.bikever").css({'display':'block'})
                $("div.biketyp").css({'display':'block'})
                $("div.fullview").css({'display':'block'})
                $("div.fb-like").css({'display':'block'})
                $("div.google-like").css({'display':'block'})
                zoomed = false;
            }

            function zoomIn() {
                $image.animate({
                    width:options.big_width + 'px',
                    height:options.big_height + 'px',
                    top:  a_top / 2  + 'px',
                    left: a_left / 2 + 'px'
                }, options.duration, swapWithBig);
                $image.css({
                    'cursor' : 'move',
                    'z-index' : '20'
                })
                $("div.bikever").css({'display':'none'})
                $("div.biketyp").css({'display':'none'})
                $("div.fullview").css({'display':'none'})
                $("div.fb-like").css({'display':'none'})
                $("div.google-like").css({'display':'none'})
                zoomed = true;
            }

            function swapWithBig() {
                $image.attr('src', big_path);
                $image.draggable({
                    containment: 'parent',
                    start: handleStartDrag,
                    stop: handleStopDrag
                });
            }

            function swapWithSmall() {
                $image.attr('src', small_path);
                $image.draggable('destroy');
            }

            function handleStartDrag() {
                dragging = true;
            }

            function handleStopDrag() {
                dragging = false;
            }
        });
    }
})(jQuery)
