
$(function(){
	// GALLERIFIC JQUERY IMAGE GALLERY
	var onMouseOutOpacity = 1;
	if(onMouseOutOpacity < 1 && onMouseOutOpacity >= 0){
	$('#thumbs-adv ul.thumbs li').css('opacity', onMouseOutOpacity)
		.hover(
			function () {
				$(this).not('.selected').fadeTo('fast', 1.0);
			},
			function () {
				$(this).not('.selected').fadeTo('fast', onMouseOutOpacity);
			}
		);
	}
		$("#thumbs-adv a[rel=lightbox]").lightBox({useOverlayInIE:false, fixedNavigation:true, imageRoot:"/cirkuit/includes/"});
		
	var galleryCurrentPage = 0;
	var showingFirstImageOfNewPage = false;
	var galleryInTransition = false; //double-click fix hack
	var galleryAdv = $('#thumbs-adv').galleriffic({
		delay:                  5000,
		numThumbs:              15,
		preloadAhead:           10,
		enableTopPager:         true,
		enableBottomPager:      true,
		maxPagesToShow:         6,
		imageContainerSel:      '#slideshow-adv',
		controlsContainerSel:   '#controls-adv',
		captionContainerSel:    '#caption-adv',
		loadingContainerSel:    '#loading-adv',
		renderSSControls:       false,
		renderNavControls:      false,
		playLinkText:           'Play Slideshow',
		pauseLinkText:          'Pause Slideshow',
		prevLinkText:           '< Previous Image',
		nextLinkText:           'Next Image >',
		nextPageLinkText:       'Next Page >',
		prevPageLinkText:       '< Prev Page',
		enableHistory:          false,
		autoStart:              false,
		syncTransitions:		false, //allows overlapping transitions
		defaultTransitionDuration: 400,
		onSlideChange:          function(prevIndex, nextIndex) {
			$('#thumbs-adv').find('ul.thumbs').children()
				.eq(prevIndex).fadeTo('fast', onMouseOutOpacity).end()
				.eq(nextIndex).fadeTo('fast', 1.0);
		},
		onTransitionOut:        function(slide, caption, isSync, callback){
			galleryInTransition = true;  //double-click fix hack
			$(slide).css('display','block').fadeTo('fast', 0, function(){
				galleryInTransition = false;  //double-click fix hack
				callback();
			});
		},
		onTransitionIn:         function(slide, caption, isSync) {
						if(showingFirstImageOfNewPage){ //first image lightbox fix when not using slideshow
				showingFirstImageOfNewPage = false;
				return;
			}
						if (galleryInTransition) { //double-click fix hack
				$(slide).remove(); 
				$(caption).remove();
				return;
			}
			$(caption).show().css('filter','');
			$(slide).css('display','block').fadeTo(150, 1.0);
			$("a[rel=lightbox]", caption).lightBox({useOverlayInIE:false, fixedNavigation:true, imageRoot:"/cirkuit/includes/"});
			
						//lightbox from thumbnail click
			var $thumbs = $("#thumbs-adv");
			$("a[rel=lightbox]:eq("+this.currentImage.index+")", $thumbs).click();
			$(".selected", $thumbs).removeClass("selected");
					},
		onPageTransitionOut:    function(callback) {
			showingFirstImageOfNewPage = true;
			if(galleryCurrentPage == this.currentImage.index){ //first time the page loads, this gets called. dont animate this
				callback();
				return;
			}
			var showNextPage=false, showPrevPage=false;
			if(this.currentImage.index == 0 || this.currentImage.index < galleryCurrentPage) showPrevPage=true;
			else showNextPage=true;
			galleryCurrentPage = this.currentImage.index;
								//hide items by sliding them out
					$thumbs = $('#thumbs-adv ul.thumbs');
					$thumbsWrapper = $("#thumbsWrapper");
					if ($thumbsWrapper.length == 0) {
						$thumbsWrapper = $('#thumbs-adv .thumbs').wrap('<div id="thumbsWrapper" />').parent().css('overflow','hidden');
					}
					
					if ($.browser.msie && parseInt(jQuery.browser.version.substr(0, 1)) <= 7) {
						$thumbs.animate({
								opacity: 0
							}, {
								duration: 100,
								complete: callback
							});
					}
					else {
						$thumbs.css('position', 'relative');
						if (showNextPage) { //slide left
							$thumbs.animate({
								left: '-110%'
							}, {
								duration: 200,
								complete: callback
							});
						}
						else { //slide right
							$thumbs.animate({
								left: '110%'
							}, {
								duration: 200,
								complete: callback
							});
						}
					}
							},
		onPageTransitionIn:     function() {
								//hide items by sliding them out
					$thumbs = $('#thumbs-adv ul.thumbs');
					$thumbsWrapper = $("#thumbsWrapper");
					if ($thumbsWrapper.length == 0) {
						$thumbsWrapper = $('#thumbs-adv .thumbs').wrap('<div id="thumbsWrapper" />').parent().css('overflow','hidden');
					}
					
					$thumbs.css({
						position:'static',
						top: 0,
						left: 0,
						'height':$thumbs.parent().height()
					});
					$thumbs.css('opacity','');
					$thumbs.css('height','');

					$thumbs.fadeIn('fast');
								$(".selected", galleryAdv).removeClass("selected").fadeTo(1, onMouseOutOpacity);
					}
	});
	
		$(".selected", galleryAdv).removeClass("selected").fadeTo(1, onMouseOutOpacity);
		
	//huge hack so clicking the lightbox next buttons moves the image gallery. had to use mouseup cause click gets caught early in lightbox
	$('#lightbox-nav-btnPrev, #lightbox-nav-btnNext').live('mouseup', function(){
		if(this.id == 'lightbox-nav-btnPrev')
			galleryAdv.previous();
		else galleryAdv.next();
	});
});

