
var Fader = function(args)
{
	this.init(args);
}

Fader.prototype = {
	init: function(args)
	{
		var self = this;
		this.currentItem = 1;
		
		// Rearrange items and absolutize
		this.items = [];
		$jQ(args.selector.item, args.selector.wrap)
			.each(function(i)
			{
				$jQ(this)
					.toggleClass('rel abs')
					.css(
					{
						opacity: (i == 0) ? '1' : '0',
						top: 0,
						left: 0,
					});
				self.items.push(this);
			});
		
		$jQ(args.selector.items, args.selector.wrap).html(this.items.reverse());
		if(args.naviButtons.show && this.items.length > 1)
		{
			this.addNaviButtons(args);
		}
		
		// Auto rotate
		this.autoRotate(args);
		$jQ(args.selector.wrap)
			.mouseenter(function()
			{
				$jQ(this).stopTime('fadeAutoRotate');
			})
			.mouseleave(function()
			{
				self.autoRotate(args);
			});
	},
	
	/*
	 *		FADE TO NEXT CASE
	 */
	
	fadeNext: function(args)
	{
		var self = this;
		
		if(!$jQ(args.selector.item, args.selector.wrap).is(':animated'))
		{
			if(this.currentItem == this.items.length)
				this.currentItem = 1;
			else
				this.currentItem++;
			
			var item = $jQ(args.selector.item, args.selector.wrap).eq(this.items.length - 1);
			var itemClone = $jQ(item).clone();
			$jQ(itemClone).prependTo(args.selector.items, args.selector.wrap);
			$jQ(item).remove();
			
			this.updateCurrent(this.currentItem);
			
			$jQ(args.selector.item, args.selector.wrap).eq(this.items.length - 1)
				.animate(
				{
					opacity: '1'
				},
				{
					queue: true,
					duration: args.speed,
					complete: function()
					{
						$jQ(this).siblings().eq(0)
							.css('opacity', 0);
					}
				});
		}
	},
	
	/*
	 *		FADE TO PREVIOUS CASE
	 */
	
	fadePrev: function(args)
	{
		var self = this;
		
		if(!$jQ(args.selector.item, args.selector.wrap).is(':animated'))
		{
			if(this.currentItem == 1)
				this.currentItem = this.items.length;
			else
				this.currentItem--;
			
			var item = $jQ(args.selector.item, args.selector.wrap).eq(0);
			var itemClone = $jQ(item).clone();
			$jQ(itemClone).appendTo(args.selector.items, args.selector.wrap);
			$jQ(item).remove();
			
			this.updateCurrent(this.currentItem);
			
			$jQ(itemClone)
				.animate(
				{
					opacity: '1'
				},
				{
					queue: true,
					duration: args.speed,
					complete: function()
					{
						$jQ(this).prev(args.selector.item)
							.css('opacity', '0');
					}
				});
		}
	},
	
	/*
	 *		AUTO ROTATE
	 */
	
	autoRotate: function(args)
	{
		var self = this;
		
		$jQ(args.selector.wrap)
			.everyTime(6000, 'fadeAutoRotate', function()
			{
				self.fadeNext(args);
			});
	},
	
	/*
	 *		ADD NAVIGATION AND SEQUENCE COUNTER
	 */
	
	addNaviButtons: function(args)
	{
		var self = this;
		
		var naviWrap = $jQ('<p>',
		{
			'class': 'navi-wrap abs hid clear'
		}).appendTo(args.selector.wrap);
		
		var prevButton = $jQ('<a>',
		{
			'class': 'prev rel hid',
			'href': '#',
			'click': function(e)
			{
				e.preventDefault();
				self.fadePrev(args);
			},
			'text': args.naviButtons.prevText
		}).appendTo('.navi-wrap', args.selector.wrap);
		
		var sequenceIndicator = $jQ('<span>',
		{
			'class': 'sequence',
			'html': '<span class="current">1</span>/' + self.items.length
		}).appendTo('.navi-wrap', args.selector.wrap);
		
		var nextButton = $jQ('<a>',
		{
			'class': 'next rel hid',
			'href': '#',
			'click': function(e)
			{
				e.preventDefault();
				self.fadeNext(args);
			},
			'text': args.naviButtons.nextText
		}).appendTo('.navi-wrap', args.selector.wrap);
	},
	
	/*
	 *		UPDATE SEQUENCE COUNTER
	 */
	
	updateCurrent: function(current)
	{
		$jQ('.current', '.navi-wrap').text(current);
	}
};

$jQ(function()
{
	var BRANNFader = new Fader(
	{
		selector: {
			wrap: '.fade-wrap',
			items: '.items',
			item: '.item'
		},		
		naviButtons: {
			show: true,
			prevText: 'Forrige',				
			nextText: 'Neste',
			displaySequence: true
		},
		speed: 200
	});
});
