$slideshow = {
    context: false,
    tabs: false,
    timeout: 5000,      // time before next slide appears (in ms)
    slideSpeed: 1000,   // 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: 'scrollLeft',   // the slide effect to use
    fx: 'fade',   // 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 = $('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();
    },
    
    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 > ul', $slideshow.context).cycle({
            fx: $slideshow.fx,
            timeout: $slideshow.timeout,
            speed: $slideshow.slideSpeed,
            fastOnEvent: $slideshow.tabSpeed,
            pager: $('ul.slides-nav', $slideshow.context),
            pagerAnchorBuilder: $slideshow.prepareTabs,
            before: $slideshow.activateTab,
            pauseOnPagerHover: true,
            pause: true
        });            
    },
    
    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
        var activeTab = $('a[href="#' + nextSlide.id + '"]', $slideshow.context);
        
        // 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');
            
        }            
    }            
};


$(function() {
    // add a 'js' class to the body
    $('body').addClass('js');
    
    // FitnessFirst Single Post Gallery
	$(".slides ul").css({"display" : "block", 'width' : '540px', 'height' : '380px'});

});

// Once the window has loaded - load in the image

var int = 0;
var imgSrc = "";

$(window).bind("load", function() {
	imgSrc = $(".attachment-large").attr('src');
	int = setInterval("displayMe()",350);
});

function displayMe() {
	// Set up the image sizes
	var frame_width = 540;
    var img = new Image();
    img.src = imgSrc;
    var new_img_width = img.width;
    var new_img_height = img.height;
	
	// Find the size of the image and then set its properties
	if (new_img_width > frame_width) {
			
		var multiplier = new_img_width / frame_width;
		var newHeight = new_img_height / multiplier;
		var imgWidth = frame_width;
		var marginLeft = 0;
						
	} else {
	
		imgWidth = new_img_width;
		newHeight = new_img_height;
		var newMargin = frame_width - imgWidth;
		marginLeft = newMargin / 2;
	
	}
		
	// Set the new image width, height, add margins & then load the images
	$(".img_holder").fadeIn(650);
	//$(".attachment-large").width(imgWidth).height(newHeight).css("margin-left",marginLeft+"px").fadeIn(650);
	$(".attachment-large").fadeIn(650);
	$(".slides-copy").fadeIn(650);
	$(".slides-nav").slideDown(450);

				
	// clear the interval
	clearInterval(int);
	delete int;
	
	// initialise the slideshow when the DOM is ready
    $slideshow.init();
}
