// Patrick Moore
$(function() {
    slideshow = {
        slides : [
            {img :"images/slideshow/slide01.jpg", link: "p_features1.html?features/arbors03.html"}, 
            {img :"images/slideshow/slide02.jpg", link: "p_features1.html?features/arbors01.html"}, 
            {img :"images/slideshow/slide03.jpg", link: "p_profiles2.html?profiles/santaclara05.html"},
            {img :"images/slideshow/slide04.jpg", link: "p_features3.html?features/sustainable07.html"},
            {img :"images/slideshow/slide05.jpg", link: "p_profiles1.html?profiles/paloalto1_03.html"},
            {img :"images/slideshow/slide06.jpg", link: "p_profiles1.html?profiles/cupertino03.html"},
            {img :"images/slideshow/slide07.jpg", link: "p_features3.html?features/sustainable06.html"},
            {img :"images/slideshow/slide08.jpg", link: "p_features3.html?features/hardsurf06.html"} 
        ],
        active_slide : 0,
        next_slide : 1,
        fadeheight: 296,
        fadewidth: 657,
        delay : 3000,
        generate : function() {
            // generate the slideshow images
            var slideshow_location = $('<div style="position:relative;width:'+slideshow.fadewidth+'px;height:'+slideshow.fadeheight+'px;overflow:hidden"></div>');
            $( "#slideshow").append(slideshow_location);
            for(var x=0; x < slideshow.slides.length; x++) {
                var slide = slideshow.slides[x];
                var slide_element = $('<a id="slide_'+ x +'" class="slide" href="'+slide.link+'"><img src="'+slide.img+'"/></a>').attr('style',
                        'position:absolute;width:'+slideshow.fadewidth+'px;height:'+slideshow.fadeheight+'px;top:0;left:0;');
                slideshow_location.append(slide_element);
                if ( slideshow.active_slide != x ) {
                    slide_element.hide();
                }
            }
        },
        startFadeOut: function() {
            // called to start the fadeOut
            // set the zindex of the current slide to 1 so will hid the next slide being visible
            var old_slide = $("#slide_"+slideshow.active_slide).addClass("overlaid_slide");
            // make the next slide visible 
            $("#slide_"+slideshow.next_slide).show();
            slideshow.runEffect(old_slide[0]);
        },
        runEffect : function(slide) {
            var selectedEffect = "fade" ;//$( "#effectTypes" ).val();
        
            // most effect types need no options passed by default
            var options = {};
            // some effects have required parameters
            if ( selectedEffect === "scale" ) {
                options = { percent: 0 };
            } else if ( selectedEffect === "transfer" ) {
                options = { to: "#button", className: "ui-effects-transfer" };
            } else if ( selectedEffect === "size" ) {
                options = { to: { width: 200, height: 60 } };
            }
            $(slide).effect( selectedEffect, options, slideshow.delay, slideshow.finishTransition );
        },
        finishTransition: function() {
            $("#slide_"+slideshow.active_slide).hide().removeClass("overlaid_slide");
            slideshow.active_slide = slideshow.next_slide;
            slideshow.next_slide = ( slideshow.next_slide +1 ) % slideshow.slides.length;
            setTimeout(slideshow.startFadeOut, slideshow.delay );
        }
    };
    slideshow.generate();
    setTimeout(slideshow.startFadeOut, slideshow.delay );
}
);
