﻿$(document).ready(function() {
    var menuAnchor = $("#menu");
    var curlies = $("#curlies");
    var curlieLeft = $("#nav_left_curly");
    var curlieMid = $("#nav_mid_curly");
    var curlieRight = $("#nav_right_curly");

    function addMega() {
        $(this).addClass('hovering');

        var lateralDivs = $(this).find('div.lateral');
        var newWidth = 0;
        lateralDivs.each(function(i) {
            newWidth += $(this).outerWidth(true);
        });

        newWidth = newWidth + "px"
        //lateralDivs.last().css('border', '0px');

        var dropContainer = $(this).find('div.collapse');
        dropContainer.width(newWidth);
        var rightEdge = fncRightEdge(dropContainer);
        var overage = fncOverage(rightEdge, curlieMid);
        if (overage < 0) {
            if (dropContainer.outerWidth(true) > curlieMid.outerWidth(true)) {
                dropContainer.width(dropContainer.width() + curlieMid.outerWidth(true) - dropContainer.outerWidth(true));
            }
            var tempOffset = dropContainer.offset().left + fncOverage(fncRightEdge(dropContainer), curlieMid);
            dropContainer.offset({ left: tempOffset });
        }

        // Calculate the height that all of the divs should be
        var resizeBool = false;
        var storeBiggest = 0;
        lateralDivs.each(function(i) {
            if ($(this).outerHeight(true) < dropContainer.innerHeight()) {
                resizeBool = true
            }
            if ($(this).outerHeight(true) > storeBiggest) storeBiggest = $(this).outerHeight();
        });

                if (resizeBool) {
                    //lateralDivs.height(dropContainer.innerHeight() - parseInt(dropContainer.css("padding-bottom")));
                    lateralDivs.height(storeBiggest);
                }

        lateralDivs.last().css({ 'border-right': '0px' });
    }

    function removeMega() {
        $(this).removeClass("hovering");
    }

    var megaConfig = {
        interval: 300,
        sensitivity: 4,
        over: addMega,
        timeout: 400,
        out: removeMega
    };

    jQuery.fn.center = function() {
        this.css("position", "absolute");
        this.css("left", ($(window).width() - this.outerWidth()) / 2 + $(window).scrollLeft() + "px");
        return this;
    }

    // calculate the right edge of the dropdown container
    function fncRightEdge(elm) {
        return elm.offset().left + elm.outerWidth(true);
    }

    // calculate how far over the edge the container is going
    function fncOverage(edge, baseElm) {
        return (baseElm.offset().left + baseElm.outerWidth(true)) - edge;
    }

    function fncNavCurlies() {
        var anchorPosition = menuAnchor.offset();
        var anchorWidth = menuAnchor.width();

        var menuBottom = (anchorPosition.top) - 18;
        curlies.offset({ top: menuBottom, left: anchorPosition.left });
        curlies.width(anchorWidth);
        curlieMid.width(anchorWidth - curlieLeft.width() - curlieRight.width());
    }

    // #################################################################################################################### //

    if (!($(window).width() < 1000)) {
        $("#pageContent").center();
    }

    if (menuAnchor.length) {
        $("li.mega").hoverIntent(megaConfig);

        fncNavCurlies();
    }
});

function launchNewsletter() {
    $.fancybox({
        'type': 'iframe',
        'href': 'newsletter.htm',
        'width': 202,
        'height' : 140
    });
}
