jQuery(function(){
	jQuery('.photo-list').jqueryTabs({
		fadeSpeed: 150,
		tabLinks: 'a.tab-photo',
		event:'mouseenter'
	});
	initGallery();
});

// jquery tabs plugin
jQuery.fn.jqueryTabs = function(_options){
	// default options
	var _options = jQuery.extend({
		addToParent:false,
		holdHeight:false,
		activeClass:'active',
		tabLinks:'a.tab',
		fadeSpeed:0,
		event:'click'
	},_options);

	return this.each(function(){
		var _holder = jQuery(this);
		var _fadeSpeed = _options.fadeSpeed;
		var _activeClass = _options.activeClass;
		var _addToParent = _options.addToParent;
		var _holdHeight = _options.holdHeight;
		var _tabLinks = jQuery(_options.tabLinks, _holder);
		var _tabset = (_addToParent ? _tabLinks.parent() : _tabLinks);
		var _event = _options.event;
		var _animating = false;

		// tabs init
		_tabLinks.each(function(){
			var _tmpLink = jQuery(this);
			var _tmpTab = jQuery(_tmpLink.attr('href'));
			var _classItem = (_addToParent ? _tmpLink.parent() : _tmpLink);
			if(_tmpTab.length) {
				if(_classItem.hasClass(_activeClass)) _tmpTab.show();
				else _tmpTab.hide();
			}
		});

		// tab switcher
		function switchTab(_switcher) {
			if(!_animating) {
				var _link = jQuery(_switcher);
				var _newItem = (_addToParent ? _link.parent() : _link);
				var _newTab = jQuery(_link.attr('href'));
				if(_newItem.hasClass(_activeClass)) return;

				var _oldItem = jQuery(_addToParent ? _tabset : _tabLinks).filter('.'+_activeClass);
				var _oldTab = jQuery(jQuery(_addToParent ? _oldItem.children('a') : _oldItem).attr('href'));
				if(_newTab.length) {
					_animating = true;
					if(_oldItem.length) {
						_newItem.addClass(_activeClass);
						_oldItem.removeClass(_activeClass);

						var _parent = _oldTab.parent();
						if(_holdHeight) _parent.css({height:_parent.height()});

						_oldTab.fadeOut(_fadeSpeed,function(){
							_newTab.fadeIn(_fadeSpeed,function(){
								_animating = false;
							});
							if(_holdHeight) _parent.css({height:'auto'});
						});
					} else {
						_newItem.addClass(_activeClass);
						_newTab.fadeIn(_fadeSpeed,function(){
							_animating = false;
						});
					}
				}
			}
		}

		// control
		_tabLinks.each(function(){
			jQuery(this).bind(_event,function(){
				switchTab(this);
				return false;
			});
		});
	});
}

// initGallery
function initGallery(){
	jQuery('.gallery-box').each(function(){
		var _gallery = jQuery('.gallery', this),
			_el = jQuery('> ul > li', _gallery),
			_visualHolder = jQuery('.gallery-image', this),
			_current = jQuery('.option-pane .current', this),
			_summa = jQuery('.option-pane .summa', this),
			_btnNext = jQuery('.option-pane .btn-next', this),
			_btnPrev = jQuery('.option-pane .btn-prev', this),
			_btnNextSlide = jQuery('.gallery-btn-next', this),
			_slideEl = 10,
			_slideStep = _el.eq(0).outerWidth(),
			_slide = 0,
			_activeIndex = 0;
		
		_el.removeClass('active').eq(_activeIndex).addClass('active');
		_activeIndex = _el.index(_el.filter('.active'));
		_summa.html(_el.length);
		current(_activeIndex);
		createVisual(_el.filter('.active'));
		
		// current
		function current(i){
			_current.html(i+1);
		}
		
		// createVisual
		function createVisual(j){
			if(_visualHolder.find('.more-content').length > 0){
				_visualHolder.find('.more-content').stop().animate({opacity: 0}, 100, function(){
					_visualHolder.html(j.find('.more-content').clone().css({opacity: 0}))
					_visualHolder.find('.more-content').animate({opacity: 1}, 100)
				})
			} else {
				_visualHolder.html(j.find('.more-content').clone())
			}
		}
		
		// _el.click
		_el.click(function(){
			if(!jQuery(this).hasClass('active')){
				_activeIndex = _el.index(jQuery(this));
				switchEl();
			}
			return false;
		});
		
		// _btnNext.click
		_btnNext.click(function(){
			if(_activeIndex < _el.length - 1){
				_activeIndex++;
				switchEl();
			}
			return false;
		});
		
		// _btnPrev.click
		_btnPrev.click(function(){
			if(_activeIndex > 0){
				_activeIndex--;
				switchEl();
			}
			return false;
		});
		
		// _btnNextSlide.click
		_btnNextSlide.click(function(){
			if(_slide + _slideEl * 2 < _el.length){
				_slide += _slideEl;
			} else if (_slide < _el.length - _slideEl) {
				_slide = _el.length - _slideEl;
			} else {
				_slide = 0;
			}
			slide(_slide);
			return false;
		});
		
		// testCurrent
		function testCurrent(i){
			var _curSlideSet = Math.ceil((i + 1) / _slideEl);
			_slide = _slideEl * (_curSlideSet - 1);
			if(_slide + _slideEl < _el.length){
				_slide = _slide;
			} else {
				_slide = _el.length - _slideEl;
			}
			slide(_slide);
		}
		
		// switchEl
		function switchEl(){
			_el.removeClass('active');
			_el.eq(_activeIndex).addClass('active');
			createVisual(_el.eq(_activeIndex));
			current(_activeIndex);
			testCurrent(_activeIndex);
		}
		
		// slide
		function slide(o){
			jQuery('> ul', _gallery).stop().animate({marginLeft: - o * _slideStep}, 500);
		}
	})
}
