// Check that the page has loaded
$(function () {
    $('.subitem h2').click(function () {
        var t = $(this);
        var s = t.parents('.subitem');
        var a = $('.articleBody', s);

        if (a.is(':visible'))
            a.slideUp();
        else
            a.slideDown();
    });
    $('#credits').mouseenter(function () {
        $('#credits #lnk').hide();
        $('#links').show();
    }).mouseleave(function () {
        $('#credits #lnk').show();
        $('#links').hide();
    });

    // Cycles
    // Homepage quotes
    // Set content area to height of longest quote
    var testimonialsHeight = 0;
    $('.homepageContent .directoryQuote').each(function () {
        // Determine highest aricle
        if (testimonialsHeight < $(this).outerHeight()) {
            testimonialsHeight = $(this).outerHeight();
        }

        // Set height
        $('.homepageContent .testimonials').height(testimonialsHeight + 50);
    });
    $('.homepageContent .testimonials').cycle();

    // Features
    // Set content area to height of longest quote
    var featuresHeight = 0;
    $('.featureFade .feature').each(function () {
        // Determine highest aricle
        if (featuresHeight < $(this).outerHeight()) {
            featuresHeight = $(this).outerHeight();
        }
        // Set height
        $('.featureFade').height(featuresHeight);
    });

    $('.featureFade').cycle({
        timeoutFn: function (currElement) {
            return parseInt($(currElement).attr('data-duration'), 10);
        }
    });

    //RunFilter();

    // Cufon
    Cufon.replace('.georgia ', {
        fontFamily: 'Georgia'
    });

    // TOP NAV
    // Rollover
    $('#topNav .navItem').mouseenter(function () {
        // Open
        if (!$(this).hasClass('over')) {
            $(this).addClass('over').find('.rollover').fadeIn(150);
        }
    });

    $('#topNav .navItem').mouseleave(function () {
        // Close
        $(this).find('.rollover').fadeOut(100, function () {
            $(this).parents('.navItem').removeClass('over');
        });

    });

    // Link color change
    $('.quickLinks LI A, .arrowLink A').hover(
	    function () {
	        $(this).parent().addClass('hover');
	    },
	    function () {
	        $(this).parent().removeClass('hover');
	    }
    );

    // CLEAR/CONTROL FORM FIELDS
    $('.textBox INPUT, .textArea TEXTAREA').focus(function () {
        // Get title(original value) and current value
        var title = $(this).attr('title');
        var value = $(this).val();

        // Clear if original value only
        if (title == value) {
            $(this).val("");
        }
    });
    $('.textBox INPUT, .textArea TEXTAREA').blur(function () {
        // Get title(original value) and current value
        var title = $(this).attr('title');
        var value = $(this).val();

        // If empty, re-populate
        if (value == "") {
            $(this).val(title);
        }
    });

    // PEOPLE BROWSE
    $('#btnEmployeeFormSubmit').hide();
    $('#lnkEmployeeFormSubmit').removeClass('hide');

    //disable button used for people without javascript
    $('#btnPeopleFilter').hide();

    // Add end classes
    $('#peopleBrowseList .person').filter(function (index) {
        return (index + 7) % 6 == 0;
    }).addClass('endRow');

    $('#peopleBrowseList ul li.person').each(function (i) {
        var $t = $(this);
        var $div = $('<div>').append($('.description', $t).clone());
        $("body").append("<div class='jsHide tooltip' id='tooltip" + i + "'>" + $div.html() + "</div>");
        var tip = $("#tooltip" + i);
        $t.click(function () {
            $('.tooltip').fadeOut('fast').css({ opacity: 1, display: "none" });
            var left = $t.offset().left;
            var mstr = $('#master').offset().left + $('#master').width() + 50;
            if (left >= mstr - tip.width()) {
                left = mstr - tip.width();
            }
            tip.css({ left: left, top: $t.offset().top }).fadeIn();
            $('#peopleBrowseList ul li').css({ 'background': '#333', 'border-color': '#333' });
            $('#peopleBrowseList ul li .details, .peopleBrowse ul li>img').css({ opacity: 0.4, 'z-index': 1 });
            return false;
        });
        $('.close', tip).click(function () {
            $('.tooltip').fadeOut('fast');
            $('#peopleBrowseList ul li .details, .peopleBrowse ul li>img').css({ opacity: 1 });
            $('#peopleBrowseList ul li').css({ 'background': '#f3f1ed', 'border-color': '#fff' });
            return false;
        });
    });

    // NEWS ARTICLE OPEN/CLOSE
    // Set content area to height of longest article (could be done on open if required, but this prevents footer moving)
    var newsHeight = $('.newsPage').height();
    $('.newsArticle').each(function () {
        // Determine highest aricle
        if (newsHeight < $(this).outerHeight()) {
            newsHeight = $(this).outerHeight();
        }
        // Set height
        $('.newsPage').height(newsHeight);
    });

    // Show article
    $('.newsItem .synopsis a').click(function () {
        var t = $(this);
        var news = t.parents('.newsItem');
        // Close other articles
        $('.newsItem .newsArticle').hide();
        //$(this).parents('.newsItem').find('.newsMini').hide();
        var art = news.find('.newsArticle');
        art.css({ top: news.offset().top - news.outerHeight(true) }).show();
        return false;
    });

    // Close article
    $('.newsArticle .sectionHeading .close').click(function () {
        $(this).parents('.newsArticle').hide();
        return false;
    });

    //News draggable
    $(".newsArticle").draggable({ handle: 'h2' });
    $(".tooltip").draggable({ handle: 'h3' });

    $('.expandCollapse H2').click(function () {
        var t = $(this);
        // Show or hide accordingly
        if (t.parent().hasClass('collapsed')) {
            t.next('.showHideContent').slideDown();
            t.parent().addClass('expanded').removeClass('collapsed');
        } else if (t.parent().hasClass('expanded')) {
            t.next('.showHideContent').slideUp(function () {
                t.parent().addClass('collapsed').removeClass('expanded');
            }).addClass('test');
        }
    });
});
