// page init
$(function(){
	initGallery();
	initGalleryTooltips();
	hoverForIE6('#nav li', 'hover');
});

// gallery init
function initGallery() {
	$('div.gallery').scrollGallery({
		sliderHolder: '>div.inner',
		slider:'>ul',
		slides: '>li',
		pagerLinks:'div.pager a',
		btnPrev:'a.link-prev',
		btnNext:'a.link-next',
		activeClass:'active',
		pauseOnHover:true,
		autoRotation:false,
		switchTime:5000,
		duration:650
	});
}

// gallery tooltips
function initGalleryTooltips() {
	var _reactSpeed = 250;
	var _fadeSpeed = ($.browser.msie ? 0 : 300);

	$('div.gallery').each(function(){
		// init
		var _holder = $(this);
		var _slider = _holder.find('div.inner > ul');
		var _items = _slider.children();
		var _slideWidth = _items.eq(0).outerWidth(true);
		var _tooltipsList = $('<div class="tooltips-list" />').appendTo(_holder);
		var _tooltips = _items.find('div.popup').appendTo(_tooltipsList).hide();

		// tooltip show/hide
		_items.each(function(_ind){
			var _opener = $(this);
			var _timer;

			_opener.hover(function(){
				if(_timer) clearTimeout(_timer);
				_timer = setTimeout(function(){
					var _positionIndex = Math.round((parseInt(_slider.css('marginLeft')) + _opener.position().left)/_slideWidth)+1;
					_tooltips.not(_tooltips.eq(_ind)).fadeOut(_fadeSpeed);
					_tooltips.eq(_ind).removeClass('pos1 pos2 pos3').addClass('pos'+_positionIndex)
					_tooltips.eq(_ind).fadeIn(_fadeSpeed);
				},_reactSpeed);
			},function(){
				_timer = setTimeout(function(){
					_tooltips.eq(_ind).fadeOut(_fadeSpeed);
				},_reactSpeed);
			});

			_tooltips.eq(_ind).hover(function(){
				if(_timer) clearTimeout(_timer);
			},function(){
				_timer = setTimeout(function(){
					_tooltips.eq(_ind).fadeOut(_fadeSpeed);
				},_reactSpeed);
			});

		});
	});
}

// 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);
		});
	}
}

// scrolling gallery plugin
jQuery.fn.scrollGallery = function(_options){
	var _options = jQuery.extend({
		sliderHolder: '>div',
		slider:'>ul',
		slides: '>li',
		pagerLinks:'div.pager a',
		btnPrev:'a.link-prev',
		btnNext:'a.link-next',
		activeClass:'active',
		pauseOnHover:true,
		autoRotation:false,
		switchTime:5000,
		duration:650,
		easing:'swing',
		event:'click',
		vertical:false,
		step:false
	},_options);

	return this.each(function(){
		// gallery options
		var _this = jQuery(this);
		var _sliderHolder = jQuery(_options.sliderHolder, _this);
		var _slider = jQuery(_options.slider, _sliderHolder);
		var _slides = jQuery(_options.slides, _slider);
		var _btnPrev = jQuery(_options.btnPrev, _this);
		var _btnNext = jQuery(_options.btnNext, _this);
		var _pagerLinks = jQuery(_options.pagerLinks, _this);
		var _pauseOnHover = _options.pauseOnHover;
		var _autoRotation = _options.autoRotation;
		var _activeClass = _options.activeClass;
		var _easing = _options.easing;
		var _duration = _options.duration;
		var _switchTime = _options.switchTime;
		var _controlEvent = _options.event;
		var _step = _options.step;
		var _vertical = _options.vertical;

		// gallery init
		if(!_slides.length) return;
		var _currentStep = 0;
		var _sumWidth = 0;
		var _sumHeight = 0;
		var _hover = false;
		var _stepWidth;
		var _stepHeight;
		var _stepCount;
		var _offset;
		var _timer;
		var _animateProperty = (_vertical ? 'marginTop' : 'marginLeft');

		_slides.each(function(){
			_sumWidth+=$(this).outerWidth(true);
			_sumHeight+=$(this).outerHeight(true);
		});

		// calculate gallery offset
		function recalcOffsets() {
			if(_vertical) {
				if(_step) {
					_stepHeight = _slides.eq(_currentStep).outerHeight(true);
					_stepCount = Math.ceil((_sumHeight-_sliderHolder.height())/_stepHeight)+1;
					_offset = -_stepHeight*_currentStep;
				} else {
					_stepHeight = _sliderHolder.height();
					_stepCount = Math.ceil(_sumHeight/_stepHeight);
					_offset = -_stepHeight*_currentStep;
					if(_offset < _stepHeight-_sumHeight) _offset = _stepHeight-_sumHeight;
				}
			} else {
				if(_step) {
					_stepWidth = _slides.eq(_currentStep).outerWidth(true);
					_stepCount = Math.ceil((_sumWidth-_sliderHolder.width())/_stepWidth)+1;
					_offset = -_stepWidth*_currentStep;
				} else {
					_stepWidth = _sliderHolder.width();
					_stepCount = Math.ceil(_sumWidth/_stepWidth);
					_offset = -_stepWidth*_currentStep;
					if(_offset < _stepWidth-_sumWidth) _offset = _stepWidth-_sumWidth;
				}
			}
		}

		// gallery control
		if(_btnPrev.length) {
			_btnPrev.bind(_controlEvent,function(){
				prevSlide();
				return false;
			});
		}
		if(_btnNext.length) {
			_btnNext.bind(_controlEvent,function(){
				nextSlide();
				return false;
			});
		}
		if(_pagerLinks.length) {
			_pagerLinks.each(function(_ind){
				jQuery(this).bind(_controlEvent,function(){
					if(_currentStep != _ind) {
						_currentStep = _ind;
						switchSlide();
					}
					return false;
				});
			});
		}

		// gallery animation
		function prevSlide() {
			recalcOffsets();
			if(_currentStep > 0) _currentStep--;
			else _currentStep = _stepCount-1;
			switchSlide();
		}
		function nextSlide() {
			recalcOffsets();
			if(_currentStep < _stepCount-1) _currentStep++;
			else _currentStep = 0;
			switchSlide();
		}
		function refreshStatus() {
			if(_pagerLinks.length) _pagerLinks.removeClass(_activeClass).eq(_currentStep).addClass(_activeClass);
		}
		function switchSlide() {
			recalcOffsets();
			if(_vertical) _slider.animate({marginTop:_offset},{duration:_duration,queue:false,easing:_easing});
			else _slider.animate({marginLeft:_offset},{duration:_duration,queue:false,easing:_easing});
			refreshStatus();
			autoSlide();
		}

		// autoslide function
		function autoSlide() {
			if(!_autoRotation || _hover) return;
			if(_timer) clearTimeout(_timer);
			_timer = setTimeout(nextSlide,_switchTime+_duration);
		}
		if(_pauseOnHover) {
			_this.hover(function(){
				_hover = true;
				if(_timer) clearTimeout(_timer);
			},function(){
				_hover = false;
				autoSlide();
			});
		}
		refreshStatus();
		autoSlide();
	});
}


