﻿/*
 * 	Video Slider - jQuery plugin
 *	written by Vasiliy Nesterenko
 *	vasiliy.v.nesterenko@gmail.com
 *	
 *	Idea: Alen Grakalic
 *	http://cssglobe.com/post/3783/jquery-plugin-easy-image-or-content-slider
 *
 *	Built for jQuery library
 *	http://jquery.com
 *
 */
 
/*
 *	markup example for $("#video-list").slidernah();
 *	
 * 	<div id="video-list">
 *		<ul>
 *			<li>
 *				<img src="images/01.jpg" alt="image" />
 *				<div class="content">
 *					<h3><a href="#">Cool Video Name</a></h3>
 *					<div class="views">Views: <strong>3541</strong></div>
 *				</div>
 *			</li>
 *			<li>
 *				<img src="images/02.jpg" alt="image" />
 *				<div class="content">
 *					<h3><a href="#">Cool Video Name</a></h3>
 *					<div class="views">Views: <strong>3541</strong></div>
 *				</div>
 *			</li>
 *			<li>
 *				<img src="images/03.jpg" alt="image" />
 *				<div class="content">
 *					<h3><a href="#">Cool Video Name</a></h3>
 *					<div class="views">Views: <strong>3541</strong></div>
 *				</div>
 *			</li>
 *			<li>
 *				<img src="images/04jpg" alt="image" />
 *				<div class="content">
 *					<h3><a href="#">Cool Video Name</a></h3>
 *					<div class="views">Views: <strong>3541</strong></div>
 *				</div>
 *			</li>
 *		</ul>
 *	</div>
 *
 */

(function($) {

	$.fn.slidernah = function(options){
	  
		// default configuration properties
		var defaults = {
			prevId: 'slide-left',
			nextId: 'slide-right',
			itemsPerPage: 3,
			startToGet: 2,
			url: '.',
			speed: 400
		}; 
		
		var options = $.extend(defaults, options);  
		
		return this.each(function() {  
			var container = $(this);
			var visibleArea = container.width();
			
			var list = $("ul:first", container); 
			var countItems = $("li", list).length;
			var itemWidth = $("li:first", list).outerWidth();
			var listWidth = countItems * itemWidth;
			var position = 0;
			var nothingToGet = false;
			
			container.css('overflow', 'hidden');
			list.css('width', listWidth);
			
			$("#"+options.nextId).click(function(){
				animate("next");
				return false;
			});
			
			$("#"+options.prevId).click(function(){
				animate("prev");
				return false;
			});
			
			function animate(dir){
				if (dir == "next"){
					if (position < countItems - options.itemsPerPage)
						position += 1;
					if (countItems - options.itemsPerPage - position < options.startToGet){
						getNext();
					}
				} else {
					position = (position > 0) ? position -= 1 : position;
				};
				
				list.animate({ marginLeft: position * itemWidth * -1 }, options.speed);
			}
			
			function getNext(){
				if (!nothingToGet){
					$.get(options.url, {
						skip: countItems
					},
					function(data){
						list.append(data);
						var oldCountItems = countItems;
						countItems = $("li", list).length;
						listWidth = countItems * itemWidth;
						list.css('width', listWidth);
						
						if (oldCountItems == countItems){
							nothingToGet = true;
						}
					});
				}
			}
		});
	};

})(jQuery);
