

/*
 * Namespace for Parkplaza
 */
var PP = window.PP ||
{};

PP.Plaza = {};
var imagesVisible = 6;
var currentImage = 0;
var imageWidth = 66;


PP.Plaza.Gallery = (function() {
    var slideshowEngine;
    var config = {
        fadeInterval: 6000,
        fadeSpeed: 1000
    };
    function setStripLength() {
        var imageCount = 0;
        var galleryStrip = "";

        if (document.getElementById('imagestrip') != null) {
            galleryStrip = document.getElementById('imagestrip');

        }

        if (document.getElementById('ctl00_Gallery_imageCount') != null) {
            imageCount = document.getElementById('ctl00_Gallery_imageCount').value;
            pxSize = (imageWidth * imageCount) + 10;
            galleryStrip.style.width = pxSize + 'px';
        }



    }
    function create() {
        if ($('#gallery_section')) {
            window.setTimeout(function showGallery() {

            $('#gallery_maincontainer #images').removeClass("hidden");
            $('#gallery_maincontainer #default').hide();
            }, 7000);

       
            // create slideshow and set up image array
            slideshowEngine = new imageslideshowEngine();
            slideshowEngine.images = $('#images img').get();
            setStripLength();
            // set up control click event handlers
            $('#imagestrip img').css({
                cursor: 'pointer'
            });
            $('#imagestrip img').each(function(i) {
                $(this).click(function(e) {
                    selectImage(i);
                    e.preventDefault();
                });
            });


            //setup move left event handler

            $("#gallerygroup #moveleft").click(function(e) {
                if (currentImage > 0) {
                    currentImage -= 1;
                    var galleryStrip = document.getElementById('imagestrip');
                    var newLeft = '-' + (currentImage * imageWidth) + 'px';
                    galleryStrip.style.left = newLeft;
                }
                return false;

                e.preventDefault();
            });
            //setup move right event handler and the minus 30 gets rid of the extra space after the last image
            $("#gallerygroup #moveright").click(function(e) {
                var imageCount = document.getElementById('ctl00_Gallery_imageCount').value

                if (currentImage < (imageCount - imagesVisible)) {
                    currentImage += 1;
                    var galleryStrip = document.getElementById('imagestrip');
                    var newLeft = '-' + (currentImage * imageWidth - 30) + 'px';
                    galleryStrip.style.left = newLeft;
                }
                return false;

                e.preventDefault();
            });



            $('#pause').click(function(e) {
                pause();
                e.preventDefault();
            });
            $('#play').click(function(e) {
                unpause();
                e.preventDefault();
            });

            // start the engine
            slideshowEngine.init(config.fadeInterval, config.fadeSpeed, imageSwitched);
        }
    }

    function selectImage(i) {
        slideshowEngine.jumpToPair(i, true);
        imageSwitched({
            to: i
        });
    }

    function imageSwitched(indexes) {
        // activate the indexed thumbnail, deactivate others
        $('#imagestrip img').eq(indexes.to).addClass('active').siblings().removeClass('active');
    }

    function pause() {
        if (slideshowEngine) {
            slideshowEngine.pauseEngine();
            $('#pause').addClass('pause_active');
            $('#play').removeClass('play_active');

        }
    }
    function unpause() {
        if (slideshowEngine) {
            slideshowEngine.unpauseEngine();
            $('#pause').removeClass('pause_active');
            $('#play').addClass('play_active');
        }
    }

    return {
        create: create,
        selectImage: selectImage,
        imageSwitched: imageSwitched,
        pause: pause,
        unpause: unpause
    };

} ());


// initialisations

$(document).ready(PP.Plaza.Gallery.create);





