$(document).ready(function() {

	if( !document.location.href.match('#') ) { // the URL doesn't contain an anchor
		$('#' + $('div.photo:first-child').attr('id')).show().addClass('current');
		$('#' + $('#thumbs li img:first-child').attr('id')).addClass('current_nav');
	}

	var anchor = jQuery.param.fragment();

	// Bind an event to window.onhashchange that, when the history state changes,
	// gets the url from the hash and displays either our cached content or fetches
	// new content to be displayed.
	$(window).bind('hashchange', function(e) {
		if( document.location.href.match('#') ) {
			$('.current').hide().removeClass('current');
			$('.current_nav').removeClass('current_nav');
			
			// Get the hash (fragment) as a string, with any leading # removed. Note that
			// in jQuery 1.4, you should use e.fragment instead of $.param.fragment().
			anchor = jQuery.param.fragment();
			$('#' + anchor).show().addClass('current');
			$('#' + anchor + '-nav').addClass('current_nav');
		}
	});

	// Since the event is only triggered when the hash changes, we need to trigger
	// the event now, to handle the hash the page may have loaded with.
	$(window).trigger('hashchange');

	//Get our elements for faster access and set overlay width
	var		div = $('div.scroller'),
			ul = $('ul.scroller'),
			ulPadding = 4; // unordered list's left margin

	//Get menu width
	var divWidth = div.width();

	//Remove scrollbars
	div.css({overflow: 'hidden'});

	//Find last image container
	var lastLi = ul.find('li:last-child');

	//Set left
	var left = 0;

	//When user move mouse over menu
	div.mousemove(function(e){

		//As images are loaded ul width increases,
		//so we recalculate it each time
		var ulWidth = lastLi[0].offsetLeft + lastLi.outerWidth() + ulPadding;

		var left = (e.pageX - div.offset().left) * (ulWidth-divWidth) / divWidth;
		div.scrollLeft(left);
	});

	// Add Fancybox
	$('.photo a').fancybox({
		'zoomSpeedIn':		300, 
		'zoomSpeedOut':		250,
		'loop':				true
	});

});
