/* 
Copyright 2011 Avacari.com | License required for use.
*/
(function($) {
	
	var ThumbnailSlideshow = function(element, options) {
		
		var position;
		var children;
		
		var zIndexCounter = 1000;
		
		var intervalId;
		var currentElem;
	
		var defaults = $.extend({}, $.fn.thumbnailSlideshow.defaults, options);
		var $element = $(element);
		
		children = $element.children().length;
		
		position = 0;
		
		$element.css('width', $element.find('img:eq(0)').width() + 'px');
		$element.css('height', $element.find('img:eq(0)').height() + 'px');
			
		$element.bind({
			mouseenter: function() {
				currentElem = this;
				intervalId = setInterval(function() { next();}, defaults['delay']);
			},
			mouseleave: function() {
				if(defaults['returnToStart']) {
					$(currentElem).find('img').fadeOut(1, function() {
						$(currentElem).find('img:eq(0)').show();
						position = 0;
					});
				}
				clearInterval(intervalId);
			}
		});
		
		var next = function() {
			var actualTransition = defaults['transition'] / 2;
			var $currentElement = $(currentElem);
			
			switch(defaults['effect']) {
				case 'none':
					$currentElement.find(' img:eq(' + position + ')').hide();
					incrementPosition();
					$currentElement.find(' img:eq(' + position + ')').show();
					break;
				case 'fade':
					$currentElement.find(' img:eq(' + position + ')').fadeOut(actualTransition, function() {
						incrementPosition();
						$currentElement.find(' img:eq(' + position + ')').fadeIn(actualTransition);
					});
					break;
				case 'crossfade':
					$currentElement.find(' img:eq(' + position + ')').fadeOut(defaults['transition']);
					incrementPosition();
					$currentElement.find(' img:eq(' + position + ')').fadeIn(defaults['transition']);		
					break;
				case 'slidedown':
					incrementPosition();
					$currentElement.find(' img:eq(' + position + ')').css({'margin-top' : '-' + $currentElement.find(' img:eq(0)').height() + 'px', 'z-index' : zIndexCounter, 'display' : 'inline'});
					$currentElement.find(' img:eq(' + position + ')').animate({
						'marginTop': 0
					}, defaults['transition']);
					zIndexCounter++;
					break;
				case 'slideup':
					incrementPosition();
					$currentElement.find(' img:eq(' + position + ')').css({'margin-top' : $currentElement.find(' img:eq(0)').height() + 'px', 'z-index' : zIndexCounter, 'display' : 'inline'});
					$currentElement.find(' img:eq(' + position + ')').animate({
						'marginTop': 0
					}, defaults['transition']);
					zIndexCounter++;
					break;
				case 'slideright':
					incrementPosition();
					$currentElement.find(' img:eq(' + position + ')').css({'margin-left' : '-' + $currentElement.find(' img:eq(0)').width() + 'px', 'z-index' : zIndexCounter, 'display' : 'inline'});
					$currentElement.find(' img:eq(' + position + ')').animate({
						'marginLeft': 0
					}, defaults['transition']);
					zIndexCounter++;
					break;
				case 'slideleft':
					incrementPosition();
					$currentElement.find(' img:eq(' + position + ')').css({'margin-left' : $currentElement.find(' img:eq(0)').width() + 'px', 'z-index' : zIndexCounter, 'display' : 'inline'});
					$currentElement.find(' img:eq(' + position + ')').animate({
						'marginLeft': 0
					}, defaults['transition']);
					zIndexCounter++;					break;
				default:
					$currentElement.find(' img:eq(' + position + ')').hide();	
			}

		}
		
		var incrementPosition = function() {
			position++;
			
			if(position == children) {
				position = 0;
			}
		}
	}
	
	$.fn.thumbnailSlideshow = function(options) {
		
		$(this).each(function() {
			var ts = new ThumbnailSlideshow(this, options);
		});
	}
	
	$.fn.thumbnailSlideshow.defaults = {
		effect: 'crossfade',
		delay: 1500,
		transition: 500,
		returnToStart: false
	};
	
})(jQuery);

