$(document).ready(function() {

	$('#thumbs ul').hide();
	$('div.content').css('display', 'block');

	// Galleriffic Gallery
	var gallery = $('#thumbs').galleriffic({
		imageContainerSel:      '#slideshow',
		controlsContainerSel:   '#controls',
		enableHistory: true, // Specifies whether the url's hash and the browser's history cache should update when the current slideshow image changes
		delay: 3000, // in milliseconds
		numThumbs: 9999, // The number of thumbnails to show page
		preloadAhead: 5, // Set to -1 to preload all images
		syncTransitions: false, // Specifies whether the out and in transitions occur simultaneously or distinctly
		defaultTransitionDuration: 1000, // If using the default transitions, specifies the duration of the transitions					
	});
	
	$('#slide-prev').click(function(e) {
		gallery.previous();
		e.preventDefault();
	});

	$('#slide-next').click(function(e) {
		gallery.next();
		e.preventDefault();
	});
	
	/**** Functions to support integration of galleriffic with the jquery.history plugin ****/

	// PageLoad function
	// This function is called when:
	// 1. after calling $.historyInit();
	// 2. after calling $.historyLoad();
	// 3. after pushing "Go Back" button of a browser
	function pageload(hash) {
		// alert("pageload: " + hash);
		// hash doesn't contain the first # character.
		if(hash) {
			$.galleriffic.gotoImage(hash);
		} else {
			gallery.gotoIndex(0);
		}
	}

	// Initialize history plugin.
	// The callback is called at once by present location.hash. 
	$.historyInit(pageload, "advanced.html");

	// set onlick event for buttons using the jQuery 1.3 live method
	$("a[rel='history']").live('click', function(e) {
		if (e.button != 0) return true;

		var hash = this.href;
		hash = hash.replace(/^.*#/, '');

		// moves to a new page. 
		// pageload is called at once. 
		// hash don't contain "#", "?"
		$.historyLoad(hash);

		return false;
	});
	
});
