(function(){
    // redefining yui shortcuts here allows for better file compression
    var yuiDom = YAHOO.util.Dom;
    var yuiEvent = YAHOO.util.Event;

    var SlideShow = (function(){
        // --- private vars --- //
        var elements = {
            groupName:null,
            count:null,
            total:null
        };
        var groups = [];
        var groupIndex = 0;
        var photoIndex = 0;
        var previousGroup = null;
        var previousPhotoElement = null;
        var previousCaptionElement = null;

        // --- private methods --- //
        // display methods
        function displayGroup(){
            // highlight group name and set name to caption
            if(previousGroup !== null){
                yuiDom.removeClass(previousGroup.navElement,"active");
                yuiDom.removeClass(previousGroup.imageGroupElement,"show");
            }
            var thisGroup = groups[groupIndex];
            elements.groupName.innerHTML = thisGroup.name;
            yuiDom.addClass(thisGroup.navElement,"active");
            yuiDom.addClass(thisGroup.imageGroupElement,"show");
            previousGroup = thisGroup;
        }
        function displayPhoto(){
            // show photo and update numbers in caption
            if(previousPhotoElement !== null){
                yuiDom.removeClass(previousPhotoElement,"show");
                yuiDom.removeClass(previousCaptionElement,"show");
            }
            var thisGroup = groups[groupIndex];
            elements.count.innerHTML = photoIndex+1;
            elements.total.innerHTML = thisGroup.images.length;
            yuiDom.addClass(thisGroup.images[photoIndex],"show");
            previousPhotoElement = thisGroup.images[photoIndex];
            if(thisGroup.captions){
                yuiDom.addClass(thisGroup.captions[photoIndex],"show");
                previousCaptionElement = thisGroup.captions[photoIndex];
            }

        }
        // methods to set new group/photo indexes & call display methods
        function showGroup(index){
            photoIndex = 0;
            if(groupIndex != index){
                groupIndex = index;
                displayGroup();
            }
            displayPhoto();
        }
        function showPrevious(){
            photoIndex--;
            if(photoIndex < 0){
                groupIndex--;
                if(groupIndex < 0){
                    groupIndex = groups.length - 1;
                }
                displayGroup();
                photoIndex = groups[groupIndex].images.length - 1;
            }
            displayPhoto();
        }
        function showNext(){
            photoIndex++;
            if(photoIndex > groups[groupIndex].images.length - 1){
                groupIndex++;
                if(groupIndex > groups.length - 1){
                    groupIndex = 0;
                }
                displayGroup();
                photoIndex = 0;
            }
            displayPhoto();
        }

        // --- privelledged methods --- //
        var self = {
            initialize:function(){
                // get important elements (NOTE: this arrays are position based; ie the first element in each array is related)
                var imageGroups = yuiDom.getElementsByClassName("imageGroup","li","imageContainer");
                var imageCaptionGroups = yuiDom.getElementsByClassName("imageCaptionGroup","li","imageCaptionContainer");
                var navContainers = Array.Copy(yuiDom.get("navigationContainer").getElementsByTagName("li"));
                var navLinks = Array.Copy(yuiDom.get("navigationContainer").getElementsByTagName("a"));

                // get references to caption items
                elements.groupName = yuiDom.get("groupName");
                elements.count = yuiDom.get("count");
                elements.total = yuiDom.get("total");

                // setup group structure and add handlers for groups
                imageGroups.forEach(function(imageGroup,i){
                    var nav = navContainers[i];
                    var navLink = navLinks[i];
                    yuiEvent.addListener(navLink, "click", self.showGroupBridge, i);
                    var imageContainers = Array.Copy(imageGroup.getElementsByTagName("li"));
                    if(yuiDom.hasClass(imageGroup, "show")) {
                        groupIndex = i;
                    }
                    if(imageCaptionGroups.length > 0){
                        var captionContainers = Array.Copy(imageCaptionGroups[i].getElementsByTagName("li"));
                    }                   
                    groups.push({
                        name:navLink.innerHTML,
                        navElement:nav,
                        imageGroupElement:imageGroup,
                        images:imageContainers,
                        captions:captionContainers
                    });
                });
                // show first image
                displayGroup();
                displayPhoto();

                // attach navigation handlers
                yuiEvent.addListener("previousButton","click",self.showPreviousBridge);
                yuiEvent.addListener("nextButton","click",self.showNextBridge);
            },

        // bridge methods/event handlers
            showGroupBridge:function(e,index){
                showGroup(index);
                yuiEvent.preventDefault(e);
            },
            showPreviousBridge:function(e){
                showPrevious();
                yuiEvent.preventDefault(e);
            },
            showNextBridge:function(e){
                showNext();
                yuiEvent.preventDefault(e);
            }
        };
        return self;
    })();

    // initial slideshow
    yuiEvent.onDOMReady(SlideShow.initialize,SlideShow,true);
    
    // other stuff which should not be coupled directly with slideshow functionality
    yuiEvent.onDOMReady(function(){
        yuiEvent.addListener("closewin","click",function(){
            window.close();
        });
        yuiEvent.addListener("bookNowLink","click",function(e){
            if(window.opener){
                // only run if in popup (should always be the case, but this is safe). Otherwise, let the button work as normal.
                var anchor = yuiEvent.getTarget(e);
                window.opener.location.href = anchor.href;
                window.close();
                yuiEvent.preventDefault(e);
            }
        });
    });
})();
