/**
 * WebPoint Slider
 *
 * Copyright (c) 2010 Por Design (pordesign.eu)
 * Dual licensed under the MIT and GPL licenses:
 * http://www.opensource.org/licenses/mit-license.php
 * http://www.gnu.org/licenses/gpl.html
 *
 */

(function($)
{
	$.fn.slider = function(options)
	{
		$slider = $(this);
		
		options = $.extend
		({
			nav: 'ul.nav',
			items: 'ul.items',
			visible: 3,
			slide: 1,
			speed: 200,
			wrapper_class: 'slider-wrapper',
			fade: false,
			auto_slide: false,
			auto_delay: 500,
			auto_direction: 'right',
			easing: 'swing',
			auto_height: true,
			auto_height_parent: false
		}, options);
		
		$slider.each( function()
		{
			$slider = $(this);
			
			$slider_nav = $(this).children(options.nav);
			$slider_items = $(this).children(options.items);
				
			$slider_items.after($slider_items.clone().hide());
				
			$slider_items_content = $(this).children(options.items).eq(1);
			
			$slider_items.css
			({
				'position': 'absolute',
				'width': 9999
			}).wrap
			(
				$('<div />').addClass(options.wrapper_class).css
				({
					'position': 'relative',
					'overflow': 'hidden',
					'height': $slider_items.outerHeight(true)
				})
			).children('li').css
			({
				'float': 'left'
			});
				
			$slider_items.children('li').remove();
			$slider_items.data('last-index', options.visible);
			
			height = 0;
			
			for (i = 0; i < options.visible; i++)
			{
				$slider_items.append($slider_items_content.children('li').eq(i).clone());
				
				if (options.auto_height)
				{
					if ($slider_items.children('li').last().outerHeight(true) > height)
					{
						height = $slider_items.children('li').last().outerHeight(true);
					}
				}
			}

			if (options.auto_height && height != 0)
			{
				if (options.auto_height_parent)
				{
					$slider_items.parent('div.' + options.wrapper_class).parent().height(height);
				} else
				{
					$slider_items.parent('div.' + options.wrapper_class).height(height);
				}
			}
				
			slide_left = function()
			{
				$slider = $(this).data('slider');
				options = $slider.data('options');
				$slider_nav = $slider.children(options.nav);
				$slider_items = $slider.children('div.' + options.wrapper_class).children(options.items);				
				$slider_items_content = $slider.children(options.items);
				
				if ($slider_items.is(':not(:animated)'))
				{
					start_index = $slider_items.data('last-index') - options.visible - 1;
					height = 0;
					
					for (i = start_index; i > start_index - options.slide ; i--)
					{
						if (i < 0)
						{
							index = $slider_items_content.children('li').length + i;
						} else
						{
							index = i;
						}
		
						$slider_items.prepend($slider_items_content.children('li').eq(index).clone());
						
						if (options.auto_height)
						{
							if ($slider_items.children('li').first().outerHeight(true))
							{
								height = $slider_items.children('li').first().outerHeight(true);
							}
						}
						
						$slider_items.data('last-index', index + options.visible);
					}
						
					$slider_items.css('left', -$slider_items.children('li').outerWidth(true) * options.slide);
						
					if (options.fade)
					{
						for (i = $slider_items.children('li').length - 1; i > $slider_items.children('li').length - options.slide - 1; i--)
						{
							$slider_items.children('li').eq(i).fadeOut(options.speed);
						}
					}
					
					if (options.auto_height && height != 0)
					{
						if (options.auto_height_parent)
						{
							$slider_items.parent('div.' + options.wrapper_class).parent().animate
							({
								'height': height
							}, options.speed);
						} else
						{
							$slider_items.parent('div.' + options.wrapper_class).animate
							({
								'height': height
							}, options.speed);
						}
					}
						
					$slider_items.animate
					({
						'left': '+=' + $slider_items.children('li').outerWidth(true) * options.slide
					}, options.speed, options.easing, function()
					{
						for (i = 0; i < options.slide; i++)
						{
							$slider_items.children('li').last().remove();
						}
					});
				}
					
				return false;
			};
			
			slide_right = function()
			{
				$slider = $(this).data('slider');
				options = $slider.data('options');
				$slider_nav = $slider.children(options.nav);
				$slider_items = $slider.children('div.' + options.wrapper_class).children(options.items);
				$slider_items_content = $slider.children(options.items);
				
				if ($slider_items.is(':not(:animated)'))
				{
					start_index = $slider_items.data('last-index');
					height = 0;
					
					for (i = start_index; i < start_index + options.slide ; i++)
					{
						if (i >= $slider_items_content.children('li').length)
						{
							index = i - $slider_items_content.children('li').length;
						} else
						{
							index = i;
						}
						
						$slider_items.append($slider_items_content.children('li').eq(index).clone());
						
						if (options.auto_height)
						{
							if ($slider_items.children('li').last().outerHeight(true))
							{
								height = $slider_items.children('li').last().outerHeight(true);
							}
						}
						
						$slider_items.data('last-index', index + 1);
					}
						
					if (options.fade)
					{
						for (i = 0; i < options.slide; i++)
						{
							$slider_items.children('li').eq(i).fadeOut(options.speed);
						}
					}
					
					if (options.auto_height && height != 0)
					{
						if (options.auto_height_parent)
						{
							$slider_items.parent('div.' + options.wrapper_class).parent().animate
							({
								'height': height
							}, options.speed);
						} else
						{
							$slider_items.parent('div.' + options.wrapper_class).animate
							({
								'height': height
							}, options.speed);
						}
					}
						
					$slider_items.animate
					({
						'left': '-=' + $slider_items.children('li').outerWidth(true) * options.slide
					}, options.speed, options.easing, function()
					{
						$slider_items.css('left', 0);

						for (i = 0; i < options.slide; i++)
						{
							$slider_items.children('li').first().remove();
						}
					});
				}
					
				return false;
			};
			
			$slider.data('options', options);
			$slider_nav.children('li.prev').data('slider', $slider).click(slide_left);
			$slider_nav.children('li.next').data('slider', $slider).click(slide_right);
				
			if (options.auto_slide)
			{
				auto_timeout = setTimeout( function()
				{
					setInterval( function()
					{
						if (options.auto_direction == 'left')
						{
							$slider.children(options.nav).children('li.prev').click();
						} else
						{
							$slider.children(options.nav).children('li.next').click();
						}
					}, options.auto_delay);
					
					clearTimeout(auto_timeout);
				}, options.auto_delay);
			}
		});
	
		return this;
	}
})(jQuery);

