jQuery(function(){
	try {
		jQuery('.slideshow').galleryCircle({
		holderList: 'div.gallery-holder',
		scrollElParent: 'ul',
		scrollEl: '> li',
		btPrev: 'a.prev',
		btNext: 'a.next',
		step: true,
		switchTime: false,
		duration : 400
	});
	}
	catch(e){}
})

/*
 * jQuery galleryCircle v1.0.4
 *****************************************************************
	btPrev: 'a.prev',				button prev
	btNext: 'a.next',				button next
	holderList: 'div',				holder of elements window were you see sliding elements !  CSS for this element:	 div {position: relative;	width: "your width" ; 	overflow: hidden;}
	scrollElParent: 'ul',			holder of all sliding elements
	scrollEl: 'li',					element wich would slide
	numHolder: 'div.num-control',	holder of control element
	numCreate: false,				create new num control or use default (wich exist in markup) can be: False or True
	step: false,					boolean false= scroll on window width, true = scroll on each element width
	innerMargin: 0,					if "ul" has some default margin that is not  "0" set it here
	switchTime: false,				time between sliding image: 	false or some integer 1000 = 1 sec
	duration : 1500					time of animation			some integer 1000 = 1 sec
 *****************************************************************
 */

jQuery.fn.galleryCircle = function(_options){
	// defaults options
	var _options = jQuery.extend({
		btPrev: 'a.prev',
		btNext: 'a.next',
		holderList: 'div',
		scrollElParent: 'ul',
		scrollEl: 'li',
		numHolder: false,
		numCreate: false,
		step: false,
		innerMargin: 0,
		curPage: false,
		onClick: null,
		onChangeFunc: null,
		easing: 'linear',
		switchTime: false,
		duration : 1500
	},_options);

	return this.each(function(){
		var _this = jQuery(this);
		var _next = jQuery(_options.btNext, _this).length ? jQuery(_options.btNext, _this) : false;
		var _prev = jQuery(_options.btPrev, _this).length ? jQuery(_options.btPrev, _this) : false;
		var _holderList = jQuery(_options.holderList, _this);
		var _scrollElParent = jQuery(_options.scrollElParent, _holderList);
		var _scrollEl = jQuery(_options.scrollEl, _scrollElParent);
		var _numHolder = false ;
		if (_options.numHolder) _numHolder = jQuery(_options.numHolder, _this).length ? jQuery(_options.numHolder, _this) : false;
		var _step, _t = null;
		var _widthSum = 0;
		_scrollEl.each(function(){_widthSum += jQuery(this).outerWidth(true);})
		var _startPosition = _scrollEl.index(_scrollEl.filter('.active'));
		if (_startPosition==-1) _startPosition=0;
		_scrollEl.removeClass('active');
		var _easing = _options.easing;
		if(_options.switchTime) var _switchTime = _options.switchTime + _options.duration;

		if (!_options.step) _step = _holderList.innerWidth();
		var _margin = _widthSum;
		if(_widthSum > _holderList.innerWidth()){
			_scrollElParent.append(_scrollEl.clone(true));
			_scrollElParent.prepend(_scrollEl.clone(true));

			//auto rotation
			if (_switchTime){
				_t = setTimeout(function(){
					nextSlides();
				},_switchTime)
			}

			//button next "click"
			if (_next) {
				_next.click(function(){
					if (!_scrollElParent.is(':animated')) {
						if (jQuery.isFunction(_options.onClick)) _options.onClick.apply(_this);
						nextSlides();
					}
					return false;
				});
			}

			//button prev "click"
			if (_prev) {
				_prev.click(function(){
					if (!_scrollElParent.is(':animated')) {
						if (jQuery.isFunction(_options.onClick)) _options.onClick.apply(_this);
						prevSlides();
					}
					return false;
				});
			}

			//curent position
			function getCurElIndex(){
				var _curMargin = parseInt(_scrollElParent.css('marginLeft')) + _widthSum - _options.innerMargin;
				for(i=0; i < _scrollEl.length; i++){
					if (_curMargin == 0) return i;
					if (_curMargin <= _options.innerMargin) _curMargin += _scrollEl.eq(i).innerWidth(true);
					else _curMargin -= _scrollEl.eq(i).innerWidth(true);
					if (_curMargin == _options.innerMargin) return i+1;
				}
			}

			// offset of gallery if when activ element not first at start 
			function culcOffset(_ind){
				var _tmpcounter=0;
				var _pos=0;
				while (_tmpcounter < _ind){
					_pos += _scrollEl.eq(_tmpcounter).outerWidth(true);
					_tmpcounter++;
				};
				return _pos;
			}

			var _offsetStartPosition =0;
			_offsetStartPosition = culcOffset(_startPosition);
			_scrollElParent.css('marginLeft', (-_margin+_options.innerMargin-_offsetStartPosition));

			//go next slide
			function nextSlides(){
				if (_t) clearTimeout(_t);
				if (_options.step) {
					_curElIndex = getCurElIndex();
					_step = _scrollEl.eq(_curElIndex).innerWidth(true);
				};
				_margin = -parseInt(_scrollElParent.css('marginLeft'));
				_margin += _step;
				
				_scrollElParent.animate({'marginLeft':(-_margin+_options.innerMargin)}, {duration:_options.duration, easing: _easing, complete:function(){
					if (_margin >= _widthSum*2) {
						_margin = _widthSum + (_margin - _widthSum*2);
					}
					_scrollElParent.css({'marginLeft':-_margin+_options.innerMargin});
					jQuery.fn.galleryCircle.numListActive(_numHolder, _scrollEl);

					if (jQuery.isFunction(_options.onChangeFunc)) {
						_options.onChangeFunc.apply(_holderList);
					}
					//autoslide
					if (_switchTime) {
						_t = setTimeout(function(){
							nextSlides();
						},_switchTime)
					}
				}});
			}

			//go prev slide
			function prevSlides(){
				if (_t) clearTimeout(_t);
				if (_options.step) {
					_curElIndex = getCurElIndex();
					if (_curElIndex == 0) _curElIndex= _scrollEl.length;
					_step = _scrollEl.eq(_curElIndex-1).innerWidth(true);
				};
				_margin = -parseInt(_scrollElParent.css('marginLeft'));
				_margin -= _step;
				_scrollElParent.animate({'marginLeft':(-_margin+_options.innerMargin)}, {duration:_options.duration, easing: _easing, complete:function(){
					if (_margin < _widthSum) {
						_margin = _widthSum*2 - (_widthSum - _margin);
					}
					_scrollElParent.css({'marginLeft':-_margin+_options.innerMargin});
					jQuery.fn.galleryCircle.numListActive(_numHolder, _scrollEl);

					if (jQuery.isFunction(_options.onChangeFunc)) {
						_options.onChangeFunc.apply(_holderList);
					}
					//autoslide
					if (_switchTime) {
						_t = setTimeout(function(){
							nextSlides();
						},_switchTime)
					}
				}});
			}

			// Number list Create
			jQuery.fn.galleryCircle.numListCreate = function(_numHolder, _scrollEl){
				var _numListElC = '';
				for(var i=0; i<_scrollEl.length; i++){
					_numListElC += '<li><a href="">'+(i+1)+'</a></li>';
				}
				jQuery(_numHolder).html('<ul>'+_numListElC+'</ul>');
			};

			// Number list Activate
			jQuery.fn.galleryCircle.numListActive = function(_numHolder, _scrollEl){
				_curElIndex = getCurElIndex();
				if (jQuery(_options.curPage, _this).length && _options.curPage) jQuery(_options.curPage, _this).text('Pagina '+(getCurElIndex()+1)+'/'+_scrollEl.length);
				if (_numHolder) {
					jQuery('a',_numHolder).removeClass('active');
					jQuery('a',_numHolder).eq(_curElIndex).addClass('active');
				}
			};

			//click on control elemens
			function numClick() {
				jQuery(_options.numHolder, _this).find('a').click(function(){
					if(_scrollElParent.is(':animated')) _scrollElParent.stop(true, true);
					if (_t) clearTimeout(_t);
					var _aList = jQuery(_options.numHolder, _this).find('a');
					var _index = _aList.index(jQuery(this));
					_margin = _widthSum + _index * _scrollEl.outerWidth(true);
					_scrollElParent.animate({'marginLeft':(-_margin+_options.innerMargin)}, {duration:_options.duration, easing: _easing, complete:function(){
						if (_margin >= _widthSum*2) {
							_margin = _widthSum + (_margin - _widthSum*2);
						}
						_scrollElParent.css({'marginLeft':-_margin+_options.innerMargin});
						_aList.removeClass('active').eq(_index).addClass('active');

						if (jQuery.isFunction(_options.onChangeFunc)) {
							_options.onChangeFunc.apply(_holderList);
						}
						//autoslide
						if (_switchTime) {
							_t = setTimeout(function(){
								nextSlides();
							},_switchTime)
						}
					}});
					return false;
				});
			};

			// init creating num list
			if (_options.numCreate) jQuery.fn.galleryCircle.numListCreate(_numHolder, _scrollEl);

			// pagination first init (example Page 2/6)
			if (jQuery(_options.curPage, _this).length && _options.curPage) jQuery(_options.curPage, _this).text('Pagina '+(getCurElIndex()+1)+'/'+_scrollEl.length);

			// init activate num list item and init numClick()
			if (_options.numHolder) {
				jQuery.fn.galleryCircle.numListActive(_numHolder, _scrollEl);
				numClick();
			}
		}else{
			if (_prev) _prev.css('visibility','hidden');
			if (_next) _next.css('visibility','hidden');
		}
	});
}
