var people = {
    option: {
        classScrollLeft: 'previous',
        classScrollRight: 'next',
        classShowQuote: 'quote',
        classCarousel: 'staff'
    },
    itemWidth: 0,
    carouselWidth: 0,
    carouselFullWidth: 0,
    leftIndent: 0,
    stopScrolling: false,
    stopQuotes: false,
    _currentHash: false,
    init: function () {
        // Initiate the scroll
        this.scrollInit();
        // Bind the scroll actions
        $('.' + this.option.classCarousel + ' .' + this.option.classScrollLeft).click(function(){return people.scrollLeft()});
        $('.' + this.option.classCarousel + ' .' + this.option.classScrollRight).click(function(){return people.scrollRight()});
        // Bind the showing quote action
        //$('.' + this.option.classCarousel + ' .' + this.option.classShowQuote + ':not(.main)').click(this.showQuote);
        // Start checking the page hash for changes
        if (people._currentHash.length == 0) {
            location.href += '#' + $('.staff ul li:first').attr('id').replace('-person', '');
        }

        // Display Scroll Buttons
        $('.staff .previous, .staff .next').fadeIn(2000);

        $('a.people-more').each(function(){
            var element = $(this)
            var link = element.attr('href');
            element.hide();
            element.parent().click(function(){window.location.href = link;});
        });
        setInterval(people._check, 500);
    },
    _check: function(){
        // If the location hash is different then change person
        if (location.hash == '') {
            if ($('a.people-more').length > 0) {
                window.location.hash = $('a.people-more:first').attr('href').replace('/people', '');
            }
        }
        if (location.hash != people._currentHash) {
            people._currentHash = location.hash;
            people._changePerson(people._currentHash);
        }
    },
    _changePerson: function(person){
        if (people.stopQuotes) return false;
        people.stopQuotes = true;
        var li = $(person + '-person');
        var quote = li.find('div:first');
        var blockquote = quote.find('blockquote');
        var figure = quote.find('div.figure');
        var blockquoteWidth = blockquote.width();
        // Work out position for quote
        var position = quote.position();

        var leftIndent = (position.left * -1) + (people.carouselWidth / 2) - 100;
        if (leftIndent > 0) {
            leftIndent = 0;
        }
        if (leftIndent < (people.carouselWidth - people.carouselFullWidth - 30)) {
            leftIndent = (people.carouselWidth - people.carouselFullWidth - 65);
        }
        people.leftIndent = leftIndent;
        people.scrollAnimate();
        people.toggleButtons();

        var offset = position.left + people.leftIndent;
        var fullOffset = offset + blockquoteWidth;
        
        if (fullOffset > people.carouselWidth - 30) {
            offset = people.carouselWidth - blockquoteWidth - 45;
        }

//        if (blockquote.hasClass('active')) {
//            // Click on an already active quote, close
//            figure.addClass('no-back');
//            $('.quote.main blockquote').fadeOut(function(){
//                $(this).addClass('no-show');
//                location.href = location.href + '#' + li.attr('id').replace('-person', '');
//                people.stopQuotes = false;
//            });
//            blockquote.removeClass('active');
        if ($('.quote.main blockquote').hasClass('no-show')) {
            // Open from inactive
            $('.' + people.option.classCarousel + ' ul blockquote').removeClass('active');
            $('.' + people.option.classCarousel + ' ul .figure').addClass('no-back');
            $('.quote.main blockquote .content').html(blockquote.html());
            var width = 260;
            if (li.hasClass('partner')) $('.quote.main').addClass('partner');
            else $('.quote.main').removeClass('partner');
            if (li.hasClass('partner')) width = 475;
            $('.quote.main blockquote').removeClass('no-show').css({left:offset, width:width}).fadeIn(function() {
                figure.removeClass('no-back');
                people.stopQuotes = false;
            });
            blockquote.addClass('active');
        } else {
            // All other situations
            $('.' + people.option.classCarousel + ' ul blockquote').removeClass('active');
            $('.' + people.option.classCarousel + ' ul .figure').addClass('no-back');
            $('.quote.main blockquote .content').find('p').css({overflow:'hidden'});
            $('.quote.main blockquote .content').slideUp(400, function() {
                $('.quote.main blockquote .content').html(blockquote.html());
                $('.quote.main blockquote .content').find('p').css({overflow:'hidden'});
                var width = 260;
                if (li.hasClass('partner')) width = 475;
                $('.quote.main blockquote').animate({left:offset, width:width}, 400, function(){
                    if (li.hasClass('partner')) $('.quote.main').addClass('partner');
                    else $('.quote.main').removeClass('partner');
                    figure.removeClass('no-back');
                    $('.quote.main blockquote .content').slideDown(400, function(){
                        $('.quote.main blockquote .content').find('p').css({overflow:'auto'});
                    });
                    people.stopQuotes = false;
                });
            })
            blockquote.addClass('active');
        }
    },
    scrollInit: function(){
        $('.' + this.option.classCarousel + ' ul').css({'left': 0});
        this.carouselWidth = $('.' + this.option.classCarousel).width();
        this.itemWidth = $('.' + this.option.classCarousel + ' ul li').outerWidth();
        var itemCount = $('.' + this.option.classCarousel + ' ul li').length;
        this.carouselFullWidth = itemCount * this.itemWidth;
        $('.' + this.option.classCarousel + ' ul').width(this.carouselFullWidth);
        this.toggleButtons();
    },
    scrollLeft: function () {
        if (people.stopScrolling) return false;
        var leftIndent = parseInt($('.' + this.option.classCarousel + ' ul').css('left')) + parseInt(this.itemWidth) * 3;
        if (leftIndent > 0) {
            leftIndent = 0;
        }
        this.leftIndent = leftIndent;
        this.hideAllQuotes();
        this.scrollAnimate();
        this.toggleButtons();
        return false;
    },
    scrollRight: function () {
        if (people.stopScrolling) return false;
        var leftIndent = parseInt($('.' + this.option.classCarousel + ' ul').css('left')) - parseInt(this.itemWidth) * 3;
        if (leftIndent < (people.carouselWidth - people.carouselFullWidth - 35)) {
            leftIndent = (people.carouselWidth - people.carouselFullWidth - 65);
        }
        this.leftIndent = leftIndent;
        this.hideAllQuotes();
        this.scrollAnimate();
        this.toggleButtons();
        return false;
    },
    scrollAnimate: function () {
        people.stopScrolling = true;
        $('.' + people.option.classCarousel + ' ul').animate({'left':people.leftIndent}, function () {
            people.stopScrolling = false;
        });
    },
    hideAllQuotes: function () {
        $('.' + people.option.classCarousel + ' ul blockquote').removeClass('active');
        $('.' + people.option.classCarousel + ' ul .figure').addClass('no-back');
        $('.quote.main blockquote').hide().addClass('no-show');
    },
    toggleButtons: function () {
        if (this.leftIndent == 0) $('.' + people.option.classCarousel + ' a.previous').addClass('previous-disabled');
        else $('.' + people.option.classCarousel + ' a.previous').removeClass('previous-disabled');
        if ((this.carouselWidth - this.leftIndent) > this.carouselFullWidth) $('.' + people.option.classCarousel + ' a.next').addClass('next-disabled');
        else $('.' + people.option.classCarousel + ' a.next').removeClass('next-disabled');
    }
}
$(document).ready(function(){
    if ($('.' + people.option.classCarousel).length > 0) {
        people.init();
    }
});
