/* Author: Dave Gareth Lewis */
$(document).ready(function() {
    
    if (navigator.userAgent.match(/iPhone/i) || navigator.userAgent.match(/iPad/i)) {
        iosYes = true;
        $('#parallax-bg1, #parallax-bg2, #parallax-bg3').css('display','none');
    } else {
    	iosYes = false;
    };
    
    //try to make ios resize bug go away
    if (iosYes){
	    var viewportmeta = document.querySelector('meta[name="viewport"]');
	    if (viewportmeta) {
	        viewportmeta.content = 'width=device-width, minimum-scale=1.0, maximum-scale=1.0, initial-scale=1.0';
	        document.body.addEventListener('gesturestart', function () {
	            viewportmeta.content = 'width=device-width, minimum-scale=0.25, maximum-scale=1.6';
	        }, false);
	    };
    };
    
// parallax scrolling code adapted from Jonathan Nicol (f6design.com)
    function parallaxScroll(){
    	var scrolled = $(window).scrollTop();
    	$('#parallax-bg1').css('top',(0-(scrolled*.1))+'px');
    	$('#parallax-bg2').css('top',(0-(scrolled*.3))+'px');
    	$('#parallax-bg3').css('top',(0-(scrolled*.85))+'px');
    };
     
// Set nav active state
    function navActive(){
    	// The top of each section is offset by half the distance to the previous section.
    	var section1Top =  $('#samples').offset().top;
    	var section2Top =  $('#bio').offset().top - (($('#contact').offset().top - $('#bio').offset().top) / 2);
    	var section3Top =  $('#contact').offset().top - (($(document).height() - $('#contact').offset().top) / 2);
    	$('.navMain').removeClass('navActive');
            
    	if($(document).scrollTop() >= section1Top && $(document).scrollTop() < section2Top){
    		$('a#linkSamples').addClass('navActive');
    	} else if ($(document).scrollTop() >= section2Top && $(document).scrollTop() < section3Top){
    		$('a#linkBio').addClass('navActive');
    	} else if ($(document).scrollTop() >= section3Top) {
    		$('a#linkContact').addClass('navActive');
    	};	
    };
     
// video box
    $('.playBox').hover(
        function () {
            $(this).find('span').fadeTo(100, 1);
        },
        function () {
        	$(this).find('span').fadeTo(100, 0.6);
        }
    );
    
	vidMarkA = '<iframe width="640" height="360" src="http://www.youtube.com/embed/';
	vidMarkB = '?&autoplay=1&controls=0&modestbranding=1&rel=0" frameborder="0" allowfullscreen></iframe>';
	
//video IDS
	sampVidID = {
	    "sampleSN1": "-tCDQ5kyThQ", // sun
	    "sampleSN2": "HAPsHcdU3hU", // city
	    "sampleSN3": "TaoAlXEzzBc", // snow
	    "sampleSN4": "8gHK8flZSrY" // forest
	};
	
    $('.playBox').click(function(){
		thisFig = $(this).closest('section').attr('id');
		thisVidID = sampVidID[thisFig];
		$(this).html(vidMarkA + thisVidID + vidMarkB);
//		 responsive width for video plugin    
		$(".playBox").fitVids();
		return false;
    });


// next video buttons
	$('.nextSample a').click(function(){
		nextSamp = $(this).closest('section').next('section');
		if($(nextSamp).length !== 0){
			$('html, body').animate({
				scrollTop:$(nextSamp).offset().top
			}, 400);
		};
		return false;
	});
    
    $('.toTop a').click(function(){
    	$('html, body').animate({
    		scrollTop:$('#container').offset().top
    	}, 400);
    	return false;
    });
    
// Scroll event handler
    if(!iosYes){
	    $(window).bind('scroll',function(e){
	    	parallaxScroll();
			navActive();
	    });
    };
    
// nav btn click handlers

	$('a#linkSamples').click(function(){
    	$('html, body').animate({
    		scrollTop:$('#samples').offset().top
    	}, 800, function() {
	    	parallaxScroll(); // Callback is required for iOS
		});
    	return false;
	});
	$('a#linkBio').click(function(){
		$('html, body').animate({
			scrollTop:$('#bio').offset().top
		}, 800, function() {
	    	parallaxScroll(); // Callback is required for iOS
		});
		return false;
	});
	$('a#linkContact').click(function(){
		$('html, body').animate({
			scrollTop:$('#contact').offset().top
		}, 800, function() {
	    	parallaxScroll(); // Callback is required for iOS
		});
		return false;
	});
	
	navActive();

});
