(function ($) { 
	
	$.fn.slider = function(options) {
		var defaults = {
			speed : 1000,
			pause : 2000,
			small_w : 62,
			small_h : 65,
			big_w : 652, /* максимум 632 */
			big_h : 389,
			changeText :true
		},
		
		options = $.extend(defaults, options);
		
		options.small_w += 2; // расстояние между миниатюрами

		if (options.pause <= options.speed) 
			options.pause = options.speed + 1000;
			
		return this.each(function () {

			var $this = $(this),
				list = $this.children('#s-wrap'),
				big = $this.children('#current'),
				middle = Math.floor(list.children().length / 2);
				
			$this.show();
			list.width(options.small_w * (list.children().length - 1 ) + options.big_w);
			list.height(options.big_h);
			
			big.css({ 
				'left' : Math.floor(middle) * options.small_w
			});
			big.children('#big').attr('src', list.children().eq(middle).children().first().attr('rel'));
			big.children('#big-image').attr('href', list.children().eq(middle).children().first().attr('rel'));
			
			var is_loading = false,
				is_clicked = false,
				end_animation = true,
				pause_timing = 150,
				click_speed = 300,
				animation_speed,
				to_pic= '',
				click_interval,
				move_interval;
			
			list.delegate('li','click',clickandmove);
			
			fixPos();
			move();
			
			function clickandmove(evt) {
				evt.stopPropagation();
				evt.preventDefault();
				if (is_clicked) return;
				
				is_clicked = true;
				
				var liEl = $(this),
					indexEl = liEl.index();
				to_pic = liEl.find('img:first').attr('src');
				clearInterval(move_interval);
				
				click_interval = setInterval(
					function() {
						if (indexEl > middle) {
							moving(to_pic, click_speed);
						} else {
							movingRight(to_pic, click_speed);	
						}
					}, pause_timing);
			}
			
			function moving(target, to_speed) {
				if (is_loading || !end_animation) return;
				end_animation = false;
				list.children().first().clone().insertAfter(list.children().last()).css({
					'left' : list.width(),
					'opacity' : 0,
					'top' : 128
				});

				list.children().first().animate(
					{
						'opacity' : 0,
						'left' : '-=' + options.small_w,
						'top' : '+=7'
					}, 
					to_speed,
					function () {
						list.children().first().remove();
						big.children('#big').animate(
							{
								'opacity' : 0
							},
							to_speed / 3,
							function() { 
								is_loading = true;
								var img = new Image();
								var imgsrc = list.children().eq(middle).children().first().attr('rel');
								img.onload = function() {
									big.children('#big').attr('src', imgsrc);
									big.children('#big-image').attr('href', imgsrc);
									big.children('#big').animate(
										{
											'opacity' : 1
										},
										to_speed / 3
									);
									var myelm = list.children().eq(middle).children().first().parent();
									$('.frontentTitle a').text(myelm.find('.fTitle').val()).attr('href', myelm.find('.openSubPage').attr('alt'));
									$('.frontentBody').html(myelm.find('.fBody').html());
									$('.frontentDate').html(myelm.find('.fDate').html());
									$('.frontentHref').html('<a href="'+myelm.find('.openSubPage').attr('alt')+'"><img src="http://voices-festival.fr/img/goSlider.jpg" /></a>');
									Cufon.refresh();
									
									is_loading = false;
									end_animation = true;
									if (target && myelm.find('img:first').attr('src') == target) {
										is_clicked = false;
										clearInterval(click_interval);
										move_interval = setInterval(
											function() {
												moving('', options.speed)	
											}, options.pause);
									}
								}
								img.src = imgsrc
							}
						);
					}
				);
				

				list.children().last().animate({'opacity':1},{duration:to_speed,queue:false});
				list.children().eq(middle).animate({'opacity':1},{duration:to_speed,queue:false});
				list.children().eq(middle).next().animate({'opacity':0},{duration:to_speed,queue:false});
				
				list.children().animate(
					{
						'left' : '-=' + options.small_w,
						'top' : '+=' + 6.5
					},
					to_speed,
					function (){
						fixPos();
					}
				);
			}
			
			function movingRight(target, to_speed) {
				if (is_loading || !end_animation) return;
				end_animation = false;
				list.children().last().clone().insertBefore(list.children().first()).css({
					'left' : -options.small_w,
					'opacity' : 0,
					'top' : 206.5
				});

				list.children().last().animate(
					{
						'opacity' : 0,
						'left' : '+=' + options.small_w,
						'top' : '-=7'
					}, 
					to_speed,
					function () {
						list.children().last().remove();
						big.children('#big').animate(
							{
								'opacity' : 0
							},
							to_speed / 3,
							function() { 
								is_loading = true;
								var img = new Image();
								var imgsrc = list.children().eq(middle).children().first().attr('rel');
								img.onload = function() {
									big.children('#big').attr('src', imgsrc);
									big.children('#big-image').attr('href', imgsrc);
									big.children('#big').animate(
										{
											'opacity' : 1
										},
										to_speed / 3
									);
									var myelm = list.children().eq(middle).children().first().parent();
									$('.frontentTitle a').text(myelm.find('.fTitle').val()).attr('href', myelm.find('.openSubPage').attr('alt'));
									$('.frontentBody').html(myelm.find('.fBody').html());
									$('.frontentDate').html(myelm.find('.fDate').html());
									$('.frontentHref').html('<a href="'+myelm.find('.openSubPage').attr('alt')+'"><img src="http://voices-festival.fr/img/goSlider.jpg" /></a>');
									Cufon.refresh();
									
									is_loading = false;
									end_animation = true;
									if (target && myelm.find('img:first').attr('src') == target) {
										is_clicked = false;
										clearInterval(click_interval);
//										move_interval = setInterval(
//											function() {
//												moving('', options.speed)	
//											}, options.pause);
									}
								}
								img.src = imgsrc
							}
						);
					}
				);
				
				list.children().first().animate({'opacity':1},{duration:to_speed,queue:false});
				list.children().eq(middle).animate({'opacity':1},{duration:to_speed,queue:false});
				list.children().eq(middle).next().css({'opacity':1})
				
				list.children().animate(
					{
						'left' : '+=' + options.small_w,
						'top' : '-=' + 6.5
					},
					to_speed,
					function (){
						fixPos();
					}
				);
			}
			
			function move() {
				move_interval = setInterval(
					function() {
						moving(undefined, options.speed)	
					}, options.pause);
			}
/**/
			function fixPos(){
				for(var i = 0; i <= list.children().length - 1; i++) {
					if ( i <= middle ) {
						list.children().eq(i).css({
							'left' : options.small_w * i,
							'top' : 200 - i * 6
						});
					} else {
						list.children().eq(i).css({
							'left' : options.small_w * (i - 1) + options.big_w,
							'top' : 170 - i * 6
						});
					}
				}

				list.children().eq(middle).css( { 
					'opacity' : 0,
					'top' : 182
				} );
				
			}
		}); //end of each
		
	} // end of plugin
	
}) (jQuery);

