$(function(){
	initGallery();
	initSubGallery();
});

// gallery init
function initGallery() {
	// settings
	var _autoSlide = true;
	var _switchTime = 5000;
	var _speed = 1000;

	$('.home-gallery').each(function(){
		// gallery options
		var _holder = $(this);
		var _slidesHolder = _holder.find('.frame');
		var _slider = _slidesHolder.find('>ul');
		var _slides = _slider.children();
		var _slidesCount = _slides.length;
		var _slideWidth = _slides.eq(0).outerWidth(true);
		
		var _currentIndex = 1;
		var _oldIndex = 0;
		var _nextIndex = 2;
		var _prevIndex;
		var _timer;

		// slider height
		_slider.css({height:_slides.eq(0).outerHeight(true)});
		_slides.show().css({position:'absolute',top:0,left:_slideWidth*2});
		_slides.eq(_currentIndex).css({left:0});
		_slides.eq(_oldIndex).css({left:-_slideWidth});
		_slides.eq(_nextIndex).css({left:_slideWidth});

		function nextSlide() {
			_prevIndex = _oldIndex;
			_oldIndex = _currentIndex;
			_currentIndex = _nextIndex;
			if(_nextIndex < _slidesCount-1) _nextIndex++;
			else _nextIndex = 0;
			switchSlide();
		}
		// gallery animation
		function switchSlide() {
			_slides.eq(_currentIndex).stop().animate({left:0},{duration:_speed});
			_slides.eq(_nextIndex).stop().animate({left: _slideWidth},{duration:_speed});
			_slides.eq(_oldIndex).stop().animate({left: -_slideWidth},{duration:_speed});
			_slides.eq(_prevIndex).stop().animate({left: -(_slideWidth*2)},{duration:_speed, complete: function(){
				jQuery(this).css({left: _slideWidth*2});
			}});
			autoSlide();
		}
		function autoSlide() {
			if(!_autoSlide) return;
			if(_timer) clearTimeout(_timer);
			_timer = setTimeout(nextSlide,_switchTime);
		}
		_slidesHolder.mouseenter(function(){
			if(_timer) clearTimeout(_timer);
		}).mouseleave(function(){
			if(_timer) clearTimeout(_timer);
			autoSlide();
		});
		autoSlide();
	});
}

//init sub gallery
function initSubGallery() {
	// settings
	var _waitAnimation = true;
	var _activeClass = 'active';
	var _speed = 1000;

	$('.sub-gallery').each(function(){
		// gallery options
		var _holder = $(this);
		var _slidesHolder = _holder.find('div.frame');
		var _slider = _slidesHolder.find('>ul');
		var _slides = _slider.children();
		var _slidesCount = _slides.length;
		var prev = _holder.find('a.prev');
		var next = _holder.find('a.next');
		var _currentIndex = 0;
		var _oldIndex;
		var _animating = false;
		var _direction;
		var _slideWidth = 0;
		var switcher = _holder.find('.switcher');
		switcher.empty();
		var _list = $('<ul />');
		for(var i=0; i<_slidesCount; i++) $('<li><a href="#">'+(i+1)+'</a></li>').appendTo(_list);
		_list.appendTo(switcher);
			
		var pagerLinks = _list.children();
		pagerLinks.removeClass(_activeClass).eq(_currentIndex).addClass(_activeClass);
		prev.hide();

		prev.click(function(){
			if(next.is(':hidden')) next.show();
			prevSlide();
			return false;
		});
		next.click(function(){
			if(prev.is(':hidden')) prev.show();
			nextSlide();
			return false;
		});
		pagerLinks.each(function(i){
			$(this).click(function(){
				switchSlideNum(i);
				return false;
			})
		});
		
		function prevSlide() {
			if(_animating) return;
			_oldIndex = _currentIndex;
			if(_currentIndex > 0) _currentIndex--;
			else return;
			if(_currentIndex == 0) prev.hide();
			_slideWidth -= _slides.eq(_currentIndex).outerWidth();
			switchSlide();
		}
		function nextSlide() {
			if(_animating) return;
			_oldIndex = _currentIndex;
			if(_currentIndex < _slidesCount-1) _currentIndex++;
			else return;
			if(_currentIndex == _slidesCount-1) next.hide();
			_slideWidth += _slides.eq(_oldIndex).outerWidth();
			switchSlide();
		}
		function switchSlideNum(n) {
			if(_animating) return;
			if (n == _currentIndex) return;
			else if (n < _currentIndex) {
				_oldIndex = _currentIndex;
				_currentIndex = n;
				for(var i=_oldIndex; i > _currentIndex; i--){
					_slideWidth -= _slides.eq(i-1).outerWidth();
				}
			}
			else {
				_oldIndex = _currentIndex;
				_currentIndex = n;
				for(var i=_oldIndex; i < _currentIndex; i++){
					_slideWidth += _slides.eq(i).outerWidth();
				}
			}
			
			if(_currentIndex == 0) prev.hide();
			else prev.show();
			if(_currentIndex == _slidesCount-1) next.hide();
			else next.show();
			
			switchSlide();
		}
		// gallery animation
		function switchSlide() {
			if(_waitAnimation) _animating = true;
			if(pagerLinks.length) pagerLinks.removeClass(_activeClass).eq(_currentIndex).addClass(_activeClass);
			_slider.stop().animate({marginLeft:-_slideWidth},{duration:_speed, complete:function(){
				_animating = false;
			}});
			
		}
	});
}
