$(document).ready(function(){

    var inTravel = false;

    /* Waypoint Setup */
    $('nav').waypoint(function(event, direction) {  
       if (direction === 'down') {
           $(this).addClass('sticky');
           $('.home_spacer').addClass('sticky');
        } else {
           $(this).removeClass('sticky'); 
           $('.home_spacer').removeClass('sticky');
        }
    });  


    $('a.header').waypoint(function(event, direction) {  
       if (direction === 'up') {
           $('#about').addClass('up');
           $('#portfolio').addClass('up');
           $('#contact').addClass('up');
        }
    
       
    });

    $('.bottom').waypoint(function(event, direction) {  
       if (direction === 'down') {
           $('#about').removeClass('up');
           $('#portfolio').removeClass('up');
           $('#contact').removeClass('up');
        }
    });

    $('.home_spacer').waypoint(function(event, direction) {  
       if (direction === 'down') {
            $('#about').removeClass('up');
            $('#portfolio').removeClass('up');
            $('#contact').removeClass('up');
        } else {
            if( $('nav li a.active').attr('href') != '#home'  ) {
               $('nav li a').removeClass('active');
               $('nav li a[href="#home"]').addClass('active');
            };
        }
    });

    $('header h2').waypoint(function(event, direction) {  
        var activeSection = '#' + $(this).prev('a').attr('id');
        var activeNav =  $('nav li a.active').attr('href');

        if (activeSection != activeNav && inTravel == false) {
            $('nav li a').removeClass('active');
            $('nav li a[href="'+ activeSection+ '"]').addClass('active');
        };
        
    });

    // Navigation & Smooth scrolling for internal links
    $('nav ul li a').click(function(){
        $('nav ul li a').removeClass('active');
        $(this).addClass('active');
    });

    var scrollElement = 'html, body';
	$('html, body').each(function () {
		var initScrollTop = $(this).attr('scrollTop');
		$(this).attr('scrollTop', initScrollTop + 1);
		if ($(this).attr('scrollTop') == initScrollTop + 1) {
			scrollElement = this.nodeName.toLowerCase();
			$(this).attr('scrollTop', initScrollTop);
			return false;
		}    
	});

	$("a[href^='#']").click(function(event) {
		event.preventDefault();
		
		var $this = $(this),
		target = this.hash,
		$target = $(target);
        inTravel = true;
		
		$(scrollElement).stop().animate({
			'scrollTop': $target.offset().top
		}, 1500, 'swing', function() {
			window.location.hash = target;
            inTravel = false;
		});
		
	});

    /* Lettering Setup */
    $('h1').lettering('lines').children('span').lettering('words').children('span').lettering();
    $('#about h2').lettering();


    /* Logo and Image Rotation Setup */
    if ( $.browser.msie ) {
        $('#img_1').attr('src','images/logo_ie.png');
        $('#img_2').remove(); 
        $('#img_3').remove(); 
        var rotation1 = function (){
               $("#img_1").rotate({
                  angle:0, 
                  animateTo:360, 
                  callback: rotation1,
                  easing: $.easing.easeInOutElastic,
                  duration: 8000
               });
            }
        rotation1();
    } else {
        var rotation1 = function (){
           $("#img_1").rotate({
              angle:0, 
              animateTo:360, 
              callback: rotation1,
              easing: $.easing.easeInOutElastic,
              duration: 8000
           });
        }
        rotation1();

        var rotation2 = function (){
           $("#img_2").rotate({
              angle:360, 
              animateTo:0, 
              callback: rotation2,
              easing: $.easing.easeOutBounce,
              duration: 9000
           });
        }
        rotation2();


        var rotation3 = function (){
           $("#img_3").rotate({
              angle:360, 
              animateTo:0, 
              callback: rotation3,
              easing: $.easing.easeOutBounce,
              duration: 7000
           });
        }
        rotation3();
    }
    

    /* Slide Show */
    $(".main_image .desc").show(); //Show Banner
    $(".main_image .block").animate({ opacity: 0.95 }, 1 ); //Set Opacity

    $(".image_thumb ul li:first").addClass('active'); //Add the active class (highlights the very first list item by default)
    $(".image_thumb ul li").click(function(){
        //Set Variables
        var imgAlt = $(this).find('img').attr("alt"); //Get Alt Tag of Image
        var imgTitle = $(this).find('a').attr("href"); //Get Main Image URL
        var imgDesc = $(this).find('.block').html();  //Get HTML of the "block" container
        var imgDescHeight = $(".main_image").find('.block').height(); //Find the height of the "block"

        if ($(this).is(".active")) {  //If the list item is active/selected, then...
            return false; // Don't click through - Prevents repetitive animations on active/selected list-item
        } else { //If not active then...
            //Animate the Description
            $(".main_image").animate({ opacity: 0 }, 700 , function() { //Pull the block down (negative bottom margin of its own height)
                $(".main_image .block").html(imgDesc); //swap the html of the block
                $(".main_image img").attr({ src: imgTitle , alt: imgAlt}); //Switch the main image (URL + alt tag)
                $(".main_image").animate({ opacity: 1 } , 700 );
            });
        }
        //Show active list-item
        $(".image_thumb ul li").removeClass('active'); //Remove class of 'active' on all list-items
        $(this).addClass('active');  //Add class of 'active' on the selected list
        return false;

    }) .hover(function(){ //Hover effects on list-item
        $(this).addClass('hover'); //Add class "hover" on hover
        }, function() {
        $(this).removeClass('hover'); //Remove class "hover" on hover out
    });    



});


