// page init
$(function(){
	initSlideShow();
	hoverForIE6('#nav li');
});

// slideshow gallery
function initSlideShow() {
	// settings
	var _autoSlide = true;
	var _activeClass = 'active';
	var _switchTime = 3500;
	var _fadeSpeed = 550;
	var _slideSpeed = 350;

	$('div#slideshow').each(function(){
		// gallery options
		var _holder = $(this);
		var _thumbSlider = _holder.find('ul.thumbs');
		var _thumbLinks = _thumbSlider.find('li');
		_thumbSlider.append(_thumbLinks.clone(true));
		var _newLinks = _thumbSlider.find('li');
		var _thumbHeight = _thumbLinks.eq(0).outerHeight(true);
		var _slidesHolder = _holder.find('div.slideset');
		var _slides = _slidesHolder.children();
		var _slidesCount = _slides.length;
		var _currentIndex;
		var _oldIndex;
		var _timer;

		// gallery init
		_oldIndex = _currentIndex = 0;
		_slides.hide().eq(_currentIndex).show();
		_thumbSlider.css({marginTop: -_thumbHeight+'px'});

		// gallery control
		_newLinks.each(function(_ind){
			$(this).click(function(){
				if(_currentIndex != _ind){
					_oldIndex = _currentIndex;
					_currentIndex = _ind;
					_currentIndex = _currentIndex > _slidesCount-1 ? (_currentIndex - _slidesCount) : _currentIndex;
					switchSlide();
				}
				return false;
			});
		});

		// gallery animation
		function prevSlide() {
			_oldIndex = _currentIndex;
			if(_currentIndex > 0) _currentIndex--;
			else _currentIndex = _slidesCount-1;
			switchSlide();
		}
		function nextSlide() {
			_oldIndex = _currentIndex;
			if(_currentIndex < _slidesCount-1) _currentIndex++;
			else _currentIndex = 0;
			switchSlide();
		}
		function switchSlide() {
			if(_currentIndex == 0){
				_thumbSlider.animate({marginTop:-_thumbHeight*(_slidesCount+1)},{duration:_slideSpeed,queue:false,complete:function(){
					_thumbSlider.css({marginTop: -_thumbHeight+'px'});
				}});
			}else{
				_thumbSlider.animate({marginTop:-_thumbHeight*(_currentIndex+1)},{duration:_slideSpeed,queue:false});
			}

			_slides.removeClass('active').eq(_currentIndex).addClass(_activeClass);
			_slides.eq(_oldIndex).fadeOut(_fadeSpeed);
			_slides.eq(_currentIndex).fadeIn(_fadeSpeed);
			autoSlide();
		}
		function autoSlide() {
			if(!_autoSlide) return;
			if(_timer) clearTimeout(_timer);
			_timer = setTimeout(nextSlide,_switchTime);
		}
		autoSlide();
	});
}

// hover for IE
function hoverForIE6(_list, _class) {
	var _hoverClass = 'hover';
	if(_class) _hoverClass = _class;
	if ($.browser.msie && $.browser.version < 7) {
		$(_list).hover(function() {
			document.title = _hoverClass;
			$(this).addClass(_hoverClass);
		}, function() {
			$(this).removeClass(_hoverClass);
		});
	}
}