/*
 * jQuery Elegant Slider v1.0
 *
 * TERMS OF USE - jQuery Elegant Slider
 * 
 * Copyright © 2011 Fabrice Spee
 * All rights reserved.
 * 
*/


(function($){
 
    $.fn.extend({
         
        //pass the options variable to the function
        elegantcarousel: function(options) {
 
            //Set the default values, use comma to separate the settings, example:
            var defaults = {
																delay:150,
																fade:300,
																slide:500,
																effect:'fade',
																orientation: 'horizontal',
																captionFade: 150,
																loop: false,
																autoplay: true,
																time: 3000
            }
                 
            var options =  $.extend(defaults, options);
 
            return this.each(function() {
                var o = options;
				
								//  get general settings
								var carousel = this;
								$carousel_container = $(carousel).find('.carousel_container');
								$li = $(carousel).find('.carousel_container ul li');
								
								var visible_width 	= parseInt($carousel_container.css('width'), 10);
								var visible_height 	= parseInt($carousel_container.css('height'), 10);
								
								var number_items = $li.size();
								var item_width = parseInt($li.css('width'), 10);
								var item_height = parseInt($li.css('height'), 10);
								var item_marginRight = parseInt($li.css('marginRight'), 10);
								var item_marginLeft = parseInt($li.css('marginLeft'), 10);
								var item_marginTop = parseInt($li.css('marginTop'), 10);
								var item_marginBottom = parseInt($li.css('marginBottom'), 10);
								
								var item_height_total = item_height+item_marginTop+item_marginBottom;
								var item_width_total  = item_width+item_marginRight+item_marginLeft;
	
		 						var curpos = 0;
		 						var move_unit = 1;
		 						var item_num_in_container = 3;
		 						
						
								$float_easing="easeInOutQuart";  //  easing effect
								$li.each(function(index) {
										$(this).css('position','absolute');
										$(this).css('left','0');
										
										if(index < item_num_in_container)
										{
												$(this).css('display','block');
												$(this).css('top',item_height_total * index);												
										}
										else
										{
												$(this).css('display','none');
												$(this).css('top',visible_height);
										}
								});

								
								function slideItems()
								{
										$li.each(function(index) {
												var pos = index - curpos;
												if( pos < 0 ) pos = pos + number_items;
												$(this).css('top',pos * item_height_total);
										});

										$li.each(function(index) {
												var pos = index - curpos;
												if( pos < 0 ) pos = pos + number_items;
												
												if(pos >= item_num_in_container + move_unit) return;
												
												var delay = pos * o.delay;
												$(this).css('display','block');
												
												var top_pos = -1 * item_height_total;
												if(pos >= move_unit) top_pos = (pos-move_unit) * item_height_total; 
												
												$(this).delay(o.delay).animate({top: top_pos}, o.slide, $float_easing);
												
										});
										
		 								curpos = curpos + move_unit - number_items;
		 								if(curpos < 0) curpos = curpos + number_items;
								}
								
								//  Set autoplay if true 
								if (o.autoplay) { 
								   var interval = setInterval(function() {
													slideItems();
								   }, o.time);
								}
								
				// Fade in Caption
			//	$li.hover(function() { clearInterval(interval); $(this).find('.caption').fadeIn(o.captionFade); }, function () { $(this).find('.caption').fadeOut(o.captionFade); });
             
            });
        }
    });
     
})(jQuery);


