jQuery(function () {

    var projectList = [
        {
            image : './media/home-slides/box-office.jpg',
            title : 'The Box Office',
            location : 'Providence, Rhode Island',
            credit : '12 office/studio spaces constructed from 32 recycled shipping containers. Photo by Nat Rea.'
        },
        {
            image : './media/home-slides/steel-yard.jpg',
            title : 'The Steel Yard',
            location : 'Providence, Rhode Island',
            credit : 'Innovative EPA-funded brownfield site retrofit.'
        },
        {
            image : './media/home-slides/751-case-study.jpg',
            title : '751 Mixed-Use Development',
            location : 'Durham County, North Carolina',
            credit : 'Master plan drawing by Civitech, Inc.'
        },
        {
            image : './media/home-slides/attleboro.jpg',
            title : 'Attleboro Riverfront District',
            location : 'Attleboro, Massachusetts',
            credit : 'Transportation planning charrette: consensus transportation framework.'
        },
        {
            image : './media/home-slides/baldwin-brothers.jpg',
            title : 'Baldwin Brothers Offices',
            location : 'Marion, Massachusetts',
            credit : 'Phase I of a hamlet master plan, construction completed fall 2008.'
        },
        {
            image : './media/home-slides/cottages.jpg',
            title : 'Cottages on Greene',
            location : 'East Greenwich, Rhode Island',
            credit : 'Eco-sensitive 15 unit cottage court.'
        },
        {
            image : './media/home-slides/epa-infrastructure.jpg',
            title : 'Smart Growth & Conventional Suburban Development',
            location : 'US EPA',
            credit : 'An infrastructure case study completed for the US EPA.'
        },
        {
            image : './media/home-slides/grants-parking.jpg',
            title : 'Grants Block “Green” Parking Retrofit',
            location : 'Providence, Rhode Island',
            credit : 'Sustainable stormwater management techniques.'
        }
    ];

    var imageList = [];
    var controls = jQuery('#slideshow_controls');
    var controlItemMaster = controls.find('li').eq(0);
    for (var i=0; i < projectList.length; i++) {
        imageList.push(projectList[i].image);
        if (i == 0) {
            continue;
        }
        var newItem = controlItemMaster.clone();
        newItem.find('a').removeClass('selected');
        controls.append(newItem);
    }

    jQuery('#slideshow_viewer').slideviewer({
        imageUrlList : imageList,
        transitionSpeed : 500,
        onChange : function () {
            var currentProject;
            if (! (currentProject = projectList[this.currentIndex])) {
                return;
            }
            jQuery('#slide_info .name').html(currentProject.title);
            jQuery('#slide_info .location').html(currentProject.location);
            jQuery('#slide_info .slide_credit').html(currentProject.credit);
            jQuery('#slide_info').slideDown('fast');
        }
    });

    var slideviewer = jQuery.slideviewer.instanceList[0];

    function createImageSwitcher (index) {
        return function () {
            clearTimeout(slideChangeTimeout);
            changeSlide(index);
            return false;
        }
    }

    function changeSlide (index) {
        if (slideviewer.isTransitioning) {
            return false;
        }
        jQuery('#slide_info').slideUp('fast');
        if (typeof index == 'undefined') {
            index = slideviewer.next();
        } else {
            slideviewer.changeTo(index);
        }
        selectSlideshowControl(index);
        return true;
    }


    jQuery('#slideshow_controls a').each(function (index) {
        jQuery(this).click(createImageSwitcher(index));
    });

    function selectSlideshowControl (index) {
        jQuery('#slideshow_controls a')
            .removeClass('selected')
            .eq(index).addClass('selected');
    }
    

    var autoChangeTime = 5000;
    var slideChangeTimeout = setTimeout(
        function loopChangeSlide () {
            changeSlide();
            slideChangeTimeout = setTimeout(loopChangeSlide, autoChangeTime);
        },
        autoChangeTime
    );
        


});


jQuery(window).load(function () {

    var mainBody = jQuery('.main_body_container');
    var mainBodyContent = jQuery('.main_body_content');

    jQuery(window).resize(updateMainBodyHeight);
    updateMainBodyHeight();

    function updateMainBodyHeight ()
    {
        mainBody.height(
            jQuery(window).height() - mainBody.offset().top
        );
        if (mainBodyContent.height() > mainBody.height()) {
            mainBody.height(mainBodyContent.height());
        }
    }

});
