	/**
	 * @author
	 */

 
	$(document).ready(function() {
		
		var start = 0;
		var value = 0;
		var offset = 0;
		var diff = 0;
		
		// Вся ширина
		//var width = Math.round($('#slide').css("width").replace('px',''));
		//width = document.getElementById('slide').clientWidth;
		//alert(width);
		
		/*
		$('#start').html(start);
		$('#value').html(value);
		$('#offset').html(offset);
		$('#diff').html(diff);
		*/
		
		$("#slider").slider();
		
		$("#slide").css({"right":"0px"});

		$('#slider').bind('slide', function(event, ui) {
			
			width = document.getElementById('slide').clientWidth;
			visible_width = document.getElementById('pictures').clientWidth;
			
			$('#sliderContent').css({ width: width });
			
			//$('#_width').html(width);
			
			value = $('#slider').slider('option', 'value');

			//$('#_value').html(value);
			
			// На сколько сдвинуть картинки
			offset = Math.round( (width - visible_width) * (value/100) );

			//$('#_offset').html(offset);
			
			// Разница между начальным положением и текущим
			diff = value - start;
			
			//$('#_start').html(start);
			
			tmp = $("#slide").css("right");
			tmp = tmp.replace('px','');
			right = Math.round(tmp);
			//$('#_right').html(right);
			
			//$('#_diff').html(diff);
			
			$("#slide").animate({
				"right": + offset + "px"
			}, 0);
		});
		
		$('#slider').bind('slidestart', function(event, ui) {
						
			start = $('#slider').slider('option', 'value');
			
			//$('#_start').html(start);
			
		});
		
		
		$('img[id^=img]').hover(
			function() {
				$(this).animate( $(this).css({opacity: "1"}), 500 );
			},
			function() {
				if ( $(this).hasClass('iActive') ) {
					//
				} else {
					$(this).animate( $(this).css({opacity: "0.3"}), 500 )
				}
			}
		);
		
		
	});
	
	var imgId = '';
	
	
	function loadPreview(id) {
		
		active_id = 'img' + id;
		
		$('.slideImg').each( function (i) {
			
			$(this).removeClass('iActive');
			
			if ( active_id == this.id ) {
				this.style.opacity = "1";
			} else {
				this.style.opacity = "0.3";
			}
		});
		
		$('#' + active_id).addClass('iActive');
		
		imgId = 'imgLarge'+id;
		
		// Подгружаем большую фотку
		$('.previewImg').css({ display: "none" });
		$('.previewImg').attr({
			src: 'pictures/large/'+id+'.jpg'
		});
		
		$('.previewImg').attr({ id: id });
	
		
		$('.loading').css({ display: "block" });
		
		$('.previewImg').load(function() {
			$('.loading').css({ display: "none" });
			$('.previewImg').fadeIn("slow");
		});
		
		// Подгружаем текст
		previewText = $('#img'+id).attr("text");
		$('#previewText').html(previewText);
	}
	
	
	function showNext(cur_id) {
		
		// Пробегаемся по всем картинкам, номер текущей активной превьюшки
		
		j = 0;
		
		$('.slideImg').each( function (i) {
			
			j++;
			
			preview_id = $(this).attr('id').replace('img','');
			
			if ( cur_id == preview_id ) {
				cur_pos = j;
			}
		});
		
		j = 0;
		
		$('.slideImg').each( function(i) {
			
			j++;
			
			// На всякий случай запоминаем id первой картинки
			
			if ( 1 == j ) {
				f_id = $(this).attr('id').replace('img','');
			}
			
			if ( ( (cur_pos + 1) == j ) ) {
				
				t_id = $(this).attr('id').replace('img','');
				
				//$('#_length').html($('.slideImg').length);
				//$('#_j').html(j);
				
				//if ( $('.slideImg').length == j ) {
				//	loadPreview(f_id);
				//} else {
					loadPreview(t_id);
				//}
				
			}
			
		});
		
			// Двигаем слайдер
			
			width = document.getElementById('slide').clientWidth;
			visible_width = document.getElementById('pictures').clientWidth;
			
			// Определяем на сколько нужно сдвинуть слайдер
			
			o_left = document.getElementById('img'+t_id).offsetLeft;
			o_width = document.getElementById('img'+t_id).clientWidth;
			
			if ( (o_left > visible_width/2) ) {
			
				offset = o_left - visible_width/2 + o_width/2;
				
				if ( offset + visible_width > width ) {
					offset = width - visible_width;
				}
				
				//s_right = document.getElementById('slide').offsetLeft + document.getElementById('slide').clientWidth - visible_width - o_width;
				
				//dummy = 100 * offset / (width - visible_width);
				
				$("#slide").animate({
					"right": + offset + "px"
				}, 700 );
				
				//$('#_offset').html(offset);
				//$('#_width').html(width);
				//$('#_dummy').html(dummy);
				
				value = Math.round(100 * offset / (width - visible_width));
				
				$('#slider').slider('option', 'value', value);
			}
		
	}
