var slipnslideCount = 0;

jQuery.fn.slipnslide = function(options)
{
	// BOOM! Plugin number 2 from Something for the Weekend!
	jQuery.each(this, function()
	{
		new Slider(this, options);
	});
	return this;
}

function Slider(element, options)
{
	var settings = jQuery.extend({
		// Core settings
		direction: 'horizontal',
		animationDuration: 500,
		startingPosition: 1,
		autoplay: true,
		
		// Cycle settings:
		cycle: false,
		
		// Triggers:
		nextTrigger: null,
		prevTrigger: null,
		
		// Interval settings:
		useInterval: false,
		interval: 6000,
		intervalDirection: 'next'
	}, options);
	
	this.container 		= jQuery(element);
	this.slides			= this.container.children();
	this.currentSlide 	= 1;
	this.totalSlides 	= this.slides.length;
	
	// Make sure this baby has an ID we can reference: 
	if(!this.container.attr('id'))
	{
		this.container.attr('id', 'slipnslide' + slipnslideCount);
	}
	
	var slidepaper = jQuery('<div class="slidepaper" />');
	this.container.css('overflow', 'hidden').css('position', 'relative');

	var totalHeight = 0;
	var totalWidth = 0;
	this.slides.each(function(idx)
	{
		if(settings.direction == 'vertical')
		{
			totalHeight += jQuery(this).outerHeight();
			totalWidth = 'auto';
		}
		else
		{
			totalWidth += jQuery(this).outerWidth(true);
			totalHeight = 'auto';
		}
		
		// Attach a class to the slides to say what order they're in:
		jQuery(this).addClass('slide-' + idx);
	});
	
	slidepaper.css('width', totalWidth).css('height', totalHeight);
	
	this.slides.wrapAll(slidepaper);
	this.slidepaper = jQuery('.slidepaper', this.container);
	this.slidepaper.css('position', 'relative');
	
	// We're done with settings, so assign it to the object:
	this.settings = settings;
	
	// Setup the slider object on the data field:
	this.container.data('slipnslide', this);
	
	// And events:
	if(this.settings.prevTrigger != null)
	{		jQuery(this.settings.prevTrigger).click(jQuery.proxy(this.previous, this));		}
	
	if(this.settings.nextTrigger != null)
	{		jQuery(this.settings.nextTrigger).click(jQuery.proxy(this.next, this));			}
	
	if(this.settings.useInterval && this.settings.autoplay)
	{
		this.play();
	}
	
	slipnslideCount ++;
	
	// Now we're all ready, move into position if possible:
	if(this.settings.startingPosition != 1)
	{
		//this.currentSlide = this.settings.startingPosition;
		this.slideIntoPosition(this.settings.startingPosition);
	}
}

Slider.prototype.GLaDOS = function()
{
	console.log('Now, these points of data\nMake a beautiful line.\nAnd we\'re out of beta.\nWe\'re releasing on time!')
}

Slider.prototype.previous = function()
{
	this.lastDirection = 'previous';
	var prevSlide = (this.currentSlide == 1)? this.totalSlides : this.currentSlide - 1;
	this.slideIntoPosition(prevSlide);
}

Slider.prototype.next = function()
{
	this.lastDirection = 'next';
	var nextSlide = (this.currentSlide == this.totalSlides)? 1 : this.currentSlide + 1;
	this.slideIntoPosition(nextSlide);
}

Slider.prototype.play = function()
{
	var cmd = 'jQuery("#' + this.container.attr('id') + '").data("slipnslide").' + this.settings.intervalDirection + '()';
	this.sliderinterval = setInterval(cmd, this.settings.interval);
	this.is_playing = true;
}

Slider.prototype.pause = function()
{
	clearInterval(this.sliderinterval);
	this.is_playing = false;
}

Slider.prototype.slideIntoPosition = function(pos)
{
	var left, top;
	
	this.preSlide(this.currentSlide, pos);
	this.currentSlide = pos;

	// Find that slide:
	pos --;	// We zero index the slides.
	var newCurrentSlide = jQuery('.slide-' + pos, this.container);
	
	if(this.settings.direction == 'vertical')
	{
		left = 0;
		top = '-' + newCurrentSlide.position().top;
	}
	else
	{
		top = 0;
		left = '-' + newCurrentSlide.position().left;
	}
	
	jQuery(this.slidepaper).animate({
		left: left,
		top: top
	}, this.settings.animationDuration, jQuery.proxy(this.postSlide, this));
}

Slider.prototype.preSlide = function(movingFrom, movingTo)
{
	if(this.settings.cycle)
	{
		if(this.lastDirection == 'previous' || this.settings.intervalDirection == 'previous')
		{
			// Remove and attach the siblings and slide at the beginning:
			var currentSlide = jQuery('.slide-' + (movingFrom-1), this.container);
			var nextSlide = jQuery('.slide-' + (movingTo-1), this.container);
			var nextSiblings = nextSlide.nextAll();
			
			nextSiblings = jQuery.makeArray(nextSiblings);
			this.slidepaper.prepend(nextSiblings);
			
			this.slidepaper.prepend(nextSlide);
			
			if(this.settings.direction == 'vertical')
			{
				this.slidepaper.css('top', '-' + currentSlide.position().top + 'px');
			}
			else
			{
				this.slidepaper.css('left', '-' + currentSlide.position().left + 'px');
			}
		}
	}
}

Slider.prototype.postSlide = function()
{
	if(this.settings.cycle)
	{
		// Get the previous children and shove them on the end, in reverse order:
		var prev = jQuery.makeArray(jQuery('.slide-' + (this.currentSlide-1), this.container).prevAll());
		prev.reverse();
		this.slidepaper.append(prev);

		this.slides = this.slidepaper.children();
		
		if(this.settings.direction == 'vertical')
		{		this.slidepaper.css('top', 0);		}
		else
		{		this.slidepaper.css('left', 0);		}
	}
}


