var t = null;

function allowPause() {
	$('.photoBox').hover(pauseSlide, resumeSlide);
	$('#imgLion').hover(pauseSlide, resumeSlide);
}

function launchSlide() {
	slideOver();
	t = setInterval(slideOver, 3500)
}

function pauseSlide() {
	if ($(this).attr('id').match('photoBox')) {
		greyAll();
		var img = $(this).children('a').children('img').first();
		img.attr('src', img.attr('src').replace("gallery/thumbs", "gallery/popthumbs"));
	}
	clearInterval(t);
	t = null;
}

function resumeSlide() {
	if (t === null) {
		t = setTimeout(launchSlide, 1000);
	}
}

function greyAll() {
	$('.photoLink > img').map(function() {
		var src = $(this).attr('src');
		if (src.match("gallery/popthumbs")) {
			$(this).attr('src', src.replace("gallery/popthumbs", "gallery/thumbs"));
		}
	});
}

function anyPop() {
	var pop = false;
	$('.photoLink > img').map(function() {
		var src = $(this).attr('src');
		if (src.match("gallery/popthumbs")) {
			pop = true;
		}
	});
	return pop;
}

function slideOver() {
	greyAll();
	$('.photoBox').animate({left: '-=170'}, 1000, resetSlide);
}

function resetSlide() {
	$('.photoBox').map(function() {
		if($(this).css('left') == '-170px') {
			$(this).css('left', '1360px');
		}
		if(!anyPop()) {
			if($(this).css('left') == '340px') {
				var img = $(this).children('a').children('img').first();
				img.attr('src', img.attr('src').replace("gallery/thumbs", "gallery/popthumbs"));
			}
		}
	});
}

$(document).ready(function(){
	$('#imgLion').css('display', 'none');
	$('.photoBox').css('display', 'block');
});

$(window).load(function(){
	$('#imgLion').fadeIn(1000, function() {
		setTimeout(launchSlide, 1000);
		setTimeout(allowPause, 4550);
	});
});

