﻿/*
*       Developed by Justin Mead
*       ©2009 MeadMiracle
*		www.meadmiracle.com / meadmiracle@gmail.com
*       Version 1.0
*       Testing: IE7/Windows XP
*                Firefox/Windows XP
*       Licensed under the Creative Commons GPL http://creativecommons.org/licenses/GPL/2.0/
*
*       OPTIONS LISTING:
*           *Lheight, Lwidth            - the height and width to use for the center image (landscape)
*           *Lshrink                    - the function to use when shrinking an image to a smaller size.  must take in and return an integer value. (landscape)
*           *Lzoom                      - the function to use when enlarging an image for the zoom view.  must take in and return an integer value. (landscape)
*           *Pheight, Pwidth            - the height and width to use for the center image (portrait)
*           *Pshrink                    - the function to use when shrinking an image to a smaller size.  must take in and return an integer value. (portrait)
*           *Pzoom                      - the function to use when enlarging an image for the zoom view.  must take in and return an integer value. (portrait)
*           *defaultLayout              - the layout attribute to apply when the image has no layout attribute
*           *startClass                 - the class label of the image to place in the center slot at the start of the gallery
*           *slideSpeed                 - the animation speed of sliding. use jQuery animation speed values
*           *zoomSpeed                  - the animation speed of zooming. use jQuery animation speed values
*           *gutterWidth                - the horizontal distance between each of the images. use a pixel value
*           *captionUpPath              - the path of the image to use for the "caption up" button
*           *captionUpWidth             - the pixel width of the "caption up" image
*           *captionUpHeight            - the pixel height of the "caption up" image
*           *captionUpID                - the ID attribute to use for the "caption up" image
*           *captionDownPath            - the path of the image to use for the "caption down" button
*           *captionDownWidth           - the pixel width of the "caption down" image
*           *captionDownHeight          - the pixel height of the "caption down" image
*           *captionDownID              - the ID attribute to use for the "caption down" image
*           *captionHeight              - the function to use to determine the height of the caption. takes in an integer value (usually the height of the image to caption)
                                          and returns an integer value
*           *captionStyle               - the CSS style value to apply to the captions
*           *captionClass               - the CSS class to apply to the captions
*           *captionID                  - the ID attribute to apply to the currently active caption
*           *captionTextAttribute       - the attribute containing the text to use in captions
*           *useCaptions                - allow captions to be shown
*
*       All options have default values, and as such, are optional.  Check the 'options' JSON object below to see the defaults.
*/

