(function($) {
	
	var jqSummary = function(container, options)
	{
		this.container = container;
		this.options = options;
		
		this._index = null;
		this._length = this.container.children().length;
		this._busy = false;
		this._width = parseInt(this.options.horizontal ? this.container.innerWidth() : this.container.innerHeight());
		
		this._build();
		this._setup();
	};
	
	jqSummary.prototype._build = function()
	{
		var width = this.container.innerWidth();
		var height = this.container.innerHeight();
		var position = this.container.css('position');
		
		if(position != 'absolute') this.container.css('position', 'relative');
		this.container.children().css({'position': 'absolute', 'top': 0, 'left': 0, 'width': width, 'height': height});
	};
	
	jqSummary.prototype._setup = function()
	{
		var self = this;
		
		this.container.children().css(this._css(-1));
		this.index(0, true);
		setInterval(function() { self.index((self._index + 1) % self._length); }, this.options.delay);
	};
	
	jqSummary.prototype._css = function(position)
	{
		if(position == 0) return {left: 0, top: 0};
		
		if(this.options.horizontal) return {left: (position * this._width) + 'px'}
		else return {top: (position * this._width) + 'px'}
	};
	
	jqSummary.prototype.index = function(index, immediate)
	{	
		if(typeof(index) == 'number' && ! this._busy) {
			
			if(immediate) this.item(index).css(this._css(0));
			else this.item(index).css(this._css(1)).animate(this._css(0), this.options.speed, this.options.easing);
			
			if(typeof(this._index) == 'number') {
				if(immediate) this.item(this._index).css(this._css(-1));
				else this.item(this._index).animate(this._css(-1), this.options.speed, this.options.easing);
			}
			
			this._index = index;
		}
		
		return this._index;
	}
	
	jqSummary.prototype.item = function(index)
	{
		return this.container.children().eq(index);
	}
	
	$.fn.summary = function(options)
	{
		if(this.length < 1) return this;
		if(this.length > 1) var container = this.eq(0).parent();
		else var container = this;
		
		var s = new jqSummary(container, $.extend($.fn.summary.defaults, options));
		this.data('summary', s);
		
		return this;
	};
	
	$.fn.summary.defaults = {
		horizontal: true,
		delay: 7000,
		speed: 250,
		easing: 'linear',
		start: 0
	};
	
})(jQuery);
