$(document).ready(function(){
	initGallery();
});
function initGallery(){
	$('.gallery').Gallery({
		speed: 300,
		autoRotation: 5000,
		slider: 'ul',
		imagesList:'.visual .gal-item'
	});
};

jQuery.fn.Gallery = function(_options){
	// default options
	var _options = jQuery.extend({
		speed: 1200,
		autoRotation: 4000,
		slider: 'ul.slider',
		prev: '.prev, .link-prev, .btn-prev',
		next: '.next, .link-next, .btn-next',
		pager: 'ul.switcher',
		dinamicPagination: true,
		vertical: false,
		infinitive: true,
		stopOnHover: true,
		play: '.play',
		pause: '.pause'
	},_options);
	
	return this.each(function(){
		// options
		var _hold = jQuery(this);
		var _speed = _options.speed;
		var _autoRotation = _options.autoRotation;
		var _slider = _hold.find(_options.slider);
		var _holder = _slider.parent();
		var _list = _slider.children();
		var _imagesList = $(_options.imagesList);
		var _prev = _hold.find(_options.prev);
		var _next = _hold.find(_options.next);
		var _pause = _hold.find(_options.pause);
		var _play = _hold.find(_options.play);
		var _vert = _options.vertical;
		var _inf = _options.infinitive;
		var _stop = _options.stopOnHover;
		var _f = true;
		var _p = _options.dinamicPagination;
		var _d = _list.eq(0).outerWidth(true);
		
		var _a = 5;
		_list.removeClass('active');
		var _max = _list.length;
/*--------ADDING SLIDES----------*/
		var _vis = Math.ceil(_holder.width()/_d);
		if (_inf) {
			for	(var i=0; i < _vis; i++){
				_list.eq(i).clone().appendTo(_slider);
			};
		};
		_list = _slider.children();
/*--------CREATING THUMBNAILS----------*/
		var _num = _hold.find(_options.pager);
		if(_p){	_num.empty();
			_list.each(function(i){
				$('<li><a href="#">'+(i+1)+'</a></li>').appendTo(_num);
			});	};
		var _thumb = _num.find('li');
/*-------------------------------------*/
		_thumb.removeClass('active').eq(_a+1).addClass('active');
		_list.eq(_a+1).addClass('active');
		_slider.css({left:-_a*_d});
		var _ie = 9;
		if (jQuery.browser.msie && jQuery.browser.version < _ie){		_imagesList.hide().eq(_a).show();
		}else{		_imagesList.show().css({opacity:0}).eq(_a).css({opacity:1});	}
		
		
		var _x=0, _new, _t;

		if (_f){ if (_autoRotation) Run(_a);}
		function Run(_a){
			_t = setTimeout(function(){
				if (_inf) {
					if (_a < _max){_a++
					}else {	_a = 1;	};
				}else{
					if (_a < _max-1){_a++
					}else {	_a = 0;	};
				}
				Slide(_a);
			}, _autoRotation);
		};
		function Slide(_new){
			_x = (_new) * _d;
			_a = _new;
			_list.removeClass('active').eq(_new+1).addClass('active');
			if (_new != _max){
				if(jQuery.browser.msie && jQuery.browser.version < _ie){
					_imagesList.removeClass('active').hide().eq(_new).addClass('active').show();
				}else{
					_imagesList.removeClass('active').animate({opacity:0}, {queue:false, duration:_speed});
					_imagesList.eq(_new).addClass('active').animate({opacity:1}, {queue:false, duration:_speed});
				}
			}
			_thumb.removeClass('active').eq(_new+1).addClass('active');
			_slider.animate({left: -_x}, {queue:false, duration:_speed, easing:'linear', complete:function(){
				if (_inf) {
					if (_new == _max){
						_slider.css({left:0});
						_new=0;
						_a = 0;
						_list.removeClass('active').eq(_new+1).addClass('active');
						if(jQuery.browser.msie && jQuery.browser.version < _ie){
							_imagesList.removeClass('active').hide().eq(_new).addClass('active').show();
						}else{
							_imagesList.removeClass('active').animate({opacity:0}, {queue:false, duration:_speed});
							_imagesList.eq(_new).addClass('active').animate({opacity:1}, {queue:false, duration:_speed});
						}
					}
				}
			}});
			if(_t) clearTimeout(_t);
			if (_autoRotation) Run(_a);
		};
		_list.mouseenter(function(){
			$(this).addClass('hovered');
		}).mouseleave(function(){
			$(this).removeClass('hovered');
		}).click(function(){
			_a = _list.index($(this))-1;
			if (_a >= _max) _a = ( _a-_max)
			if (_a == -1){
				_a = ( _a+_max)
				_slider.css({left:-_max*_d});
			}
			Slide(_a);
			return false;
		})

		_next.click(function(){
			if (_inf) {
				if (_a < _max){_a++
				}else {	_a = 1;	};
			}else{
				if (_a < _max-1){_a++
				}else {	_a = 0;	};
			}
			Slide(_a);
			return false;
		});
		
		_prev.click(function(){
			if (_a>0){_a--
			}else {
				if (_inf) {
					 _slider.css({left:-_max*_d});
				}
				_a = _max - 1;
			};
			Slide(_a);
			return false;
		});
	});
};
