/*
Page image slideshow, with click-to-select controls.
Uses the Revealing Module Pattern.
*/
try {
	var test = COMO.ns;
}
catch (e) {
	COMO = {};
}

COMO.PageImages = (function($) {
    var ns = 'COMO.PageImages';
    var description = 'Page image slideshow and click-to-select-image controls.';
    var slideshowEngine;
    var captionSlideshowEngine;


    function createSlideshow() {
        // create slideshow and set up image array
        slideshowEngine = new imageslideshowEngine();
        slideshowEngine.images = $('#pageimages img').get();

        // set up control click event handlers
        $('#pageimages .controls li a').each(function(i) {
            $(this).click(function(event) {
                selectImage(i);
                event.preventDefault();
            });
        });

        // start the engine
        slideshowEngine.init(5000, 500, imageSwitched);
    }

    //gallery methods
    function createGallerySlideshow() {
        // create slideshow and set up image array
        slideshowEngine = new imageslideshowEngine();
        slideshowEngine.images = $('#galleryimages img').get();

        // set up control click event handlers
        $('#galleryimages .controls li a').each(function(i) {
            $(this).click(function(event) {
                selectImage(i);
                event.preventDefault();
            });
        });

        // start the engine
        slideshowEngine.init(5000, 500, imageSwitched);


        //engine for image captions
        captionSlideshowEngine = new imageslideshowEngine();
        captionSlideshowEngine.images = $('#galleryimages h2').get();
        captionSlideshowEngine.init(5000, 500);

        //control buttons
        $("#gallery_previous").click(function() {

            galleryNavigate("previous");
        });

        $("#gallery_pause").click(function() {
            slideshowEngine.pauseEngine();
            captionSlideshowEngine.pauseEngine();
        });

        $("#gallery_next").click(function() {
            galleryNavigate("next");
        });
    }

    function galleryNavigate(direction) {
        var currentIndex = 0;
        var topindex = 0;
        //stop the engine so the image order does not change.
        slideshowEngine.pauseEngine();
        captionSlideshowEngine.pauseEngine();

        //find current index
        topindex = $('#galleryimages img').size();

        currentIndex = slideshowEngine.currentIndex;

        if (direction == "previous") {

            //go back one
            if (currentIndex > 0) {
                slideshowEngine.jumpToPair(currentIndex - 1, true);
                imageSwitched({
                    to: currentIndex - 1
                });

                captionSlideshowEngine.jumpToPair(currentIndex - 1, true);
                imageSwitched({
                    to: currentIndex - 1
                });
            }
            else {
                //otherwise jump to topindex which sould be the last img.
                slideshowEngine.jumpToPair(topindex, true);
                imageSwitched({
                    to: topindex
                });

                captionSlideshowEngine.jumpToPair(topindex, true);
                imageSwitched({
                    to: topindex
                });
            }
        }
        else {
            //go forward one
            if (currentIndex != topindex) {
                slideshowEngine.jumpToPair(currentIndex + 1, true);
                imageSwitched({
                    to: currentIndex + 1
                });

                captionSlideshowEngine.jumpToPair(currentIndex + 1, true);
                imageSwitched({
                    to: currentIndex + 1
                });
            }
            else {
                //otherwise you are at last img so jump to beginning again.
                slideshowEngine.jumpToPair(0, true);
                imageSwitched({
                    to: 0
                });

                captionSlideshowEngine.jumpToPair(0, true);
                imageSwitched({
                    to: 0
                });
            }
        }

        //start up again
        slideshowEngine.unpauseEngine();
        captionSlideshowEngine.unpauseEngine();
    }

    function showGallery() {
        /*var cancelhideGallery = window.setTimeout(function hideGallery() {
            $("#galleryimages .imageoverlay").show("slide", { direction: "down" }, 1000);
            //$("#galleryimages h2").show(2000);
            $("#gallerycontrols").show(2000);

        }, 4000);*/
        
        function galleryVisibility(visibility) {
            alert("hello");
            if (visibility)
            {
                $("#galleryimages .imageoverlay").show("slide", { direction: "down" }, 1000);
                //$("#galleryimages h2").show(2000);
                $("#gallerycontrols").show(2000);
             }
             else
             {
                $("#galleryimages .imageoverlay").hide("slide", { direction: "down" }, 1000);
                //$("#galleryimages h2").show(2000);
                $("#gallerycontrols").hide(500);
             }
        }
        
        $("#imagehover").hover(
           function () {galleryVisibility(true)},
           function () {galleryVisibility(false)}
        );
    }
    //gallery methods
    
    
    
    function selectImage(i) {
        slideshowEngine.jumpToPair(i, true);
        imageSwitched({
            to: i
        });
    }

    function imageSwitched(indexes) {
        // deactivate all controls items
        $('#pageimages .controls li.active').removeClass('active');

        // activate the indexed control item
        $('#pageimages .controls li').eq(indexes.to).addClass('active');

        // change caption
        $('#pageimages .caption').text($('#pageimages img').eq(indexes.to).attr('alt'));
    }

    function pause() {
        if (slideshowEngine) {
            slideshowEngine.pauseEngine();
        }
    }
    function unpause() {
        if (slideshowEngine) {
            slideshowEngine.unpauseEngine();
        }
    }

    return {
        ns: ns,
        description: description,
        createSlideshow: createSlideshow,
        selectImage: selectImage,
        imageSwitched: imageSwitched,
        pause: pause,
        unpause: unpause,
        createGallerySlideshow: createGallerySlideshow,
        showGallery: showGallery
    };

} (jQuery));


jQuery(document).ready(COMO.PageImages.createSlideshow);

jQuery(document).ready(COMO.PageImages.createGallerySlideshow);

jQuery(document).ready(COMO.PageImages.showGallery);


