jQuery(function(){
	
	initAjaxGallery();
});

function initAjaxGallery() {
	// settings
	var _waitAnimation = true;
	var _autoSlide = false;
	var _easing = 'linear';
	var _activeClass = 'active';
	var _loadingClass = 'ajax-loading';
	var _loadedClass = 'loaded';
	var _switchTime = 5000;
	var _speed = 600;

	jQuery('div.events-box, div.stripes-box').each(function(){
		// gallery options
		var _holder = jQuery(this);
		var _btnPrev = _holder.find('a.prev');
		var _btnNext = _holder.find('a.next');
		var _slidesHolder = _holder.find('div.c, div.box');
		var _slider = _slidesHolder.find('>ul.g1');
		var _slides = _slider.children();
		var _slidesCount = _slides.length;
		var _slideWidth = _slides.eq(0).outerWidth(true);
		var _currentIndex = 0;
		var _oldIndex = _currentIndex;
		var _animating = false;
		var _direction;
		var _timer;

		// slider height
		_holder.css({position:'relative'});
		_slider.css({height:70});
		_slides.show().css({position:'absolute',top:0,left:_slideWidth});
		_slides.eq(_currentIndex).css({left:0});

		_slides.each(function(){
			jQuery(this).attr('meta', jQuery(this).attr('title')).attr('title', '')
		})

		function prevSlide() {
			if(!_animating) {
				_oldIndex = _currentIndex;
				if(_currentIndex > 0) _currentIndex--;
				else _currentIndex = _slidesCount-1;
				_direction = false;
				switchSlide();
			}
			return false;
		}
		function nextSlide() {
			if(!_animating) {
				_oldIndex = _currentIndex;
				if(_currentIndex < _slidesCount-1) _currentIndex++;
				else _currentIndex = 0;
				_direction = true;
				switchSlide();
			}
			return false;
		}
		_btnPrev.click(prevSlide);
		_btnNext.click(nextSlide);

		// gallery animation
		function refreshStatus() {
			if(_slider.length){
				if(!_slides.eq(_currentIndex).hasClass(_loadedClass)) {
					_slides.eq(_currentIndex).addClass(_loadedClass).addClass(_loadingClass);
					jQuery.ajax({
						url:_slides.eq(_currentIndex).attr('meta'),
						type:'get',
						success:function(msg){
							var box = jQuery('<div>').html(msg).hide();
							setTimeout(function(){
								_slides.eq(_currentIndex).removeClass(_loadingClass).append(box);
								box.show();
								_slides.eq(_currentIndex).height(_slides.eq(_currentIndex).outerHeight(true));
								_slider.animate({height:_slides.eq(_currentIndex).height()}, {queue:false, duration:300});
								box.hide();
								box.fadeIn(500);
							},1000)
						},
						error:function(){
							alert('AJAX Error!')
						}
					})
				}
				else{
					_slider.animate({height:_slides.eq(_currentIndex).height()}, {queue:false, duration:300});
				}
			}
		}
		function switchSlide() {
			if(_waitAnimation) _animating = true;
			_slides.eq(_currentIndex).css({left:(_direction ? _slideWidth : -_slideWidth)}).animate({left:0},{duration:_speed, queue:false,easing:_easing,complete:function(){
				_animating = false;
			}});
			_slides.eq(_oldIndex).animate({left:(_direction ? -_slideWidth : _slideWidth)},{duration:_speed, queue:false,easing:_easing});
			refreshStatus();
			autoSlide();
		}
		function autoSlide() {
			if(!_autoSlide) return;
			if(_timer) clearTimeout(_timer);
			_timer = setTimeout(nextSlide,_switchTime);
		}
		refreshStatus();
		// autoSlide();
	});
}
