$slideshow = {
    context: false,
    tabs: false,
    timeout: 8000,      // time before next slide appears (in ms)
    slideSpeed: 2000,   // time it takes to slide in each slide (in ms)
    tabSpeed: 2000,      // time it takes to slide in each slide (in ms) when clicking through tabs
    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,
			next: '#next',
			prev: '#prev'
        });            
    },
    
    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(){
	$.ajax({
		type: "GET",
		url: "xml/rss.xml",
		dataType: "xml",
		success: function(xml){
			$(xml).find('item').eq(0).each(function(){
				var title0 = $(this).find("title").text();
				var description0 = $(this).find("description").text();
				var link0 = $(this).find("link").text();
				var pubDate0 = $(this).find("pubDate").text();
				var image0 = $(this).find("image").text();
				$('#image0').attr({src: image0});
				$('#title0').append('<a href="'+link0+'" class="title">'+title0+'</a>');
				$('#link0').attr({href: link0});
				$('#description0').append(description0);
				$('#pubDate0').append(pubDate0);
			});
			$(xml).find('item').eq(1).each(function(){
				var title1 = $(this).find("title").text();
				var description1 = $(this).find("description").text();
				var link1 = $(this).find("link").text();
				var pubDate1 = $(this).find("pubDate").text();
				var image1 = $(this).find("image").text();
				$('#image1').attr({src: image1});
				$('#title1').append('<a href="'+link1+'" class="title">'+title1+'</a>');
				$('#link1').attr({href: link1});
				$('#description1').append(description1);
				$('#pubDate1').append(pubDate1);
			});
			$(xml).find('item').eq(2).each(function(){
				var title2 = $(this).find("title").text();
				var description2 = $(this).find("description").text();
				var link2 = $(this).find("link").text();
				var pubDate2 = $(this).find("pubDate").text();
				var image2 = $(this).find("image").text();
				$('#image2').attr({src: image2});
				$('#title2').append('<a href="'+link2+'" class="title">'+title2+'</a>');
				$('#link2').attr({href: link2});
				$('#description2').append(description2);
				$('#pubDate2').append(pubDate2);
			});
			$(xml).find('item').eq(3).each(function(){
				var title3 = $(this).find("title").text();
				var description3 = $(this).find("description").text();
				var link3 = $(this).find("link").text();
				var pubDate3 = $(this).find("pubDate").text();
				var image3 = $(this).find("image").text();
				$('#image3').attr({src: image3});
				$('#title3').append('<a href="'+link3+'" class="title">'+title3+'</a>');
				$('#link3').attr({href: link3});
				$('#description3').append(description3);
				$('#pubDate3').append(pubDate3);
			});
			$(xml).find('item').eq(4).each(function(){
				var title4 = $(this).find("title").text();
				var description4 = $(this).find("description").text();
				var link4 = $(this).find("link").text();
				var pubDate4 = $(this).find("pubDate").text();
				var image4 = $(this).find("image").text();
				$('#image4').attr({src: image4});
				$('#title4').append('<a href="'+link4+'" class="title">'+title4+'</a>');
				$('#link4').attr({href: link4});
				$('#description4').append(description4);
				$('#pubDate4').append(pubDate4);
			});
			$(xml).find('item').eq(5).each(function(){
				var title5 = $(this).find("title").text();
				var description5 = $(this).find("description").text();
				var link5 = $(this).find("link").text();
				var pubDate5 = $(this).find("pubDate").text();
				var image5 = $(this).find("image").text();
				$('#image5').attr({src: image5});
				$('#title5').append('<a href="'+link5+'" class="title">'+title5+'</a>');
				$('#link5').attr({href: link5});
				$('#description5').append(description5);
				$('#pubDate5').append(pubDate5);
			});
			$(xml).find('item').eq(6).each(function(){
				var title6 = $(this).find("title").text();
				var description6 = $(this).find("description").text();
				var link6 = $(this).find("link").text();
				var pubDate6 = $(this).find("pubDate").text();
				var image6 = $(this).find("image").text();
				$('#image6').attr({src: image6});
				$('#title6').append('<a href="'+link6+'" class="title">'+title6+'</a>');
				$('#link6').attr({href: link6});
				$('#description6').append(description6);
				$('#pubDate6').append(pubDate6);
			});
			$(xml).find('item').eq(7).each(function(){
				var title7 = $(this).find("title").text();
				var description7 = $(this).find("description").text();
				var link7 = $(this).find("link").text();
				var pubDate7 = $(this).find("pubDate").text();
				var image7 = $(this).find("image").text();
				$('#image7').attr({src: image7});
				$('#title7').append('<a href="'+link7+'" class="title">'+title7+'</a>');
				$('#link7').attr({href: link7});
				$('#description7').append(description7);
				$('#pubDate7').append(pubDate7);
			});
		}
	});

    
    // initialise the slideshow when the DOM is ready
    $slideshow.init();
});


