/*
Author: Marc Lloyd (And Dave)
*/

var delay = 6000; /* set the delay between slides */
var speed = 600; /* set the speed of slide movement */

var numberOfLis;
var currentPosition
var offset;
var timerid;

jQuery(function(){

	/* SHUFFLE THE ELEMENTS ON LOAD SO IT STARTS FROM A RANDOM POINT */
	jQuery.fn.shuffle = function() {

        var allElems = this.get(),
            getRandom = function(max) {
                return Math.floor(Math.random() * max);
            },
            shuffled = jQuery.map(allElems, function(){
                var random = getRandom(allElems.length),
                    randEl = jQuery(allElems[random]).clone(true)[0];
                allElems.splice(random, 1);
                return randEl;
           });

        this.each(function(i){
            jQuery(this).replaceWith(jQuery(shuffled[i]));
        });

        return jQuery(shuffled);

    };
	/* SHUFFLE THE ELEMENTS ON LOAD SO IT STARTS FROM A RANDOM POINT */
	jQuery('#banner li').shuffle();

	/* COPY FIRST LI AND REPEAT AT END OF LIST */
	var last_item = jQuery('#banner li:last-child').clone();
	var first_item = jQuery('#banner li:first-child').clone();
	last_item.insertBefore('#banner li:first-child');
	first_item.insertAfter('#banner li:last-child');

	// Move the CSS so that to the user it looks normal and as if nothing has happened.
	jQuery('#banner ul').css('left', '-985px');
	
	/* GET NUMBER OF LIST ITEMS AND SET CURRENT POSITION TO 2 */
	numberOfLis = jQuery('#banner li').length;
	currentPosition = 2;
		
	move = function(back){

		if(back) {
			// Moving backwards.
			offset = '-' + ((currentPosition * 985)-1970) + 'px';
			jQuery('#banner ul').animate({'left' : offset},speed, '', function() {
				currentPosition--;
				// If we've got to the start, what we need to do is jump to the end
				if(currentPosition == 1) {
					toTheEnd();
				}
			});
		} else {
			// Moving forwards
			offset = '-' + (currentPosition * 985) + 'px';
			jQuery('#banner ul').animate({'left' : offset},speed, '', function() {
				currentPosition++;
				// If we've got to the end, jump back to the start once the animation has finished.
				if(currentPosition == numberOfLis) {
					toTheStart();
				}
			});
		}
	};

	
	jQuery('#banner a#next-img').stop(true, true).click(function(){
		// IF an animation is currently running, don't process this request.
		if (jQuery(this).parent().find('ul').is(':animated') ) { return false; }
		// move forwards...
		clearMoveStart(false);
		return false;
	});
	
	jQuery('#banner a#prev-img').stop(true, true).click(function(){
		// IF an animation is currently running, don't process this request.
		if (jQuery(this).parent().find('ul').is(':animated') ) { return false; }
		// move backwards...
		clearMoveStart(true);
		return false;
	});

	/**
	 *  Clear the interval for the timer, move (forward if back = false, back if back = true)
	 *  Start the interval for the timer again.
	 */
	clearMoveStart = function(back) {
		clearInterval(timerid);
		move(back);
		timerid = setInterval('move()',delay);
		return false;
	}

	toTheStart = function() {
		// QUICK! To the start.
		jQuery('#banner ul').css('left', '-985px');
		currentPosition = 2;
	}

	toTheEnd = function() {
		// QUICK! To the end.
		jQuery('#banner ul').css('left', '-' + ((985 * (numberOfLis - 2))) + 'px');
		currentPosition = (numberOfLis - 1);
	}

	// Get going!
	timerid = setInterval('move()',delay);
	
});

