$(document).ready(function(){
	initGallery()
})
// gallery init
function initGallery() {
	// settings
	var _waitAnimation = true;
	var _autoSlide = true;
	var resize = true;
	var _easing = 'linear';
	var _activeClass = 'active';
	var _switchTime = 6000;
	var _speed = 1;

	$('div.gallery-slideshow').each(function(){
		// gallery options
		var _holder = $(this);
		var _btnPrev = _holder.find('a.btn-prev');
		var _btnNext = _holder.find('a.btn-next');
		var _slider = _holder.find('>ul');
		var _slides = _slider.find('>li');
		var _slidesCount = _slides.length;
		var _slidesHeight = _slides.eq(0).outerHeight(true);
		var windowHeight = $(window).height();
		var windowWidth = $(window).width();
		var _slideWidth = windowWidth;
		var _currentIndex = 0;
		var _oldIndex = _currentIndex;
		var _animating = false;
		var _direction;
		var _timer;
		var switcher=_holder.find('div.switcher');
		// slider height
		if( windowWidth < 960 ){
			_slideWidth = 960;
		}
		else{
			_slideWidth = windowWidth;
		}
		_slides.each(function(i, el){
			_slides.eq(i).css({
				left: _slideWidth*i
			})
		})
		function prevSlide(){
			if(!_animating) {
				_oldIndex = _currentIndex;
				if(_currentIndex > 0) _currentIndex--;
				else _currentIndex = _slidesCount-1;
				_direction = true;
				switchSlide();
			}
			return false;
		}
		function nextSlide() {
			if(!_animating) {
				_oldIndex = _currentIndex;
				if(_currentIndex < _slidesCount-1) _currentIndex++;
				else _currentIndex = 0;
				_direction = true;
				switchSlide();
			}
			return false;
		}
		if( _btnPrev.length ){
			_btnPrev.bind('click', function(){
				prevSlide();
				return false;
			})
		}
		if( _btnNext.length ){
			_btnNext.bind('click', function(){
				nextSlide();
				return false;
			})
			
		}
		//Stop slide onhover content
		_slider.hover(
			function(){
				if(_timer) clearTimeout(_timer);
			},
			function(){
				autoSlide();
			}
		)
		function elNum(){
			if (switcher) {
				var switcherEl='';
				var num=1;
				var difference=1;
				while (difference <= _slides.length){
					switcherEl+='<li><a href="">'+ num + '</a></li>';
					num++;
					difference++;
				}
				switcher.html('<ul>'+ switcherEl+'</ul>');
				if(_currentIndex!=-1) {
					switcher.find('li').removeClass('active').eq(_currentIndex).addClass('active')
				}
			}
			var link = switcher.find('li');
			link.click(function(){
				if(_currentIndex != link.index(jQuery(this))) {
					_currentIndex=link.index(jQuery(this));
					switchSlide();
				}
				if( _timer!=-1 ) clearTimeout(_timer)
				return false;
			});
		}
		elNum();
		// gallery animation
		function switchSlide() {
			if(_waitAnimation) _animating = true;
			_slider.animate({ left: -_slideWidth*_currentIndex }, {  queue: false, duration: _speed, complete: function(){
				_animating = false;
				autoSlide();
			}});
			switcher.find('li').removeClass('active').eq(_currentIndex).addClass('active');
		}
		function autoSlide() {
			if(!_autoSlide) return;
			if(_timer) clearTimeout(_timer);
			_timer = setTimeout(nextSlide,_switchTime);
		}
		autoSlide();
		if(resize){
			$(window).resize(function(){
				windowHeight = $(window).height();
				windowWidth = $(window).width();
				if( windowWidth < 960 ){
					_slideWidth = 960;
				}
				else{
					_slideWidth = windowWidth;
				}
				_slider.css({ left: -_slideWidth*_currentIndex })
				_slides.each(function(i, el){
					_slides.eq(i).css({
						left: _slideWidth*i
					})
				})
			})
		}
	});
}
