jQuery(document).ready(function(){
	initGallery();
});
function initGallery(){
	$('div.carousel3, .slider-holder').gallSlide({
		duration: 700,
		quantity: 1,
		autoSlide: 4000
	});
};
jQuery.fn.gallSlide = function(_options){
	// defaults options
	var _options = jQuery.extend({
		duration: 700,
		quantity: 1,
		autoSlide: 8000
	},_options);

	return this.each(function(){
		var _hold = $(this);
		var _speed = _options.duration;
		var _duration = _options.autoSlide;
		var _num = _options.quantity;
		var _holder = _hold;
		var _slide = _hold.find('ul.slider');
		var _list = _slide.find('>li');
		var _prev = _hold.find('a.link-prev');
		var _next = _hold.find('a.link-next');
		var _holder = _hold.find('.holder')
	
		var _d = _list.eq(0).outerWidth(true);
		var _x=0;
		var _wh = _holder.width();
		var _ws = _list.length*_d;
		var _margin = parseInt(_list.eq(0).css('marginRight'));
		var _vis = Math.ceil(_hold.width()/_d);
		var _wp2 = _vis*_d;
		for	(var i=0; i < _vis; i++){
			_list.eq(i).clone().appendTo(_slide);
		};
	/*--------CREATING THUMBNAILS----------*/
		var _num = $('.switcher').empty();
		_list.each(function(i){
			$('<li><a href="#">'+(i+1)+'</a></li>').appendTo(_num);
		});
	/*-------------------------------------*/
		var _thumb = _hold.find('.switcher li');	
		var _a = _list.index(_list.filter('.active:eq(0)'));
		if(_a == -1) {
			_a = 0; 
			_thumb.eq(_a).addClass('active');
			_list.eq(_a).addClass('active');
		}
		var _new, _old = _a, _t, _t1;
		Run(_a,_x);
		function Run(_a){
			_t = setTimeout(function(){
				if (_x<_ws){
					_x = _x + _d;
					_a++
					if (_a==_list.length){_a=0}
				}else {
					_x = _d; 
					_a = 1;
				};
				Slide(_a,_x);
			}, _duration);
		};
		function Slide(_new,_x){
			_slide.animate({marginLeft: -_x}, {queue:false, duration:_speed, complete:function(){
				_list.eq(_old).removeClass('active');
				_thumb.eq(_old).removeClass('active');
				_list.eq(_new).addClass('active');
				_thumb.eq(_new).addClass('active');
				if (_new == 0){_slide.css({marginLeft:0}); }
				_old=_new;_a=_new;
			}});
			if(_t) clearTimeout(_t);
			Run(_new,_x);
		};
		_thumb.each(function(j){
			$(this).click(function(){
				_x = (j)*_d;
				_a = j;
				Slide(_a,_x)
				return false;
			});
		});
		_next.click(function(){
			if (_x<_ws){
				_x = _x + _d;
				_a++
				if (_a==_list.length){_a=0}
			}else {
				_x = _d; 
				_a = 1;
			};
			Slide(_a,_x);
			return false;
		});
		_prev.click(function(){
			if (_x>0){
					_x = _x - _d;
					_a--
					if (_a==-1){_a=_list.length - 1};
				}else {
					_slide.css({marginLeft:-_ws});
					_x = _ws-_d;
					_a=_list.length - 1;
				};
			Slide(_a,_x);
			return false;
		});
	});
}
