try{Typekit.load();}catch(e){}

function homeBehavior() {
    
}

function projectsBehavior() {
    if ($('.project-index-panel').length) {
        /* Projects Index */
        
        /* If we are on the index, but actually filtered by category
           e.g. /projects/residential/ not /projects/#residential
           we can't use isotope because we don't have the full index.
           So… our links should link to /projects/#residential to
           get us back on track. */
        
        //initialize variables
        var filter = window.location.hash ? window.location.hash.replace('#', '') : 'featured';
        
        //Highlight active link
        $('#sub-menu a[title="'+filter+'"]').parent().addClass('active');
        
        //Filter isotope on click
        $('#sub-menu a').click(function(e){
            e.preventDefault();
            var title = $(this).attr('title');
            window.location.hash = title;
            selectPage(0);
            $('.project-index-panel')
                .isotope({filter: '.'+title})
                .animate({opacity: 1}, 800, function() {
                    $(this).isotope('reLayout', function() {
                        panelNavButtons();
                    });
            });
            $('#sub-menu .active').removeClass('active');
            $(this).parent().addClass('active');
        });
        
        //Setup page buttons
        function panelNavButtons() {
            var width = $('.project-index-panel').width(),
                pages = Math.ceil(width/1150),
                $nav = $('.project-index-nav'),
                links = $nav.find('a').length;
            
            if (links > pages) {
                $nav.find('a:gt(' + (pages - 1) + ')').addClass('remove').fadeOut(350, function() {
                    $(this).remove();
                });
                centerPanelNav();
            } else if (links < pages) {
                for (i = links; i < (pages); i++) {
                    var $link = $('<a href="javascript:void(0)">Page ' + (i + 1) + '</a>');
                    $link.css({'display': 'none'});
                    $('.project-index-nav').append($link);
                    $link.fadeIn();
                }
                centerPanelNav();
            }
        }
        
        //Center page buttone
        function centerPanelNav() {
            var $nav = $('.project-index-nav'),
                link_width = 80;
                nav_width = $nav.find('a:not(.remove)').length * link_width,
                left = 570 - (nav_width/2);
            $nav.animate({'left': left}, 350);
        }
        
        //Animate to page position
        function selectPage(index) {
            $('.project-index-nav .active').removeClass('active');
            $('.project-index-nav a:eq(' + index + ')').addClass('active');
            $('.project-index-container').animate({
                'scrollLeft': index * 1140
            }, {
                duration: 600,
                easing: 'easeOutCubic'
            });
        }
        
        //Bind navigation handlers
        $('.project-index-nav a').live('click', function() {
            var index = $(this).index();
            selectPage(index);
        });
        
        //Initialize isotope
        $('.project-index-panel').isotope({
            itemSelector: '.project',
            layoutMode: 'masonryHorizontal',
            filter: '.'+filter
        }).animate({opacity: 1}, 800, function() {
            $(this).isotope('reLayout', function() {
                panelNavButtons();
                selectPage(0);
            });
        });
        
        //Initialize panel nav
        $('#content').append('<div class="project-index-nav"></div>');
        centerPanelNav();
        
        //Project title effects
        $('.project-title').css({bottom: -75});
        $('.project').css('cursor', 'pointer').click(function() {
            var href = $(this).find('a').attr('href');
            window.location.href = href;
        }).hover(function() {
            $(this).find('.project-title').animate({bottom: 0}, 100);
        }, function() {
            $(this).find('.project-title').animate({opacity: 0}, 200, function() {
                $(this).css({opacity: 1, bottom: -75});
            });
        });
    } else {
        /* Projects Detail */
        
        //initialize variables
        interval = false;
        $('body').data('slideshow', false);
        
        function selectPhoto(i) {
            var $panel = $('.project-photos-panel');
            var $photos = $panel.find('img');
            var photo_count = $photos.length;
            
            if (i < 0) {
                i = photo_count - 1;
            } else if (i > (photo_count - 1)) {
                i = 0;
            }
            
            var $photo = $panel.find('img:eq('+i+')');
            var pos = $photo.position();
            var left = pos.left * -1;
            
            $photos.css({opacity: 0.1});
            $photo.animate({opacity: 1}, 150);
            $panel.stop().animate({left: left}, {duration: 600, easing: 'easeInOutCubic'});
            $('#caption')
                .css({opacity: 0})
                .html($photo.attr('alt') + '&nbsp;')
                .animate({opacity: 1}, 150);
            $('body').data('current-photo', i);
            
        }
        
        function nextPhoto() {
            var i = $('body').data('current-photo');
            selectPhoto(i + 1);
        }
        
        function previousPhoto() {
            var i = $('body').data('current-photo');
            selectPhoto(i - 1);
        }
        
        var panel_width = 0;
        function showNav() {
            $('.project-photos-nav').animate({bottom: 0}, 100);
        }
        
        function hideNav() {
            $('.project-photos-nav').animate({opacity: 0}, 200, function() {
                $(this).css({opacity: 1, bottom: -50});
            });
        }
        
        $('.project-photos-container').hoverIntent({
            over: showNav,
            out: hideNav,
            timeout: 1000
        }).click(function(e) {
            var left = Number($('.project-photos-panel').css('left').replace('px', ''));
            var click = e.offsetX;
            var end = panel_width + left;
            var current = $('body').data('current-photo');
            if (click > end) {
                selectPhoto(current + 1);
            }
        });
        
        $('.project-photos-panel img').each(function() {
            panel_width += $(this).width() + 1;
        }).click(function() {
            var next = $('body').data('current-photo') + 1;
            selectPhoto(next);
        });
        
        //slideshow controls
        function startSlideshow() {
            $('body').data('slideshow', true);
            var i = $('body').data('current-photo');
            selectPhoto(i + 1);
            interval = setInterval(nextPhoto, 3000);
            $('#play').hide();
            $('#pause').show();
        }
        
        function stopSlideshow() {
            $('body').data('slideshow', false);
            clearInterval(interval);
            $('#play').show();
            $('#pause').hide();
        }
        
        function toggleSlideshow() {
            var slideshow = $('body').data('slideshow');
            if (slideshow) {
                stopSlideshow();
            } else {
                startSlideshow();
            }
        }
        
        $('#previous').live('click', function() {
            stopSlideshow();
            previousPhoto();
        });
        
        $('#next').live('click', function() {
            stopSlideshow();
            nextPhoto();
        });
        
        $('#play').live('click', function() {
            startSlideshow();
        });
        
        $('#pause').live('click', function() {
            stopSlideshow();
        });
        
        $('#sub-menu a').each(function(){
            var title = $(this).attr('title');
            $(this).attr('href', '/projects/#'+title);
        });
        
        $('.project-photos-panel').width(panel_width);
        
        $('.project-photos-container').css({overflow: 'hidden'}).append('<div class="project-photos-nav"><div id="controls"><a id="previous" href="javascript:void(0)">Previous</a> <a id="next" href="javascript:void(0)">Next</a></div><div id="caption"></div><div id="play-pause"><a id="play" href="javascript:void(0)">Play</a> <a id="pause" style="display:none;" href="javascript:void(0)">Pause</a></div>');
        selectPhoto(0);
        
        /* Keyboard Navigation */
        $(window).keydown(function(e) {
            var code;
            e = e || window.event;
            if (e.keyCode) {
                code = e.keyCode;
            } else if (e.which) {
                code = e.which;
            }
            if (code == 32) {
                e.cancelBubble = true;
                e.returnValue = false;
                if (e.stopPropagation) {
                    e.stopPropagation();
                    e.preventDefault();
                }
                return false;
            }
        });
        
        $(document.documentElement).keydown(function (e) {
            var KEYCODE_SPACE = 32,
                KEYCODE_LEFT = 37,
                KEYCODE_RIGHT = 39;
            if (e.keyCode === KEYCODE_SPACE) {
                //$(window).scrollTop(0);
                toggleSlideshow();
            } else if (e.keyCode === KEYCODE_LEFT) {
                stopSlideshow();
                previousPhoto();
            } else if (e.keyCode === KEYCODE_RIGHT) {
                stopSlideshow();
                nextPhoto();
            }
        });
    }
}

