jQuery(function(){
	ieHover('.gallery-switcher li, .slider-switcher li');
	initGallery();
})

// ieHover
function ieHover(_selector, _class){
	if(_class == null) _class = 'hover';
	if (jQuery.browser.msie && jQuery.browser.version < 7) {
		jQuery(_selector).each(function(){
			jQuery(this).mouseenter(function(){
				jQuery(this).addClass(_class)
			}).mouseleave(function(){
				jQuery(this).removeClass(_class)
			})
		})
	}
}

// initGallery
function initGallery(){
	jQuery('.gallery').each(function(){
		var $this = jQuery(this),
			$gallery = jQuery('.gallery-list', $this),
			galleryEl = [],
			gallerySwitch = [],
			_currentSlide = 0, _prevSlide, _timer, _timer2;
			var delay = 3000;
			var autoslide = true;
		
		if($gallery.children().length > 1){
			var _prev = jQuery('<span class="link-prev">prev</span>').appendTo($this),
				_next = jQuery('<span class="link-next">next</span>').appendTo($this),
				_switcherGallery = jQuery('<ul class="gallery-switcher" />').appendTo($this);
			
			$gallery.children().each(function(){
				var _thumbneilG = jQuery('.gallery-thumbneil', this).appendTo(_switcherGallery).wrap('<li />')
			})
		}
		
		jQuery('.gallery-item', $this).each(function(){
			$slider = jQuery('.slider-list', this);
		
			if($slider.children().length > 1){
				var _switcherSlider = jQuery('<ul class="slider-switcher" />').appendTo(this);
				
				$slider.children().each(function(){
					var _thumbneilS = jQuery('.slider-thumbneil', this).appendTo(_switcherSlider).wrap('<li />');
				})
			}
			$slider.children().each(function(){
				jQuery('.slider-thumbneil', this).remove();
			})
		})
		jQuery('.gallery-switcher > li', $this).each(function(){
			jQuery(this).data('pos', jQuery(this).position().left);
		})
		$gallery.children().each(function(i){
			jQuery('.gallery-thumbneil', this).remove();
			var thisSwitch = jQuery('.slider-switcher', this),
				_w = 0;
			
			jQuery(this).show();
			
			if((jQuery('.gallery-switcher > li', $this).eq(i).data('pos') + thisSwitch.outerWidth() - 7) > $this.width()){
				thisSwitch.css({
					left: 'auto',
					right: 4
				})
			} else {
				thisSwitch.css({
					left: jQuery('.gallery-switcher > li', $this).eq(i).data('pos') - 7
				})
			}
		})
		
		jQuery('.gallery-item').each(function(i){
			jQuery('.slider-list > li', this).each(function(){
				galleryEl.push(this);
			})
			if(jQuery('.slider-switcher > li', this).length){
				jQuery('.slider-switcher > li', this).each(function(){
					gallerySwitch.push(this);
				})
			} else {
				gallerySwitch.push(jQuery('.gallery-switcher > li')[i]);
			}
		})
		
		jQuery('.gallery-item').not(':first').hide();
		jQuery(galleryEl).not(':first').css({opacity: 0}).hide();
		_prev.addClass('link-disabled');
		jQuery('.gallery-switcher > li').eq(0).addClass('active');
		if(jQuery(galleryEl).eq(0).parents('.gallery-item:eq(0)').find('.slider-switcher > li').length){
			jQuery(galleryEl).eq(0).parents('.gallery-item:eq(0)').find('.slider-switcher > li').eq(0).addClass('active');
		}
		
		_prev.click(function(){
			if(_currentSlide > 0){
				_prevSlide = _currentSlide;
				_currentSlide--;
				_next.removeClass('link-disabled');
				nextSlide();
			}
			if(_timer) clearInterval(_timer);
		})
		
		_next.click(function(){
			if(_currentSlide < jQuery(galleryEl).length - 1){
				_prevSlide = _currentSlide;
				_currentSlide++;
				_prev.removeClass('link-disabled');
				nextSlide();
			}
			if(_timer) clearInterval(_timer);
			if(_timer2) clearInterval(_timer2);
		})

		jQuery('.gallery-switcher > li', $this).each(function(){
			var $thisEl = jQuery(this);
			$thisEl.click(function(){
				if(!jQuery(this).hasClass('active')){
					_prevSlide = _currentSlide;
					_currentSlide = jQuery(gallerySwitch).index($thisEl);
					if(_currentSlide < 0){
						_currentSlide = jQuery(gallerySwitch).index(jQuery('.slider-switcher > li:eq(0)', jQuery('.gallery-item', $this).eq(jQuery('.gallery-switcher > li', $this).index($thisEl))))
					}
					_prev.removeClass('link-disabled');
					_next.removeClass('link-disabled');
					nextSlide();
				}
				if(_timer) clearInterval(_timer);
				if(_timer2) clearInterval(_timer2);
				return false;
			})
		})
		
		jQuery('.slider-switcher > li', $this).each(function(){
			var _thisEl = jQuery(this);
			_thisEl.click(function(){
				if(!jQuery(this).hasClass('active')){
					_prevSlide = _currentSlide;
					_currentSlide = jQuery(gallerySwitch).index(_thisEl);
					_prev.removeClass('link-disabled');
					_next.removeClass('link-disabled');
					nextSlide();
				}
				if(_timer) clearInterval(_timer);
				if(_timer2) clearInterval(_timer2);
				return false;
			})
		})
		
		function nextSlide(){
			if(_currentSlide == 0){
				_prev.addClass('link-disabled');
			}
			else if(_currentSlide == jQuery(galleryEl).length - 1){
				_next.addClass('link-disabled');
			}
			
			jQuery(galleryEl).eq(_prevSlide).animate({opacity: 0}, {queue: false, duration: 300, complete: function(){
				jQuery(this).hide()
			}})
			jQuery(galleryEl).eq(_currentSlide).show().animate({opacity: 1}, {queue: false, duration: 300, complete: function(){
				jQuery(this).css({opacity: 'auto'});
			}})
			
			jQuery('.gallery-item', $this).hide();
			jQuery(galleryEl).eq(_currentSlide).parents('.gallery-item:eq(0)').show();
			
			jQuery('.gallery-switcher > li', $this).removeClass('active').eq(jQuery('.gallery-item', $this).index(jQuery(galleryEl).eq(_currentSlide).parents('.gallery-item:eq(0)'))).addClass('active');
			jQuery('.slider-switcher > li', jQuery(galleryEl).eq(_currentSlide).parents('.gallery-item:eq(0)')).removeClass('active').eq(jQuery(galleryEl).eq(_currentSlide).parents('.gallery-item:eq(0)').find('.slider-list > li').index(jQuery(galleryEl).eq(_currentSlide))).addClass('active');
		}
		
		$this.mouseenter(function(){
			jQuery(this).attr('flag', 'hover');
			clearTimeout(_timer2);
		}).mouseleave(function(){
			jQuery(this).removeAttr('flag');
			autoSlide();
		});
		
		if(autoslide){autoSlide();}
		
		function autoSlide(){
			_timer2 = setTimeout(function(){
				_next.trigger('click');
				if(autoslide && !$this.attr('flag')){autoSlide();}
			}, delay);
		}
		
	})
}
