
jQuery.fn.galleryCircle = function(_options){
	// defaults options
	var _options = jQuery.extend({
		btPrev: 'a.prev',
		btNext: 'a.next',
		scrollElParent: '.gallery-wrapper div.gallery',
		scrollElHolder: '.gallery-wrapper ul',
		scrollEl: '.gallery-wrapper ul > li',
		slideNum: '.slide-num',
		duration : 1500,
		circleSlide: true,
		autoSlide: 4000,
		step: false,					/*not done*/
		disableClass: 'disable',		/*not done*/
		funcOnclick: null,				/*not done*/
		innerMargin:0,					/*not done*/
		stepWidth:false,				/*not done*/
        controlsStyle: 'A'
	},_options);
	if(jQuery(_options.scrollEl).length <= 1) return;
	
	return this.each(function(){

	var _this = jQuery(this);

    if ( _options.controlsStyle == 'A' ) {
        var controls = jQuery( '<div class="controlbar">'
            + '<span class="left">&nbsp;</span>'
            + '<div class="middle"><a class="prev" href="javascript:void(0);">prev</a><div class="slide-num"></div>'
            + '<a class="next" href="javascript:void(0);">next</a></div>'
            + '<span class="right">&nbsp;</span></div>' )
        _this.data('controls', controls)
        _this.prepend( controls )
    }
    else if ( _options.controlsStyle == 'B' ) {
        var controls = jQuery('<div class="bigcontrolbar"><a class="prev" href="javascript:void(0);">prev</a><a class="next" href="javascript:void(0);">next</a></div>')
        _this.data('controls', controls)
        _this.before( controls )
    }
    else if ( _options.controlsStyle == 'C' ) {
        var controls = jQuery('<a class="controlballs prev" href="javascript:void(0);">prev</a><a class="controlballs next" href="javascript:void(0);">next</a>')
        _this.data('controls', controls)
        _this.append( controls )
    }

	var _autoSlide = _options.autoSlide;
	var _duration = _options.duration;
	var _circleSlide = jQuery(_options.circleSlide);
	var _scrollElParent = jQuery(_options.scrollElParent, _this);
	var _scrollElHolder = jQuery(_options.scrollElHolder, _this);
	var _scrollEl = jQuery(_options.scrollEl, _this);
	var _slideNum = jQuery(_options.slideNum, _this.parent() );
	var _btPrev = jQuery( _options.btPrev, _this.parent() );
	var _btNext = jQuery( _options.btNext, _this.parent() );
	var _liSum = jQuery(_options.scrollEl,_this).length;
	var _mM = _scrollElHolder.width();
	var marginNow = _mM;
	var _ElWidth = _scrollEl.width();
	var _timerSlide = null;

	// Number list
	jQuery.fn.galleryCircle.numListCreate = function(_elNumList, _liSum){
		var _numListElC = '';
		var _num = 1;
		var _difference = _scrollEl.length;
		
		while(_difference > 0)
		{
			_numListElC += '<li><a href="">'+_num+'</a></li>';
			_num++;
			_difference--;
		}
		jQuery(_elNumList).html('<ul>'+_numListElC+'</ul>');
	};

	//Call Create Num List
	jQuery.fn.galleryCircle.numListCreate(jQuery(_options.slideNum, _this.parent()), _liSum);
	
	//for control element
	var _CurItem =0;
	var _NumLinks = _slideNum.find('a');
	
	//set active on load to num list
	_NumLinks.eq(_CurItem).parents('li').addClass('active');

		//Coppy item list and set start margin
		_scrollElHolder.clone(true).insertAfter(_scrollElHolder);
		_scrollElHolder.clone(true).insertBefore(_scrollElHolder);
		_mM = _scrollElHolder.width();
		_scrollElParent.css('marginLeft', -_mM);
		
		
		
		
		var _AnimatedStep =false;
		//next click

        if ( _options.controlsStyle == 'B' ) {
            jQuery(_btNext).hover(function(){
                jQuery(this).animate( {'margin-right': '-10px'}, 200 )
            }, function(){
                jQuery(this).stop()
                jQuery(this).animate({'margin-right': '0px'}, 200 )
            })
        }

		jQuery(_btNext).bind('click', function(){
			if(!_AnimatedStep) {
				_AnimatedStep = true;
				_scrollElParent.animate({
					'marginLeft':'-=' + parseInt(_scrollEl.width())
				},_duration,function(){
					marginNow = parseInt(_scrollElParent.css('margin-left'));
					_CurItem =Math.abs((marginNow+parseInt(_scrollElHolder.width()))/_scrollEl.width());
					if (_CurItem ==_scrollEl.length) {_CurItem=0;}
					_NumLinks.parents('li').removeClass('active');
					_NumLinks.eq(_CurItem).parents('li').addClass('active');
					if (marginNow <= -2*_scrollElHolder.width()) {
						_scrollElParent.css({'margin-left': -_scrollElHolder.width()});
					}
					_AnimatedStep = false;
				});
			}

			if (_timerSlide) {
				clearTimeout(_timerSlide);
				_timerSlide = setTimeout(function(){
					autoSlide(_options.autoSlide);
				}, _options.autoSlide);
			}
			return false;
		});

		//prev click

        if ( _options.controlsStyle == 'B' ) {
            jQuery(_btPrev).hover( function(){
                jQuery(this).animate( {'margin-left': '-10px'}, 200 )
            }, function(){
                jQuery(this).stop()
                jQuery(this).animate({'margin-left': '0px'}, 200 )
            })
        }

		jQuery(_btPrev).bind('click',function(){
			if(!_AnimatedStep) {
				_AnimatedStep = true;
				_scrollElParent.animate({
					'marginLeft':'+=' + parseInt(_scrollEl.width())
				},_duration,function(){
					marginNow = parseInt(_scrollElParent.css('margin-left'));
					_CurItem =(marginNow+parseInt(_scrollElHolder.width()))/_scrollEl.width();
					if (_CurItem<0) {
						_NumLinks.parents('li').removeClass('active');
						_NumLinks.parents('li').eq(Math.abs(_CurItem)).addClass('active');
					} else if (_CurItem>0) {
						_NumLinks.parents('li').removeClass('active');
						_NumLinks.parents('li').eq(_scrollEl.length-Math.abs(_CurItem)).addClass('active');
					} else if (_CurItem==0) {
						_NumLinks.parents('li').removeClass('active');
						_NumLinks.parents('li').eq(0).addClass('active');
					};
					if (marginNow >= 0) {
						_scrollElParent.css({'margin-left': -_scrollElHolder.width()});
					}
					_AnimatedStep = false;
				});
			}
			return false;
		});
		//Num link Click
		_NumLinks.click(function(){
			var _index = $(_slideNum.find('a')).index(this);
			_mM = _ElWidth*_index + _scrollElHolder.width();
			_scrollElParent.animate({marginLeft: -_mM}, _duration);
			_NumLinks.parents('li').removeClass('active');
			$(this).parents('li').addClass('active');
			return false;
		});
		//autoslide
		if (_autoSlide) {
				_timerSlide = setTimeout(function(){
					autoSlide(_autoSlide);
				}, _autoSlide);
			_scrollElParent.hover(function(){
				clearTimeout(_timerSlide);
			}, function(){
				_timerSlide = setTimeout(function(){
					autoSlide(_autoSlide);
				}, _autoSlide);
			});
		}
		// auto slide
		function autoSlide(autoSlideDuration){
			//if (_options.circleSlide) {
				_btNext.trigger('click');
			//}
		};
	});
};