function teamBehavior() {
    function selectMember(i) {
        if (i !== $('body').data('current-photo')) {
            var $panel = $('#team-photos-panel');
            var $photos = $panel.find('img');
            var photo_count = $photos.length;
            
            if (i < 0) {
                i = 0;
            } else if (i > (photo_count - 1)) {
                i = 0;
            }
            
            var id = $('.team-member:eq('+i+')').attr('id');
            var $photo = $panel.find('img:eq('+i+')');
            var pos = $photo.position();
            var left = (pos.left * -1);
            
            window.location.hash = id;
            $photos.css({opacity: 0.25});
            $photo.animate({opacity: 1}, 150);
            $panel.animate({left: left}, {duration: 600, easing: 'easeInOutCubic'});
            $('.team-member').hide().css({opacity: 0, top: -20});
            $('.team-member:eq('+i+')').show().animate({top: 0, opacity: 1}, {duration: 250, easing: 'easeOutCubic'});
            $('body').data('current-photo', i);
        }
    }
    
    var panel_width = 0;
    $('#team-photos-panel img').each(function() {
        panel_width += $(this).width() + 1;
    }).click(function() {
        var next = $(this).index();
        selectMember(next);
    });
    
    $('#team-photos-panel').width(panel_width);
    $('#team-photos-container')
        .css({overflow: 'hidden'})
        .click(function(e) {
            var left = Number($('#team-photos-panel').css('left').replace('px', ''));
            var click = e.offsetX;
            var end = panel_width + left;
            var current = $('body').data('current-photo');
            if (click > end) {
                selectMember(current + 1);
            }
        });
    
    var hash = window.location.hash.replace('#', '');
    if (hash !== '' && $('#'+hash).length) {
        var index = $('#'+hash).index() - 1;
        selectMember(index);
    } else {
        selectMember(0);
    }
}

$(window).load(function() {
    $('#homepage-slider').nivoSlider({
        effect:'fade', // Specify sets like: 'fold,fade,sliceDown'
        animSpeed:500, // Slide transition speed
        pauseTime:3500, // How long each slide will show
        startSlide:0, // Set starting Slide (0 index)
        directionNav:false, // Next & Prev navigation
        controlNav:false, // 1,2,3... navigation
        keyboardNav:true, // Use left & right arrows
        pauseOnHover:false, // Stop animation while hovering
        manualAdvance:false, // Force manual transitions
        captionOpacity:0 // Universal caption opacity
    });
});

$(document).ready(function() {
    $section = $('body').attr('id');
    if ($section == 'Home') {
        homeBehavior();
    } else if ($section == 'Projects' || $section == 'Private Residence') {
        projectsBehavior();
    } else if ($section == 'Team') {
        teamBehavior();
    }
});
