// initallery
function initallery(){
	var _gallery = $('#sidebar>.gallery');
	var duration = 4000;
	
	_gallery.each(function(){
		allery = $(this);
		var _slideEl = allery.find('ul');
		var _slideStep = allery.find('.over').outerWidth();
		var _btnPrev = allery.find('.btn-prev');
		var _btnNext = allery.find('.btn-next');
		var _play = allery.find('.play');
		var _pause = allery.find('.pause');
		var _stepsMax = _slideEl.find('>li').length - 1 ;
		var _steps = 0;
		var _flag = false;
		var _int;
		
		function rotate(){
			if (_steps < _stepsMax) {
				_steps++;
				_slideEl.animate({
					'left' : -_steps*_slideStep
				})
			}
				else {
					_steps = 0;
					_slideEl.animate({
						'left' : -_steps*_slideStep
					})
				}
		}
		
		_btnPrev.click(function(){
			if (_int) {
				clearInterval(_int);
				_flag = true;
			}
			rotate();
			if (_flag) _int = setInterval(rotate, duration);
			return false;
		})
		
		_btnNext.click(function(){
			if (_int) {
				clearInterval(_int);
				_flag = true;
			}
			if (_steps > 0) {
				_steps--;
				_slideEl.animate({
					'left' : -_steps*_slideStep
				})
			}
				else {
					_steps = _stepsMax;
					_slideEl.animate({
						'left' : -_steps*_slideStep
					})
				}
			if (_flag) _int = setInterval(rotate, duration);
			return false;
		})
		
		_play.click(function(){
			if (_int) clearInterval(_int);
			_int = setInterval(rotate, duration);
			return false;
		})
		_play.click();
		_pause.click(function(){
			if (_int) clearInterval(_int);
			_flag = false;
			return false;
		})
	})
}


