var visited, watch_display, floor = Math.floor;

$(document).ready(function(){
    //{{{ time on site
    if (!document.cookie.match(/\W*timeonsite=/))
    {
        document.cookie = 'timeonsite=' + new Date().getTime();
    }
    var tos = $('#tos');
    tos.text('Time on Site ')
        .append('<span />');
    watch_display = $('> span', tos);
    visited = (Number)(/\W*timeonsite=(\d+)/.exec(document.cookie)[1]);

    function show_tos()
    {
        var duration = new Date().getTime() - visited,
            watch = '',
            hours, minutes, seconds;

        duration /= 1000;
        hours = (String)(floor(duration / 3600));
        duration %= 3600;
        minutes = (String)(floor(duration / 60));
        seconds = (String)(floor(duration % 60));

        if (hours != '0')
        {
            watch += hours + ':' + (minutes.length == 1 ? '0' + minutes : minutes) + ':';
        }
        else if (minutes != '0')
        {
            watch += minutes + ':';
        }
        watch += seconds.length == 1 ? '0' + seconds : seconds;
        watch_display.text(watch);
    }

    show_tos();
    setInterval(show_tos, 1000);
    //}}}
    //{{{ clients
    $('#clients .client')
        .mouseover(function() {
            var el = $(this);
            $('> div', el).addClass('hover');
            el.addClass('hover');
        })
        .mouseout(function() {
            var el = $(this);
            $('> div', el).removeClass('hover');
            el.removeClass('hover');
        });
    //}}}
    // {{{ team
    var team = $('#team'),
        team_ul = $('> ul', team),
        team_selected = $('> .selected', team_ul),
        team_content = $('> .content', team),
        team_border = $('<div class="border" />'),
        quick_hide = team_selected.length > 0;
    team_border
        .css({
            'border' : '4px solid #E96C39',
            'background-color' : 'transparent',
            'width' : '107px',
            'height' : '101px',
            'position' : 'absolute',
            'top' : '0px',
            'left' : '0px'
        })
        .hide();
    $('> li', team_ul).each(function(i){
        var el = $(this),
            quote = $('> .quote', el),
            overlay = $('> .overlay', el).fadeTo('fast', 0.5),
            info = $('> .info', el),
            bio = $('> .bio > a', el);
        quote.hide().appendTo(team_content);
        info.hide().appendTo(team_content);
        bio.append(team_border.clone());
        el.data('index', i)
            .hover(function(){
                if (location.pathname == '/marketing_team/' && !el.hasClass('selected') && quick_hide)
                {
                    team_selected.removeClass('selected');
                    quick_hide = false;
                }
                quote.show();
                info.show();
                el.siblings(':not(.selected)').each(function(){ 
                    var index = $(this).data('index');
                    $('> .bio > a > .border', this).hide();
                    $('> .overlay', this).show();
                    $('> .quote', team_content).eq(index).hide();
                    $('> .info', team_content).eq(index).hide();
                })
                $('> .overlay', el).hide();
                $('> .border', bio).show();
            }, function(){
                if (!$(this).hasClass('selected') && quick_hide)
                {
                    $('> .bio > a > .border', this).hide();
                    $('> .overlay', this).show();
                }
            });
    });
    team_selected.hover();
    // }}}
    // {{{ optimization model
    var opt_model = $('#opt-model'),
        port = $('.port', opt_model),
        port_left = parseInt(port.css('left')),
        slides = $('> .slide', port),
        slide_width = slides.eq(0).width(),
        prev_btn = $('.prev', opt_model),
        next_btn = $('.next', opt_model);
    next_btn.click(function(){
        if ($(this).data('working'))
        {
            return;
        }
        next_btn.data('working', true);
        prev_btn.data('working', true);
        var current = parseInt(port.css('left')),
            offset = current - slide_width,
            total_width = port_left * slides.length;
        prev_btn.css('visibility', 'visible');
        port.animate({
            'left' : offset + 'px'
        }, 'slow', 'swing', function(){
            next_btn.data('working', false);
            prev_btn.data('working', false);
        });
        if (offset <= total_width - (slide_width * (slides.length - 1)))
        {
            $(this).css('visibility', 'hidden');
        }
    });
    prev_btn.click(function(){
        if ($(this).data('working'))
        {
            return;
        }
        next_btn.data('working', true);
        prev_btn.data('working', true);
        var current = parseInt(port.css('left')),
            offset = current + slide_width;
        next_btn.css('visibility', 'visible');
        port.animate({
            'left' : offset + 'px'
        }, 'slow', 'swing', function(){
            next_btn.data('working', false);
            prev_btn.data('working', false);
        });
        if (port_left == offset)
        {
            $(this).css('visibility', 'hidden');
        }
    });
    // }}}
    //{{{ contact
    $('#contact-info .row').each(function() {
        var label_row = $('> .label', this),
            label_text = label_row.text(),
            input = $(':input', this),
            input_val = input.val();
        label_row.hide();
        if (label_text)
        {
            input
                .val(input_val ? input_val : label_text)
                .focus(function() {
                    var el = $(this);
                    if (el.val() == label_text)
                    {
                        el.val('');
                    }
                })
                .blur(function() {
                    var el = $(this);
                    if (el.val() == '')
                    {
                        el.val(label_text);
                    }
                });
        }
    });
    //}}}
    // {{{ news print link
    $('#news > .news-detail > .social')
        .append('<a href="javascript:window.print()">Print</a>');

    // }}}
});
