$.cycler = function(elem)
{
	this.elem = $(elem);
	this.cycles = $('.cycle', this.elem);
	this.currentIndex = 0;
	
	this.prevElem = $('.prevArrow', this.elem).bind('click', this, this.prev);
	this.nextElem = $('.nextArrow', this.elem).bind('click', this, this.next);
	me = this;
	setTimeout(function(){ me.next({data: me}); }, 5000);
}

$.extend($.cycler, {
	prototype:
	{
		next:function(e)
		{
			me = e.data;
			cur = me.cycles[me.currentIndex];
			me.currentIndex = (me.currentIndex + 1 >= me.cycles.length) ? 0 : me.currentIndex + 1;
			next =  me.cycles[me.currentIndex];
			$(cur).slideUp(1000);
	
			clearTimeout(me.running);
			clearTimeout(me.running2);
			me.running = setTimeout(function(){ $(next).slideDown(1500); }, 1420);
			me.running2 = setTimeout(function(){ me.next({data: me}); }, 5000);
		},
		prev:function(e)
		{
			me = e.data;
			cur = me.cycles.eq(me.currentIndex);
			me.currentIndex = (me.currentIndex - 1 < 0) ? me.cycles.length : me.currentIndex - 1;
			next =  me.cycles.eq(me.currentIndex);
			$(cur).slideUp(1000);
	
			clearTimeout(me.running);
			clearTimeout(me.running2);
			me.running = setTimeout(function(){ $(next).slideDown(1500); }, 1420);
			me.running2 = setTimeout(function(){ me.next({data: me}); }, 5000);
		}
	}
});

$.cycle = function(num, array)
{
	cur = array[num];
	num = (num + 1 >= array.length) ? 0 : num + 1;
	next = array[num];
	$(cur).fadeOut(1000);
	
	setTimeout(function(){ $(next).fadeIn(1500); }, 1420);
	setTimeout(function(){ $.cycle(num, array); }, 5000);
}
$.fn.startCycle = function(){ $.cycle(0, $(this).children()); }
