$(document).ready(function() {
    $('.popout > ul > li > a').click(function() {
        if(this.rel == 'follow') {
            return true;
        }
        var a = $(this), prev = $('.popout > ul > li > a.active'), div = a.siblings('div'), popout = a.parents('.popout').eq(0), mask = popout.find('.mask');
        //var whatWeDo = (a.parents('#what-we-do').length == 1) ? true : false;
        if(a.hasClass('active')) {
            a.removeClass('active').find('span').html('&gt;');
            div.removeClass('active').animate({
                height: '1px'
            }, function() {
                div.hide();
            });
            mask.animate({
                height: '35px'
            });
            popout.removeClass('show');
        }
        else {
            prev.removeClass('active').find('span').html('&gt;');
            a.addClass('active').find('span').html('&and;');
            if(popout.hasClass('show')) {
                popout.find('div.active').removeClass('active').fadeOut('medium', function() {
                    div.height(475).fadeIn('medium').addClass('active');
                });
            }
            else {
                var iframe = false, iframeSrc;
                if(div.find('iframe').length > 0) {
                    //iframe = div.find('.top iframe').remove();
                    iframe = div.find('.top iframe');
                    //var iframeSrc = 'http://maps.google.com/maps?f=q&source=s_q&hl=en&geocode=&q=1809+Seventh+Ave,+Suite+111+Seattle+WA&sll=37.0625,-95.677068&sspn=37.871902,93.076172&ie=UTF8&hq=&hnear=1809+7th+Ave,+Seattle,+King,+Washington+98101&view=map&ll=47.613722,-122.335634&spn=0.020251,0.036478&z=14&iwloc=A&output=embed';
                    iframeSrc = iframe.attr('src');
                    iframe.attr('src', '');
                }
                div.css('height', '1px').show().animate({
                    height: '475px'
                }, function() {
                    if(iframe) {
                        //iframe.appendTo(div.find('.top > div'));
                        iframe.attr('src', iframeSrc);
                    }
                }).addClass('active');
                popout.addClass('show');
                mask.animate({
                    height: '475px'
                });
            }
        }
        return false;
    });
    $('#team-list .column a').mouseenter(function() {
        $('#team-list .column a').removeClass('active');
        $(this).addClass('active');
        $('#team-icons img').removeClass('active');
        $('#thumb_' + this.rel).addClass('active');
    });
    $('#team-icons a img').mouseenter(function() {
        var rel = this.id.split('_')[1];
        $('#team-icons img').removeClass('active');
        $('#team-list .column a').removeClass('active');
        $(this).addClass('active');
        $('#team-list .column a[rel=' + rel +']').addClass('active');
    });

    $('.case-studies #sidebar li a').click(function() {
        $('#sidebar li.active').removeClass('active');
        $(this).parent().addClass('active');
        $('#main table:not(.inviz)').addClass('inviz');
        $('#main table' + $(this).attr('href')).removeClass('inviz');
        this.blur();
        //return false;
    });
    if($('.case-studies').length > 0 && window.location.hash) {
        $('.case-studies #sidebar li a[href='+window.location.hash+']').click();
    }

    $('.news #sidebar li a').click(function() {
        $('#sidebar li.active').removeClass('active');
        $(this).parent().addClass('active');
        $('#main table').hide();
        $('#main table' + $(this).attr('href')).show();
        this.blur();
        //return false;
    });
    $('.news #sidebar dt a').toggle(function() {
        $('.news #sidebar dl').addClass('open');
        this.blur();
    }, function() {
        $('.news #sidebar dl').removeClass('open');
        this.blur();
    });
    if($('.news').length > 0 && window.location.hash) {
        $('.news #sidebar li a[href='+window.location.hash+']').click();
        if(window.location.hash != '#all') $('.news #sidebar dt a').click();
    }

    if($('#slideshow .inner > *').length > 1) {
        function makeSlide() {
            //for some reason the jScrollpane plugin is causing a conflict, this is a silly workaround:
            $('#slidernav a').attr('href', 'javascript:void()');
            $('#slidernav div').show();
            var allTitles = [];
            $('#slideshow .inner .page_name').each(function() {
                allTitles.push($(this).text());
            });
            $('#slideshow .inner').cycle({
                speed:  800,
                timeout: 0,
                pager:  '#slidernav span'
            });
            $('#slidernav a').each(function() {
                if(allTitles.length > 0)
                    this.title = allTitles.shift();
            });
            $('#slidernav a').wTooltip({
                id: 'tooltip',
                offsetX: 0,
                offsetY: 22,
                style: {
                    padding: '3px',
                    color: '#e76c1f',
                    border: '1px solid #e76c1f',
                    fontWeight: 'bold',
                    zIndex: 2000
                }
            });
        }
        if(typeof $.fn.cycle == 'undefined') {
            $.getScript('/js/cycle.js', makeSlide);
        }
        else {
            makeSlide();
        }
    }

    $('#slideshow .noimage .slidecontent li').each(function() {
        this.innerHTML = '<span>' + this.innerHTML + '</span>';
    });

    $('#panel').jScrollPane({scrollbarWidth: 10});

    $('#team-nav a').wTooltip({
        id: 'tooltip',
        offsetX: 0,
        offsetY: 22,
        style: {
            padding: '3px',
            color: '#8d1e40',
            border: '1px solid #8d1e40',
            fontWeight: 'bold',
            zIndex: 2000
        }
    });

    if(ie6) {
        $('.nav>li').hover(function() {
            $(this).addClass('over');
        }, function() {
            $(this).removeClass('over');
        });
    }
});
