$(function(){
	initPage();
	initHomeGallery();
	initGallery();
})

function initPage() {

	// UI Fixes
	jQuery('.noJs').removeClass('noJs');
	
	// Print Buttons
	jQuery('.printPage').click(function() {
  		window.print();
  		return false;
 	});

}

function initHomeGallery(){
	$('div.slideshow').gallery({
		listOfSlides:'ul.image-list > li',
		switcher:'div.switcher > ul > li',
		autoRotation: 4000,
		onStart: function(obj){
			obj.holder.gallery({
				listOfSlides: '.text-block ul > li',
				switcher: obj.switcher,
				effect: true,
				autoRotation: 4000
			});
		}
	});
}

function initGallery(){
	var _thumbs = $('.gallery-slider .slider-holder ul li');
	$('.gallery').fadeGallery({
		slideElements: '.gallery-holder ul li',
		pagerLinks: '.gallery-slider .slider-holder ul li',
		btnNext: '.link-next',
		btnPrev: '.link-prev',
		duration:700,
		autoRotation:true,
		onChange: function(_this, _slides, _prevIndex, _currentIndex){
			if (_thumbs.eq(_currentIndex).position().left < -parseInt(_this.css('margin-left'))) {
				var _step = _currentIndex * _thumbs.eq(0).outerWidth()
				$('.gallery-slider .slider-holder ul').animate({
					'margin-left': -_step
				}, 700)
				$('.gallery-slider').get(0).curInd = Math.floor(_thumbs.eq(_currentIndex).position().left/$('.gallery-slider .slider-holder').width()) + 1;
				_this.get(0).switchSlide
			}
			if (_thumbs.eq(_currentIndex).position().left > -parseInt(_this.css('margin-left')) + $('.gallery-slider .slider-holder').width()) {
				if (_currentIndex > _thumbs.length-5) _currentIndex = _thumbs.length-5;
				var _step = _currentIndex * _thumbs.eq(0).outerWidth()
				$('.gallery-slider .slider-holder ul').animate({
					'margin-left': -_currentIndex * _thumbs.eq(0).outerWidth()
				}, 700)
				$('.gallery-slider').get(0).curInd = Math.floor(_thumbs.eq(_currentIndex).position().left/$('.gallery-slider .slider-holder').width()) + 1;
				_this.get(0).switchSlide
			}
		}
	})
	$('.gallery-slider').scrollGallery({
		sliderHolder: '.slider-holder',
		btnNext: '.link-next',
		btnPrev: '.link-prev',
		duration: 700
	})
}

