; if (!window.jqsDCCWMGSS) jqsDCCWMGSS = jQuery;

//Global variable to check if window is already loaded
//Used for calling GalleryView after page has loaded
var window_loaded = false;
(function($) {
    $.fn.galleryView = function(options) {
        var gvObj = {};
        /*
        **	GalleryView options and default values
        */
        gvObj.defaults = {
            show_panels: true, 				//BOOLEAN - flag to show or hide panel portion of gallery
            show_filmstrip: true, 			//BOOLEAN - flag to show or hide filmstrip portion of gallery
            panel_width: 600, 				//INT - width of gallery panel (in pixels)
            panel_height: 400, 				//INT - height of gallery panel (in pixels)
            frame_width: 60, 				//INT - width of filmstrip frames (in pixels)
            frame_height: 40, 				//INT - width of filmstrip frames (in pixels)
            start_frame: 1, 					//INT - index of panel/frame to show first when gallery loads
            filmstrip_size: 3,
            transition_speed: 800, 			//INT - duration of panel/frame transition (in milliseconds)
            transition_interval: 4000, 		//INT - delay between panel/frame transitions (in milliseconds)
            overlay_opacity: 0.7, 			//FLOAT - transparency for panel overlay (1.0 = opaque, 0.0 = transparent)
            frame_opacity: 0.3, 				//FLOAT - transparency of non-active frames (1.0 = opaque, 0.0 = transparent)
            pointer_size: 8, 				//INT - Height of frame pointer (in pixels)
            nav_theme: 'dark', 				//STRING - name of navigation theme to use (folder must exist within 'themes' directory)
            easing: 'swing', 				//STRING - easing method to use for animations (jQuery provides 'swing' or 'linear', more available with jQuery UI or Easing plugin)
            filmstrip_position: 'bottom', 	//STRING - position of filmstrip within gallery (bottom, top, left, right)
            overlay_position: 'bottom', 		//STRING - position of panel overlay (bottom, top, left, right)
            panel_scale: 'nocrop', 			//STRING - cropping option for panel images (crop = scale image and fit to aspect ratio determined by panel_width and panel_height, nocrop = scale image and preserve original aspect ratio)
            frame_scale: 'crop', 			//STRING - cropping option for filmstrip images (same as above)
            frame_gap: 5, 					//INT - spacing between frames within filmstrip (in pixels)
            show_captions: false, 			//BOOLEAN - flag to show or hide frame captions
            fade_panels: true, 				//BOOLEAN - flag to fade panels during transitions or swap instantly
            pause_on_hover: false, 			//BOOLEAN - flag to pause slideshow when user hovers over the gallery
            overlay_color: 'black',
            overlay_text_color: 'white',
            show_overlay: true,
            show_navigation: true,
            border_color: 000,
            border_width: 1,
            padding: 5,
            disable_rightclick: false
        };
        gvObj.ulList = null;
        gvObj.opts = $.extend(gvObj.defaults, options);
        gvObj.iterator = 0; 	// INT - Currently visible panel/frame
        gvObj.item_count = 0; 	// INT - Total number of panels/frames       
        gvObj.paused = false; 	// BOOLEAN - flag to indicate whether automated transitions are active        
        gvObj.frame_caption_size = 20;
        // Arrays used to scale frames and panels
        gvObj.frame_img_scale = {};
        gvObj.panel_img_scale = {};
        gvObj.img_h = {};
        gvObj.img_w = {};
        // Flag indicating whether to scale panel images
        gvObj.scale_panel_images = true;
        gvObj.panel_nav_displayed = true;
        /*
        **	showItem(int)
        **		Transition from current frame to frame i (1-based index)
        */
        function showItem(i) {
            // Disable next/prev buttons until transition is complete
            // This prevents overlapping of animations
            $('.nav-next-overlay', gvObj.j_gallery).unbind('click');
            $('.nav-prev-overlay', gvObj.j_gallery).unbind('click');
            $('.nav-next', gvObj.j_gallery).unbind('click');
            $('.nav-prev', gvObj.j_gallery).unbind('click');
            gvObj.j_frames.unbind('click');

            //If necessary, fade out all panels while fading in target panel
            if (gvObj.opts.show_panels && gvObj.opts.fade_panels) {
                gvObj.j_panels.fadeOut(gvObj.opts.transition_speed).eq(i).fadeIn(gvObj.opts.transition_speed, function() {
                    //If no filmstrip exists, re-bind click events to navigation buttons
                    if (!gvObj.opts.show_filmstrip && gvObj.opts.show_navigation) {
                        $('.nav-prev-overlay', gvObj.j_gallery).click(showPrevItem);
                        $('.nav-next-overlay', gvObj.j_gallery).click(showNextItem);
                        $('.nav-prev', gvObj.j_gallery).click(showPrevItem);
                        $('.nav-next', gvObj.j_gallery).click(showNextItem);
                    }
                });
            }
        };
        gvObj.showItem = showItem;
        /*
        **	extraWidth(jQuery element)
        **		Return the combined width of the border and padding to the elements left and right.
        **		If the border is non-numerical, assume zero (not ideal, will fix later)
        **		RETURNS - int
        */
        function extraWidth(el) {
            if (!el) { return 0; }
            if (el.length == 0) { return 0; }
            el = el.eq(0);
            var ew = 0;
            ew += getInt(el.css('paddingLeft'));
            ew += getInt(el.css('paddingRight'));
            ew += getInt(el.css('borderLeftWidth'));
            ew += getInt(el.css('borderRightWidth'));
            return ew;
        };
        gvObj.extraWidth = extraWidth;

        /*
        **	extraHeight(jQuery element)
        **		Return the combined height of the border and padding above and below the element
        **		If the border is non-numerical, assume zero (not ideal, will fix later)
        **		RETURN - int
        */
        function extraHeight(el) {
            if (!el) { return 0; }
            if (el.length == 0) { return 0; }
            el = el.eq(0);
            var eh = 0;
            eh += getInt(el.css('paddingTop'));
            eh += getInt(el.css('paddingBottom'));
            eh += getInt(el.css('borderTopWidth'));
            eh += getInt(el.css('borderBottomWidth'));
            return eh;
        };
        gvObj.extraHeight = extraHeight;
        /*
        **	showNextItem()
        **		Transition from current frame to next frame
        */
        function showNextItem() {
            try {
                var mid = this.ModuleId || parseInt($(this).attr("ModuleId"));
                DCCMGSS = DCCMGSS_SetConstants(mid);
                gvObj = DCCMGSS.SlideShow;
                if (DCCMGSS.SlideShow.iterator == DCCMGSS.SlideShow.j_frames.length) {
                    DCCMGSS.SlideShow.iterator = 0;
                }
                $(gvObj).stopTime("transition");
                try {
                    if ($(gvObj.j_panels[gvObj.iterator]).find("img")[0].complete) {
                        showItem(gvObj.iterator++);
                        // If automated transitions haven't been cancelled by an option or gvObj.paused on hover, re-enable them
                        if (!gvObj.paused) {
                            $(gvObj).everyTime(gvObj.opts.transition_interval, "transition", function() {
                                var moduleId = mid;
                                DCCMGSS = DCCMGSS_SetConstants(this.ModuleId);
                                showNextItem.call(DCCMGSS.SlideShow);
                            });
                            if ((gvObj.iterator >= gvObj.j_frames.length - 1) && !DCCMGSS.SlideShow.isComplete) {
                                MGSS.AddSlides(DCCMGSS.ModuleId, DCCMGSS.GalleryId, DCCMGSS.FolderId, ++DCCMGSS.PageNo);
                            }
                        }
                        return;
                    }
                    else {
                        $(gvObj.j_panels[gvObj.iterator]).find("img").load(function() {
                            $(gvObj).everyTime(gvObj.opts.transition_interval, "transition", function() {
                                var moduleId = mid;
                                DCCMGSS = DCCMGSS_SetConstants(this.ModuleId);
                                showNextItem.call(DCCMGSS.SlideShow);
                            });
                        });
                        if ((gvObj.iterator >= gvObj.j_frames.length - 1) && !DCCMGSS.SlideShow.isComplete) {
                            MGSS.AddSlides(DCCMGSS.ModuleId, DCCMGSS.GalleryId, DCCMGSS.FolderId, ++DCCMGSS.PageNo);
                        }
                    }
                }
                catch (exc) { }
            }
            catch (err) { }
        };
        gvObj.showNextItem = showNextItem;
        /*
        **	showPrevItem()
        **		Transition from current frame to previous frame
        */
        function showPrevItem() {
            // Cancel any transition timers until we have completed this function
            $(gvObj).stopTime("transition");
            if (--gvObj.iterator < 0) { gvObj.iterator = gvObj.item_count - 1; }
            // We've already written the code to transition to an arbitrary panel/frame, so use it
            showItem(gvObj.iterator);
            // If automated transitions haven't been cancelled by an option or gvObj.paused on hover, re-enable them
            if (!gvObj.paused) {
                $(gvObj).everyTime(gvObj.opts.transition_interval, "transition", function() {
                    showNextItem();
                });
            }
        };
        gvObj.showPrevItem = showPrevItem;
        /*
        **	getPos(jQuery element
        **		Calculate position of an element relative to top/left corner of gallery
        **		If the gallery bounding box itself is passed to the function, calculate position of gallery relative to top/left corner of browser window
        ** 		RETURNS - JSON {left: int, top: int}
        */
        function getPos(el) {
            var left = 0, top = 0;
            var el_id = el.id;
            if (el.offsetParent) {
                do {
                    left += el.offsetLeft;
                    top += el.offsetTop;
                } while (el = el.offsetParent);
            }
            //If we want the position of the gallery itself, return it
            if (el_id == gvObj.id) { return { 'left': left, 'top': top }; }
            //Otherwise, get position of element relative to gallery
            else {
                var gPos = getPos(gvObj.j_gallery[0]);
                var gLeft = gPos.left;
                var gTop = gPos.top;

                return { 'left': left - gLeft, 'top': top - gTop };
            }
        };
        gvObj.getPos = getPos;
        /*
        **	enableFrameClicking()
        **		Add an onclick event handler to each frame
        **		Exception: if a frame has an anchor tag, do not add an onlick handler
        */
        function enableFrameClicking() {
            gvObj.j_frames.each(function(i) {
                if ($('a', this).length == 0) {
                    $(this).click(function() {
                        // Prevent transitioning to the current frame (unnecessary)
                        if (gvObj.iterator != i) {
                            $(gvObj).stopTime("transition");
                            showItem(i);
                            gvObj.iterator = i;
                            if (!gvObj.paused) {
                                $(gvObj).everyTime(gvObj.opts.transition_interval, "transition", function() {
                                    showNextItem();
                                });
                            }
                        }
                    });
                }
            });
        };
        gvObj.enableFrameClicking = enableFrameClicking;
        /*
        **	buildPanels()
        **		Construct gallery panels from <div class="panel"> elements
        **		NOTE - These DIVs are generated automatically from the content of the UL passed to the plugin
        */
        function buildPanels(mid) {
            DCCMGSS = DCCMGSS_SetConstants(mid);
            gvObj = DCCMGSS.SlideShow;
            // If panel overlay content exists, add the necessary overlay background DIV
            // The overlay content and background are separate elements so the background's opacity isn't inherited by the content
            gvObj.j_panels.each(function(i) {
                $(this).append(
                $('<div class="overlay-background"></div>').css({
                    'position': 'absolute',
                    'zIndex': '998',
                    'width': gvObj.opts.panel_width + 'px',
                    'background-color': gvObj.opts.overlay_color,
                    'left': '0',
                    'opacity': gvObj.opts.overlay_opacity,
                    'height': gvObj.opts.overlay_height,
                    'display': (gvObj.opts.show_overlay ? 'inline' : 'none')
                }));
            });
            // Set the height and width of each panel, and position it appropriately within the gallery
            gvObj.j_panels.each(function(i) {
                $(this).css({
                    'width': (gvObj.opts.panel_width - extraWidth(gvObj.j_panels)) + 'px',
                    'height': (gvObj.opts.panel_height - extraHeight(gvObj.j_panels)) + 'px',
                    'position': 'absolute',
                    'overflow': 'hidden',
                    'display': 'none',
                    'text-align': 'center'
                }).parent().css({
                    'background-color': gvObj.opts.background_color
                });
            });
            // Position each panel overlay within panel
            $('.panel-overlay', gvObj.j_panels).css({
                'position': 'absolute',
                'zIndex': '999',
                'width': gvObj.opts.panel_width + 'px',
                'left': '0',
                'text-align': 'left',
                'height': gvObj.opts.overlay_height,
                'display': (gvObj.opts.show_overlay ? 'inline' : 'none')
            }).find("h2,p").css({
                'color': gvObj.opts.overlay_text_color,
                opacity: 1.0
            });
            $('.overlay-background', gvObj.j_panels).css({
                'position': 'absolute',
                'zIndex': '998',
                'width': gvObj.opts.panel_width + 'px',
                'background-color': gvObj.opts.overlay_color,
                'left': '0',
                'opacity': gvObj.opts.overlay_opacity,
                'height': gvObj.opts.overlay_height,
                'display': (gvObj.opts.show_overlay ? 'inline' : 'none')
            });
            if (gvObj.opts.overlay_position == 'top') {
                $('.panel-overlay', gvObj.j_panels).css('top', 0);
                $('.overlay-background', gvObj.j_panels).css('top', 0);
            } else {
                $('.panel-overlay', gvObj.j_panels).css('bottom', 0);
                $('.overlay-background', gvObj.j_panels).css('bottom', 0);
            }

            $('.panel iframe', gvObj.j_panels).css({
                'width': gvObj.opts.panel_width + 'px',
                'height': gvObj.opts.panel_height + 'px',
                'border': '0'
            });
        };
        gvObj.buildPanels = buildPanels;

        function updatePanels(mid) {
            DCCMGSS = DCCMGSS_SetConstants(mid);
            gvObj = DCCMGSS.SlideShow;
            // If panel overlay content exists, add the necessary overlay background DIV
            // The overlay content and background are separate elements so the background's opacity isn't inherited by the content

            gvObj.tempJ_panels.each(function(i) {
                $(this).append(
                $('<div class="overlay-background"></div>').css({
                    'position': 'absolute',
                    'zIndex': '998',
                    'width': gvObj.opts.panel_width + 'px',
                    'background-color': gvObj.opts.overlay_color,
                    'left': '0',
                    'opacity': gvObj.opts.overlay_opacity,
                    'height': gvObj.opts.overlay_height,
                    'display': (gvObj.opts.show_overlay ? 'inline' : 'none')
                }));
            });
            // If there is no filmstrip in this gallery, add navigation buttons to the panel itself
            if (!gvObj.opts.show_filmstrip && gvObj.opts.show_navigation) {
                $('<img />').addClass('nav-next').attr('ModuleId', gvObj.ModuleId).attr('src', gvObj.theme_path + gvObj.opts.nav_theme + '/next.gif').appendTo(gvObj.j_gallery).css({
                    'position': 'absolute',
                    'zIndex': '1100',
                    'cursor': 'pointer',
                    'top': ((gvObj.opts.panel_height - 22) / 2) + gvObj.gallery_padding + 'px',
                    'right': '10px',
                    'display': 'none'
                }).click(showNextItem);
                $('<img />').addClass('nav-prev').attr('ModuleId', gvObj.ModuleId).attr('src', gvObj.theme_path + gvObj.opts.nav_theme + '/prev.gif').appendTo(gvObj.j_gallery).css({
                    'position': 'absolute',
                    'zIndex': '1100',
                    'cursor': 'pointer',
                    'top': ((gvObj.opts.panel_height - 22) / 2) + gvObj.gallery_padding + 'px',
                    'left': '10px',
                    'display': 'none'
                }).click(showPrevItem);

                $('<img />').addClass('nav-next-overlay').attr('ModuleId', gvObj.ModuleId).attr('src', gvObj.theme_path + gvObj.opts.nav_theme + '/panel-nav-next.gif').appendTo(gvObj.j_gallery).css({
                    'position': 'absolute',
                    'zIndex': '1099',
                    'top': ((gvObj.opts.panel_height - 22) / 2) + gvObj.gallery_padding - 10 + 'px',
                    'right': '0',
                    'display': 'none',
                    'cursor': 'pointer',
                    'opacity': 0.75
                }).click(showNextItem);

                $('<img />').addClass('nav-prev-overlay').attr('ModuleId', gvObj.ModuleId).attr('src', gvObj.theme_path + gvObj.opts.nav_theme + '/panel-nav-prev.gif').appendTo(gvObj.j_gallery).css({
                    'position': 'absolute',
                    'zIndex': '1099',
                    'top': ((gvObj.opts.panel_height - 22) / 2) + gvObj.gallery_padding - 10 + 'px',
                    'left': '0',
                    'display': 'none',
                    'cursor': 'pointer',
                    'opacity': 0.75
                }).click(showPrevItem);
            }
            // Set the height and width of each panel, and position it appropriately within the gallery
            gvObj.tempJ_panels.each(function(i) {
                $(this).css({
                    'width': (gvObj.opts.panel_width - extraWidth(gvObj.j_panels)) + 'px',
                    'height': (gvObj.opts.panel_height - extraHeight(gvObj.j_panels)) + 'px',
                    'position': 'absolute',
                    'overflow': 'hidden',
                    'display': 'none',
                    'text-align': 'center'
                }).parent().css({
                    'background-color': gvObj.opts.background_color
                });
            });



            // Position each panel overlay within panel
            $('.panel-overlay', gvObj.tempJ_panels).css({
                'position': 'absolute',
                'zIndex': '999',
                'width': gvObj.opts.panel_width + 'px',
                'left': '0',
                'text-align': 'left',
                'height': gvObj.opts.overlay_height,
                'display': (gvObj.opts.show_overlay ? 'inline' : 'none')
            }).find("h2,p").css({
                'color': gvObj.opts.overlay_text_color,
                opacity: 1.0
            });
            $('.overlay-background', gvObj.tempJ_panels).css({
                'position': 'absolute',
                'zIndex': '998',
                'width': gvObj.opts.panel_width + 'px',
                'left': '0',
                'background-color': gvObj.opts.overlay_color,
                'opacity': gvObj.opts.overlay_opacity,
                'height': gvObj.opts.overlay_height,
                'display': (gvObj.opts.show_overlay ? 'inline' : 'none')
            });
            if (gvObj.opts.overlay_position == 'top') {
                $('.panel-overlay', gvObj.j_panels).css('top', 0);
                $('.overlay-background', gvObj.j_panels).css('top', 0);
            } else {
                $('.panel-overlay', gvObj.j_panels).css('bottom', 0);
                $('.overlay-background', gvObj.j_panels).css('bottom', 0);
            }

            $('.panel iframe', gvObj.tempJ_panels).css({
                'width': gvObj.opts.panel_width + 'px',
                'height': gvObj.opts.panel_height + 'px',
                'border': '0'
            });
        };
        gvObj.updatePanels = updatePanels;

        /*
        **	mouseIsOverGallery(int,int)
        **		Check to see if mouse coordinates lie within borders of gallery
        **		This is a more reliable method than using the mouseover event
        **		RETURN - boolean
        */
        function mouseIsOverGallery(x, y) {
            var pos = getPos(gvObj.j_gallery[0]);
            var top = pos.top;
            var left = pos.left;
            return x > left && x < left + gvObj.gallery_width + (gvObj.filmstrip_orientation == 'horizontal' ? (gvObj.gallery_padding * 2) : gvObj.gallery_padding + Math.max(gvObj.gallery_padding, gvObj.filmstrip_margin)) && y > top && y < top + gvObj.gallery_height + (gvObj.filmstrip_orientation == 'vertical' ? (gvObj.gallery_padding * 2) : gvObj.gallery_padding + Math.max(gvObj.gallery_padding, gvObj.filmstrip_margin));
        };
        gvObj.mouseIsOverGallery = mouseIsOverGallery;
        /*
        **	getInt(string)
        **		Parse a string to obtain the integer value contained
        **		If the string contains no number, return zero
        **		RETURN - int
        */
        function getInt(i) {
            i = parseInt(i, 10);
            if (isNaN(i)) { i = 0; }
            return i;
        };
        gvObj.getInt = getInt;
        /*
        **	buildGallery()
        **		Construct HTML and CSS for the gallery, based on user options
        */
        function buildGallery(mid) {
            DCCMGSS = DCCMGSS_SetConstants(mid);
            gvObj = DCCMGSS.SlideShow;
            var gallery_images = gvObj.opts.show_filmstrip ? $('img', gvObj.j_frames) : $('img', gvObj.j_panels);
            // For each image in the gallery, add its original dimensions and scaled dimensions to the appropriate arrays for later reference
            gallery_images.each(function(i) {
                gvObj.img_h[i] = this.height;
                gvObj.img_w[i] = this.width;
                if (gvObj.opts.frame_scale == 'nocrop') {
                    gvObj.frame_img_scale[i] = Math.min(gvObj.opts.frame_height / gvObj.img_h[i], gvObj.opts.frame_width / gvObj.img_w[i]);
                } else {
                    gvObj.frame_img_scale[i] = Math.max(gvObj.opts.frame_height / gvObj.img_h[i], gvObj.opts.frame_width / gvObj.img_w[i]);
                }

                if (gvObj.opts.panel_scale == 'nocrop') {
                    gvObj.panel_img_scale[i] = Math.min(gvObj.opts.panel_height / gvObj.img_h[i], gvObj.opts.panel_width / gvObj.img_w[i]);
                } else {
                    gvObj.panel_img_scale[i] = Math.max(gvObj.opts.panel_height / gvObj.img_h[i], gvObj.opts.panel_width / gvObj.img_w[i]);
                }
            });

            // Size gallery based on position of filmstrip
            gvObj.j_gallery.css({
                'position': 'relative',
                'width': gvObj.gallery_width + (gvObj.filmstrip_orientation == 'horizontal' ? (gvObj.gallery_padding * 2) : gvObj.gallery_padding + Math.max(gvObj.gallery_padding, gvObj.filmstrip_margin)) + 'px',
                'height': gvObj.gallery_height + (gvObj.filmstrip_orientation == 'vertical' ? (gvObj.gallery_padding * 2) : gvObj.gallery_padding + Math.max(gvObj.gallery_padding, gvObj.filmstrip_margin)) + 'px'
            });


            // Build panels if necessary
            if (gvObj.opts.show_panels) {
                buildPanels(mid);
            }
            $("#dvMainSldeShowContainer_" + mid).find("div.gallery").css(
            {
                'border': gvObj.opts.border_width + "px solid " + gvObj.opts.border_color,
                'width': gvObj.opts.panel_width + 'px',
                'height': gvObj.opts.panel_height + 'px'
            });
            // If user gvObj.opts to pause on hover, or no filmstrip exists, add some mouseover functionality
            if (gvObj.opts.pause_on_hover || (gvObj.opts.show_panels && !gvObj.opts.show_filmstrip)) {
                $(document).mousemove(function(e) {
                    if (mouseIsOverGallery(e.pageX, e.pageY)) {
                        // If the user gvObj.opts to pause on hover, kill automated transitions
                        if (gvObj.opts.pause_on_hover) {
                            if (!gvObj.paused) {
                                // Pause slideshow in 500ms
                                $(gvObj).oneTime(500, "animation_pause", function() {
                                    $(gvObj).stopTime("transition");
                                    gvObj.paused = true;
                                });
                            }
                        }
                        // Display panel navigation on mouseover
                        if (gvObj.opts.show_panels && !gvObj.opts.show_filmstrip && gvObj.opts.show_navigation && !gvObj.panel_nav_displayed) {
                            $('.nav-next-overlay', gvObj.j_gallery).fadeIn('fast');
                            $('.nav-prev-overlay', gvObj.j_gallery).fadeIn('fast');
                            $('.nav-next', gvObj.j_gallery).fadeIn('fast');
                            $('.nav-prev', gvObj.j_gallery).fadeIn('fast');
                            gvObj.panel_nav_displayed = true;
                        }
                    } else {
                        // If the mouse leaves the gallery, stop the pause timer and restart automated transitions
                        if (gvObj.opts.pause_on_hover) {
                            $(gvObj).stopTime("animation_pause");
                            if (gvObj.paused) {
                                $(gvObj).everyTime(gvObj.opts.transition_interval, "transition", function() {
                                    showNextItem.call(gvObj);
                                });
                                gvObj.paused = false;
                            }
                        }
                        // Hide panel navigation
                        if (gvObj.opts.show_panels && !gvObj.opts.show_filmstrip && gvObj.opts.show_navigation && gvObj.panel_nav_displayed) {
                            $('.nav-next-overlay').fadeOut('fast');
                            $('.nav-prev-overlay').fadeOut('fast');
                            $('.nav-next', gvObj.j_gallery).fadeOut('fast');
                            $('.nav-prev', gvObj.j_gallery).fadeOut('fast');
                            gvObj.panel_nav_displayed = false;
                        }
                    }
                });
            }
            var gallery_images = $('img', gvObj.j_panels);
            if (gallery_images[0].complete) {
                $("#dvMainSldeShowContainer_" + mid).find("div.loader").fadeOut('1000', function() {
                    // Hide loading box and display gallery
                    gvObj.j_filmstrip.css('visibility', 'visible');
                    gvObj.j_gallery.css('visibility', 'visible');
                    var moduleId = mid;
                    DCCMGSS_SetConstants(moduleId);
                    showNextItem.call(DCCMGSS.SlideShow);
                    $(this).remove();
                });
            }
            else {
                gallery_images.load(function() {
                    $("#dvMainSldeShowContainer_" + mid).find("div.loader").fadeOut('1000', function() {
                        // Hide loading box and display gallery
                        gvObj.j_filmstrip.css('visibility', 'visible');
                        gvObj.j_gallery.css('visibility', 'visible');
                        var moduleId = mid;
                        DCCMGSS_SetConstants(moduleId);
                        showNextItem.call(DCCMGSS.SlideShow);
                        $(this).remove();
                        $(this).remove();
                    });
                });
            }
        };
        gvObj.buildGallery = buildGallery;
        function updateGallery(mid) {
            try {
                DCCMGSS = DCCMGSS_SetConstants(mid);
                gvObj = DCCMGSS.SlideShow;
                var gallery_images = $('img', gvObj.j_panels);
                // For each image in the gallery, add its original dimensions and scaled dimensions to the appropriate arrays for later reference
                DCCMGSS = DCCMGSS_SetConstants(mid);
                gallery_images.each(function(i) {
                    gvObj.img_h[i] = getInt($(this).attr("height"));
                    gvObj.img_w[i] = getInt($(this).attr("width"));
                    if (gvObj.opts.frame_scale == 'nocrop') {
                        gvObj.frame_img_scale[i] = Math.min(gvObj.opts.frame_height / gvObj.img_h[i], gvObj.opts.frame_width / gvObj.img_w[i]);
                    } else {
                        gvObj.frame_img_scale[i] = Math.max(gvObj.opts.frame_height / gvObj.img_h[i], gvObj.opts.frame_width / gvObj.img_w[i]);
                    }

                    if (gvObj.opts.panel_scale == 'nocrop') {
                        gvObj.panel_img_scale[i] = Math.min(gvObj.opts.panel_height / gvObj.img_h[i], gvObj.opts.panel_width / gvObj.img_w[i]);
                    } else {
                        gvObj.panel_img_scale[i] = Math.max(gvObj.opts.panel_height / gvObj.img_h[i], gvObj.opts.panel_width / gvObj.img_w[i]);
                    }
                });

                // Size gallery based on position of filmstrip
                gvObj.j_gallery.css({
                    'position': 'relative',
                    'width': gvObj.gallery_width + (gvObj.filmstrip_orientation == 'horizontal' ? (gvObj.gallery_padding * 2) : gvObj.gallery_padding + Math.max(gvObj.gallery_padding, gvObj.filmstrip_margin)) + 'px',
                    'height': gvObj.gallery_height + (gvObj.filmstrip_orientation == 'vertical' ? (gvObj.gallery_padding * 2) : gvObj.gallery_padding + Math.max(gvObj.gallery_padding, gvObj.filmstrip_margin)) + 'px'
                });


                // Build panels if necessary
                if (gvObj.opts.show_panels) {
                    updatePanels(mid);
                }

                // Hide loading box and display gallery                
                gvObj.j_gallery.css('visibility', 'visible');
                $("#dvMainSldeShowContainer_" + mid).find("div.gallery").css({
                    'border': gvObj.opts.border_width + "px solid " + gvObj.opts.border_color,
                    'width': gvObj.opts.panel_width + 'px',
                    'height': gvObj.opts.panel_height + 'px'
                });
            }
            catch (err) {
            }
        };
        gvObj.updateGallery = updateGallery;
        gvObj.createImageList = function(ul) {
            try {
                $(ul).wrap("<div></div>");
                gvObj.tempJ_gallery = $(ul).parent();
                gvObj.tempJ_gallery.css('visibility', 'hidden').addClass('gallery');
                // Assign filmstrip class to UL
                $(ul).removeAttr('id').addClass('filmstrip');
                gvObj.tempJ_filmStrip = $('.filmstrip', gvObj.tempJ_gallery);
                gvObj.tempJ_frames = $('li', gvObj.tempJ_filmStrip);
                gvObj.tempJ_frames.addClass('frame');
                var mid = parseInt(gvObj.j_frames.find('img').eq(0).attr('mid'));
                DCCMGSS = DCCMGSS_SetConstants(mid);
                gvObj = DCCMGSS.SlideShow;
                // If the user wants panels, generate them using the filmstrip images
                if (gvObj.opts.show_panels) {
                    for (i = gvObj.tempJ_frames.length - 1; i >= 0; i--) {
                        if (gvObj.tempJ_frames.eq(i).find('.panel-content').length > 0) {
                            gvObj.tempJ_frames.eq(i).find('.panel-content').remove().prependTo(gvObj.tempJ_gallery).addClass('panel');
                        } else {
                            p = $('<div>');
                            p.addClass('panel');
                            im = $('<img />');
                            im.attr('src', gvObj.tempJ_frames.eq(i).find('img').eq(0).attr('src')).attr('mid', gvObj.tempJ_frames.eq(i).find('img').eq(0).attr('mid')).appendTo(p);
                            p.prependTo(gvObj.tempJ_gallery);
                            gvObj.tempJ_frames.eq(i).find('.panel-overlay').remove().css({
                                'position': 'absolute',
                                'zIndex': '999',
                                'width': gvObj.opts.panel_width + 'px',
                                'left': '0',
                                'text-align': 'left',
                                'height': gvObj.opts.overlay_height,
                                'display': (gvObj.opts.show_overlay ? 'inline' : 'none')
                            }).appendTo(p);
                            if (gvObj.opts.overlay_position == 'top') {
                                $('.panel-overlay', gvObj.j_panels).css('top', 0);
                                $('.overlay-background', gvObj.j_panels).css('top', 0);
                            } else {
                                $('.panel-overlay', gvObj.j_panels).css('bottom', 0);
                                $('.overlay-background', gvObj.j_panels).css('bottom', 0);
                            }
                            if (im.height == 0 || im.width == 0) {
                                im.height = parseInt(gvObj.tempJ_frames.eq(i).find('img').eq(0).attr('tempheight'));
                                im.width = parseInt(gvObj.tempJ_frames.eq(i).find('img').eq(0).attr('tempwidth'));
                            }
                        }
                    }
                } else {
                    $('.panel-overlay', gvObj.tempJ_frames).remove();
                    $('.panel-content', gvObj.tempJ_frames).remove();
                }
                gvObj.tempJ_panels = $('.panel', gvObj.tempJ_gallery);

                gvObj.tempJ_gallery.find("div.panel").each(function() {
                    gvObj.j_gallery.append(this);
                });
                gvObj.tempJ_frames.each(function() {
                    gvObj.j_frames.push(this);
                });
                gvObj.tempJ_panels.each(function(i) {
                    $(this).css({
                        'display': 'none'
                    })
                });
                if (gvObj.opts.disable_rightclick) {
                    gvObj.tempJ_panels.bind("contextmenu", function(e) {
                        e.preventDefault();
                    });
                }

                gvObj.tempJ_panels.each(function() {
                    gvObj.j_panels.push(this);
                });
                // Number of frames in filmstrip
                gvObj.item_count = gvObj.opts.show_panels ? gvObj.j_panels.length : gvObj.j_frames.length;
                var gallery_images = gvObj.opts.show_filmstrip ? $('img', gvObj.tempJ_frames) : $('img', gvObj.tempJ_panels);
                
                updateGallery(mid);

            }
            catch (err) {
            }
        }
        /*
        **	MAIN PLUGIN CODE
        */
        gvObj.init = function() {
            var mid = null;
            //Hide the unstyled UL until we've created the gallery
            $(this.ulList).css('visibility', 'hidden');

            // Wrap UL in DIV and transfer ID to container DIV
            $(this.ulList).wrap("<div></div>");
            gvObj.j_gallery = $(this.ulList).parent();
            gvObj.j_gallery.css('visibility', 'hidden').attr('id', $(this.ulList).attr('id')).addClass('gallery');

            // Assign filmstrip class to UL
            $(this.ulList).removeAttr('id').addClass('filmstrip');

            // If the transition or pause timers exist for any reason, stop them now.
            $(gvObj).stopTime("transition");
            $(gvObj).stopTime("animation_pause");

            // Save the id of the UL passed to the plugin
            gvObj.id = gvObj.j_gallery.attr('id');

            // If the UL does not contain any <div class="panel-content"> elements, we will scale the UL images to fill the panels
            gvObj.scale_panel_images = $('.panel-content', gvObj.j_gallery).length == 0;

            // Define dimensions of pointer <div>
            gvObj.pointer_height = gvObj.opts.pointer_size;
            gvObj.pointer_width = gvObj.opts.pointer_size * 2;

            // Determine filmstrip orientation (vertical or horizontal)
            // Do not show captions on vertical filmstrips (override user set option)
            gvObj.filmstrip_orientation = (gvObj.opts.filmstrip_position == 'top' || gvObj.opts.filmstrip_position == 'bottom' ? 'horizontal' : 'vertical');
            if (gvObj.filmstrip_orientation == 'vertical') { gvObj.opts.show_captions = false; }

            // Determine path between current page and plugin images
            // Scan script tags and look for path to GalleryView plugin
            $('script').each(function(i) {
                var s = $(this);
                if (s.attr('src') && s.attr('src').match(/jquery\.galleryview/)) {
                    loader_path = s.attr('src').split('jquery.galleryview')[0];
                    gvObj.theme_path = s.attr('src').split('jquery.galleryview')[0] + 'themes/';
                }
            });
            // Assign elements to variables to minimize calls to jQuery
            gvObj.j_filmstrip = $('.filmstrip', gvObj.j_gallery);
            gvObj.j_frames = $('li', gvObj.j_filmstrip);
            gvObj.j_frames.addClass('frame');
            mid = parseInt(gvObj.j_frames.find('img').eq(0).attr('mid'));
            DCCMGSS = DCCMGSS_SetConstants(mid);
            gvObj = DCCMGSS.SlideShow;
            $("#dvMainSldeShowContainer_" + mid).find("div.loader").css(
            {
                'border': gvObj.opts.border_width + "px solid " + gvObj.opts.border_color,
                'width': gvObj.opts.panel_width + 'px',
                'height': gvObj.opts.panel_height + 'px'
            }).show();
            // If the user wants panels, generate them using the filmstrip images
            if (gvObj.opts.show_panels) {
                for (i = gvObj.j_frames.length - 1; i >= 0; i--) {
                    if (gvObj.j_frames.eq(i).find('.panel-content').length > 0) {
                        gvObj.j_frames.eq(i).find('.panel-content').remove().prependTo(gvObj.j_gallery).addClass('panel');
                    } else {
                        p = $('<div>');
                        p.addClass('panel');
                        im = $('<img />');
                        im.attr('src', gvObj.j_frames.eq(i).find('img').eq(0).attr('src')).attr('mid', gvObj.j_frames.eq(i).find('img').eq(0).attr('mid')).appendTo(p);
                        p.prependTo(gvObj.j_gallery);
                        gvObj.j_frames.eq(i).find('.panel-overlay').remove().css({
                            'position': 'absolute',
                            'zIndex': '999',
                            'width': gvObj.opts.panel_width + 'px',
                            'left': '0',
                            'text-align': 'left',
                            'height': gvObj.opts.overlay_height,
                            'display': (gvObj.opts.show_overlay ? 'inline' : 'none')
                        }).appendTo(p);
                        if (gvObj.opts.overlay_position == 'top') {
                            $('.panel-overlay', gvObj.j_panels).css('top', 0);
                            $('.overlay-background', gvObj.j_panels).css('top', 0);
                        } else {
                            $('.panel-overlay', gvObj.j_panels).css('bottom', 0);
                            $('.overlay-background', gvObj.j_panels).css('bottom', 0);
                        }
                    }
                }
            } else {
                $('.panel-overlay', gvObj.j_frames).remove();
                $('.panel-content', gvObj.j_frames).remove();
            }
            // If the user doesn't want a filmstrip, delete it
            if (!gvObj.opts.show_filmstrip) {
                gvObj.j_filmstrip.remove();
            }
            else {
                // Wrap the frame images (and links, if applicable) in container divs
                // These divs will handle cropping and zooming of the images
                gvObj.j_frames.each(function(i) {
                    if ($(this).find('a').length > 0) {
                        $(this).find('a').wrap('<div class="img_wrap"></div>');
                    } else {
                        $(this).find('img').wrap('<div class="img_wrap"></div>');
                    }
                });
                gvObj.j_frame_img_wrappers = $('.img_wrap', gvObj.j_frames);
            }
            gvObj.j_panels = $('.panel', gvObj.j_gallery);
            if (gvObj.opts.disable_rightclick) {
                gvObj.j_panels.bind("contextmenu", function(e) {
                    e.preventDefault();
                });
            }
            if (!gvObj.opts.show_panels) {
                gvObj.opts.panel_height = 0;
                gvObj.opts.panel_width = 0;
            }
            // Determine final frame dimensions, accounting for user-added padding and border
            gvObj.f_frame_width = gvObj.opts.frame_width + extraWidth(gvObj.j_frame_img_wrappers);
            gvObj.f_frame_height = gvObj.opts.frame_height + extraHeight(gvObj.j_frame_img_wrappers);

            // Number of frames in filmstrip
            gvObj.item_count = gvObj.opts.show_panels ? gvObj.j_panels.length : gvObj.j_frames.length;

            // Number of frames that can display within the gallery block
            // 64 = width of block for navigation button * 2 + 20
            if (gvObj.filmstrip_orientation == 'horizontal') {
                gvObj.strip_size = gvObj.opts.show_panels ? Math.floor((gvObj.opts.panel_width - ((gvObj.opts.frame_gap + 22) * 2)) / (gvObj.f_frame_width + gvObj.opts.frame_gap)) : Math.min(gvObj.item_count, gvObj.opts.filmstrip_size);
            } else {
                gvObj.strip_size = gvObj.opts.show_panels ? Math.floor((gvObj.opts.panel_height - (gvObj.opts.frame_gap + 22)) / (gvObj.f_frame_height + gvObj.opts.frame_gap)) : Math.min(gvObj.item_count, gvObj.opts.filmstrip_size);
            }

            // Determine animation method for filmstrip
            // If more items than strip size, slide filmstrip
            // Otherwise, slide pointer
            if (gvObj.strip_size >= gvObj.item_count) {
                gvObj.slide_method = 'pointer';
                gvObj.strip_size = gvObj.item_count;
            }
            else { gvObj.slide_method = 'strip'; }
            gvObj.iterator = (gvObj.strip_size < gvObj.item_count ? gvObj.item_count : 0) + gvObj.opts.start_frame - 1;
            // Determine dimensions of various gallery elements
            gvObj.filmstrip_margin = (gvObj.opts.show_panels ? getInt(gvObj.j_filmstrip.css('marginTop')) : 0);
            gvObj.j_filmstrip.css('margin', '0px');

            if (gvObj.filmstrip_orientation == 'horizontal') {
                // Width of gallery block
                gvObj.gallery_width = gvObj.opts.show_panels ? gvObj.opts.panel_width : (gvObj.strip_size * (gvObj.f_frame_width + gvObj.opts.frame_gap)) + 44 + gvObj.opts.frame_gap;

                // Height of gallery block = screen + filmstrip + captions (optional)
                gvObj.gallery_height = (gvObj.opts.show_panels ? gvObj.opts.panel_height : 0) + (gvObj.opts.show_filmstrip ? gvObj.f_frame_height + gvObj.filmstrip_margin + (gvObj.opts.show_captions ? gvObj.frame_caption_size : 0) : 0);
            } else {
                // Width of gallery block
                gvObj.gallery_height = gvObj.opts.show_panels ? gvObj.opts.panel_height : (gvObj.strip_size * (gvObj.f_frame_height + gvObj.opts.frame_gap)) + 22;

                // Height of gallery block = screen + filmstrip + captions (optional)
                gvObj.gallery_width = (gvObj.opts.show_panels ? gvObj.opts.panel_width : 0) + (gvObj.opts.show_filmstrip ? gvObj.f_frame_width + gvObj.filmstrip_margin : 0);
            }

            // Width of filmstrip
            if (gvObj.filmstrip_orientation == 'horizontal') {
                if (gvObj.slide_method == 'pointer') { gvObj.strip_width = (gvObj.f_frame_width * gvObj.item_count) + (gvObj.opts.frame_gap * (gvObj.item_count)); }
                else { gvObj.strip_width = (gvObj.f_frame_width * gvObj.item_count * 3) + (gvObj.opts.frame_gap * (gvObj.item_count * 3)); }
            } else {
                gvObj.strip_width = (gvObj.f_frame_width + gvObj.filmstrip_margin);
            }
            if (gvObj.filmstrip_orientation == 'horizontal') {
                gvObj.strip_height = (gvObj.f_frame_height + gvObj.filmstrip_margin + (gvObj.opts.show_captions ? gvObj.frame_caption_size : 0));
            } else {
                if (gvObj.slide_method == 'pointer') { gvObj.strip_height = (gvObj.f_frame_height * gvObj.item_count + gvObj.opts.frame_gap * (gvObj.item_count)); }
                else { gvObj.strip_height = (gvObj.f_frame_height * gvObj.item_count * 3) + (gvObj.opts.frame_gap * (gvObj.item_count * 3)); }
            }
            // Width of filmstrip wrapper (to hide overflow)
            gvObj.wrapper_width = ((gvObj.strip_size * gvObj.f_frame_width) + ((gvObj.strip_size - 1) * gvObj.opts.frame_gap));
            gvObj.wrapper_height = ((gvObj.strip_size * gvObj.f_frame_height) + ((gvObj.strip_size - 1) * gvObj.opts.frame_gap));
            gvObj.gallery_padding = getInt(gvObj.j_gallery.css('paddingTop'));
            gvObj.j_gallery.css('padding', '0px');
            // Place loading box over gallery until page loads
            galleryPos = getPos(gvObj.j_gallery[0]);
            $(document).ready(function() {
                buildGallery(mid);
            });
        };
        return gvObj;
    };
})(jqsDCCWMGSS);
