$carousels = {
    context: false,
    tabs: false,
    timeout: 2000,      // time before next slide appears (in ms)
    slideSpeed: 2000,   // time it takes to slide in each slide (in ms)
    tabSpeed: 300,      // time it takes to slide in each slide (in ms) when clicking through tabs
    fx: 'fade',   // the slide effect to use
    
    init: function(automaticRotation) {
        // set the context to help speed up selectors/improve performance
        this.context = $('.carousels');
        
        // set tabs to current hard coded navigation items
        this.tabs = $('ul.slides-nav 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(automaticRotation);
    },
    
    prepareSlideshow: function(automaticRotation) {
        // initialise the jquery cycle plugin -
        if (automaticRotation){
        	$('div.slides > ul', $carousels.context).cycle({
                fx: $carousels.fx,
                timeout: $carousels.timeout,
                speed: $carousels.slideSpeed,
                fastOnEvent: $carousels.tabSpeed,
                pager: $('ul.slides-nav', $carousels.context),
                pagerAnchorBuilder: $carousels.prepareTabs,
                before: $carousels.activateTab,
                pauseOnPagerHover: true,
                pause: true
            });
        }else{
        	$('div.slides > ul', $carousels.context).cycle({
                fx: $carousels.fx,
                timeout: $carousels.timeout,
                speed: $carousels.slideSpeed,
                fastOnEvent: $carousels.tabSpeed,
                pager: $('ul.slides-nav', $carousels.context),
                pagerAnchorBuilder: $carousels.prepareTabs,
                before: $carousels.activateTab,
                pauseOnPagerHover: true,
                pause: true,
                autostop: true, 
                autostopCount: 1
            });
        }
    },
    
    prepareTabs: function(i, slide) {
        // return markup from hardcoded tabs for use as jQuery cycle tabs
        // (attaches necessary jQuery cycle events to tabs)
        return $carousels.tabs.eq(i);
    },

    activateTab: function(currentSlide, nextSlide) {
        // get the active tab
        var activeTab = $('a[href="#' + nextSlide.id + '"]', $carousels.context);
        
        // if there is an active tab
        if(activeTab.length) {
            // remove active styling from all other tabs
            $carousels.tabs.removeClass('on');
            
            // add active styling to active button
            activeTab.parent().addClass('on');
        }            
    }            
};
  
