/*UI Controls*/

//Setup pagewide variables
window.carousel_button_timer = null;

$(window).resize(function(){
  	bluejux_scale_smart_crop();
	bluejux_align_projects();
});

function bluejux_scale_smart_crop() {
	if($('div#home_page-carousel').length){
		
		var carousel_aspect_ration = $('div#carousel-container').width() / $('div#carousel-container').height();
		if(carousel_aspect_ration < 1.5){
			$('img.sddg_home').addClass('ar_small');
		}else{
			$('img.sddg_home').removeClass('ar_small');
		}
	}
}

function bluejux_remove_danglers(){
	// make text same height on projects
	
	$("main.people .people_bio p").each(function(){	
		var str = $(this).html().trim();
			var word = ' ';
			var newWord = '&nbsp;';

			// find the index of last time word was used
			// please note lastIndexOf() is case sensitive
			var n = str.lastIndexOf(word);

			// slice the string in 2, one from the start to the lastIndexOf
			// and then replace the word in the rest
			str = str.slice(0, n) + str.slice(n).replace(word, newWord);
			// result abc def test xyz
			$(this).html(str);


	});
	
	$("main.projects .project_desc_copy").each(function(){	
		var str = $(this).html().trim();
			var word = ' ';
			var newWord = '&nbsp;';
			var n = str.lastIndexOf(word);
			str = str.slice(0, n) + str.slice(n).replace(word, newWord);
			$(this).html(str);
	});
	
	$("main.process .project_copy div").each(function(){	
		var str = $(this).html().trim();
			var word = ' ';
			var newWord = '&nbsp;';
			var n = str.lastIndexOf(word);
			str = str.slice(0, n) + str.slice(n).replace(word, newWord);
			$(this).html(str);
	});
}

function bluejux_align_projects(){
	// make text same height on projects
	
	$("main.projects .grid-layout.patern2").each(function(){	
		var maxHeightTitle = 0;
		$("div.project_title_cont", this).css("height", "auto");
		$("div.project_title_cont", this).each(function(){
			if ($(this).height() > maxHeightTitle) { maxHeightTitle = $(this).height(); }
		});
		$("div.project_title_cont", this).css("height", maxHeightTitle);
	});
}


$(document).ready(function(){
	// fill height for smart crop on home page - bluejux 
	bluejux_scale_smart_crop();
	bluejux_remove_danglers();	
	bluejux_align_projects();
		
	//Fade splash screens
	$(".splashscreen-text").each(function(){
		if($(this).attr('id') == 'people-splashscreen-text'){
			var fade_in_timer = 1000;
			var fade_out_delay = 1000;
			var fade_out_timer = 1000;
		}
		else{
			var fade_in_timer = 1000;
			var fade_out_delay = 2000;
			var fade_out_timer = 1500;
		}
		
		$(this).hide().fadeIn(fade_in_timer, function(){
			$(this).delay(fade_out_delay).fadeOut(fade_out_timer);
		});
	});
	
	$(".splashscreen").each(function(){
		if($(this).attr('id') == 'people-splashscreen'){
			var fade_out_delay = 2000;
			var fade_out_timer = 1500;
		}
		else{
			var fade_out_delay = 3000;
			var fade_out_timer = 1500;
		}
		
		$(this).delay(fade_out_delay).fadeOut(fade_out_timer);
	});
	
	
	$(".splashscreen").on('click', function(){
		$(this).hide();
	});
	
	$("main").on('mouseover', function(){
		$('.topbar-nav-submenu').each(function(){
			$(this).hide();
			$(this).find('.topbar-nav--item').hide();
		});
	});

	$(".teaser--item").on('mouseover', function(){
		var $image = $(this).find("img");
		
		if($image){
			var moimage = $image.attr("data-mouseoverimage");
		
			if(moimage){
				var tempsrc = $image.attr("src");
				$image.attr("data-mouseoverimage", tempsrc);
				$image.attr("src", moimage);
			}
		}
	}).on('mouseout', function(){
		var $image = $(this).find("img");
		
		if($image){
			var moimage = $image.attr("data-mouseoverimage");
		
			if(moimage){
				var tempsrc = $image.attr("src");
				$image.attr("data-mouseoverimage", tempsrc);
				$image.attr("src", moimage);
			}
		}
	});
});


function showProjectLightbox(project_id){
	//console.log("Project ID" + project_id);
	$.get("ajax_loader.php",
		{ "data-request": "project-lightbox",
		"project_id": project_id},
		function(data){
			//console.log('data received, calling featherlight');
			//console.log(data);
			$.featherlight(data,{
				variant: 'project-lightbox'
			});
			
			//Now recall carousel setup
			//setupImageCarousels($('.project-lightbox'), false);
		});
}

