    var direction = 'prev';
    var newPosition = 3000;
    var linkLocation;
    var currentColour = $('body').attr('class');
    var targetColour = $('body').attr('class');
    var currentPosition = $('body').attr('class');
    var targetPosition = $('#logo').attr('class');
    var targetHex;

    var lightgreyHex = '#d5d6d2';
    var darkgreyHex = '#616365';
    var greenHex = '#69be28';
    var slidecount = $('#background img').length;

    $(document).ready(function(){ 
        
        swapValue = [];
        $("#Search input").each(function(i){
           swapValue[i] = $(this).val();
           $(this).focus(function(){
              if ($(this).val() == swapValue[i]) {
                 $(this).val("");
              }
              $(this).addClass("focus");
           }).blur(function(){
              if ($.trim($(this).val()) == "") {
                 $(this).val(swapValue[i]);
             $(this).removeClass("focus");
              }
           });
        });

        ResetOverlayBackground();
        
        tiles = $(".block").fadeTo(0,0.01);

        $(window).scroll(function(d,h) {
            checkFades();
        });

        $("#overlay").fadeIn('fast',function(){
            $("#background").fadeIn('slow');
        });

        $("#trigger").hover(
            function() {
                $(this).children('a').addClass('highlight');
            },
            function() {
                $(this).children('a').removeClass('highlight');
            }
        );

        $("#trigger").click(function(){
            $("#trigger").hide();
            $("#overlay").fadeOut('slow');
            $("#slide-carousel").fadeIn('slow');
        });

        $('#Search input').focus(function() {
            $("#Search").stop().animate({ opacity: '1' }, 250);
            $("#Search input").css({ color: '#616365' });
        }).blur(function() {
            $("#Search").stop().animate({ opacity: '0.5' }, 250);
            $("#Search input").css({ color: '#d5d6d2' });
        });

        $('.gridmenu ul li').hover(
            function() {
                $(this).addClass('hovered');
                $(this).siblings().stop().animate({ opacity: '0.5' }, 250);
            },
            function() {
                $(this).siblings().stop().animate({ opacity: '1' }, 250);
                $(this).removeClass('hovered');
            }
        );

        $('.gridmenu ul li').mousedown(function(){
            $(this).siblings().stop().animate({ opacity: '1' }, 250);
        });


        $('#footer').hover(
            function() {
                $(this).fadeTo('fast',1);
            },
            function() {
                $(this).fadeTo('fast',0.5);
        });
        

        $(".staff").click(function(){
            if (!$(this).hasClass('active')){
                $('.active').removeClass('active');
                $("#staff-description").hide();
                $(this).fadeTo('fast',1);

                $('#loading').fadeIn('fast');

                var jsonHref = $(this).attr('rel');
                                        
                $.getJSON(jsonHref, function(data) {
                    $("#staff-description h2").html(data.Position);
                    $("#PersonDescription").html(data.Content);
                    $("#PersonLink").attr('href',data.Link);

                    $('#loading').fadeOut('fast');                    
                    $('#details').fadeIn('fast');                    
                });

                var currentStaffPosition = $(this).position();
                var staffDescriptionTop = currentStaffPosition.top + 145;
                $("#staff-description").css('top',staffDescriptionTop);
                $("#staff-description").slideToggle('fast');
                $(this).addClass('active');
                $(".staff:not(.active)").fadeTo('fast',0.25);
            } else {
                $('.active').removeClass('active');
                $('#details').fadeOut('fast');  
                $('#loading').fadeIn('fast');                    
                $("#staff-description").slideToggle('fast');
                $(".staff").fadeTo('fast',1);
            }
        });

        $('.filter-category span').click(function(){
            if ($(this).siblings('.filter-options:visible').length){
                $('.filter-options:visible').slideToggle('fast');
                $('.active').removeClass('active');
            }
            else {
                $('.filter-options:visible').slideToggle('fast');
                $('.active').removeClass('active');
                $(this).siblings('.filter-options').slideToggle('fast');
                $(this).children('a').addClass('active');
            }
        });

        $(".project,.practice,.recognition,.people,.staff").live({
            mouseenter:
                function()
                {
                    if ($(this).children('.overlay').length == 1) {
                        $(this).children('.overlay').fadeIn('fast');
                    }
                },
            mouseleave:
                function()
                {
                    if ($(this).children('.overlay').length == 1) {
                        $(this).children('.overlay').fadeOut('fast');
                    }
                }
            }
        );

        $('#related-projects a').live('click', function() {

            direction = 'prev';
            newPosition = -3000;

            var innerElem = $('#inner');
            //var srcURL = $(this).attr('href').substring(2);
            var srcURL = $(this).attr('rel');

            var innerPosition = $(innerElem).position();
            $(innerElem).css({'position':'fixed','left':innerPosition.left,'top':innerPosition.top});

            $(innerElem).animate({'left':newPosition},'slow','easeInOutExpo',function(){
                 $(this).css({'left':3000});
              
                $('#project').load(srcURL +' #transition', function(response, status, xhr) {
                    if (status == "error") {
                        var msg = "Sorry but there was an error: ";
                        $("#error").html(msg + xhr.status + " " + xhr.statusText);
                    }
                    else {
                        $(" #inner .lettering").lettering('lines');
                        $(".words").lettering('words');
                        $(innerElem).animate({'left':innerPosition.left,'top':innerPosition.top},'slow','easeInOutExpo',function(){
                            $("#inner").css({'position':'relative','left':'0','top': '0'});
                            $("#content").fadeTo('slow',1);
                            $("#footer").fadeTo('fast',0.5);
                        });
                    }
                }); 
                
            });

            return false;
        });

        $(".slide p,.lettering,.recognition .title p,.staff .name p,#text-highlight p,.blog-post-title p").lettering('lines');
        $(".words").lettering('words');

        $('#background').cycle({ 
            timeout: 5000, 
            speed: 1000,
            fit: true,
            center: true,
            containerResize: true,
            before:  onBeforeSlide, 
            after:   onAfterSlide 
         });

         $('#slide-pagination #next').click(function(){
            direction = 'next';
            newPosition = -3000;
            $('.slide').each(function() {
                if (!$(this).hasClass("active")) {
                    $(this).css({'left':3000});
                }
            });

            $('#background').cycle('next');
         });

         $('#slide-pagination #prev').click(function(){
            direction = 'prev';
            newPosition = 3000;
            $('.slide').each(function() {
                if (!$(this).hasClass("active")) {
                    $(this).css({'left':-3000});
                }
            });
            $('#background').cycle('prev');
         });

        $('#project-pager #next').live('click', function() {
            direction = 'next';
            newPosition = -3000;

            var innerElem = $('#inner');
            //var srcURL = $(this).attr('href').substring(2);
            var srcURL = $(this).attr('rel');

            var innerPosition = $(innerElem).position();
            $(innerElem).css({'position':'fixed','left':innerPosition.left,'top':innerPosition.top});

            $(innerElem).animate({'left':newPosition},'slow','easeInOutExpo',function(){
                 $(this).css({'left':3000});
              
                $('#project').load(srcURL +' #transition', function(response, status, xhr) {
                    if (status == "error") {
                        var msg = "Sorry but there was an error: ";
                        //alert(msg + xhr.status + " " + xhr.statusText);
                    }
                    else {
                        $(" #inner .lettering").lettering('lines');
                        $(".words").lettering('words');
                        $(innerElem).animate({'left':innerPosition.left,'top':innerPosition.top},'slow','easeInOutExpo',function(){
                            $("#inner").css({'position':'relative','left':'0','top': '0'});
                            $("#content").fadeTo('slow',1);
                            $("#logo").fadeTo('slow',1);
                            $("#footer").fadeTo('fast',0.5);
                        });
                    }
                }); 
                
            });

            return false;

         });

        $('#project-pager #prev').live('click', function() {
            direction = 'prev';
            newPosition = 3000;

            var innerElem = $('#inner');
            //var srcURL = $(this).attr('href').substring(2);
            var srcURL = $(this).attr('rel');

            var innerPosition = $(innerElem).position();
            $(innerElem).css({'position':'fixed','left':innerPosition.left,'top':innerPosition.top});

            $(innerElem).animate({'left':newPosition},'slow','easeInOutExpo',function(){
                 $(this).css({'left':-3000});
              
                $('#project').load(srcURL +' #transition', function(response, status, xhr) {
                    if (status == "error") {
                        var msg = "Sorry but there was an error: ";
                        //alert(msg + xhr.status + " " + xhr.statusText);
                    }
                    else {
                        $(" #inner .lettering").lettering('lines');
                        $(".words").lettering('words');
                        $(innerElem).animate({'left':innerPosition.left,'top':innerPosition.top},'slow','easeInOutExpo',function(){
                            $("#inner").css({'position':'relative','left':'0','top': '0'});
                            $("#content").fadeTo('slow',1);
                            $("#footer").fadeTo('fast',0.5);
                        });
                    }
                }); 
                
            });

            return false;
        });

        $('div.blog-text').expander({
            slicePoint: 400,
            expandPrefix: ' ',
            expandEffect: 'show',
            expandText: 'Read More',
            collapseTimer: 0,
            userCollapseText: 'Close'
        });

        $(window).resize(function() {
            ResetOverlayBackground();
            ResizeBackgroundImages();
        });

        $("a.transition").click(function(event){
            event.preventDefault();

            linkLocation = this.href;           

            currentColour = $('body').attr('class');

            currentPosition = $('#logo').attr('class');

            $.getJSON('pages.json', function(data) {
                $.each(data, function(key, val) {
                
                    if (key == linkLocation){
                        targetColour = val[0].colour;
                        targetPosition = val[0].position;
                    }
                    
                });

                if (targetColour == 'lightgrey'){  
                    targetHex = lightgreyHex;
                }

                if (targetColour == 'darkgrey'){
                    targetHex = darkgreyHex;
                }

                if (targetColour == 'green'){
                    targetHex = greenHex;
                }                

                if (currentPosition != targetPosition) {
                    $("nav").animate({'top':-50},'fast');
                    if ($("#overlay").length == 1) {
                        $("#overaly,#slide-carousel,#background").fadeOut(1000,function(){
                            redirectPage();
                        });
                    }
                    else {
                        $("#breadcrumbs,#footer,#content").fadeOut(1000,function(){
                            animateTransition(currentPosition,targetPosition);
                            //redirectPage();
                        });
                    }
                }
                else {
                    $("nav").animate({'top':-50},'fast');
                    $("#breadcrumbs,#footer,#content").fadeOut(1000,function(){
                        redirectPage();
                    });                
                }
               
            });

        });
    });

    function ResizeBackgroundImages(){
        $('#background img').each(function() {
           var doc_width = $(document).width();
           var doc_height = $(document).height();
           var image_width = $(this).width();
           var image_height = $(this).height();
           var image_ratio = image_width/image_height;
           var new_width = doc_width;
           var new_height = Math.round(new_width/image_ratio);
           $(this).width(new_width);
           $(this).height(new_height);

           if(new_height<doc_height){
                new_height = doc_height;
                new_width = Math.round(new_height*image_ratio);
                $(this).width(new_width);
                $(this).height(new_height);
                var width_offset = Math.round((new_width-doc_width)/2);
                $(this).css("left","-"+width_offset+"px");
           }
        });
    }

    $(window).load(function(){ 
        $('#grid-inner').masonry({
          itemSelector: '.block',
          columnWidth: 205,
          gutterWidth: 10
        });

        $("#content").fadeTo('slow',1);
        $("#footer").fadeTo('slow',0.5);
        $("nav").animate({'top':0},'fast');

        checkFades();
        ResizeBackgroundImages();
    });

    function checkFades(){
        tiles.each(function(i) {
            a = $(this).offset().top + 50;
            b = $(window).scrollTop() + $(window).height();
            if (a < b) $(this).fadeTo(500,1);
        });
    }

    function animateTransition(currentPosition,targetPosition){

        // POS 1
        if (targetPosition == 'pos1') {
            $('#m')
                .animate({'top':51}, {duration: 'slow', queue: true})
                .animate({'left':0}, {duration: 'slow', queue: true});
            $('#k')
                .animate({'top':51}, {duration: 'slow', queue: true})
                .animate({'left':51},'slow',function(){
                    logoCallback();
            });
        }

        // POS 2
        if (targetPosition == 'pos2') {
            $('#m')
                .animate({'top':51}, {duration: 'slow', queue: true})
                .animate({'left':51}, {duration: 'slow', queue: true});
            $('#k')
                .animate({'top':102}, {duration: 'slow', queue: true})
                .animate({'left':51},'slow',function(){
                    logoCallback();
            });

        }

        // POS 3
        if (targetPosition == 'pos3') {
            $('#m').animate({'top':0,'left':51}, {duration: 'slow', queue: true})
            $('#k').animate({'top':51,'left':51}, 'slow',function(){
                    logoCallback();
            });
        }

        // POS 4
        if (targetPosition == 'pos4') {
            $('#m').animate({'top':0,'left':51}, {duration: 'slow', queue: true})
            $('#k').animate({'top':0,'left':102}, 'slow',function(){
                   logoCallback();
            });
        }

        // POS 5
        if (targetPosition == 'pos5') {
            $('#m').animate({'top':0,'left':51}, {duration: 'slow', queue: true})
            $('#k').animate({'top':51,'left':0}, 'slow',function(){
                    logoCallback();
            });
        }

        // POS 6
        if (targetPosition == 'pos6') {
            $('#m').animate({'top':51,'left':0}, {duration: 'slow', queue: true})
            $('#k').animate({'top':102,'left':0}, 'slow',function(){
                    logoCallback();
            });
        }

    }

    function logoCallback(){
        $('body').animate({'backgroundColor':targetHex},'fast',function(){
            $('body').addClass(targetColour);
            $('body').removeClass(currentColour);

            redirectPage();
        });  
    }

    function redirectPage() {
        window.location = linkLocation;
    }

    function onBeforeSlide(curr, next, opts){

        $('.active').animate({'left':newPosition},'slow','easeInOutExpo',function(){
            $('.active').removeClass('active');
        });

        ResizeBackgroundImages();

    }

    function onAfterSlide(curr, next, opts){
        

        //$('.slide').not(':eq('+opts.currSlide+')').removeClass('active').css({'left':newPosition});
        
        var currentSlide = $('.slide:eq('+opts.currSlide+')');
        
        $(currentSlide).animate({'left':0},'slow',function(){
            $(currentSlide).addClass('active');    
        });

        if (direction == 'next') {
            $('.slide').not(':eq('+opts.currSlide+')').css({'left':3000});
        }
        else {
            $('.slide').not(':eq('+opts.currSlide+')').css({'left':-3000});
        }   

        ResizeBackgroundImages();

    }

    function ResetOverlayBackground(){
        var overlay_position = $("#nav-inner").offset().left + 'px 0px';
        $('#overlay').css({backgroundPosition: overlay_position});
    }       
