/*
 * 	Easy Slider - jQuery plugin
 *	written by Alen Grakalic	
 *	http://cssglobe.com/post/3783/jquery-plugin-easy-image-or-content-slider
 *
 *	Copyright (c) 2009 Alen Grakalic (http://cssglobe.com)
 *	Dual licensed under the MIT (MIT-LICENSE.txt)
 *	and GPL (GPL-LICENSE.txt) licenses.
 *
 *	Built for jQuery library
 *	http://jquery.com
 *
 */
 
/*
 *	markup example for $("#images").easySlider();
 *	
 * 	<div id="images">
 *		<ul>
 *			<li><img src="images/01.jpg" alt="" /></li>
 *			<li><img src="images/02.jpg" alt="" /></li>
 *			<li><img src="images/03.jpg" alt="" /></li>
 *			<li><img src="images/04.jpg" alt="" /></li>
 *			<li><img src="images/05.jpg" alt="" /></li>
 *		</ul>
 *	</div>
 *
 */

(function($) {

	$.fn.easySlider = function(options){
	  
		// default configuration properties
		var defaults = {
			action: 		'slide',
			prevId: 		'prevBtn',
			prevText: 		'',
			nextId: 		'nextBtn',	
			nextText: 		'',
			orientation:	'', //  'vertical' is optional;
			speed: 			800,
			time:			5000,
			timeStartAgain:	30000
		}; 
		
		var options = $.extend(defaults, options);  
		
		return this.each(function() {  
			obj = $(this); 				
			var s = $("li", obj).length;
			var w = obj.find('li').width(); 
			var h = obj.height(); 
			var ts = s-1;
			var t = 0;
			var timer;
			var vertical = (options.orientation == 'vertical');
			$("ul", obj).css('width',s*w);
			if(!vertical) $("li", obj).css('float','left');
			$("a#"+options.nextId).click(function(){		
				stopTimer(timer);
				animate("next");
				$("a#"+options.prevId).fadeIn();
			});
			$("a#"+options.prevId).click(function(){		
				stopTimer(timer);
				animate("prev");
				$("a#"+options.nextId).fadeIn();
			});
			$('a.navPoints').each(function(i){				
				var count = $('.navPoints').size() - 1;
				$(this).click(function(){
					stopTimer(timer);
					t = i;
					animate('open');
				});
			})			
			
			function stopTimer(){
				clearTimeout(timer);
				timer = setTimeout(function(){
					setTimerAction(options.time);
				},options.timeStartAgain);
			};
			
			function setTimerAction(time){
				animate('next');
				timer = setTimeout(function(){
					setTimerAction(time);
				},time);
			};
			timer = setTimeout(function(){
				setTimerAction(options.time);
			},options.time);
			
			function animate(dir){
				
				var pt = 0;

				if(dir == "next"){
					t = (t>=ts) ? 0 : t+1;	
				} else if(dir == "open"){
					t = t;
				} else{
					t = (t<=0) ? ts : t-1;
				};
				
				if(!vertical) {
					if (options.action == 'slide') {
						p = (t*w*-1);
						$('.navPoints.active').removeClass('active');
						$('#pointNav_'+t).addClass('active');
						$("ul",obj).animate(
							{ marginLeft: p }, 
							options.speed
						);
					} else {
						p = t;
						
						$('a.navPoints').each(function(i) {
							if ($(this).hasClass('active'))
								pt = i;
						});

						$('.navPoints.active').removeClass('active');						
						$('#pointNav_'+t).addClass('active');
						
						$(obj).find('li').each(function(i) {
							
							if (i == p)
								$(this).fadeIn(options.speed);
							else if (i == pt)
								$(this).fadeOut(options.speed);

						});
						
					}
				} else {
					p = (t*h*-1);
					$("ul",obj).animate(
						{ marginTop: p }, 
						options.speed
					);					
				}
			};
			if(s>1) $("a#"+options.nextId).fadeIn();
			if (s <= 1) $('.navBlock').hide();
		});
	  
	};

})(jQuery);
