var totalWidth = 0;

 jQuery(function( $ ){
 
	$("#content").click(function(){
    	window.location=$(this).find("a").attr("href"); return false;
		alert('test');
	});
 
	//Fix body width
	fixBodyWidth();
	
	$(window).resize(function() {
		fixBodyWidth();
	});
	
	//Lazy load images
	//See http://www.appelsiini.net/projects/lazyload
	$('#gallery img').lazyload({         
		 placeholder : '../themes/hi/loading.gif',
		 threshold : 500,
		 effect : "fadeIn"
	});
	
	$('a.first').click(function(){
		$('#gallery li').removeClass('selected');
		$('#gallery li:first').addClass('selected');
		$.scrollTo( 0, 1200, { 
			easing: 'easeOutQuart', 
			onAfter:function(){
				$('body').trigger('notify', 0);
			}
		});
	});
	
	$('#gallery li:first').addClass('selected');
	
	$('#gallery li').hover(
		function(){
			$(this).addClass('hovered');
			$(this).find('a').after('<div class="description">' + $(this).find('img').attr('alt') + '</div>');
		},
		function(){
			$(this).removeClass('hovered');
			$(this).find('div.description').remove();
		}
	);

	$('body').serialScroll({
		items:'#gallery li',
		prev:'#gallery-album a.prev',
		next:'#gallery-album a.next',
		onBefore:function(e,el,$p,$i,pos){
		  $('#gallery li').removeClass('selected');
		  $('#gallery li div.description').remove();
		  
		  $('#gallery li').eq(pos).addClass('selected');
		  $('#gallery li').eq(pos).find('a').after('<div class="description">' + $('#gallery li').eq(pos).find('img').attr('alt') + '</div>');
		},
		offset: -350,
		duration:1200,
		force:false,
		stop:true,
		lock:false,
		cycle:false, //don't pull back once you reach the end
		easing:'easeOutQuart', //use this easing equation for a funny effect
	});
});

function fixBodyWidth(){
	$('#gallery li').each(function(){
		//Add li width
		totalWidth += $(this).outerWidth();
		
		//Add margin
		totalWidth +=  100;	
	});
	//Add sidebar width
	totalWidth += 325;
	$('body').width(totalWidth);
}

// Easing equation, borrowed from jQuery easing plugin
// http://gsgd.co.uk/sandbox/jquery/easing/
jQuery.easing.easeOutQuart = function (x, t, b, c, d) {
	return -c * ((t=t/d-1)*t*t*t - 1) + b;
};
