﻿/*
*       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 gallery2
*           *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($) {
    $.gallery2Utility = {};
    $.gallery2Utility.centerImage = {};
    $.gallery2Utility.rightImage = {};
    $.gallery2Utility.leftImage = {};
    $.gallery2Utility.rightImageStorage = {};
    $.gallery2Utility.leftImageStorage = {};
    $.gallery2Utility.zoomImage = {};
    $.gallery2Utility.gallery2 = {};

    $.gallery2Utility.Options = {
        container: null,
        Lheight: 400,
        Lwidth: 600,
        Lshrink:
        function(dimension) {
            return dimension ;
        },
        Lzoom:
        function(dimension) {
            return dimension * 2;
        },
        Pheight: 400,
        Pwidth: 300,
        Pshrink:
        function(dimension) {
            return dimension ;
        },
        Pzoom:
        function(dimension) {
            return dimension * 2;
        },
        defaultLayout: 'landscape',
        startClass: 'start',
        slideSpeed: 'normal',
        zoomSpeed: 'fast',
        gutterWidth: 10,
        captionUpPath: 'Images/Slidinggallery2/captionUpArrow.png',
        captionUpWidth: 24,
        captionUpHeight: 17,
        captionUpID: 'captionArrowUp',
        captionDownPath: 'Images/Slidinggallery2/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: false
    };

    $.fn.slidinggallery2 = function(options) {
        //global settings
        $.extend($.gallery2Utility.Options, options);
        //eliminate overflow
        $('body').css('overflow-x', 'hidden');
        var container = null;
        if (!$.gallery2Utility.Options.container) {
            $.gallery2Utility.Options.container = $('body');
        } else {
            $.gallery2Utility.Options.container.css('position', 'relative');
        }
        $.gallery2Utility.gallery2 = $(this).css('cursor', 'pointer');
        $.gallery2Utility.definePositions();
        if ($.gallery2Utility.Options.useCaptions) {
            $.gallery2Utility.Options.container.append('<img src="' + $.gallery2Utility.Options.captionUpPath + '" style="width: ' + $.gallery2Utility.Options.captionUpWidth + '; display: none;  border-width:0px;"' + 'id="' + $.gallery2Utility.Options.captionUpID + '" />').append('<img src="' + $.gallery2Utility.Options.captionDownPath + '" style="width: ' + $.gallery2Utility.Options.captionDownWidth + '; display: none; border-width:0px;"' + 'id="' + $.gallery2Utility.Options.captionDownID + '" />');
            $('#' + $.gallery2Utility.Options.captionUpID + ',#' + $.gallery2Utility.Options.captionDownID).css('cursor', 'help');
        }
        


        //setup existing images
        var lastindex2 = 0;
        var gallery2Size = $.gallery2Utility.gallery2.each(function(i) {
            $(this).attr({
                'index2': i,
                'prev2': (i - 1),
                'next2': (i + 1)
            }).css('position', 'absolute');
            if (($(this).attr('layout') !== 'portrait') && ($(this).attr('layout') !== 'landscape')) {
                $(this).attr('layout', $.gallery2Utility.Options.defaultLayout);
            }
            lastindex2 = i;
        }).hide().size();

        //fill in gallery2 with duplicates until there are at least 7
        var currindex2 = 0;
        while (gallery2Size < 7) {
            var $clone = $.gallery2Utility.gallery2.filter('[index2=' + currindex2 + ']').clone().attr({
                'index2': lastindex2 + 1,
                'prev2': lastindex2,
                'next2': lastindex2 + 2
            }).removeClass($.gallery2Utility.Options.startClass);
            $.gallery2Utility.gallery2.filter('[index2=' + (lastindex2) + ']').after($clone);
            $.gallery2Utility.gallery2 = $.gallery2Utility.gallery2.add('img[index2=' + (lastindex2 + 1) + ']');
            lastindex2++;
            currindex2++;
            gallery2Size++;
        }
        $.gallery2Utility.gallery2.filter('[index2=' + lastindex2 + ']').attr('next2', 0);
        $.gallery2Utility.gallery2.filter('[index2=0]').attr('prev2', lastindex2);

        //set images
        $.gallery2Utility.setCenter($.gallery2Utility.gallery2.filter('.' + $.gallery2Utility.Options.startClass).show());
        $.gallery2Utility.setLeft($.gallery2Utility.gallery2.filter('[index2=' + $.gallery2Utility.centerImage.image.attr('prev2') + ']').show());
        $.gallery2Utility.setRight($.gallery2Utility.gallery2.filter('[index2=' + $.gallery2Utility.centerImage.image.attr('next2') + ']').show());
        $.gallery2Utility.setLeftStorage($.gallery2Utility.gallery2.filter('[index2=' + $.gallery2Utility.leftImage.image.attr('prev2') + ']'));
        $.gallery2Utility.setRightStorage($.gallery2Utility.gallery2.filter('[index2=' + $.gallery2Utility.rightImage.image.attr('next2') + ']'));

        //bind events
        $.gallery2Utility.leftImage.image.one('click', $.gallery2Utility.slideLeft);
        $.gallery2Utility.rightImage.image.one('click', $.gallery2Utility.slideLeft);
        $.gallery2Utility.centerImage.image.one('click', $.gallery2Utility.slideLeft);
        $(window).resize(function() {
            //            $.gallery2Utility.definePositions();
            //            $.gallery2Utility.setCenter($.gallery2Utility.centerImage.image);
            //            $.gallery2Utility.setLeft($.gallery2Utility.leftImage.image);
            //            $.gallery2Utility.setRight($.gallery2Utility.rightImage.image);
            //            $.gallery2Utility.setLeftStorage($.gallery2Utility.leftImageStorage.image);
            //            $.gallery2Utility.setRightStorage($.gallery2Utility.rightImageStorage.image);
            });

        //return the objects (for chaining purposes)
        var initCenterX = 0;
        var initLeftX = 0;
        initCenterX = -560 + parseInt($.gallery2Utility.leftImage.image.css('width').replace('px', '')) + 20;
        initLeftX = initCenterX + parseInt($.gallery2Utility.centerImage.image.css('width').replace('px', '')) + 10;
        $.gallery2Utility.leftImage.image.css({
            'left': -560

        });
        $.gallery2Utility.centerImage.image.css({
            'left': initCenterX

        });
        $.gallery2Utility.rightImage.image.css({
            'left': initLeftX

        });
        return $(this);
    };

    $.gallery2Utility.slideRight = function() {
        var liLeft = $.gallery2Utility.leftImage.left($.gallery2Utility.leftImageStorage.image, $.gallery2Utility.leftImage.image);
        var riLeft = $.gallery2Utility.rightImage.left($.gallery2Utility.leftImage.image);
        var risLeft = $.gallery2Utility.rightImageStorage.left($.gallery2Utility.centerImage.image);
        if ($.gallery2Utility.leftImageStorage.image.attr('layout') === 'landscape') {
            $.gallery2Utility.leftImageStorage.image.animate({
                'top': $.gallery2Utility.leftImage.Ltop,
                'left': liLeft,
                'height': $.gallery2Utility.leftImage.Lheight,
                'width': $.gallery2Utility.leftImage.Lwidth,
                'opacity': 'show'
            },
            $.gallery2Utility.Options.slideSpeed, 'linear', function() {
                $(this).one('click', $.gallery2Utility.slideLeft);
            });
        } else {
            $.gallery2Utility.leftImageStorage.image.animate({
                'top': $.gallery2Utility.leftImage.Ptop,
                'left': liLeft,
                'height': $.gallery2Utility.leftImage.Pheight,
                'width': $.gallery2Utility.leftImage.Pwidth,
                'opacity': 'show'
            },
            $.gallery2Utility.Options.slideSpeed, 'linear', function() {
                $(this).one('click', $.gallery2Utility.slideLeft);
            });
        }
        if ($.gallery2Utility.leftImage.image.attr('layout') === 'landscape') {
            $.gallery2Utility.leftImage.image.unbind().animate({
                'top': $.gallery2Utility.centerImage.Ltop,
                'left': $.gallery2Utility.centerImage.Lleft,
                'height': $.gallery2Utility.centerImage.Lheight,
                'width': $.gallery2Utility.centerImage.Lwidth,
                'opacity': 'show'
            },
            $.gallery2Utility.Options.slideSpeed, 'linear', function() {
                $(this).one('click', $.gallery2Utility.zoomIn);
            });
        } else {
            $.gallery2Utility.leftImage.image.unbind().animate({
                'top': $.gallery2Utility.centerImage.Ptop,
                'left': $.gallery2Utility.centerImage.Pleft,
                'height': $.gallery2Utility.centerImage.Pheight,
                'width': $.gallery2Utility.centerImage.Pwidth,
                'opacity': 'show'
            },
            $.gallery2Utility.Options.slideSpeed, 'linear', function() {
                $(this).one('click', $.gallery2Utility.zoomIn);
            });
        }
        if ($.gallery2Utility.centerImage.image.attr('layout') === 'landscape') {
            $.gallery2Utility.centerImage.image.unbind().animate({
                'top': $.gallery2Utility.rightImage.Ltop,
                'left': riLeft,
                'height': $.gallery2Utility.rightImage.Lheight,
                'width': $.gallery2Utility.rightImage.Lwidth
            },
            $.gallery2Utility.Options.slideSpeed, 'linear', function() {
                $(this).one('click', $.gallery2Utility.slideLeft);
            });
        } else {
            $.gallery2Utility.centerImage.image.unbind().animate({
                'top': $.gallery2Utility.rightImage.Ptop,
                'left': riLeft,
                'height': $.gallery2Utility.rightImage.Pheight,
                'width': $.gallery2Utility.rightImage.Pwidth
            },
            $.gallery2Utility.Options.slideSpeed, 'linear', function() {
                $(this).one('click', $.gallery2Utility.slideLeft);
            });
        }
        if ($.gallery2Utility.rightImage.image.attr('layout') === 'landscape') {
            $.gallery2Utility.rightImage.image.unbind().animate({
                'top': $.gallery2Utility.rightImageStorage.Ltop,
                'left': risLeft,
                'height': $.gallery2Utility.rightImageStorage.Lheight,
                'width': $.gallery2Utility.rightImageStorage.Lwidth,
                'opacity': 'hide'
            },
            $.gallery2Utility.Options.slideSpeed, 'linear');
        } else {
            $.gallery2Utility.rightImage.image.unbind().animate({
                'top': $.gallery2Utility.rightImageStorage.Ptop,
                'left': risLeft,
                'height': $.gallery2Utility.rightImageStorage.Pheight,
                'width': $.gallery2Utility.rightImageStorage.Pwidth,
                'opacity': 'hide'
            },
            $.gallery2Utility.Options.slideSpeed, 'linear');
        }
        $.gallery2Utility.rightImageStorage.image = $.gallery2Utility.rightImage.image;
        $.gallery2Utility.rightImage.image = $.gallery2Utility.centerImage.image;
        $.gallery2Utility.centerImage.image = $.gallery2Utility.leftImage.image;
        $.gallery2Utility.leftImage.image = $.gallery2Utility.leftImageStorage.image;
        $.gallery2Utility.setLeftStorage($.gallery2Utility.gallery2.filter('[index2=' + $.gallery2Utility.leftImageStorage.image.attr('prev2') + ']'));
    };

    $.gallery2Utility.slideLeft = function() {
        
        var centerX=0;
        var leftX=0;
        var riLeft = ($.gallery2Utility.rightImage.left($.gallery2Utility.rightImage.image));
        var liLeft = ($.gallery2Utility.leftImage.left($.gallery2Utility.centerImage.image, $.gallery2Utility.rightImage.image));
        var lisLeft = ($.gallery2Utility.leftImageStorage.left($.gallery2Utility.leftImage.image, $.gallery2Utility.centerImage.image, $.gallery2Utility.rightImage.image));
        riLeft=260;
        
        centerX=-540+$.gallery2Utility.centerImage.image.attr('width');
        leftX=centerX+$.gallery2Utility.rightImage.image.attr('width')+10
        liLeft=-560
        lisLeft=-1070;
        //alert('riLeft:'+riLeft+' liLeft:'+liLeft+' lisLeft:'+lisLeft+' centerLayout:'+$.gallery2Utility.centerImage.image.attr('layout'));
        if ($.gallery2Utility.rightImageStorage.image.attr('layout') === 'landscape') {
            $.gallery2Utility.rightImageStorage.image.unbind().animate({
                'top': $.gallery2Utility.rightImage.Ltop,
                'left': leftX,
                'height': $.gallery2Utility.rightImage.Lheight,
                'width': $.gallery2Utility.rightImage.Lwidth,
                'opacity': 'show'
            //'opacity': '.5'
            },
            $.gallery2Utility.Options.slideSpeed, 'linear', function() {
                $(this).one('click', $.gallery2Utility.slideLeft);
            });
        } else {
            $.gallery2Utility.rightImageStorage.image.unbind().animate({
                'top': $.gallery2Utility.rightImage.Ptop,
                'left': leftX,
                'height': $.gallery2Utility.rightImage.Pheight,
                'width': $.gallery2Utility.rightImage.Pwidth,
                'opacity': 'show'
            //'opacity': '.5'
            },
            $.gallery2Utility.Options.slideSpeed, 'linear', function() {
                $(this).one('click', $.gallery2Utility.slideLeft);
            });
        }
        if ($.gallery2Utility.rightImage.image.attr('layout') === 'landscape') {
            $.gallery2Utility.rightImage.image.unbind().animate({
                'top': $.gallery2Utility.centerImage.Ltop,
                'left': centerX,
                'height': $.gallery2Utility.centerImage.Lheight,
                'width': $.gallery2Utility.centerImage.Lwidth
            },
            $.gallery2Utility.Options.slideSpeed, 'linear', function() {
                $(this).one('click', $.gallery2Utility.slideLeft);
            });
        } else {
            $.gallery2Utility.rightImage.image.unbind().animate({
                'top': $.gallery2Utility.centerImage.Ptop,
                'left': centerX,
                'height': $.gallery2Utility.centerImage.Pheight,
                'width': $.gallery2Utility.centerImage.Pwidth
            },
            $.gallery2Utility.Options.slideSpeed, 'linear', function() {
                $(this).one('click', $.gallery2Utility.slideLeft);
            });
        }
        if ($.gallery2Utility.centerImage.image.attr('layout') === 'landscape') {
            $.gallery2Utility.centerImage.image.unbind().animate({
                'top': $.gallery2Utility.leftImage.Ltop,
                'left': liLeft,
                'height': $.gallery2Utility.leftImage.Lheight,
                'width': $.gallery2Utility.leftImage.Lwidth,
                'opacity': 'hide',
                'opacity': '1'
            },
            $.gallery2Utility.Options.slideSpeed, 'linear', function() {
                $(this).one('click', $.gallery2Utility.slideLeft);
            });
        } else {
            $.gallery2Utility.centerImage.image.unbind().animate({
                'top': $.gallery2Utility.leftImage.Ptop,
                'left': liLeft,
                'height': $.gallery2Utility.leftImage.Pheight,
                'width': $.gallery2Utility.leftImage.Pwidth,
                'opacity': 'hide',
                'opacity': '1'
            },
            $.gallery2Utility.Options.slideSpeed, 'linear', function() {
                $(this).one('click', $.gallery2Utility.slideLeft);
            });
        }
        if ($.gallery2Utility.leftImage.image.attr('layout') === 'landscape') {
            $.gallery2Utility.leftImage.image.unbind().animate({
                'top': $.gallery2Utility.leftImageStorage.Ltop,
                'left': lisLeft,
                'height': $.gallery2Utility.leftImageStorage.Lheight,
                'width': $.gallery2Utility.leftImageStorage.Lwidth,
                'opacity': 'hide'
            },
            $.gallery2Utility.Options.slideSpeed, 'linear', function() {
                $(this).one('click', $.gallery2Utility.slideLeft);
            });
        } else {
            $.gallery2Utility.leftImage.image.unbind().animate({
                'top': $.gallery2Utility.leftImageStorage.Ptop,
                'left': lisLeft,
                'height': $.gallery2Utility.leftImageStorage.Pheight,
                'width': $.gallery2Utility.leftImageStorage.Pwidth,
                'opacity': 'hide'
            },
            $.gallery2Utility.Options.slideSpeed, 'linear', function() {
                $(this).one('click', $.gallery2Utility.slideLeft);
            });
        }
        $.gallery2Utility.leftImageStorage.image = $.gallery2Utility.leftImage.image;
        $.gallery2Utility.leftImage.image = $.gallery2Utility.centerImage.image;
        $.gallery2Utility.centerImage.image = $.gallery2Utility.rightImage.image;
        $.gallery2Utility.rightImage.image = $.gallery2Utility.rightImageStorage.image;
        $.gallery2Utility.setRightStorage($.gallery2Utility.gallery2.filter('[index2=' + $.gallery2Utility.rightImageStorage.image.attr('next2') + ']'));
    };

    $.gallery2Utility.zoomIn = function() {
        $.gallery2Utility.gallery2.unbind();
        if ($.gallery2Utility.centerImage.image.attr('layout') === 'landscape') {
            $.gallery2Utility.centerImage.image.css('z-index2', '99').animate({
                'top': $.gallery2Utility.zoomImage.Ltop,
                'left': $.gallery2Utility.zoomImage.Lleft,
                'height': $.gallery2Utility.zoomImage.Lheight,
                'width': $.gallery2Utility.zoomImage.Lwidth
            },
            $.gallery2Utility.Options.zoomSpeed, 'linear', function() {
                $.gallery2Utility.centerImage.image.one('click', $.gallery2Utility.zoomOut);
                if ($.gallery2Utility.Options.useCaptions) {
                    $('#' + $.gallery2Utility.Options.captionDownID).css({
                        'height': 0,
                        'top': $.gallery2Utility.zoomImage.Ltop + parseInt($.gallery2Utility.centerImage.image.css('borderTopWidth'), 10),
                        'left': $.gallery2Utility.zoomImage.Lleft + ($.gallery2Utility.zoomImage.Lwidth - $.gallery2Utility.Options.captionDownWidth) + parseInt($.gallery2Utility.centerImage.image.css('borderLeftWidth'), 10),
                        'z-index2': 1000,
                        'position': 'absolute'
                    }).show().animate({
                        'height': $.gallery2Utility.Options.captionDownHeight
                    }, 'fast', 'linear', function() {
                        $('#' + $.gallery2Utility.Options.captionDownID).one('click', $.gallery2Utility.LcaptionDown);
                    });
                }
            });
        } else {
            $.gallery2Utility.centerImage.image.css('z-index2', '99').animate({
                'top': $.gallery2Utility.zoomImage.Ptop,
                'left': $.gallery2Utility.zoomImage.Pleft,
                'height': $.gallery2Utility.zoomImage.Pheight,
                'width': $.gallery2Utility.zoomImage.Pwidth
            },
            $.gallery2Utility.Options.zoomSpeed, 'linear', function() {
                $.gallery2Utility.centerImage.image.one('click', $.gallery2Utility.zoomOut);
                if ($.gallery2Utility.Options.useCaptions) {
                    $('#' + $.gallery2Utility.Options.captionDownID).css({
                        'height': 0,
                        'top': $.gallery2Utility.zoomImage.Ptop + parseInt($.gallery2Utility.centerImage.image.css('borderTopWidth'), 10),
                        'left': $.gallery2Utility.zoomImage.Pleft + ($.gallery2Utility.zoomImage.Pwidth - $.gallery2Utility.Options.captionDownWidth) + parseInt($.gallery2Utility.centerImage.image.css('borderLeftWidth'), 10),
                        'z-index2': 100,
                        'position': 'absolute'
                    }).show().animate({
                        'height': $.gallery2Utility.Options.captionDownHeight
                    }, 'fast', 'linear', function() {
                        $('#' + $.gallery2Utility.Options.captionDownID).one('click', $.gallery2Utility.PcaptionDown);
                    });
                }
            });
        }
    };

    $.gallery2Utility.LcaptionDown = function() {
        $.gallery2Utility.centerImage.image.unbind();
        $.gallery2Utility.Options.container.append('<span id="' + $.gallery2Utility.Options.captionID + '" style="' + $.gallery2Utility.Options.captionStyle + '" class="' + $.gallery2Utility.Options.captionClass + '">' + $.gallery2Utility.centerImage.image.attr($.gallery2Utility.Options.captionTextAttribute) + '</span>');
        $('#' + $.gallery2Utility.Options.captionID).css({
            'top': $.gallery2Utility.zoomImage.Ltop + parseInt($.gallery2Utility.centerImage.image.css('borderTopWidth'), 10),
            'left': $.gallery2Utility.zoomImage.Lleft + parseInt($.gallery2Utility.centerImage.image.css('borderLeftWidth'), 10),
            'width': $.gallery2Utility.zoomImage.Lwidth,
            'height': 0,
            'position': 'absolute',
            'z-index2': '100'
        }).animate({
            'height': Math.round($.gallery2Utility.Options.captionHeight($.gallery2Utility.zoomImage.Lheight))
        }, 'normal', 'linear');
        $('#' + $.gallery2Utility.Options.captionDownID).animate({
            'height': 0,
            'top': $.gallery2Utility.zoomImage.Ltop + (Math.round($.gallery2Utility.Options.captionHeight($.gallery2Utility.zoomImage.Lheight))) + parseInt($.gallery2Utility.centerImage.image.css('borderTopWidth'), 10)
        }, 'normal', 'linear', function() {
            $('#' + $.gallery2Utility.Options.captionDownID).hide();
        });
        $('#' + $.gallery2Utility.Options.captionUpID).css({
            'top': $.gallery2Utility.zoomImage.Ltop + $.gallery2Utility.Options.captionDownHeight + parseInt($.gallery2Utility.centerImage.image.css('borderTopWidth'), 10),
            'left': $.gallery2Utility.zoomImage.Lleft + ($.gallery2Utility.zoomImage.Lwidth - $.gallery2Utility.Options.captionUpWidth) + parseInt($.gallery2Utility.centerImage.image.css('borderLeftWidth'), 10),
            'height': 0,
            'position': 'absolute',
            'z-index2': '100'
        }).show().animate({
            'top': $.gallery2Utility.zoomImage.Ltop + (Math.round($.gallery2Utility.Options.captionHeight($.gallery2Utility.zoomImage.Lheight))) + parseInt($.gallery2Utility.centerImage.image.css('borderTopWidth'), 10),
            'height': $.gallery2Utility.Options.captionUpHeight
        }, 'normal', 'linear', function() {
            $('#' + $.gallery2Utility.Options.captionUpID).one('click', function() {
                $.gallery2Utility.LcaptionUp(false);
            });
            $.gallery2Utility.centerImage.image.one('click', function() {
                $.gallery2Utility.LcaptionUp(true);
            });
        });
    };

    $.gallery2Utility.PcaptionDown = function() {
        $.gallery2Utility.centerImage.image.unbind();
        $.gallery2Utility.Options.container.append('<span id="' + $.gallery2Utility.Options.captionID + '" style="' + $.gallery2Utility.Options.captionStyle + '" class="' + $.gallery2Utility.Options.captionClass + '">' + $.gallery2Utility.centerImage.image.attr($.gallery2Utility.Options.captionTextAttribute) + '</span>');
        $('#' + $.gallery2Utility.Options.captionID).css({
            'top': $.gallery2Utility.zoomImage.Ptop + parseInt($.gallery2Utility.centerImage.image.css('borderTopWidth'), 10),
            'left': $.gallery2Utility.zoomImage.Pleft + parseInt($.gallery2Utility.centerImage.image.css('borderLeftWidth'), 10),
            'width': $.gallery2Utility.zoomImage.Pwidth,
            'height': 0,
            'position': 'absolute',
            'z-index2': '100'
        }).animate({
            'height': Math.round($.gallery2Utility.Options.captionHeight($.gallery2Utility.zoomImage.Pheight))
        }, 'normal', 'linear');
        $('#' + $.gallery2Utility.Options.captionDownID).animate({
            'height': 0,
            'top': $.gallery2Utility.zoomImage.Ptop + (Math.round($.gallery2Utility.Options.captionHeight($.gallery2Utility.zoomImage.Pheight))) + parseInt($.gallery2Utility.centerImage.image.css('borderTopWidth'), 10)
        }, 'normal', 'linear', function() {
            $('#' + $.gallery2Utility.Options.captionDownID).hide();
        });
        $('#' + $.gallery2Utility.Options.captionUpID).css({
            'top': $.gallery2Utility.zoomImage.Ptop + $.gallery2Utility.Options.captionDownHeight + parseInt($.gallery2Utility.centerImage.image.css('borderTopWidth'), 10),
            'left': $.gallery2Utility.zoomImage.Pleft + ($.gallery2Utility.zoomImage.Pwidth - $.gallery2Utility.Options.captionUpWidth) + parseInt($.gallery2Utility.centerImage.image.css('borderLeftWidth'), 10),
            'height': 0,
            'position': 'absolute',
            'z-index2': '100'
        }).show().animate({
            'top': $.gallery2Utility.zoomImage.Ptop + (Math.round($.gallery2Utility.Options.captionHeight($.gallery2Utility.zoomImage.Pheight))) + parseInt($.gallery2Utility.centerImage.image.css('borderTopWidth'), 10),
            'height': $.gallery2Utility.Options.captionUpHeight
        }, 'normal', 'linear', function() {
            $('#' + $.gallery2Utility.Options.captionUpID).one('click', function() {
                $.gallery2Utility.PcaptionUp(false);
            });
            $.gallery2Utility.centerImage.image.one('click', function() {
                $.gallery2Utility.PcaptionUp(true);
            });
        });
    };

    $.gallery2Utility.LcaptionUp = function(unzoom) {
        $('#' + $.gallery2Utility.Options.captionID).animate({
            'height': 0
        }, 'normal', 'linear', function() {
            $('#' + $.gallery2Utility.Options.captionID).remove();
        });
        $('#' + $.gallery2Utility.Options.captionUpID).animate({
            'top': $.gallery2Utility.zoomImage.Ltop + $.gallery2Utility.Options.captionDownHeight + parseInt($.gallery2Utility.centerImage.image.css('borderTopWidth'), 10),
            'height': 0
        }, 'normal', 'linear', function() {
            $('#' + $.gallery2Utility.Options.captionUpID).hide();
        });
        $('#' + $.gallery2Utility.Options.captionDownID).show().animate({
            'height': $.gallery2Utility.Options.captionDownHeight,
            'top': $.gallery2Utility.zoomImage.Ltop + parseInt($.gallery2Utility.centerImage.image.css('borderTopWidth'), 10)
        }, 'normal', 'linear', function() {
            if (unzoom) {
                $('#' + $.gallery2Utility.Options.captionUpID).unbind();
                $.gallery2Utility.zoomOut();
            } else {
                $('#' + $.gallery2Utility.Options.captionDownID).one('click', $.gallery2Utility.LcaptionDown);
                $.gallery2Utility.centerImage.image.one('click', $.gallery2Utility.zoomOut);
            }
        });
    };

    $.gallery2Utility.PcaptionUp = function(unzoom) {
        $('#' + $.gallery2Utility.Options.captionID).animate({
            'height': 0
        }, 'normal', 'linear', function() {
            $('#' + $.gallery2Utility.Options.captionID).remove();
        });
        $('#' + $.gallery2Utility.Options.captionUpID).animate({
            'top': $.gallery2Utility.zoomImage.Ptop + $.gallery2Utility.Options.captionDownHeight + parseInt($.gallery2Utility.centerImage.image.css('borderTopWidth'), 10),
            'height': 0
        }, 'normal', 'linear', function() {
            $('#' + $.gallery2Utility.Options.captionUpID).hide();
        });
        $('#' + $.gallery2Utility.Options.captionDownID).show().animate({
            'height': $.gallery2Utility.Options.captionDownHeight,
            'top': $.gallery2Utility.zoomImage.Ptop + parseInt($.gallery2Utility.centerImage.image.css('borderTopWidth'), 10)
        }, 'normal', 'linear', function() {
            if (unzoom) {
                $('#' + $.gallery2Utility.Options.captionUpID).unbind();
                $.gallery2Utility.zoomOut();
            } else {
                $('#' + $.gallery2Utility.Options.captionDownID).one('click', $.gallery2Utility.PcaptionDown);
                $.gallery2Utility.centerImage.image.one('click', $.gallery2Utility.zoomOut);
            }
        });
    };

    $.gallery2Utility.zoomOut = function() {
        if ($.gallery2Utility.Options.useCaptions) {
            $('#' + $.gallery2Utility.Options.captionDownID).animate({
                'height': 0
            }, 50, 'linear', $.gallery2Utility.zoomOutBody).unbind();
        } else {
            $.gallery2Utility.zoomOutBody();
        }
    };

    $.gallery2Utility.zoomOutBody = function() {
        if ($.gallery2Utility.centerImage.image.attr('layout') === 'landscape') {
            $.gallery2Utility.centerImage.image.animate({
                'top': $.gallery2Utility.centerImage.Ltop,
                'left': $.gallery2Utility.centerImage.Lleft,
                'height': $.gallery2Utility.centerImage.Lheight,
                'width': $.gallery2Utility.centerImage.Lwidth
            },
            $.gallery2Utility.Options.zoomSpeed, 'linear', function() {
                $(this).css('z-index2', '0').one('click', $.gallery2Utility.zoomIn);
                $.gallery2Utility.leftImage.image.one('click', $.gallery2Utility.slideRight);
                $.gallery2Utility.rightImage.image.one('click', $.gallery2Utility.slideLeft);
            });
        } else {
            $.gallery2Utility.centerImage.image.animate({
                'top': $.gallery2Utility.centerImage.Ptop,
                'left': $.gallery2Utility.centerImage.Pleft,
                'height': $.gallery2Utility.centerImage.Pheight,
                'width': $.gallery2Utility.centerImage.Pwidth
            },
            $.gallery2Utility.Options.zoomSpeed, 'linear', function() {
                $(this).css('z-index2', '0').one('click', $.gallery2Utility.zoomIn);
                $.gallery2Utility.leftImage.image.one('click', $.gallery2Utility.slideRight);
                $.gallery2Utility.rightImage.image.one('click', $.gallery2Utility.slideLeft);
            });
        }
    };

    $.gallery2Utility.setRightStorage = function(image) {
        $.gallery2Utility.rightImageStorage.image = image;
        if ($.gallery2Utility.rightImageStorage.image.attr('layout') === 'landscape') {
            $.gallery2Utility.rightImageStorage.image.hide().css({
                'top': $.gallery2Utility.rightImageStorage.Ltop,
                'height': $.gallery2Utility.rightImageStorage.Lheight,
                'width': $.gallery2Utility.rightImageStorage.Lwidth,
                'background-color':'#ffffff',
                'opacity':'.15'
            });
        } else {
            $.gallery2Utility.rightImageStorage.image.hide().css({
                'top': $.gallery2Utility.rightImageStorage.Ptop,
                'height': $.gallery2Utility.rightImageStorage.Pheight,
                'width': $.gallery2Utility.rightImageStorage.Pwidth,
                'opacity':'.15',
                'background-color':'#ffffff'
            });
        }
        $.gallery2Utility.rightImageStorage.image.css('left', $.gallery2Utility.rightImageStorage.left($.gallery2Utility.rightImage.image));
    };

    $.gallery2Utility.setLeftStorage = function(image) {
        $.gallery2Utility.leftImageStorage.image = image;
        if ($.gallery2Utility.leftImageStorage.image.attr('layout') === 'landscape') {
            $.gallery2Utility.leftImageStorage.image.hide().css({
                'top': $.gallery2Utility.leftImageStorage.Ltop,
                'height': $.gallery2Utility.leftImageStorage.Lheight,
                'width': $.gallery2Utility.leftImageStorage.Lwidth,
                'background-color':'#ffffff'
                
            });
        } else {
            $.gallery2Utility.leftImageStorage.image.hide().css({
                'top': $.gallery2Utility.leftImageStorage.Ptop,
                'height': $.gallery2Utility.leftImageStorage.Pheight,
                'width': $.gallery2Utility.leftImageStorage.Pwidth,
                'background-color':'#ffffff'
                
            });
        }
        $.gallery2Utility.leftImageStorage.image
        .css('left', $.gallery2Utility.leftImageStorage.left($.gallery2Utility.leftImageStorage.image, $.gallery2Utility.leftImage.image, $.gallery2Utility.centerImage.image));
    };

    $.gallery2Utility.setCenter = function(image) {
        $.gallery2Utility.centerImage.image = image;
        if ($.gallery2Utility.centerImage.image.attr('layout') === 'landscape') {
            $.gallery2Utility.centerImage.image.css({
                'top': $.gallery2Utility.centerImage.Ltop,
                'left': $.gallery2Utility.centerImage.Lleft,
                'height': $.gallery2Utility.centerImage.Lheight,
                'width': $.gallery2Utility.centerImage.Lwidth,
                'opacity':'.15',
                'background-color':'#ffffff'
            });
        } else {
            $.gallery2Utility.centerImage.image.css({
                'top': $.gallery2Utility.centerImage.Ptop,
                'left': $.gallery2Utility.centerImage.Pleft,
                'height': $.gallery2Utility.centerImage.Pheight,
                'width': $.gallery2Utility.centerImage.Pwidth,
                'opacity':'.15'
                ,
                'background-color':'#ffffff'
            });
        }
    };

    $.gallery2Utility.setRight = function(image) {
        $.gallery2Utility.rightImage.image = image;
        if ($.gallery2Utility.rightImage.image.attr('layout') === 'landscape') {
            $.gallery2Utility.rightImage.image.css({
                'top': $.gallery2Utility.rightImage.Ltop,
                'height': $.gallery2Utility.rightImage.Lheight,
                'width': $.gallery2Utility.rightImage.Lwidth,
                'opacity':'.15',
                'background-color':'#ffffff'
            });
        } else {
            $.gallery2Utility.rightImage.image.css({
                'top': $.gallery2Utility.rightImage.Ptop,
                'height': $.gallery2Utility.rightImage.Pheight,
                'width': $.gallery2Utility.rightImage.Pwidth,
                'opacity':'.15',
                'background-color':'#ffffff'
            });
        }
        $.gallery2Utility.rightImage.image.css('left', $.gallery2Utility.rightImage.left($.gallery2Utility.centerImage.image));
    };

    $.gallery2Utility.setLeft = function(image) {
        $.gallery2Utility.leftImage.image = image;
        if ($.gallery2Utility.leftImage.image.attr('layout') === 'landscape') {
            $.gallery2Utility.leftImage.image.css({
                'top': $.gallery2Utility.leftImage.Ltop,
                'height': $.gallery2Utility.leftImage.Lheight,
                'width': $.gallery2Utility.leftImage.Lwidth,
                'background-color':'#ffffff'

            });
        } else {
            $.gallery2Utility.leftImage.image.css({
                'top': $.gallery2Utility.leftImage.Ptop,
                'height': $.gallery2Utility.leftImage.Pheight,
                'width': $.gallery2Utility.leftImage.Pwidth,
                'background-color':'#ffffff'
            });
        }
        
        $.gallery2Utility.leftImage.image.css('left', $.gallery2Utility.leftImage.left($.gallery2Utility.leftImage.image, $.gallery2Utility.centerImage.image));
    };



    $.gallery2Utility.definePositions = function() {
        //var Gheight = ($.gallery2Utility.Options.Gheight || $(window).height());
        //var Gwidth = ($.gallery2Utility.Options.Gwidth || $(window).width());
        var container = $.gallery2Utility.Options.container;
        if (container[0].tagName == 'BODY') {
            container = $(window);
        }
        var Gheight = container.height();
        var Gwidth = container.width();

        $.gallery2Utility.centerImage.Lheight = Math.round($.gallery2Utility.Options.Lheight);
        $.gallery2Utility.centerImage.Lwidth = Math.round($.gallery2Utility.Options.Lwidth);
        $.gallery2Utility.centerImage.Ltop = Math.round(Gheight / 2) - ($.gallery2Utility.centerImage.Lheight / 2);
        $.gallery2Utility.centerImage.Lleft = Math.round(Gwidth / 2) - ($.gallery2Utility.centerImage.Lwidth / 2);
        $.gallery2Utility.centerImage.Pheight = Math.round($.gallery2Utility.Options.Pheight);
        $.gallery2Utility.centerImage.Pwidth = Math.round($.gallery2Utility.Options.Pwidth);
        $.gallery2Utility.centerImage.Ptop = Math.round((Gheight / 2) - ($.gallery2Utility.centerImage.Pheight / 2));
        $.gallery2Utility.centerImage.Pleft = Math.round((Gwidth / 2) - ($.gallery2Utility.centerImage.Pwidth / 2));
        $.gallery2Utility.zoomImage.Lheight = Math.round($.gallery2Utility.Options.Lzoom($.gallery2Utility.centerImage.Lheight));
        $.gallery2Utility.zoomImage.Lwidth = Math.round($.gallery2Utility.Options.Lzoom($.gallery2Utility.centerImage.Lwidth));
        $.gallery2Utility.zoomImage.Ltop = Math.round((Gheight / 2) - ($.gallery2Utility.zoomImage.Lheight / 2));
        $.gallery2Utility.zoomImage.Lleft = Math.round((Gwidth / 2) - ($.gallery2Utility.zoomImage.Lwidth / 2));
        $.gallery2Utility.zoomImage.Pheight = Math.round($.gallery2Utility.Options.Pzoom($.gallery2Utility.centerImage.Pheight));
        $.gallery2Utility.zoomImage.Pwidth = Math.round($.gallery2Utility.Options.Pzoom($.gallery2Utility.centerImage.Pwidth));
        $.gallery2Utility.zoomImage.Ptop = Math.round((Gheight / 2) - ($.gallery2Utility.zoomImage.Pheight / 2));
        $.gallery2Utility.zoomImage.Pleft = Math.round((Gwidth / 2) - ($.gallery2Utility.zoomImage.Pwidth / 2));
        $.gallery2Utility.leftImage.Lheight = Math.round($.gallery2Utility.Options.Lshrink($.gallery2Utility.centerImage.Lheight));
        $.gallery2Utility.leftImage.Lwidth = Math.round($.gallery2Utility.Options.Lshrink($.gallery2Utility.centerImage.Lwidth));
        $.gallery2Utility.leftImage.Ltop = Math.round($.gallery2Utility.centerImage.Ltop + (($.gallery2Utility.centerImage.Lheight - $.gallery2Utility.leftImage.Lheight) / 2));
        $.gallery2Utility.leftImage.left = function(left, center) {
            if (center.attr('layout') === 'landscape') {
                if (left.attr('layout') === 'landscape') {
                    //return Math.round($.gallery2Utility.centerImage.Lleft - ($.gallery2Utility.leftImage.Lwidth + $.gallery2Utility.Options.gutterWidth+40));
                    return Math.round(-560);
                } else {
                    //return Math.round($.gallery2Utility.centerImage.Lleft - ($.gallery2Utility.leftImage.Pwidth + $.gallery2Utility.Options.gutterWidth+40));
                    return Math.round(-560);
                }
            } else {
                if (left.attr('layout') === 'landscape') {
                    return Math.round(-560);
                } else {
                    return Math.round(-560);
                }
            }
        };
        $.gallery2Utility.leftImage.Pheight = Math.round($.gallery2Utility.Options.Pshrink($.gallery2Utility.centerImage.Pheight));
        $.gallery2Utility.leftImage.Pwidth = Math.round($.gallery2Utility.Options.Pshrink($.gallery2Utility.centerImage.Pwidth));
        $.gallery2Utility.leftImage.Ptop = Math.round($.gallery2Utility.centerImage.Ptop + (($.gallery2Utility.centerImage.Pheight - $.gallery2Utility.leftImage.Pheight) / 2));
        $.gallery2Utility.rightImage.Lheight = Math.round($.gallery2Utility.Options.Lshrink($.gallery2Utility.centerImage.Lheight));
        $.gallery2Utility.rightImage.Lwidth = Math.round($.gallery2Utility.Options.Lshrink($.gallery2Utility.centerImage.Lwidth));
        $.gallery2Utility.rightImage.Ltop = Math.round($.gallery2Utility.centerImage.Ltop + (($.gallery2Utility.centerImage.Lheight - $.gallery2Utility.rightImage.Lheight) / 2));
        $.gallery2Utility.rightImage.left = function(center) {
            if (center.attr('layout') === 'landscape') {
                return Math.round($.gallery2Utility.centerImage.Lleft + ($.gallery2Utility.centerImage.Lwidth + $.gallery2Utility.Options.gutterWidth));
            } else {
                return Math.round($.gallery2Utility.centerImage.Pleft + ($.gallery2Utility.centerImage.Pwidth + $.gallery2Utility.Options.gutterWidth));
            }
        };
        $.gallery2Utility.rightImage.Pheight = Math.round($.gallery2Utility.Options.Pshrink($.gallery2Utility.centerImage.Pheight));
        $.gallery2Utility.rightImage.Pwidth = Math.round($.gallery2Utility.Options.Pshrink($.gallery2Utility.centerImage.Pwidth));
        $.gallery2Utility.rightImage.Ptop = Math.round($.gallery2Utility.centerImage.Ptop + (($.gallery2Utility.centerImage.Pheight - $.gallery2Utility.leftImage.Pheight) / 2));
        $.gallery2Utility.leftImageStorage.Lheight = Math.round($.gallery2Utility.Options.Lshrink($.gallery2Utility.leftImage.Lheight));
        $.gallery2Utility.leftImageStorage.Lwidth = Math.round($.gallery2Utility.Options.Lshrink($.gallery2Utility.leftImage.Lwidth));
        $.gallery2Utility.leftImageStorage.Ltop = Math.round($.gallery2Utility.leftImage.Ltop + (($.gallery2Utility.leftImage.Lheight - $.gallery2Utility.leftImageStorage.Lheight) / 2));
        $.gallery2Utility.leftImageStorage.left = function(leftStorage, left, center) {
            if (leftStorage.attr('layout') === 'landscape') {
                return Math.round($.gallery2Utility.leftImage.left(left, center) - ($.gallery2Utility.leftImageStorage.Lwidth + $.gallery2Utility.Options.gutterWidth));
            } else {
                return Math.round($.gallery2Utility.leftImage.left(left, center) - ($.gallery2Utility.leftImageStorage.Pwidth + $.gallery2Utility.Options.gutterWidth));
            }
        };
        $.gallery2Utility.leftImageStorage.Pheight = Math.round($.gallery2Utility.Options.Pshrink($.gallery2Utility.leftImage.Pheight));
        $.gallery2Utility.leftImageStorage.Pwidth = Math.round($.gallery2Utility.Options.Pshrink($.gallery2Utility.leftImage.Pwidth));
        $.gallery2Utility.leftImageStorage.Ptop = Math.round($.gallery2Utility.leftImage.Ptop + (($.gallery2Utility.leftImage.Pheight - $.gallery2Utility.leftImageStorage.Pheight) / 2));
        $.gallery2Utility.rightImageStorage.Lheight = Math.round($.gallery2Utility.Options.Lshrink($.gallery2Utility.rightImage.Lheight));
        $.gallery2Utility.rightImageStorage.Lwidth = Math.round($.gallery2Utility.Options.Lshrink($.gallery2Utility.rightImage.Lwidth));
        $.gallery2Utility.rightImageStorage.Ltop = Math.round($.gallery2Utility.rightImage.Ltop + (($.gallery2Utility.rightImage.Lheight - $.gallery2Utility.rightImageStorage.Lheight) / 2));
        $.gallery2Utility.rightImageStorage.left = function(right) {
            if (right.attr('layout') === 'landscape') {
                return Math.round($.gallery2Utility.rightImage.left(right) + ($.gallery2Utility.rightImage.Lwidth + $.gallery2Utility.Options.gutterWidth));
            } else {
                return Math.round($.gallery2Utility.rightImage.left(right) + ($.gallery2Utility.rightImage.Pwidth + $.gallery2Utility.Options.gutterWidth));
            }
        };
        $.gallery2Utility.rightImageStorage.Pheight = Math.round($.gallery2Utility.Options.Pshrink($.gallery2Utility.rightImage.Pheight));
        $.gallery2Utility.rightImageStorage.Pwidth = Math.round($.gallery2Utility.Options.Pshrink($.gallery2Utility.rightImage.Pwidth));
        $.gallery2Utility.rightImageStorage.Ptop = Math.round($.gallery2Utility.rightImage.Ptop + (($.gallery2Utility.rightImage.Pheight - $.gallery2Utility.rightImageStorage.Pheight) / 2));
    };
})(jQuery);