(function($) {
    $.galleryUtilityV = {};
    $.galleryUtilityV.centerImage = {};
    $.galleryUtilityV.bottomImage = {};
    $.galleryUtilityV.topImage = {};
    $.galleryUtilityV.bottomImageStorage = {};
    $.galleryUtilityV.topImageStorage = {};
    $.galleryUtilityV.zoomImage = {};
    $.galleryUtilityV.gallery = {};

    $.galleryUtilityV.Options = {
       
        Lheight: 160,
        Lwidth: 196,
        Lshrink:
                        function(dimension) {
                            return dimension * 0.75;
                        },
        Lzoom:
                        function(dimension) {
                            return dimension * 2;
                        },
        Pheight: 160,
        Pwidth: 196,
        Pshrink:
                        function(dimension) {
                            return dimension * 0.75;
                        },
        Pzoom:
                        function(dimension) {
                            return dimension * 2;
                        },
        defaultLayout: 'landscape',
        startClass: 'start',
		activeClass: 'active',
        slideSpeed: 'normal',
        zoomSpeed: 'fast',
        gutterWidth: 0,
        captionUpPath: 'Images/SlidingGallery/captionUpArrow.png',
        captionUpWidth: 24,
        captionUpHeight: 17,
        captionUpID: 'captionArrowUp',
        captionDownPath: 'Images/SlidingGallery/captionDownArrow.png',
        captionDownWidth: 24,
        captionDownHeight: 17,
        captionDownID: 'captionArrowDown',
        captionHeight:
                        function(zoomHeight) {
                            return zoomHeight * 0.1;
                        },
        captionStyle: 'background-color:white; color:black; opacity: 0.6; filter: alpha(opacity = 60); font-size: 16px; text-align:center;',
        captionClass: 'captionBox',
        captionID: 'activeCaption',
        captionTextAttribute: 'caption',
        useCaptions: true
    };

    $.fn.slidingGalleryV = function(options) {
        //global settings
        $.extend($.galleryUtilityV.Options, options);
        //eliminate overflow
        $('body').css('overflow-x', 'hidden');
        var container = null;
        if (!$.galleryUtilityV.Options.container) {
            $.galleryUtilityV.Options.container = $('body');
        } else {
            $.galleryUtilityV.Options.container.css('position', 'relative');
        }
        $.galleryUtilityV.gallery = $(this).css('cursor', 'pointer');
        $.galleryUtilityV.definePositions();
        if ($.galleryUtilityV.Options.useCaptions) {
            $.galleryUtilityV.Options.container.append('<img src="' + $.galleryUtilityV.Options.captionUpPath + '" style="width: ' + $.galleryUtilityV.Options.captionUpWidth + '; display: none; border-width:0px;"' + 'id="' + $.galleryUtilityV.Options.captionUpID + '" />').append('<img src="' + $.galleryUtilityV.Options.captionDownPath + '" style="width: ' + $.galleryUtilityV.Options.captionDownWidth + '; display: none; border-width:0px;"' + 'id="' + $.galleryUtilityV.Options.captionDownID + '" />');
            $('#' + $.galleryUtilityV.Options.captionUpID + ',#' + $.galleryUtilityV.Options.captionDownID).css('cursor', 'help');
        }

        //setup existing images
        var lastIndex = 0;
        var gallerySize = $.galleryUtilityV.gallery.each(function(i) {
            $(this).attr({
                'index': i,
                'prev': (i - 1),
                'next': (i + 1)
            }).css('position', 'absolute');
            if (($(this).attr('layout') !== 'portrait') && ($(this).attr('layout') !== 'landscape')) {
                $(this).attr('layout', $.galleryUtilityV.Options.defaultLayout);
            }
            lastIndex = i;
        }).hide().size();

        //fill in gallery with duplicates until there are at least 7
        var currIndex = 0;
        while (gallerySize < 7) {
            var $clone = $.galleryUtilityV.gallery.filter('[index=' + currIndex + ']').clone().attr({
                'index': lastIndex + 1,
                'prev': lastIndex,
                'next': lastIndex + 2
            }).removeClass($.galleryUtilityV.Options.startClass);
            $.galleryUtilityV.gallery.filter('[index=' + (lastIndex) + ']').after($clone);
            $.galleryUtilityV.gallery = $.galleryUtilityV.gallery.add('img[index=' + (lastIndex + 1) + ']');
            lastIndex++;
            currIndex++;
            gallerySize++;
        }
        $.galleryUtilityV.gallery.filter('[index=' + lastIndex + ']').attr('next', 0);
        $.galleryUtilityV.gallery.filter('[index=0]').attr('prev', lastIndex);

        //set images
        $.galleryUtilityV.setCenter($.galleryUtilityV.gallery.filter('.' + $.galleryUtilityV.Options.startClass).show());
        $.galleryUtilityV.settop($.galleryUtilityV.gallery.filter('[index=' + $.galleryUtilityV.centerImage.image.attr('prev') + ']').show());
        $.galleryUtilityV.setbottom($.galleryUtilityV.gallery.filter('[index=' + $.galleryUtilityV.centerImage.image.attr('next') + ']').show());
        $.galleryUtilityV.settopStorage($.galleryUtilityV.gallery.filter('[index=' + $.galleryUtilityV.topImage.image.attr('prev') + ']'));
        $.galleryUtilityV.setbottomStorage($.galleryUtilityV.gallery.filter('[index=' + $.galleryUtilityV.bottomImage.image.attr('next') + ']'));

        //bind events
        $.galleryUtilityV.topImage.image.one('click', $.galleryUtilityV.slidebottom);
        $.galleryUtilityV.bottomImage.image.one('click', $.galleryUtilityV.slidetop);
        $.galleryUtilityV.centerImage.image.one('click', $.galleryUtilityV.zoomIn);
        $(window).resize(function() {
            $.galleryUtilityV.definePositions();
            $.galleryUtilityV.setCenter($.galleryUtilityV.centerImage.image);
            $.galleryUtilityV.settop($.galleryUtilityV.topImage.image);
            $.galleryUtilityV.setbottom($.galleryUtilityV.bottomImage.image);
            $.galleryUtilityV.settopStorage($.galleryUtilityV.topImageStorage.image);
            $.galleryUtilityV.setbottomStorage($.galleryUtilityV.bottomImageStorage.image);
        });
		$.galleryUtilityV.centerImage.image.addClass($.galleryUtilityV.Options.activeClass);
        //return the objects (for chaining purposes)
        return $(this);
    };

    $.galleryUtilityV.slidebottom = function() {
        var litop = $.galleryUtilityV.topImage.top($.galleryUtilityV.topImageStorage.image, $.galleryUtilityV.topImage.image);
        var ritop = $.galleryUtilityV.bottomImage.top($.galleryUtilityV.topImage.image);
        var ristop = $.galleryUtilityV.bottomImageStorage.top($.galleryUtilityV.centerImage.image);
        if ($.galleryUtilityV.topImageStorage.image.attr('layout') === 'landscape') {
            $.galleryUtilityV.topImageStorage.image.animate({
                'left': $.galleryUtilityV.topImage.Lleft,
                'top': litop,
                'height': $.galleryUtilityV.topImage.Lheight,
                'width': $.galleryUtilityV.topImage.Lwidth,
                'opacity': 'show'
            },
            $.galleryUtilityV.Options.slideSpeed, 'linear', function() {
                $(this).one('click', $.galleryUtilityV.slidebottom);
            });
        } else {
            $.galleryUtilityV.topImageStorage.image.animate({
                'left': $.galleryUtilityV.topImage.Pleft,
                'top': litop,
                'height': $.galleryUtilityV.topImage.Pheight,
                'width': $.galleryUtilityV.topImage.Pwidth,
                'opacity': 'show'
            },
            $.galleryUtilityV.Options.slideSpeed, 'linear', function() {
                $(this).one('click', $.galleryUtilityV.slidebottom);
            });
        }
        if ($.galleryUtilityV.topImage.image.attr('layout') === 'landscape') {
            $.galleryUtilityV.topImage.image.unbind().animate({
                'left': $.galleryUtilityV.centerImage.Lleft,
                'top': $.galleryUtilityV.centerImage.Ltop,
                'height': $.galleryUtilityV.centerImage.Lheight,
                'width': $.galleryUtilityV.centerImage.Lwidth
            },
            $.galleryUtilityV.Options.slideSpeed, 'linear', function() {
                $(this).one('click', $.galleryUtilityV.zoomIn);
            });
        } else {
            $.galleryUtilityV.topImage.image.unbind().animate({
                'left': $.galleryUtilityV.centerImage.Pleft,
                'top': $.galleryUtilityV.centerImage.Ptop,
                'height': $.galleryUtilityV.centerImage.Pheight,
                'width': $.galleryUtilityV.centerImage.Pwidth
            },
            $.galleryUtilityV.Options.slideSpeed, 'linear', function() {
                $(this).one('click', $.galleryUtilityV.zoomIn);
            });
        }
        if ($.galleryUtilityV.centerImage.image.attr('layout') === 'landscape') {
            $.galleryUtilityV.centerImage.image.unbind().animate({
                'left': $.galleryUtilityV.bottomImage.Lleft,
                'top': ritop,
                'height': $.galleryUtilityV.bottomImage.Lheight,
                'width': $.galleryUtilityV.bottomImage.Lwidth
				
				
            },
            $.galleryUtilityV.Options.slideSpeed, 'linear', function() {
                $(this).one('click', $.galleryUtilityV.slidetop);
            });
        } else {
            $.galleryUtilityV.centerImage.image.unbind().animate({
                'left': $.galleryUtilityV.bottomImage.Pleft,
                'top': ritop,
                'height': $.galleryUtilityV.bottomImage.Pheight,
                'width': $.galleryUtilityV.bottomImage.Pwidth
				
            },
            $.galleryUtilityV.Options.slideSpeed, 'linear', function() {
                $(this).one('click', $.galleryUtilityV.slidetop);
            });
        }
        if ($.galleryUtilityV.bottomImage.image.attr('layout') === 'landscape') {
            $.galleryUtilityV.bottomImage.image.unbind().animate({
                'left': $.galleryUtilityV.bottomImageStorage.Lleft,
                'top': ristop,
                'height': $.galleryUtilityV.bottomImageStorage.Lheight,
                'width': $.galleryUtilityV.bottomImageStorage.Lwidth,
                'opacity': 'hide'
            },
            $.galleryUtilityV.Options.slideSpeed, 'linear');
        } else {
            $.galleryUtilityV.bottomImage.image.unbind().animate({
                'left': $.galleryUtilityV.bottomImageStorage.Pleft,
                'top': ristop,
                'height': $.galleryUtilityV.bottomImageStorage.Pheight,
                'width': $.galleryUtilityV.bottomImageStorage.Pwidth,
                'opacity': 'hide'
            },
            $.galleryUtilityV.Options.slideSpeed, 'linear');
        }
        $.galleryUtilityV.bottomImageStorage.image = $.galleryUtilityV.bottomImage.image;
        $.galleryUtilityV.bottomImage.image = $.galleryUtilityV.centerImage.image;
        $.galleryUtilityV.centerImage.image = $.galleryUtilityV.topImage.image;
        $.galleryUtilityV.topImage.image = $.galleryUtilityV.topImageStorage.image;
        $.galleryUtilityV.settopStorage($.galleryUtilityV.gallery.filter('[index=' + $.galleryUtilityV.topImageStorage.image.attr('prev') + ']'));
		$.galleryUtilityV.centerImage.image.addClass($.galleryUtilityV.Options.activeClass);
		$.galleryUtilityV.bottomImage.image.removeClass($.galleryUtilityV.Options.activeClass);
		$.galleryUtilityV.topImage.image.removeClass($.galleryUtilityV.Options.activeClass);
    };

    $.galleryUtilityV.slidetop = function() {
        var ritop = $.galleryUtilityV.bottomImage.top($.galleryUtilityV.bottomImage.image);
        var litop = $.galleryUtilityV.topImage.top($.galleryUtilityV.centerImage.image, $.galleryUtilityV.bottomImage.image);
        var listop = $.galleryUtilityV.topImageStorage.top($.galleryUtilityV.topImage.image, $.galleryUtilityV.centerImage.image, $.galleryUtilityV.bottomImage.image);
        if ($.galleryUtilityV.bottomImageStorage.image.attr('layout') === 'landscape') {
            $.galleryUtilityV.bottomImageStorage.image.animate({
                'left': $.galleryUtilityV.bottomImage.Lleft,
                'top': ritop,
                'height': $.galleryUtilityV.bottomImage.Lheight,
                'width': $.galleryUtilityV.bottomImage.Lwidth,
                'opacity': 'show'
            },
            $.galleryUtilityV.Options.slideSpeed, 'linear', function() {
                $(this).one('click', $.galleryUtilityV.slidetop);
            });
        } else {
            $.galleryUtilityV.bottomImageStorage.image.animate({
                'left': $.galleryUtilityV.bottomImage.Pleft,
                'top': ritop,
                'height': $.galleryUtilityV.bottomImage.Pheight,
                'width': $.galleryUtilityV.bottomImage.Pwidth,
                'opacity': 'show'
            },
            $.galleryUtilityV.Options.slideSpeed, 'linear', function() {
                $(this).one('click', $.galleryUtilityV.slidetop);
            });
        }
        if ($.galleryUtilityV.bottomImage.image.attr('layout') === 'landscape') {
            $.galleryUtilityV.bottomImage.image.unbind().animate({
                'left': $.galleryUtilityV.centerImage.Lleft,
                'top': $.galleryUtilityV.centerImage.Ltop,
                'height': $.galleryUtilityV.centerImage.Lheight,
                'width': $.galleryUtilityV.centerImage.Lwidth
            },
            $.galleryUtilityV.Options.slideSpeed, 'linear', function() {
                $(this).one('click', $.galleryUtilityV.zoomIn);
            });
        } else {
            $.galleryUtilityV.bottomImage.image.unbind().animate({
                'left': $.galleryUtilityV.centerImage.Pleft,
                'top': $.galleryUtilityV.centerImage.Ptop,
                'height': $.galleryUtilityV.centerImage.Pheight,
                'width': $.galleryUtilityV.centerImage.Pwidth
            },
            $.galleryUtilityV.Options.slideSpeed, 'linear', function() {
                $(this).one('click', $.galleryUtilityV.zoomIn);
            });
        }
        if ($.galleryUtilityV.centerImage.image.attr('layout') === 'landscape') {
            $.galleryUtilityV.centerImage.image.unbind().animate({
                'left': $.galleryUtilityV.topImage.Lleft,
                'top': litop,
                'height': $.galleryUtilityV.topImage.Lheight,
                'width': $.galleryUtilityV.topImage.Lwidth
            },
            $.galleryUtilityV.Options.slideSpeed, 'linear', function() {
                $(this).one('click', $.galleryUtilityV.slidebottom);
            });
        } else {
            $.galleryUtilityV.centerImage.image.unbind().animate({
                'left': $.galleryUtilityV.topImage.Pleft,
                'top': litop,
                'height': $.galleryUtilityV.topImage.Pheight,
                'width': $.galleryUtilityV.topImage.Pwidth
            },
            $.galleryUtilityV.Options.slideSpeed, 'linear', function() {
                $(this).one('click', $.galleryUtilityV.slidebottom);
            });
        }
        if ($.galleryUtilityV.topImage.image.attr('layout') === 'landscape') {
            $.galleryUtilityV.topImage.image.unbind().animate({
                'left': $.galleryUtilityV.topImageStorage.Lleft,
                'top': listop,
                'height': $.galleryUtilityV.topImageStorage.Lheight,
                'width': $.galleryUtilityV.topImageStorage.Lwidth, 'opacity': 'hide'
            },
            $.galleryUtilityV.Options.slideSpeed, 'linear');
        } else {
            $.galleryUtilityV.topImage.image.unbind().animate({
                'left': $.galleryUtilityV.topImageStorage.Pleft,
                'top': listop,
                'height': $.galleryUtilityV.topImageStorage.Pheight,
                'width': $.galleryUtilityV.topImageStorage.Pwidth,
                'opacity': 'hide'
            },
            $.galleryUtilityV.Options.slideSpeed, 'linear');
        }
        $.galleryUtilityV.topImageStorage.image = $.galleryUtilityV.topImage.image;
        $.galleryUtilityV.topImage.image = $.galleryUtilityV.centerImage.image;
        $.galleryUtilityV.centerImage.image = $.galleryUtilityV.bottomImage.image;
        $.galleryUtilityV.bottomImage.image = $.galleryUtilityV.bottomImageStorage.image;
        $.galleryUtilityV.setbottomStorage($.galleryUtilityV.gallery.filter('[index=' + $.galleryUtilityV.bottomImageStorage.image.attr('next') + ']'));
		$.galleryUtilityV.centerImage.image.addClass($.galleryUtilityV.Options.activeClass);
		$.galleryUtilityV.bottomImage.image.removeClass($.galleryUtilityV.Options.activeClass);
		$.galleryUtilityV.topImage.image.removeClass($.galleryUtilityV.Options.activeClass);
    };

        $.galleryUtilityV.zoomIn = function() {
        var rel = $.galleryUtilityV.centerImage.image.attr('rel');
        eval(rel);
        $.galleryUtilityV.centerImage.image.one('click', $.galleryUtilityV.zoomOut);
    };

    $.galleryUtilityV.LcaptionDown = function() {
        $.galleryUtilityV.centerImage.image.unbind();
        $.galleryUtilityV.Options.container.append('<span id="' + $.galleryUtilityV.Options.captionID + '" style="' + $.galleryUtilityV.Options.captionStyle + '" class="' + $.galleryUtilityV.Options.captionClass + '">' + $.galleryUtilityV.centerImage.image.attr($.galleryUtilityV.Options.captionTextAttribute) + '</span>');
        $('#' + $.galleryUtilityV.Options.captionID).css({
            'left': $.galleryUtilityV.zoomImage.Lleft + parseInt($.galleryUtilityV.centerImage.image.css('borderleftWidth'), 10),
            'top': $.galleryUtilityV.zoomImage.Ltop + parseInt($.galleryUtilityV.centerImage.image.css('bordertopWidth'), 10),
            'width': $.galleryUtilityV.zoomImage.Lwidth,
            'height': 0,
            'position': 'absolute',
            'z-index': '100'
        }).animate({ 'height': Math.round($.galleryUtilityV.Options.captionHeight($.galleryUtilityV.zoomImage.Lheight)) }, 'normal', 'linear');
        $('#' + $.galleryUtilityV.Options.captionDownID).animate({
            'height': 0,
            'left': $.galleryUtilityV.zoomImage.Lleft + (Math.round($.galleryUtilityV.Options.captionHeight($.galleryUtilityV.zoomImage.Lheight))) + parseInt($.galleryUtilityV.centerImage.image.css('borderleftWidth'), 10)
        }, 'normal', 'linear', function() { $('#' + $.galleryUtilityV.Options.captionDownID).hide(); });
        $('#' + $.galleryUtilityV.Options.captionUpID).css({
            'left': $.galleryUtilityV.zoomImage.Lleft + $.galleryUtilityV.Options.captionDownHeight + parseInt($.galleryUtilityV.centerImage.image.css('borderleftWidth'), 10),
            'top': $.galleryUtilityV.zoomImage.Ltop + ($.galleryUtilityV.zoomImage.Lwidth - $.galleryUtilityV.Options.captionUpWidth) + parseInt($.galleryUtilityV.centerImage.image.css('bordertopWidth'), 10),
            'height': 0,
            'position': 'absolute',
            'z-index': '100'
        }).show().animate({
            'left': $.galleryUtilityV.zoomImage.Lleft + (Math.round($.galleryUtilityV.Options.captionHeight($.galleryUtilityV.zoomImage.Lheight))) + parseInt($.galleryUtilityV.centerImage.image.css('borderleftWidth'), 10),
            'height': $.galleryUtilityV.Options.captionUpHeight
        }, 'normal', 'linear', function() {
            $('#' + $.galleryUtilityV.Options.captionUpID).one('click', function() {
                $.galleryUtilityV.LcaptionUp(false);
            });
            $.galleryUtilityV.centerImage.image.one('click', function() {
                $.galleryUtilityV.LcaptionUp(true);
            });
        });
    };

    $.galleryUtilityV.PcaptionDown = function() {
        $.galleryUtilityV.centerImage.image.unbind();
        $.galleryUtilityV.Options.container.append('<span id="' + $.galleryUtilityV.Options.captionID + '" style="' + $.galleryUtilityV.Options.captionStyle + '" class="' + $.galleryUtilityV.Options.captionClass + '">' + $.galleryUtilityV.centerImage.image.attr($.galleryUtilityV.Options.captionTextAttribute) + '</span>');
        $('#' + $.galleryUtilityV.Options.captionID).css({
            'left': $.galleryUtilityV.zoomImage.Pleft + parseInt($.galleryUtilityV.centerImage.image.css('borderleftWidth'), 10),
            'top': $.galleryUtilityV.zoomImage.Ptop + parseInt($.galleryUtilityV.centerImage.image.css('bordertopWidth'), 10),
            'width': $.galleryUtilityV.zoomImage.Pwidth,
            'height': 0,
            'position': 'absolute',
            'z-index': '100'
        }).animate({ 'height': Math.round($.galleryUtilityV.Options.captionHeight($.galleryUtilityV.zoomImage.Pheight)) }, 'normal', 'linear');
        $('#' + $.galleryUtilityV.Options.captionDownID).animate({
            'height': 0,
            'left': $.galleryUtilityV.zoomImage.Pleft + (Math.round($.galleryUtilityV.Options.captionHeight($.galleryUtilityV.zoomImage.Pheight))) + parseInt($.galleryUtilityV.centerImage.image.css('borderleftWidth'), 10)
        }, 'normal', 'linear', function() { $('#' + $.galleryUtilityV.Options.captionDownID).hide(); });
        $('#' + $.galleryUtilityV.Options.captionUpID).css({
            'left': $.galleryUtilityV.zoomImage.Pleft + $.galleryUtilityV.Options.captionDownHeight + parseInt($.galleryUtilityV.centerImage.image.css('borderleftWidth'), 10),
            'top': $.galleryUtilityV.zoomImage.Ptop + ($.galleryUtilityV.zoomImage.Pwidth - $.galleryUtilityV.Options.captionUpWidth) + parseInt($.galleryUtilityV.centerImage.image.css('bordertopWidth'), 10),
            'height': 0,
            'position': 'absolute',
            'z-index': '100'
        }).show().animate({
            'left': $.galleryUtilityV.zoomImage.Pleft + (Math.round($.galleryUtilityV.Options.captionHeight($.galleryUtilityV.zoomImage.Pheight))) + parseInt($.galleryUtilityV.centerImage.image.css('borderleftWidth'), 10),
            'height': $.galleryUtilityV.Options.captionUpHeight
        }, 'normal', 'linear', function() {
            $('#' + $.galleryUtilityV.Options.captionUpID).one('click', function() {
                $.galleryUtilityV.PcaptionUp(false);
            });
            $.galleryUtilityV.centerImage.image.one('click', function() {
                $.galleryUtilityV.PcaptionUp(true);
            });
        });
    };

    $.galleryUtilityV.LcaptionUp = function(unzoom) {
        $('#' + $.galleryUtilityV.Options.captionID).animate({ 'height': 0 }, 'normal', 'linear', function() { $('#' + $.galleryUtilityV.Options.captionID).remove(); });
        $('#' + $.galleryUtilityV.Options.captionUpID).animate({
            'left': $.galleryUtilityV.zoomImage.Lleft + $.galleryUtilityV.Options.captionDownHeight + parseInt($.galleryUtilityV.centerImage.image.css('borderleftWidth'), 10),
            'height': 0
        }, 'normal', 'linear', function() {
            $('#' + $.galleryUtilityV.Options.captionUpID).hide();
        });
        $('#' + $.galleryUtilityV.Options.captionDownID).show().animate({
            'height': $.galleryUtilityV.Options.captionDownHeight,
            'left': $.galleryUtilityV.zoomImage.Lleft + parseInt($.galleryUtilityV.centerImage.image.css('borderleftWidth'), 10)
        }, 'normal', 'linear', function() {
            if (unzoom) {
                $('#' + $.galleryUtilityV.Options.captionUpID).unbind();
                $.galleryUtilityV.zoomOut();
            } else {
                $('#' + $.galleryUtilityV.Options.captionDownID).one('click', $.galleryUtilityV.LcaptionDown);
                $.galleryUtilityV.centerImage.image.one('click', $.galleryUtilityV.zoomOut);
            }
        });
    };

    $.galleryUtilityV.PcaptionUp = function(unzoom) {
        $('#' + $.galleryUtilityV.Options.captionID).animate({ 'height': 0 }, 'normal', 'linear', function() { $('#' + $.galleryUtilityV.Options.captionID).remove(); });
        $('#' + $.galleryUtilityV.Options.captionUpID).animate({
            'left': $.galleryUtilityV.zoomImage.Pleft + $.galleryUtilityV.Options.captionDownHeight + parseInt($.galleryUtilityV.centerImage.image.css('borderleftWidth'), 10),
            'height': 0
        }, 'normal', 'linear', function() {
            $('#' + $.galleryUtilityV.Options.captionUpID).hide();
        });
        $('#' + $.galleryUtilityV.Options.captionDownID).show().animate({
            'height': $.galleryUtilityV.Options.captionDownHeight,
            'left': $.galleryUtilityV.zoomImage.Pleft + parseInt($.galleryUtilityV.centerImage.image.css('borderleftWidth'), 10)
        }, 'normal', 'linear', function() {
            if (unzoom) {
                $('#' + $.galleryUtilityV.Options.captionUpID).unbind();
                $.galleryUtilityV.zoomOut();
            } else {
                $('#' + $.galleryUtilityV.Options.captionDownID).one('click', $.galleryUtilityV.PcaptionDown);
                $.galleryUtilityV.centerImage.image.one('click', $.galleryUtilityV.zoomOut);
            }
        });
    };

    $.galleryUtilityV.zoomOut = function() {
        if ($.galleryUtilityV.Options.useCaptions) {
            $('#' + $.galleryUtilityV.Options.captionDownID).animate({ 'height': 0 }, 50, 'linear', $.galleryUtilityV.zoomOutBody).unbind();
        } else {
            $.galleryUtilityV.zoomOutBody();
        }
    };

    $.galleryUtilityV.zoomOutBody = function() {
        if ($.galleryUtilityV.centerImage.image.attr('layout') === 'landscape') {
            $.galleryUtilityV.centerImage.image.animate({
                'left': $.galleryUtilityV.centerImage.Lleft,
                'top': $.galleryUtilityV.centerImage.Ltop,
                'height': $.galleryUtilityV.centerImage.Lheight,
                'width': $.galleryUtilityV.centerImage.Lwidth
            },
            $.galleryUtilityV.Options.zoomSpeed, 'linear', function() {
                $(this).css('z-index', '0').one('click', $.galleryUtilityV.zoomIn);
                $.galleryUtilityV.topImage.image.one('click', $.galleryUtilityV.slidebottom);
                $.galleryUtilityV.bottomImage.image.one('click', $.galleryUtilityV.slidetop);
            });
        } else {
            $.galleryUtilityV.centerImage.image.animate({
                'left': $.galleryUtilityV.centerImage.Pleft,
                'top': $.galleryUtilityV.centerImage.Ptop,
                'height': $.galleryUtilityV.centerImage.Pheight,
                'width': $.galleryUtilityV.centerImage.Pwidth
            },
            $.galleryUtilityV.Options.zoomSpeed, 'linear', function() {
                $(this).css('z-index', '0').one('click', $.galleryUtilityV.zoomIn);
                $.galleryUtilityV.topImage.image.one('click', $.galleryUtilityV.slidebottom);
                $.galleryUtilityV.bottomImage.image.one('click', $.galleryUtilityV.slidetop);
            });
        }
    };

    $.galleryUtilityV.setbottomStorage = function(image) {
        $.galleryUtilityV.bottomImageStorage.image = image;
        if ($.galleryUtilityV.bottomImageStorage.image.attr('layout') === 'landscape') {
            $.galleryUtilityV.bottomImageStorage.image.hide().css({
                'left': $.galleryUtilityV.bottomImageStorage.Lleft,
                'height': $.galleryUtilityV.bottomImageStorage.Lheight,
                'width': $.galleryUtilityV.bottomImageStorage.Lwidth
            });
        } else {
            $.galleryUtilityV.bottomImageStorage.image.hide().css({
                'left': $.galleryUtilityV.bottomImageStorage.Pleft,
                'height': $.galleryUtilityV.bottomImageStorage.Pheight,
                'width': $.galleryUtilityV.bottomImageStorage.Pwidth
            });
        }
        $.galleryUtilityV.bottomImageStorage.image.css('top', $.galleryUtilityV.bottomImageStorage.top($.galleryUtilityV.bottomImage.image));
    };

    $.galleryUtilityV.settopStorage = function(image) {
        $.galleryUtilityV.topImageStorage.image = image;
        if ($.galleryUtilityV.topImageStorage.image.attr('layout') === 'landscape') {
            $.galleryUtilityV.topImageStorage.image.hide().css({
                'left': $.galleryUtilityV.topImageStorage.Lleft,
                'height': $.galleryUtilityV.topImageStorage.Lheight,
                'width': $.galleryUtilityV.topImageStorage.Lwidth
            });
        } else {
            $.galleryUtilityV.topImageStorage.image.hide().css({
                'left': $.galleryUtilityV.topImageStorage.Pleft,
                'height': $.galleryUtilityV.topImageStorage.Pheight,
                'width': $.galleryUtilityV.topImageStorage.Pwidth
            });
        }
        $.galleryUtilityV.topImageStorage.image
             .css('top', $.galleryUtilityV.topImageStorage.top($.galleryUtilityV.topImageStorage.image, $.galleryUtilityV.topImage.image, $.galleryUtilityV.centerImage.image));
    };

    $.galleryUtilityV.setCenter = function(image) {
        $.galleryUtilityV.centerImage.image = image;
        if ($.galleryUtilityV.centerImage.image.attr('layout') === 'landscape') {
            $.galleryUtilityV.centerImage.image.css({
                'left': $.galleryUtilityV.centerImage.Lleft,
                'top': $.galleryUtilityV.centerImage.Ltop,
                'height': $.galleryUtilityV.centerImage.Lheight,
                'width': $.galleryUtilityV.centerImage.Lwidth
            });
        } else {
            $.galleryUtilityV.centerImage.image.css({
                'left': $.galleryUtilityV.centerImage.Pleft,
                'top': $.galleryUtilityV.centerImage.Ptop,
                'height': $.galleryUtilityV.centerImage.Pheight,
                'width': $.galleryUtilityV.centerImage.Pwidth
            });
        }
    };

    $.galleryUtilityV.setbottom = function(image) {
        $.galleryUtilityV.bottomImage.image = image;
        if ($.galleryUtilityV.bottomImage.image.attr('layout') === 'landscape') {
            $.galleryUtilityV.bottomImage.image.css({
                'left': $.galleryUtilityV.bottomImage.Lleft,
                'height': $.galleryUtilityV.bottomImage.Lheight,
                'width': $.galleryUtilityV.bottomImage.Lwidth
            });
        } else {
            $.galleryUtilityV.bottomImage.image.css({
                'left': $.galleryUtilityV.bottomImage.Pleft,
                'height': $.galleryUtilityV.bottomImage.Pheight,
                'width': $.galleryUtilityV.bottomImage.Pwidth
            });
        }
        $.galleryUtilityV.bottomImage.image.css('top', $.galleryUtilityV.bottomImage.top($.galleryUtilityV.centerImage.image));
    };

    $.galleryUtilityV.settop = function(image) {
        $.galleryUtilityV.topImage.image = image;
        if ($.galleryUtilityV.topImage.image.attr('layout') === 'landscape') {
            $.galleryUtilityV.topImage.image.css({
                'left': $.galleryUtilityV.topImage.Lleft,
                'height': $.galleryUtilityV.topImage.Lheight,
                'width': $.galleryUtilityV.topImage.Lwidth
            });
        } else {
            $.galleryUtilityV.topImage.image.css({
                'left': $.galleryUtilityV.topImage.Pleft,
                'height': $.galleryUtilityV.topImage.Pheight,
                'width': $.galleryUtilityV.topImage.Pwidth
            });
        }
        $.galleryUtilityV.topImage.image.css('top', $.galleryUtilityV.topImage.top($.galleryUtilityV.topImage.image, $.galleryUtilityV.centerImage.image));
    };

    $.galleryUtilityV.definePositions = function() {
        //var Gheight = ($.galleryUtilityV.Options.Gheight || $(window).height());
        //var Gwidth = ($.galleryUtilityV.Options.Gwidth || $(window).width());
        var container = $.galleryUtilityV.Options.container;
        if (container[0].tagName == 'BODY') {
            container = $(window);
        }
        var Gheight = container.height();
        var Gwidth = container.width();

        $.galleryUtilityV.centerImage.Lheight = Math.round($.galleryUtilityV.Options.Lheight);
        $.galleryUtilityV.centerImage.Lwidth = Math.round($.galleryUtilityV.Options.Lwidth);
        $.galleryUtilityV.centerImage.Lleft = Math.round(Gheight / 2) - ($.galleryUtilityV.centerImage.Lheight / 2);
        $.galleryUtilityV.centerImage.Ltop = Math.round(Gwidth / 2) - ($.galleryUtilityV.centerImage.Lwidth / 2);
        $.galleryUtilityV.centerImage.Pheight = Math.round($.galleryUtilityV.Options.Pheight);
        $.galleryUtilityV.centerImage.Pwidth = Math.round($.galleryUtilityV.Options.Pwidth);
        $.galleryUtilityV.centerImage.Pleft = Math.round((Gheight / 2) - ($.galleryUtilityV.centerImage.Pheight / 2));
        $.galleryUtilityV.centerImage.Ptop = Math.round((Gwidth / 2) - ($.galleryUtilityV.centerImage.Pwidth / 2));
        $.galleryUtilityV.zoomImage.Lheight = Math.round($.galleryUtilityV.Options.Lzoom($.galleryUtilityV.centerImage.Lheight));
        $.galleryUtilityV.zoomImage.Lwidth = Math.round($.galleryUtilityV.Options.Lzoom($.galleryUtilityV.centerImage.Lwidth));
        $.galleryUtilityV.zoomImage.Lleft = Math.round((Gheight / 2) - ($.galleryUtilityV.zoomImage.Lheight / 2));
        $.galleryUtilityV.zoomImage.Ltop = Math.round((Gwidth / 2) - ($.galleryUtilityV.zoomImage.Lwidth / 2));
        $.galleryUtilityV.zoomImage.Pheight = Math.round($.galleryUtilityV.Options.Pzoom($.galleryUtilityV.centerImage.Pheight));
        $.galleryUtilityV.zoomImage.Pwidth = Math.round($.galleryUtilityV.Options.Pzoom($.galleryUtilityV.centerImage.Pwidth));
        $.galleryUtilityV.zoomImage.Pleft = Math.round((Gheight / 2) - ($.galleryUtilityV.zoomImage.Pheight / 2));
        $.galleryUtilityV.zoomImage.Ptop = Math.round((Gwidth / 2) - ($.galleryUtilityV.zoomImage.Pwidth / 2));
        $.galleryUtilityV.topImage.Lheight = Math.round($.galleryUtilityV.Options.Lshrink($.galleryUtilityV.centerImage.Lheight));
        $.galleryUtilityV.topImage.Lwidth = Math.round($.galleryUtilityV.Options.Lshrink($.galleryUtilityV.centerImage.Lwidth));
        $.galleryUtilityV.topImage.Lleft = Math.round($.galleryUtilityV.centerImage.Lleft + (($.galleryUtilityV.centerImage.Lheight - $.galleryUtilityV.topImage.Lheight) / 2));
        $.galleryUtilityV.topImage.top = function(top, center) {
            if (center.attr('layout') === 'landscape') {
                if (top.attr('layout') === 'landscape') {
                    return Math.round($.galleryUtilityV.centerImage.Ltop - ($.galleryUtilityV.topImage.Lwidth + $.galleryUtilityV.Options.gutterWidth));
                } else {
                    return Math.round($.galleryUtilityV.centerImage.Ltop - ($.galleryUtilityV.topImage.Pwidth + $.galleryUtilityV.Options.gutterWidth));
                }
            } else {
                if (top.attr('layout') === 'landscape') {
                    return Math.round($.galleryUtilityV.centerImage.Ptop - ($.galleryUtilityV.topImage.Lwidth + $.galleryUtilityV.Options.gutterWidth));
                } else {
                    return Math.round($.galleryUtilityV.centerImage.Ptop - ($.galleryUtilityV.topImage.Pwidth + $.galleryUtilityV.Options.gutterWidth));
                }
            }
        };
        $.galleryUtilityV.topImage.Pheight = Math.round($.galleryUtilityV.Options.Pshrink($.galleryUtilityV.centerImage.Pheight));
        $.galleryUtilityV.topImage.Pwidth = Math.round($.galleryUtilityV.Options.Pshrink($.galleryUtilityV.centerImage.Pwidth));
        $.galleryUtilityV.topImage.Pleft = Math.round($.galleryUtilityV.centerImage.Pleft + (($.galleryUtilityV.centerImage.Pheight - $.galleryUtilityV.topImage.Pheight) / 2));
        $.galleryUtilityV.bottomImage.Lheight = Math.round($.galleryUtilityV.Options.Lshrink($.galleryUtilityV.centerImage.Lheight));
        $.galleryUtilityV.bottomImage.Lwidth = Math.round($.galleryUtilityV.Options.Lshrink($.galleryUtilityV.centerImage.Lwidth));
        $.galleryUtilityV.bottomImage.Lleft = Math.round($.galleryUtilityV.centerImage.Lleft + (($.galleryUtilityV.centerImage.Lheight - $.galleryUtilityV.bottomImage.Lheight) / 2));
        $.galleryUtilityV.bottomImage.top = function(center) {
            if (center.attr('layout') === 'landscape') {
                return Math.round($.galleryUtilityV.centerImage.Ltop + ($.galleryUtilityV.centerImage.Lwidth + $.galleryUtilityV.Options.gutterWidth - 9));
            } else {
                return Math.round($.galleryUtilityV.centerImage.Ptop + ($.galleryUtilityV.centerImage.Pwidth + $.galleryUtilityV.Options.gutterWidth - 9));
            }
        };
        $.galleryUtilityV.bottomImage.Pheight = Math.round($.galleryUtilityV.Options.Pshrink($.galleryUtilityV.centerImage.Pheight));
        $.galleryUtilityV.bottomImage.Pwidth = Math.round($.galleryUtilityV.Options.Pshrink($.galleryUtilityV.centerImage.Pwidth));
        $.galleryUtilityV.bottomImage.Pleft = Math.round($.galleryUtilityV.centerImage.Pleft + ((($.galleryUtilityV.centerImage.Pheight - $.galleryUtilityV.topImage.Pheight) / 2)+10));
        $.galleryUtilityV.topImageStorage.Lheight = Math.round($.galleryUtilityV.Options.Lshrink($.galleryUtilityV.topImage.Lheight));
        $.galleryUtilityV.topImageStorage.Lwidth = Math.round($.galleryUtilityV.Options.Lshrink($.galleryUtilityV.topImage.Lwidth));
        $.galleryUtilityV.topImageStorage.Lleft = Math.round($.galleryUtilityV.topImage.Lleft + (($.galleryUtilityV.topImage.Lheight - $.galleryUtilityV.topImageStorage.Lheight) / 2));
        $.galleryUtilityV.topImageStorage.top = function(topStorage, top, center) {
            if (topStorage.attr('layout') === 'landscape') {
                return Math.round($.galleryUtilityV.topImage.top(top, center) - ($.galleryUtilityV.topImageStorage.Lwidth + $.galleryUtilityV.Options.gutterWidth));
            } else {
                return Math.round($.galleryUtilityV.topImage.top(top, center) - ($.galleryUtilityV.topImageStorage.Pwidth + $.galleryUtilityV.Options.gutterWidth));
            }
        };
        $.galleryUtilityV.topImageStorage.Pheight = Math.round($.galleryUtilityV.Options.Pshrink($.galleryUtilityV.topImage.Pheight));
        $.galleryUtilityV.topImageStorage.Pwidth = Math.round($.galleryUtilityV.Options.Pshrink($.galleryUtilityV.topImage.Pwidth));
        $.galleryUtilityV.topImageStorage.Pleft = Math.round($.galleryUtilityV.topImage.Pleft + (($.galleryUtilityV.topImage.Pheight - $.galleryUtilityV.topImageStorage.Pheight) / 2));
        $.galleryUtilityV.bottomImageStorage.Lheight = Math.round($.galleryUtilityV.Options.Lshrink($.galleryUtilityV.bottomImage.Lheight));
        $.galleryUtilityV.bottomImageStorage.Lwidth = Math.round($.galleryUtilityV.Options.Lshrink($.galleryUtilityV.bottomImage.Lwidth));
        $.galleryUtilityV.bottomImageStorage.Lleft = Math.round($.galleryUtilityV.bottomImage.Lleft + (($.galleryUtilityV.bottomImage.Lheight - $.galleryUtilityV.bottomImageStorage.Lheight) / 2));
        $.galleryUtilityV.bottomImageStorage.top = function(bottom) {
            if (bottom.attr('layout') === 'landscape') {
                return Math.round($.galleryUtilityV.bottomImage.top(bottom) + ($.galleryUtilityV.bottomImage.Lwidth + $.galleryUtilityV.Options.gutterWidth)  );
            } else {
                return Math.round($.galleryUtilityV.bottomImage.top(bottom) + ($.galleryUtilityV.bottomImage.Pwidth + $.galleryUtilityV.Options.gutterWidth ) );
            }
        };
        $.galleryUtilityV.bottomImageStorage.Pheight = Math.round($.galleryUtilityV.Options.Pshrink($.galleryUtilityV.bottomImage.Pheight));
        $.galleryUtilityV.bottomImageStorage.Pwidth = Math.round($.galleryUtilityV.Options.Pshrink($.galleryUtilityV.bottomImage.Pwidth));
        $.galleryUtilityV.bottomImageStorage.Pleft = Math.round($.galleryUtilityV.bottomImage.Pleft + (($.galleryUtilityV.bottomImage.Pheight - $.galleryUtilityV.bottomImageStorage.Pheight) / 2));
    };
})(jQuery);
