
//FIXED BANNER ON SCROLL TO TOP

// ELEMENTS
var cpro = $("#cpro");
var header = $("#header");
var top = $("#top");


function fixOverlayElementHeight() {
	var gal1_height = $("#gal1 img").outerHeight();
	
	$(".overlay").css('height', gal1_height);
	$(".hover-box").css('height', gal1_height);
}


function fixHeroHeight() {
	var hero_img_1 = $("#hero-img-1");
	var hero_img_2 = $("#hero-img-2");
	var hero_img_3 = $("#hero-img-3");
	
	var three_heros = [hero_img_1.outerHeight(), hero_img_2.outerHeight(), hero_img_3.outerHeight()];
	var min_hero_height = Math.min.apply(null,three_heros);
	var max_hero_height = Math.max.apply(null,three_heros);

	var slideshow = $("slideshow");
	$("#slideshow").css('height', max_hero_height);

	//alert('hero img heights: ' + three_heros.join(', ') + "\nmin: " + min_hero_height)
}


function switchBanner() {
	var eTop = $('#banner-marker').offset().top; //get the offset top of the element
	var headerheight = header.outerHeight(); //get height of header
	var cproheight = cpro.outerHeight(); //get height of cpro banner
	log(eTop - $(window).scrollTop(), headerheight, cproheight); //position of the ele w.r.t window
	
	//use this if we need to shut off under screen width 480
	if ($(window).width()>480){
	}
	
	if ((eTop - $(window).scrollTop()) < headerheight) {
		cpro.addClass("fix-cpro");
		$("#back-to-form").show(); //show button to return to form
		$("#cpro").css('top', headerheight); //make sure cpro is set wherever the header height ends
		
		//CPRO width animation
		/*var winWidth = $( window ).width();
		var cproWidth = 0.92 * winWidth;
		if (winWidth > 1000) winWidth = 1000;
		$("#cpro").css('max-width', winWidth);
		$("#cpro").animate({ width: winWidth }, 'slow');
		$("#cpro").css('width', winWidth);*/
		
		
		  /*$( "#nav" ).slideUp( "slow", function() {
			// Animation complete.
		  });*/
		  /*$('#nav-box').append( $('#nav') );*/
		
		
	} else {
		cpro.removeClass("fix-cpro");
		$("#back-to-form").hide(); //hide button to return to form
		
		//CPRO width animation
		/*var cproMaxWidth = $("#wrapper").width();
		if ($("#cpro").width() > cproMaxWidth ) {
			$("#cpro").css('max-width', cproMaxWidth);
			$("#cpro").animate({ width: cproMaxWidth }, 'slow');
			$("#cpro").css('width', cproMaxWidth);
		}*/
		
		/*$( "#nav" ).show( "slow" );*/
		/*$('#header').append( $('#nav') );*/
	}
}


function fixBannerHeight() {
	var eTop = $('#banner-marker').offset().top; //get the offset top of the element
	var headerheight = header.outerHeight(); //get height of header
	var cproheight = cpro.outerHeight(); //get height of cpro banner
	log(eTop - $(window).scrollTop(), headerheight, cproheight); //position of the ele w.r.t window
	
	cproheight = cpro.outerHeight(); //get height of cpro banner	
	$("#top").css('height',cproheight); //fix height on object beneath banner to make "snapping" seamless
}




function log(txt, txt2, txt3) {
	$("#log").html("location : <b>" + txt + "</b> px <br>header height:  <b>" + txt2 + "</b> px <br>cpro height:  <b>" + txt3 + "</b> px")
}

$(document).ready(function(){

	//TEST ZONE 
	
	
	//INITIAL RUN ON THE FIXES
	fixHeroHeight(); //first run to update height on load
	fixBannerHeight();
	fixOverlayElementHeight();
	$("#hidden-testy-box").hide();
	$("#back-to-form").hide();
	
	$(window).scroll(function() { //when window is scrolled
		switchBanner();
	});
	
	// on 'touchmove' for mobile scrolling
	$('body').on({
		'touchmove': function(e) {
			switchBanner();
		}
	});
	
	$(window).resize(function() { //when window is resized
		switchBanner();
		fixBannerHeight(); //fix banner height for seamless switch
		fixHeroHeight(); //fix hero height if window is resized
		fixOverlayElementHeight();
	});
	
	
	
	//SLIDESHOW CODE
	$("#slideshow > div:gt(0)").hide();
	fixHeroHeight();
	
	setInterval(function() {
	  fixHeroHeight();
	  $('#slideshow > div:first')
		.fadeOut(1000)
		.next()
		.fadeIn(1000)
		.end()
		.appendTo('#slideshow');
	}, 7000);



	//TESTIMONIAL BOXES 
	$("#exit-testy-box").click(function(){
		$("#hidden-testy-box").hide();
		$("#testy-box1 li").css("margin-bottom", "5%");
	});
	$("#show-testy-box").click(function(){
		$("#hidden-testy-box").show();
		$("body, html").animate({ 
		  scrollTop: $('#hidden-testy-box').offset().top
		}, 600);
		$("#testy-box1 li").css("margin-bottom", "-3%");
	});
	
	
	
	//SCROLL ANIMATION TO GO BACK TO THE FORM ON CLICK
	$(".jumper").on("click", function( e ) {
		e.preventDefault();

		$("body, html").animate({ 
		  scrollTop: $('#banner-marker').offset().top - header.outerHeight()
		}, 600);
	});
	
	
	
	//NAV LINKS TO SECTIONS OF PAGE
	$("#earn-points-nav").click(function(){
		var headerheight = header.outerHeight(); //get height of header
		var cproheight = cpro.outerHeight(); //get height of cpro banner
		var scrollPoint =  $('#earn-points').offset().top - (headerheight + cproheight);
		
		$("body, html").animate({ 
		  scrollTop: scrollPoint
		}, 600);
		
	});
	
	$("#products-nav").click(function(){
		var headerheight = header.outerHeight(); //get height of header
		var cproheight = cpro.outerHeight(); //get height of cpro banner
		var scrollPoint =  $('#products').offset().top - (headerheight + cproheight);
		
		$("body, html").animate({ 
		  scrollTop: scrollPoint
		}, 600);
		
	});
	
	$("#member-nav").click(function(){
		var headerheight = header.outerHeight(); //get height of header
		var cproheight = cpro.outerHeight(); //get height of cpro banner
		var scrollPoint =  $('#member').offset().top - (headerheight + cproheight);
		
		$("body, html").animate({ 
		  scrollTop: scrollPoint
		}, 600);
		
	});
	
});


