(function($) {

    var MegaMenu = {

        basic: function(items) {
            items.hover(
                function() {
                    $(this).addClass('hovering');
                },
                function() {
                    $(this).removeClass('hovering');
                }
            );
        },
        phase3: function() {
            var megaMenu = $('#mega-menu'),
                popups = megaMenu.find('div.mega-menu-popup'),
                form = megaMenu.find('form:first'),
                body = $('body:first'),
                hoverClassName = 'hovering';

            // Add hovering effects
            $("li.mega").hover(function() {
                    var li = $(this),
                        hasVisualFormatting = li.data('hasVisualFormatting');

                    li.addClass(hoverClassName);
                    if (!hasVisualFormatting) {
                        var nodes = li.find('div.Node'),
                            maxNodeHeight = 0;
                        nodes.each(function() {
                            var height = $(this).height();
                            maxNodeHeight  = (maxNodeHeight <= height) ? height : maxNodeHeight;
                        });
                        nodes.height(maxNodeHeight);

                        li.data('hasVisualFormatting', 1);
                    }
                },
                function() {
                    var li = $(this);
                    li.removeClass(hoverClassName);
                }
            );

            popups.each(function() {
            	
            	var popup = $(this),
                nodes = popup.find('div.Node'),
                width = popup.outerWidth();

            	//Find out how many Nodes are missing from the max: 6
            	var popUpContent = popup.children('.mega-menu-popup-content');
            	var missingNodes = 6 - popUpContent.children('.Node').length;
            	//Fill those up with Blank Nodes
            	while(missingNodes > 0){
            		var popUpContent = popup.children('.mega-menu-popup-content');
            		var fillerNode = $('<div>').addClass('BlankNode').addClass('Node');
            		popUpContent = popUpContent.children('.Node').last().after(fillerNode);
            		missingNodes--;
            	}
                // Make sure all .Node elements are the same height
                var height = 0;
                nodes.each(function() {
                    var h = $(this).height();
                    height = (height <= h) ? h : height;
                });
                if (height > 0) {
                    nodes.height(height);
                }

                // How many nodes *should* fit in this popup? If there are less than that number, then there needs to be some blanks inserted into the HTML.






                nodes.find('img').css('width', '100%');


                if (body.hasClass('phase3')) {
                    var items = [];
                    popup.children('.mega-menu-footer > a').each(function() {
                        items.push($(this).remove());
                    });
                }
            });

            form.each(function() {
                var container = form.closest(),
                    textbox = form.find('input[type=text]'),
                    button = form.find('*[type=submit]'),
                    hintClassName = 'hint';


                form.width(container.width());
                button.click(function(e) {
                    if (textbox.hasClass(hintClassName)) {
                        e.preventDefault();
                    }
                });

                var list = form.closest('ul');

                // Get the widths of each mega menu item
                (function() {
                    var items = list.children('li'),
                        lastItem = items.last(),
                        a = [],
                        sum = 0,
                        diff = 0,
                        padding = {
                            top: 0,
                            right: 0,
                            bottom: 0,
                            left: 0
                        };

                    items.each(function() {
                        var item = $(this);
                        a.push(item.outerWidth());
                    });
                    // Remove the last one
                    a.pop();

                    for (var i = 0, j = a.length; i < j; i++) {
                        sum += a[i];
                    }

                    diff = list.width() - sum;

                    // Add both paddings
                    padding.right = parseInt(lastItem.css('padding-left'));
                    padding.right += parseInt(lastItem.css('padding-right'));

                    diff -= padding.right;

                    lastItem.width(diff);

                    // Now vertically center the search form
                    sum = lastItem.outerHeight();
                    sum -= form.outerHeight();
                    sum /= 2;
                    form.css('margin-top', sum);

                })();
            });



            form.find('input[type=text]').each(function() {
                var textbox = $(this),
                    hintClassName = 'hint';
            }).find('button').click(function() {

            });


            // Position the popups to be directly beneath the mega menu items
            var positionPopups = function() {
                var megaMenuHeight = megaMenu.outerHeight();
                if (megaMenuHeight > 0) {
                    popups.css({
                        top: [megaMenuHeight, 'px'].join('') // "top: XXpx"
                    });
                }
            }();
        }
    };

    $(function() {
        var body = $('body:first'),
            items = $('li.mega'),
            klass = body.attr('class');

        if (body.hasClass("phase3")) {
            MegaMenu.phase3();
        } else {
            MegaMenu.basic(items);
        }
    });

}(jQuery));
