if (typeof console == "undefined" || typeof console.log == "undefined") var console = { log: function() {} }; 
(function($){

// defaults
$.scroller = {
	defaults: {
		itemsWrapper: '.slides',
		navWrapper: '.nav',
		replaceImg: true,
		speed: 1000,
		slideshow: true,
		slideshowSpeed: 3000
	}
};

Scroller = function($scroller, config) {
	
	var scroller = this;	
	var $items = $(config.itemsWrapper, $scroller).css('position', 'relative').children();
	var $navItems = $(config.navWrapper, $scroller).children();
	var selected = 0;
	
	if ($items.length != $navItems.length) {
		console.log('Warning: please check number of items / nav items');
	}
	
	// Private methods
	var slideTo = function(index) {
		if (selected == index) { return; }
		
		// previous index
		var prev = selected;
		var speed = config.speed;
		
		var $prev = $items.eq(prev);
		var $next = $items.eq(index);
		
		// prepare for animation		
		$prev.css({ zIndex: 1 });
		$next.css({ zIndex: 2, display: 'block' });
		
		// Hide previous slide
		$prev.stop().animate({ opacity: 0 }, speed, function() {
			$prev.css('display', 'none');
		});
		// show next slide
		$next.stop().animate({ opacity: 1 }, speed, function() {});
	}
	
	var slideshowStart = function() {
		//console.log('Starting slideshow');
		sInterval = setInterval(slideshowNext, config.slideshowSpeed);
	};
	var slideshowStop = function() {
		//console.log('Stopping slideshow');
		clearInterval(sInterval);
	};
	var slideshowNext = function() {
		//console.log('Selected ' + selected);
		var next = (selected < $items.length - 1 ) ? selected + 1 : 0;
		scroller.select(next);
		//console.log('Next ' + next);
	}
	
	// API methods	
	$.extend(scroller, {
		select: function(index) {
			// clear selection
			$items.removeClass('current');
			$navItems.removeClass('current');
			$items.eq(index).addClass('current');
			$navItems.eq(index).addClass('current');
			
			slideTo(index);
			
			// Update current index
			selected = index;
		}
	});
	
	$items.each(function(i, item) {
		if (config.replaceImg) {
			if (($.browser.mozilla && $.browser.version < '2') || $.browser.opera) {
				var span = document.createElement('span');
				var img = (item.src) ? img : $('img', item).get(0);		
				span.style.backgroundImage = 'url('+img.src+')';
				$(img).replaceWith($(span).addClass('img'));	
			}		
		}
		
		$(item).css({
			position: 'absolute',
			zIndex: (i == 0) ? 2 : 1,
			display: (i == 0) ? 'block' : 'none',
			opacity: (i == 0) ? 1 : 0
		});
	});
	
	scroller.select(0);	
	
	var sInterval = null;
	
	// Bind events	
	$navItems.each(function(index, item) {
		$(item).mouseenter(function() {
			if (index != selected) {
				scroller.select(index);
			}
		});
	});
	
	if (config.slideshow) {
		$navItems.mouseenter(function(){
			//console.log('Stopping slideshow');
			slideshowStop();
		});
		$navItems.mouseleave(function(){
			//console.log('Starting slideshow');
			slideshowStart();
		});
		
		slideshowStart();
	}
};

$.fn.scroller = function(conf) {
	var scroller = this.data('scroller');
	if (scroller) { 
		return scroller;
	}
	config = $.extend({}, $.scroller.defaults, conf);
	this.each(function() {
		$scroller = $(this);
		scroller = new Scroller($scroller, config);	
		$scroller.data('scroller', scroller);
	});
	return config.api ? scroller : this;
};
})(jQuery);

