/*
 * Gallery script
 *
 * (c) 2009 KAOH WebServices
 *
 * Using:
 * jQuery JavaScript Library by John Resig
 * jQuery Galleriffic plugin by Trent Foley (http://trentacular.com)
 * jScroller by Markus Bordihn (http://markusbordihn.de)
 */

$(document).ready(function() {
	/* Set opacity for the tumbnails */
	$('.gallery-thumbs img').fadeTo(0,0.2).click(function() {
		$('.gallery-thumbs img').removeClass('currentGalleryImage');
		$(this).addClass('currentGalleryImage').fadeTo('normal',0.8);
		$('.gallery-thumbs img').not('.currentGalleryImage').fadeTo('normal',0.2);
	}).hover( function() { $(this).fadeTo('normal',0.8); }, function() { $(this).not('.currentGalleryImage').fadeTo('normal',0.2); } );
	$('.gallery-thumbs img:first').addClass('currentGalleryImage').fadeTo(0,0.8);

	/* Pop out the titlebar */
	if($('.page-title').hasClass('gallery-text-subtitle')) {
		var p_top = $('.page-title').css('top');
		$('.page-title').css('height','0px').css('top', (parseInt(p_top) + 15) + 'px').animate({height: '50px', top: p_top}, 'slow');
	}
	
	/* Set the scroller events */
	$('.gallery-thumbs').mousemove(
		function(e) {
			var slowsize = 50;
			var fastsize = 25;
			var top = $('.gallery-thumbs').offset().top;
			var bottom = top + Number((/[0-9-,.]+/.exec($('.gallery-thumbs').css('height'))||0));
			if(e.pageY < top + fastsize) {
				$.scroller.direction = 'down';
				$.scroller.speed = 4;
			} else if(e.pageY < top + slowsize) {
				$.scroller.direction = 'down';
				$.scroller.speed = 2;
			} else if(e.pageY > bottom - fastsize) {
				$.scroller.direction = 'up';
				$.scroller.speed = 4;
			} else if(e.pageY > bottom - slowsize) {
				$.scroller.direction = 'up';
				$.scroller.speed = 2;
			} else {
				$.scroller.speed = 0;
			}
		}).mouseleave(
		function() {
			$.scroller.speed = 0;
			$('.gallery-thumbs img').stop(true).not('.currentGalleryImage').fadeTo(0,0.2);
		});
	
	/* Start the gallery */
	var gallery = $('.gallery-image').galleriffic('.gallery-thumbs', {
		delay:                  5000,
		numThumbs:              50,
		preloadAhead:           40, // Set to -1 to preload all images
		enableTopPager:         false,
		enableBottomPager:      true,
		imageContainerSel:      '.gallery-image',
		controlsContainerSel:   '',
		captionContainerSel:    '.gallery-text',
		loadingContainerSel:    '',
		renderSSControls:       false,
		renderNavControls:      false,
		playLinkText:           'Play',
		pauseLinkText:          'Pause',
		prevLinkText:           'Previous',
		nextLinkText:           'Next',
		nextPageLinkText:       'Next &rsaquo;',
		prevPageLinkText:       '&lsaquo; Prev',
		enableHistory:          false,
		autoStart:              false,
		onChange:               undefined, // accepts a delegate like such: function(prevIndex, nextIndex) { ... }
		onTransitionOut:        function(callback) {
															if($('.gallery-text-titlebar').length > 0) {
																var p_top = $('.page-title').css('top');
																$('.page-title').animate({height: '0px', top: (parseInt(p_top) + 15) + 'px'}, 'slow');
															}
															if($('.gallery-text-subtitle').length > 0) {
																$('.gallery-text-subtitle').slideUp('fast')
															};
															$('.gallery-image').fadeOut('slow', callback);
														}, //undefined, // accepts a delegate like such: function(callback) { ... }
		onTransitionIn:         function() {
															$('.gallery-image').fadeIn();
															if($('.gallery-text-titlebar').length > 0) {
																var p_top = $('.page-title').css('top');
																$('.page-title').animate({height: '50px', top: (parseInt(p_top) - 15) + 'px'}, 'slow');
															}
															if($('.gallery-text-subtitle').length > 0) {
																$('.gallery-text-subtitle').slideDown('slow');
															};
														}, //undefined, // accepts a delegate like such: function() { ... }
		onPageTransitionOut:    undefined, // accepts a delegate like such: function(callback) { ... }
		onPageTransitionIn:     undefined  // accepts a delegate like such: function() { ... }
	});
	$('#scroller').css('top','0px');
});