$(document).ready(function () {

    // Define global variables
    var isVisible = false;
    var isMouseOverUnifier = false;

    // Setup unifier
    setUnifierDefaults();

    // Handle the "see more" click
    $('#uni-more').click(function () { toggleUnifier(); return false; });

    // Handle body click
    $('#unifier').hover(function () {
        isMouseOverUnifier = true;
    }, function () {
        isMouseOverUnifier = false;
    });

    $(document).click(function (e) {
        if (!isMouseOverUnifier) toggleUnifier('hide');
    });


    function toggleUnifier(option) {

        if (option) {
            if (option == 'show') show();
            if (option == 'hide') hide();
        } else {
            if (isVisible) hide();
            else show();
        }

        function show() {
            $('#unifier-wrapper').slideDown(300);
            $('#uni-more').css('background-color', $('#uni-director').css('background-color'));
            isVisible = true;
        }

        function hide() {
            $('#unifier-wrapper').fadeOut('fast');
            $('#uni-more').css('background-color', 'transparent');
            isVisible = false;
        }
    }
});

// Setup the Unifier Banner (according to #uni-banner-content width)
function setUnifierDefaults() {

    // Get #uni-banner-content width
    var width = $('#uni-banner #uni-banner-content').width();

    // Calculatet #unifier and children's margins and sizes appropriately
    var unifierWidth = width - 2; // -2 for border
    var leftWidth = (unifierWidth < 800) ? 220 : ((unifierWidth > 800) ? ((unifierWidth > 800) ? 320 : 280) : 240);
    var rightWidth = unifierWidth - leftWidth - 20; // -20 for padding
    var siteWidth = 250;

    // Apply changes
    $('#unifier-wrapper').width(unifierWidth);
    $('#uni-left').width(leftWidth - 2);
    $('#uni-right').width(rightWidth);

    // Adjust .site spacing
    if (unifierWidth < 800) {
        $('.uni-site').css('margin-left', ((rightWidth - (siteWidth * 2)) / 2));
    } else {
        $('.uni-site').css('margin-left', ((rightWidth - (siteWidth * 2)) / 2) - ((rightWidth - (siteWidth * 2)) / 6));
        $('.uni-site').css('margin-right', (rightWidth - (siteWidth * 2)) / 6);
    }
}