var countinit = 0;
var cycleStarted = 0;
var whiteSlideDelay = 1000;
var slideShowAnimDuration = 3000;
var slideShowCycleDelay = 6000;

function setupImageCarousels($container, autorun){
	$selector = $('.carousel');
	
	if($container) $selector = $container.find('.carousel');
	//console.log('configuring carousels to transition for ' + slideShowAnimDuration + ' and cycle after ' + slideShowCycleDelay);
	
	cycleStarted = performance.now();
	//Image Carousels
	$selector.cycle({
		slideCss: {}, /*prevent from overriding our positioning css*/
		fx: 'fadeout', 
		speed:  slideShowAnimDuration, 
		timeout: slideShowCycleDelay, 
		prev: ' .cycle-prev',
		next: ' .cycle-next',
		slideResize: false,
		swipe: true,
		paused: true /*start paused so our timing isn't screwy with the white slider. Will resume post init. - Josh 04/13/2020*/
	});
	
	
// animate white slide
	
$( '.carousel' ).on( 'cycle-update-view', function( event, opts ) {
	if(countinit == 0){
		console.log('init complete resuming');
		countinit = countinit + 1;
	
		//Added a default pause to the carousel to ensure our timers aren't thrown off, we'll now resume it and start our slide timer loop - Josh 04/13/2020
		
		
		$(this).cycle('resume');
		setTimeout(showslide, whiteSlideDelay);
	}
});
	
	

	
$( '.carousel' ).on( 'cycle-after', function( event, opts ) {
	//console.log("triggering showslide from cycle-after event");
	setTimeout(showslide, whiteSlideDelay);
});
	
	
function showslide(){
		return; //Disabled 07/11/24
	
	
	  $( ".cycle-overlay" ).animate({
		left: "0%",
	  }, slideShowAnimDuration, function() {
		// Animation complete.
		var delayHideSlide = slideShowCycleDelay - whiteSlideDelay - slideShowAnimDuration;
		 // console.log("Hiding in " + delayHideSlide);
		 setTimeout(hideslide, delayHideSlide);
	  });
}	
	
	
	
function hideslide(){
	 $( ".cycle-overlay" ).animate({
		left: "-100%",
	  }, slideShowAnimDuration, function() {
		// Animation complete.
	  });
}

	
	

	
$(document).keydown(function(e){
		switch(e.which){
			case 37: //left
				$selector.cycle('prev');
				break;
			case 39: //right
				$selector.cycle('next');
				break;
			default: return;
		}
		
		e.preventDefault();
	});
	
	if(autorun === false){
		$selector.each(function(){
			$(this).on('cycle-after', function(event, opts){
				$(this).cycle('pause');
			});
		});
		
		$selector.cycle('pause');
	}
	
	//Autohide carousel buttons when not in use
	window.carousel_button_timer = null;
	$selector.mousemove(function() {
		clearTimeout(window.carousel_button_timer);
		$(".carousel--nav .cycle-prev, .carousel--nav .cycle-next").fadeIn('fast');
		window.carousel_button_timer = setTimeout(function () {
			$(".carousel--nav .cycle-prev, .carousel--nav .cycle-next").fadeOut('slow');
		}, 2000);
	}).mouseleave(function() {
		clearTimeout(window.carousel_button_timer);
		$(".carousel--nav .cycle-prev, .carousel--nav .cycle-next").fadeOut('slow');  
	});
	
	//Autohide touch screen message after brief display
	$(".carousel-swipe_message").show().delay(1000).fadeOut('slow');
}

function ToggleBio(person_id){
	//Is this person's bio showing? If not, show it
	if( ! $("#people-container-" + person_id + " .toggle_bio_link").hasClass("bio-mouseover-show")){
		console.log("Adding show class #people-container-" + person_id + " .toggle_bio_link")
		//First hide all other bios 
		$('.bio-mouseover').removeClass('bio-mouseover-show').fadeOut("fast"); 
	
		// Now display this person's
		$("#people-container-" + person_id + " .bio-mouseover").fadeIn("fast", function(){
			$(this).addClass("bio-mouseover-show");
		});
	}
	else{
		// Just hide all
		$('.bio-mouseover').removeClass('bio-mouseover-show').fadeOut("fast");
	}
}


function doNothing(){
	//Placeholder function to allow having links that only trigger onclick code
}