/*
 * jquery.circleGalleryScroll.1.0
 */

/*
	************* OPTIONS ************************************** default ****************
	btPrev         - link for previos [selector]    	btPrev: 'a.link-prev'
	btNext         - link for next [selector]		btNext: 'a.link-next'
	holderList     - image list holder [Tag name]		holderList: 'div'
	scrollElParent - list [Tag name]			scrollElParent: 'ul'
	scrollEl       - list element [Tag name]		scrollEl: 'li'
	duration       - duration slide [1000 - 1sec]		duration : 1000
	step           - slide step [int]			step: false
	autoSlide      - auto slide [1000 - 1sec]               autoSlide:false
	*************************************************************************************
*/
jQuery.fn.circleGalleryScroll = function(_options){
	// defaults options	
	var _options = jQuery.extend({
		btPrev: 'a.link-prev',
		btNext: 'a.link-next',
		holderList: 'div',
		scrollElParent: 'ul',
		scrollEl: 'li',
		duration : 1000,
		step: false,
		autoSlide:false
	},_options);

	return this.each(function(){
		var _this = jQuery(this);

		var _holderBlock = jQuery(_options.holderList,_this);
		var _gWidth = _holderBlock.width();
		var _animatedBlock = jQuery(_options.scrollElParent,_holderBlock);
		var _items = jQuery(_options.scrollEl,_animatedBlock);
		var _liWidth = jQuery(_options.scrollEl,_animatedBlock).outerWidth(true);
		var _liSum = jQuery(_options.scrollEl,_animatedBlock).length * _liWidth;
		var _margin = _liSum;
		var _f = 0;
		var _step = 0;
		var _autoSlide = _options.autoSlide;
		var _t = null;
		if (!_options.step) _step = _gWidth; else _step = _options.step*_liWidth;

		_animatedBlock.append(_items.clone(true));
		_animatedBlock.prepend(_items.clone(true));
		_animatedBlock.css({marginLeft: -_margin+"px"});

		//autoslide
		if (_options.autoSlide) {
			_t = setTimeout(function(){
				jQuery(_options.btNext,_this).trigger('click');
			},_options.autoSlide)
		}

		//Next button click
		jQuery(_options.btNext,_this).bind('click',function(){
			if (!_f) {
				_f = true;
				if (_t) clearTimeout(_t);
				_margin = parseInt(_animatedBlock.css('marginLeft'));
				if (_margin-_step < -_liSum*2) {
					_margin = -_liSum - _step;
					_animatedBlock.css({marginLeft: -_liSum+"px"}).animate({marginLeft: _margin}, _options.duration , function(){
						_f = false;
						if (_options.autoSlide) {
							setTimeout(function(){
								jQuery(_options.btNext,_this).trigger('click');
							},_options.autoSlide)
						}
					});
				} else {
					_margin -= _step;
					_animatedBlock.animate({marginLeft: _margin+"px"}, _options.duration , function(){
						_f = false;
						if (_options.autoSlide) {
							setTimeout(function(){
								jQuery(_options.btNext,_this).trigger('click');
							},_options.autoSlide)
						}
					});
				}
			}
			return false;
		})

		//Prev button click
		jQuery(_options.btPrev,_this).bind('click',function(){
			if (!_f) {
				_f = true;
				if (_t) clearTimeout(_t);
				_margin = parseInt(_animatedBlock.css('marginLeft'));
				if (_margin + _step > 0) {
					_margin = -_liSum + _step;
					_animatedBlock.css({marginLeft: -_liSum+"px"}).animate({marginLeft: _margin}, _options.duration , function(){
						_f = false;
						if (_options.autoSlide) {
							setTimeout(function(){
								jQuery(_options.btPrev,_this).trigger('click');
							},_options.autoSlide)
						}
					});
				} else {
					_margin += _step;
					_animatedBlock.animate({marginLeft: _margin+"px"}, _options.duration , function(){
						_f = false;
						if (_options.autoSlide) {
							setTimeout(function(){
								jQuery(_options.btPrev,_this).trigger('click');
							},_options.autoSlide)
						}
					});
				}
			}
			return false;
		});
	});
}

