jQuery(document).ready(function($) {

    // smooth scrolling
    $('#top-pointer').click(function(e) {
        e.preventDefault();
        $.scrollTo($('#top'), 800);
    });

    // byline shit
    $.fn.byline = function(options)
    {
        // settings
        var settings = jQuery.extend({
            pause_duration: 2000,
            fade_length: 'fast'
        }, options);

        // vars
        var vars = {
            current: 0
        };

        // go fetch!
        function _run($container, pieces, settings)
        {
            // fade out current
            $container.stop().fadeOut(settings.fade_length, function(e) {
                $container.html(pieces[vars.current]);
                $container.fadeIn(settings.fade_length);
            });

            // normalize
            vars.current++;
            if (vars.current > pieces.length - 1)
            {
                vars.current = 0;
            }
        }

        // initialize
        function _initialize(container, settings)
        {
            var $container = $(container);
            var pieces = $.map($container.attr('data-words').split(';'), $.trim);
            var first = pieces.pop();
            $container.html(first).show();

            setInterval(function() { _run($container, pieces, settings); }, settings.pause_duration);
        }

        return this.each(function(i) { _initialize(this, settings); });
    };

    // run byline
    $('#byline').byline();

    // person rendering
    var max_heights = [];
    var max_height = 0;
    var count = 0;

    $('#peopledetail .person').each(function() {
        var $person = $(this);
        var curr_height = $person.outerHeight();
        var new_row = count > 0 && (count % 3 === 0);

        if (new_row)
        {
            max_heights.push(max_height);
            max_height = 0;
            count = 0;
        }

        if (curr_height > max_height)
        {
            max_height = curr_height;
        }

        count++;
    });

    if (max_heights.length > 0)
    {
        var row = 0;
        count = 0;
        $('#peopledetail .person').each(function(i) {
            var $person = $(this);

            if (max_heights[row] && $person.height() < max_heights[row])
            {
                $person.height(max_heights[row]);
            }

            if (count !== 0 && count % 2 === 0)
            {
                count = 0;
                row++;
            }
            else
            {
                count++;
            }
        });
    }

    if (typeof work_titles !== 'undefined')
    {
        $('#search-work #t').autocomplete({
            source: work_titles,
            position: { my : "right top", at: "right bottom" }
        });
    }

    // work search stuff
    var $search_form = $('#search-work');
    $('select', $search_form).bind('change', function(e) {
        e.preventDefault();

        var that = this;

        $(this).parent().find('select').each(function(i) {
            if (this !== that) {
                $(this).val('');
            }
        });

        $search_form.submit();
    });

    $('a[rel*=external]').bind('click', function(e) {
        e.preventDefault();
        return window.open(this.href);
    });

    // convert labels to placeholders
    var convert_labels = function(wrapper) {
        $('label', wrapper).each(function() {
            var $label = $(this);
            var $input = $label.next();
            if (typeof $input.attr('placeholder') === 'undefined')
            {
                $input.attr('placeholder', $label.html());
            }
        });
    }

    // queue label/placeholder functions
    // in order for them to execute after each other
    $('#contact').queue(function() {
        convert_labels(this);
        $(this).dequeue();
    });

    $('#contact').queue(function() {
        $('input[placeholder], textarea[placeholder]', this).placeholder();
        $(this).dequeue();
    });
});
