$(document).ready(collective);

function collective() {

	if ($(window).width() < 600) {
		windowWidth();
		return;
	} 

	$('.gallery .thumbs .img').each(function(index) {
		$(this).data('i',index);
		$(this).click(function() { showImage($(this).data('i')); return false; })
	});
	
	$('#wrapper').append('<div class="next">&nbsp</div><div class="prev">&nbsp</div><div class="loading">&nbsp</div>');
	
	$('.loading').hide();
	
	$('.next').click(function() {
		var i = $('.gallery').data('i') 
		i = i == $('.gallery .thumbs .img').length - 1 ? 0 : i + 1;
		showImage(i);
	});
	
	$('.prev').click(function() {
		var i = $('.gallery').data('i') 
		i = i == 0 ? $('.gallery .thumbs .img').length - 1 : i - 1;
		showImage(i);
	});
	
	showImage(0);
}

function windowWidth() {
	//if ($(window).width() < 800) {
		$('#wrapper').width($(window).width() - 10);
		$('#bigpicture, .next, .prev').hide();
		$('#footer div').attr('style', '');
		$('.thumbs .img').each(function() {
			$(this).addClass('mobile');
			var w = $('#wrapper').width() - 0;
			var src= '/phpThumb/phpThumb.php?src=../' + $(this).find('a').attr('href') + '&w=' + w;
			$(this).find('img').attr('src', src);
			$(this).click(function() { return });
			$(this).append('<div class="minfo">'+ $(this).find('a').attr('title') +'</div>');
		});
	//}
}

function showImage(i) {
	$('#bigpicture').css('opacity', 0.3);
	$('.loading').show();
	$('.gallery').data('i', i)
	$('.gallery .thumbs .img').css('opacity', 0.3);
	$('.gallery .thumbs .img').eq(i).css('opacity', 1);
	var src = $('.gallery .thumbs .img').eq(i).find('a').attr('href');
	var text = $('.gallery .thumbs .img').eq(i).find('a').attr('title');
	$('#bigpicture img').attr('src', src).load(imageLoaded)
	$('#bigpicture .info').text(text); 
}

function imageLoaded() {
	$('.loading').hide();
	$('#bigpicture').css('opacity', 1);
}
