﻿var wvCarouselDetails = {
	'author': 'Darren Waddell',
	'created': '23/04/08',
	'version': '1.01'
};
var wvCarousel = function(el, options){ 
	
	var defaults = {
		'pause': 10000
	};
	var options = $.extend(defaults, options || {}); 
	var timer, count = $(el).children('ul').children('li').length;
	var carousel, carousel_options, carousel_ticker, carousel_nextStory, prevButton, nextButton, currentItem = 0;

	var createOptions = function()	{
		carousel_options = $('<div class="carousel_options"></div>');
		$(carousel_options).appendTo($(el));
	};
	
	var createButtons = function()	{
		prevButton = $('<span class="previousButton"><img src="/magazine/graphics/buttons/b_arrow_left.png" alt="Previous stories" /></span>');
		nextButton = $('<span class="nextButton"><img src="/magazine/graphics/buttons/b_arrow_right.png" alt="Next stories" /></span>');
		prevButton.click(function()	{
			currentItem == 0 ? currentItem = count - 1 : currentItem--;
			swapItem();
			setTimer();
		});
		nextButton.click(function()	{
			currentItem < count - 1 ? currentItem++ : currentItem = 0;
			swapItem();
			setTimer();
		});
		prevButton.appendTo($(carousel_options));
		nextButton.appendTo($(carousel_options));
	};
	
	var createTicker = function() { 
            var html = ''; 
            for(i = 0; i < count; i++) { 
            html += '<li style="display: block; cursor: pointer;"><a href="#">&nbsp;&nbsp;&nbsp;</a></li>' + '\n'; 
            } 
            carousel_ticker = $('<div class="carousel_sleeve"></div>').insertAfter(prevButton); 
            $('<ul>' + html + '</ul>').appendTo(carousel_ticker); 
            $('.carousel_sleeve ul li').each(function(i) { 
            $(this).click(function() { 
            currentItem = i%4; 
            swapItem(); 
            try { window.clearInterval(timer); } catch(e) {}
            }); 
        }); 
    }; 
    /*
	
	var createTicker = function()	{
		var html = '';
		for(i = 0; i < count; i++)	{
			html += '<li style="display: block; cursor: pointer;"><a href="#">&nbsp;&nbsp;&nbsp;</a></li>' + '\n';
		}
		carousel_ticker = $('<div class="carousel_sleeve"></div>').insertAfter(prevButton);
		$('<ul>' + html + '</ul>').appendTo(carousel_ticker);
		$('.carousel_sleeve ul li').each(function(i)	{
			$(this).click(function()	{
				currentItem = i;
				swapItem();
				try { window.clearInterval(timer); } catch(e) {}
			});
		});
	};
*/	
	var createNextStory = function()	{
		carousel_nextStory = $('<p class="nextfeature"><span class="numerator">1</span>/<span class="denominator">' + count + '</span> <span class="next_headline">Next: <strong></strong></span></p>');
		carousel_nextStory.appendTo(carousel_options);	
		$(el).children('ul').children('li').find('h2 > a').each(function(i)	{
			$(carousel_ticker).find('li').eq(i).attr('rel', $(this).text());													
		});
	};
	
	var swapItem = function()	{
		$(carousel_options).find('li').removeClass('selected').eq(currentItem).addClass('selected');	
		$(el).children('ul').children('li').removeClass('current').eq(currentItem).addClass('current');	
		$(el).find('.nextfeature .numerator').text(currentItem + 1);
		currentItem < count - 1 ? $(el).find('.nextfeature strong').text($(carousel_options).find('li').eq(currentItem + 1).attr('rel')) : $(el).find('.nextfeature strong').text($(carousel_options).find('li').eq(0).attr('rel'));
		
		
	};
	
	var rotate = function()	{
		currentItem < count - 1 ? currentItem++ : currentItem = 0;
		swapItem();	
	};
	
	var setTimer = function()	{
		try { window.clearInterval(timer); } catch(e) {}
		timer = window.setInterval(rotate, options['pause']);
	};
	
	var _init = function()	{
		createOptions();
		createButtons();
		createTicker();
		createNextStory();
		swapItem();
		setTimer();
	}();
	
	this.stop = function()	{
		try { window.clearInterval(timer); } catch(e) {}
		return true;
	};
	
	this.start = function()	{
		setTimer();
		return true;
	};
	
	//Changed TES-1327
	this.skipto = function(i)	{
		if(i == 'first')		currentItem = 0;
		else if(i == 'last')	currentItem = count - 1;
		else					currentItem = i;
		swapItem();
		setTimer();
		return true;
	};
	
}

var carousels = [];

$(function() {
    $('.carousel_showing').each(function(i) {
        carousels[i] = new wvCarousel($(this));
    });

    // Publications-specific
    $("#publications .testabs-content > div")
        .hide()
        .eq(0)
        .show();

    $('#publications .testabs li a')
        .click(function() {
            var index = $(this).parents("ul").find("li").index($(this).parent()[0]);
            $("#publications .testabs-content > div")
                .hide()
                .eq(index)
                .show();
        });

    // Moves the 'more' link into the carousel
    $('div.carousel').each(function() {
        $(this).find('a.more').insertAfter($(this).find('.carousel_showing > ul'));
    });


});
