window.console || (console = {log:function(){},warn:function(){}});

$( document ).ready(function() {

	if($(window).width() > 1024) {
		$('.masthead-text-container').css('height', $(window).height() - $('.masthead-vid-thumbnails-container').outerHeight() - $('.navbar-default').outerHeight());

		// Switch the active video when a video thumbnail is clicked
	    $('.vidthumb').click(function(e) {
	    	e.preventDefault();
	    	// $('#vid-default').css('display', 'none');
	    	// $('#vid-4').css('display', 'inline-block');
	    	var oldId = $('.vidthumb.active').attr('id');
	    	var currentId = e.currentTarget.id;

	    	//ga event tracking
	    	var playingPerson = currentId.substr(9);
	    	ga('send', {
	          hitType: 'event',
	          eventCategory: 'Homepage Video',
	          eventAction: 'Video Interaction',
	          eventLabel: playingPerson + ' - Play',
	        });

	    	// Default video (autoplays)
	    	var vidSource = '/video/homepage/loop.mp4'; 
	    	var vidPlaceholder = 'images/vidplaceholders/default.jpg';
	    	
	    	// Other videos (available via click)
	    	if(currentId == 'vidthumb-sarah') {
	    		//vidSource = 'videos/Sarah_Brough_MAC_CounterManager2011.mp4';
	    		vidSource = '/video/homepage/Sarah_Brough_MAC_CounterManager2011.mp4';
	    		vidPlaceholder = 'images/vidplaceholders/sarah.jpg';
	    	}
	    	else if(currentId == 'vidthumb-mark') {
	    		//vidSource = 'videos/Mark Anthony Video low res.mp4';
	    		vidSource = '/video/homepage/Mark Anthony Video low res.mp4';
	    		vidPlaceholder = 'images/vidplaceholders/mark.jpg';
	    	}
	    	else if(currentId == 'vidthumb-nicole') {
	    		//vidSource = 'videos/Nicole Thompson MAC Senior Artist 2011.mp4';
	    		vidSource = '/video/homepage/Nicole Thompson MAC Senior Artist 2011.mp4';
	    		vidPlaceholder = 'images/vidplaceholders/nicole.jpg';
	    	}
	    	else if(currentId == 'vidthumb-alphie') {
	    		//vidSource = 'videos/alphie_sadsad_bobbi_brown_2011.mp4';
	    		vidSource = '/video/homepage/alphie_sadsad_bobbi_brown_2011.mp4';
	    		vidPlaceholder = 'images/vidplaceholders/alphie.jpg';
	    	}
	    	else if(currentId == 'vidthumb-stephan') {
	    		//vidSource = 'videos/Stephen video 2 low rees.mp4';
	    		vidSource = '/video/homepage/Stephen video 2 low rees.mp4';
	    		vidPlaceholder = 'images/vidplaceholders/stephen.jpg';
	    	}
	    	else if(currentId == 'vidthumb-kate') {
	    		//vidSource = 'videos/Kate Moore Product Manager EL and TF 2011.mp4';
	    		vidSource = '/video/homepage/Kate Moore Product Manager EL and TF 2011.mp4';
	    		vidPlaceholder = 'images/vidplaceholders/kate.jpg';
	    	}

	    	var bgVideo = document.getElementById('bgvid');
	    	
			bgVideo.src = vidSource;
			$('#bgvid').attr('poster', vidPlaceholder);

			//setUpVidControls(bgVideo);
			//$('#bgvid').attr('controls', 'controls');
			bgVideo.load();
			bgVideo.play();
			bgVideo.muted = false;

			$('.video-btnPlay').text('Pause');
			$('.video-mute').text('Mute');

			if($('.vidthumb').hasClass('vidthumb-landing')) {
				$('.masthead-vid-thumbnails-container .vidthumb.vidthumb-landing').unbind('mouseenter mouseleave');
				$('.vidthumb').removeClass('vidthumb-landing');
				$('.vidthumb').removeAttr('style');
				$('.vidthumb').hover(function() {$(this).addClass('hovered');}, function() {$(this).removeClass('hovered');});
			}

			if(typeof oldId != 'undefined') { //if there is an 'active' thumbnail
				$('#' + oldId).removeClass('active');
			}
			$('#' + currentId).addClass('active');
			$('.masthead-text-container').css('height', $(window).height() - $('.masthead-vid-thumbnails-container').outerHeight());
			window.scrollTo(0, $('.navbar-default').outerHeight());

			if($(window).width() > 1024) {
				$('.masthead-text-container').css({'opacity': '0'});
				$('.text-over-video').css('display', 'none');
			}

			if(typeof oldId != 'undefined') { //if there is an 'active' thumbnail
				$('#' + oldId).removeClass('active');
			}
	    });

	    var $thumbContainer = $('.masthead-vid-thumbnails-container');
		$('.masthead').hover(function(){
			if(typeof $('.vidthumb.active').attr('id') != 'undefined' //not default vid playing
				&& $(window).width() > 1024) { // not small screen
				$('div.video-control-container').show().delay(5000).fadeOut(300);
				$thumbContainer.css("visibility", "visible").delay(5000).fadeTo(300, 1, function() {
					$thumbContainer.css("visibility", "hidden");
				});
			}
		}, function() {
			$('div.video-control-container').hide();
			if(typeof $('.vidthumb.active').attr('id') != 'undefined' //not default vid playing
				&& $(window).width() > 1024) {
				$thumbContainer.fadeTo(300, 1, function() {
					$thumbContainer.css("visibility", "hidden");
				});
			}
		});
		$('.masthead').mousemove(function() {
			if(typeof $('.vidthumb.active').attr('id') != 'undefined') { //not default vid playing
				$('div.video-control-container').show().delay(5000).fadeOut(300);
				$thumbContainer.css("visibility", "visible").delay(5000).fadeTo(300, 1, function() {
					$thumbContainer.css("visibility", "hidden");
				});
			}
		});

		
	} //end of >1024px
	else { //<= 1024px
		//set the height to viewport height manually for mobile devices since 'vh' do not work properly for some of them
		$('.masthead-text-container').css('height', $(window).height() - $('#elc-menu-header').height()); 
	}

	//add animation to image boxes at homepage
	// $('.elc-homepage-gallery-box').mouseenter(function(e) {
	// 	var thisId = e.currentTarget.id;
	// 	$('#' + thisId).addClass('animated pulse');
	// 	$('#' + thisId).one('webkitAnimationEnd mozAnimationEnd MSAnimationEnd oanimationend animationend', function() {
	// 		$('#' + thisId).removeClass('animated pulse');
	// 	});
	// });
	//add animation to brand logos at homepage
	$('.brand-logo').mouseenter(function(e) {
		var thisId = e.currentTarget.id;
		$('#' + thisId).addClass('animated pulse');
		$('#' + thisId).one('webkitAnimationEnd mozAnimationEnd MSAnimationEnd oanimationend animationend', function() {
			$('#' + thisId).removeClass('animated pulse');
		});
	});
});

/*The preloader page, which fades out when the website is fully loaded.*/
// $(window).load(function(){
//     $('#cover').fadeOut(1000);
//     $('html, body').css('overflow', 'visible'); //make webpage scrollable
// })
