$slideshow = {
    context: false,
    tabs: false,
    timeout: 8000,      // time before next slide appears (in ms)
    slideSpeed: 1200,   // time it takes to slide in each slide (in ms)
    tabSpeed: 600,      // time it takes to slide in each slide (in ms) when clicking through tabs
	fx: 'scrollUp',   // the slide effect to use
    
    init: function() {
        // set the context to help speed up selectors/improve performance
        this.context = $('#slideshow');
        
        // set tabs to current hard coded navigation items
        this.tabs = $('#slides_nav ul li', this.context);
        
        // remove hard coded navigation items from DOM 
        // because they aren't hooked up to jQuery cycle
        this.tabs.remove();
        
        // prepare slideshow and jQuery cycle tabs
        this.prepareSlideshow();
    },
    
    prepareSlideshow: function() {
        // initialise the jquery cycle plugin -
        // for information on the options set below go to: 
        // http://malsup.com/jquery/cycle/options.html
        $('div.slides', $slideshow.context).cycle({
            fx: $slideshow.fx,
            timeout: $slideshow.timeout,
            speed: $slideshow.slideSpeed,
            fastOnEvent: $slideshow.tabSpeed,
            pager: $('#slides_nav ul', $slideshow.context),
            pagerAnchorBuilder: $slideshow.prepareTabs,
            before: $slideshow.activateTab,
            pauseOnPagerHover: true,
            pause: true,
			after: onAfter
        });            
    },
    
    prepareTabs: function(i, slide) {
        // return markup from hardcoded tabs for use as jQuery cycle tabs
        // (attaches necessary jQuery cycle events to tabs)
        return $slideshow.tabs.eq(i);
    },

    activateTab: function(currentSlide, nextSlide) {
        // get the active tab
		//console.log($(nextSlide));
        var activeTab = $('a[href="#' + nextSlide.id + '"]', $slideshow.context);
		//var fadeOut = currentSlide;
		//var fadeIn = nextSlide;
        
        // if there is an active tab
        if(activeTab.length) {
            // remove active styling from all other tabs
            $slideshow.tabs.removeClass('on');
            
            // add active styling to active button
            activeTab.parent().addClass('on');
        }
		//When slide is ready to transition, set opacity of content to zero
		if (currentSlide != nextSlide) {
			var thisContent = $(currentSlide).children('.content').children('.slide_wrap');
			//var nextContent = $(nextSlide).children('.content').children('.slide_wrap');
			
			$(thisContent).children('img').animate({
				'opacity' : 0
			}, 300, function() {
				$(thisContent).children('img').css({
					'visibility' : 'hidden'	
				});
			});
			$(thisContent).children('h2').animate({
				'opacity' : 0
			}, 300, function() {
				$(thisContent).children('h2').css({
					'visibility' : 'hidden'	
				});
			});
			$(thisContent).children('p').animate({
				'opacity' : 0
			}, 300, function() {
				$(thisContent).children('p').css({
					'visibility' : 'hidden'	
				});
			});
			$(thisContent).children('ul').animate({
				'opacity' : 0
			}, 300, function() {
				$(thisContent).children('ul').css({
					'visibility' : 'hidden'	
				});
			});
		};
    }            
};

function onAfter() {
	$(this).children('.content').children('.slide_wrap').children('img').css({ 'visibility' : 'visible' });
	$(this).children('.content').children('.slide_wrap').children('h2').css({ 'visibility' : 'visible' });
	$(this).children('.content').children('.slide_wrap').children('p').css({ 'visibility' : 'visible' });
	$(this).children('.content').children('.slide_wrap').children('ul').css({ 'visibility' : 'visible' });
	
	$(this).children('.content').children('.slide_wrap').children('img').animate({ 'opacity' : 1 }, 300);
	$(this).children('.content').children('.slide_wrap').children('h2').animate({ 'opacity' : 1 }, 300);
	$(this).children('.content').children('.slide_wrap').children('p').animate({ 'opacity' : 1 }, 300);
	$(this).children('.content').children('.slide_wrap').children('ul').animate({ 'opacity' : 1 }, 300);
};

$(function() {
    // add a 'js' class to the body
    $('body').addClass('js');
    
    // initialise the slideshow when the DOM is ready
    $slideshow.init();
}); 
