﻿/* 
	Photo Gallery Player 
	required files: jquery.jcarousel.pack.js, jquery.jcarousel.css, carousel.css
*/

var totalGallery;
var activeImage = false;
var imageGalleryObj = false;
var imageGalleryCaptions = new Array();
// Credits: Robert Penners easing equations (http://www.robertpenner.com/easing/).
jQuery.easing['BounceEaseOut'] = function(p, t, b, c, d) {
	if ((t/=d) < (1/2.75)) {
		return c*(7.5625*t*t) + b;
	} else if (t < (2/2.75)) {
		return c*(7.5625*(t-=(1.5/2.75))*t + .75) + b;
	} else if (t < (2.5/2.75)) {
		return c*(7.5625*(t-=(2.25/2.75))*t + .9375) + b;
	} else {
		return c*(7.5625*(t-=(2.625/2.75))*t + .984375) + b;
	}
};
    
function mycarousel_initCallback(carousel) {
	
	var SLIDE_DELAY = 4000;

	function stopSlideshow(c) {
		document.getElementById('carousel_slideshow_start').style.display = 'block';
		document.getElementById('carousel_slideshow_stop').style.display = 'none';		
		carousel.slideshowON = false;
		
		clearInterval(slideInterval);	
	}

	function startSlideshow(c) {
		document.getElementById('carousel_slideshow_start').style.display = 'none';
		document.getElementById('carousel_slideshow_stop').style.display = 'block';		
		carousel.slideshowON = true;
		
		function slideNext() {
			var oldScrollVal = c.options.scroll;
			var oldWrapVal = c.options.wrap;
			/* switch to single scroll and disable wrap temporarily */
			c.options.scroll = 1;
			c.options.wrap = null;
			c.next();
			c.options.scroll = oldScrollVal;
			c.options.wrap = oldWrapVal;
			/* get next thumb */
			var nextLI = $("#"+currentImageId).next();
			if (nextLI.length > 0) {
				var nextImageId = nextLI[0].id;
				/* show next image */	
				var childA = $("#" + nextImageId + " > a");
				if (childA.length > 0) {
					childA[0].onclick();    //alternative //var jsShowImage = $(childA[0]).onclick.split(';')[0]; // get first statement only //eval(jsShowImage);	
				}
				/* stop slideshow if reached right most position */
				if (nextLI.next().length == 0) stopSlideshow(c);
			}
			
			return false;
		}
		
					/* rewind to beginning if at end of carousel*/
		if ($("#"+currentImageId).next().next().length == 0)  {
			c.scroll(1);
			$("li a.thumb")[0].onclick(); // select first thumb in carousel			
		}

		
		/* start slideshow */
		slideInterval = setInterval(slideNext, SLIDE_DELAY);
	}	

	function pauseSlideshow(c) {
		clearInterval(slideInterval);	
	}

	jQuery('#carousel_slideshow_start').bind('click', function() { /* bind click on object of choice to start slideshow */			
		startSlideshow(carousel);
		return false;
	});

	jQuery('#carousel_slideshow_stop').bind('click', function() { /* bind click on object of choice to start slideshow */
		stopSlideshow(carousel);
		return false;
	});

	/* pause slideshow when mouse is over carousel */
	carousel.clip.hover(function() { if (carousel.slideshowON) pauseSlideshow(carousel);  }, 
						function() { if (carousel.slideshowON) startSlideshow(carousel);  });	

	/* stop slideshow when any carousel thumb is clicked on */
	carousel.clip.click(function() { if (carousel.slideshowON) stopSlideshow(carousel);	});
};  

function mycarousel_itemFirstInCallback(carousel, obj, index, state) {		
	
	// get collection of captions and match to index
	if ($("div.imageCaption").size() > 0) {	
		divs = $("div.imageCaption");
	
		for(var no=0;no<divs.length;no++){
			if(divs[no].className=='imageCaption') {
				imageGalleryCaptions[imageGalleryCaptions.length] = divs[no].innerHTML; 
			}		
		}	
	} else {
		divs = 0;
	}
}

$(document).ready(function() {
	jQuery('#mycarousel').jcarousel({
		easing: 'BounceEaseOut',
		animation: 1800,
		scroll: 7,
		wrap: 'both',
		initCallback: mycarousel_initCallback,
		itemFirstInCallback : mycarousel_itemFirstInCallback
	});
	
	
	// on page load, enumerate image entries to show total count
	$(function() {
		totalGallery = $("li a.thumb").length;		
		$("#gallery_stats #total_number").html(totalGallery);		
	});    
	$("#gallery_stats #current_photo_number").html("1");
	
	// select first thumb in carousel
	//$("#mycarousel").attr("class","thumb1");
	//$("li a.thumb")[0].onclick();
	
	$("#gallery_stats #current_photo_number").html("1");
	$("#mycarousel").toggleClass("thumb1");
	
	currentImageId = $('#mycarousel a:eq(0)').parent().attr('id'); // store first (selected) image in global var, used by slideshow to start from the currently selected image
	 
});
	
/* script for showing the large images */

function showImage() {
	if(activeImage){
		activeImage.style.filter = 'alpha(opacity=50)';	
		activeImage.style.opacity = 0.5;
	}	
	this.style.filter = 'alpha(opacity=100)';
	this.style.opacity = 1;	
	activeImage = this;	
}

function showPreview(imagePath,imageIndex) {
	var liElemId = $('.thumb')[imageIndex-1].parentNode.id;
	
	$("#gallery_stats #current_photo_number").html(imageIndex);
	
	document.getElementById('mycarousel').className = "jcarousel-list jcarousel-list-horizontal " + liElemId;

	var subImages = document.getElementById('bigphotoholder').getElementsByTagName('IMG');
	if(subImages.length==0){
		var img = document.createElement('IMG');
		document.getElementById('bigphotoholder').appendChild(img);
	}else img = subImages[0];

	if (document.getElementById('largeImageCaption')) {
		document.getElementById('largeImageCaption').style.display='none';
	}
	img.onload = function() { 
		if(divs != 0) {
			ShowCaption(imageIndex-1); 
		}
	};
	img.src = imagePath;
	
	currentImageId = liElemId; // store currently selected thumb in global var, used by slideshow to start from the currently selected image

}

function ShowCaption(imageIndex) {	
	if (document.getElementById('largeImageCaption')) {
		document.getElementById('largeImageCaption').innerHTML = imageGalleryCaptions[imageIndex];
		document.getElementById('largeImageCaption').style.display='block';
	}
}

/* show photo captions */
$(function() {
    $(".festivals_photos a img").mouseover(function() {
        var caption = $(this).parent().siblings("p.caption").html();

        $(".festivals_photos a img").removeClass("selected");
        $(this).toggleClass("selected");
        $(this).parents("ul").siblings("p.caption_display").html(caption);
    });
});