if(!window['juncture']) {
	window['juncture'] = {};
	var juncture = window['juncture'];
}

(function(){
	g = google;
	
	$(document).ready(function() {
		juncture.heroshots();
	});
	
	juncture.heroshots = function() {
		var heroshot = $('.heroshot-animated');

		var fade = parseInt( heroshot.attr('data-transition'), 10 ) * 1000;
		var dur = parseInt( heroshot.attr('data-show'), 10) * 1000;

		var images = heroshot.find('img');
		images.eq(0).show();
		
		var prev = $('<a href="#prev" class="prev" title="Previous">Previous</a>');
		var next = $('<a href="#next" class="next" title="Next">Next</a>');
		
		heroshot.append(prev).append(next);

		if( images.size() < 2 ) { return; }

		juncture.heroshots.go = function() {
			juncture.heroshots.interval = setInterval(function(){
				var currentImage = images.filter(':visible');
				var toImage = images.index(currentImage) < images.size() - 1 ? images.eq(images.index(currentImage) + 1) : images.eq(0);
				currentImage.fadeOut(fade);
				toImage.fadeIn(fade);
			}, fade + dur);
		}
		juncture.heroshots.go();
		
		prev.add(next).click(function(c) {
			c.preventDefault();

			if(images.filter(':animated').size()) { return; }

			clearInterval(juncture.heroshots.interval);
			var currentImage = images.filter(':visible');
			if($(this).is('.prev')) {
				var toImage = images.index(currentImage) > 0 ? images.eq(images.index(currentImage) - 1) : images.eq(images.size() - 1);
			} else {
				var toImage = images.index(currentImage) < images.size() - 1 ? images.eq(images.index(currentImage) + 1) : images.eq(0);
			}
			currentImage.fadeOut(fade);
			toImage.fadeIn(fade, function(){
				juncture.heroshots.go();
			});
		});
	}
})();