$(function() {
    (function() {
        var topic_nav
      = new TopicNav($("#topic-nav"), $("#topic-nav-button"));
        var floating_nav
      = new Popup($("ul#floating-nav"), ".floating-nav-button", function(nav) {
          if ($.browser.msie && $.browser.version < 8) {
          }
          else {
              nav.find("li").removeClass("help-enabled");
          }          
      });
        var dialog_manager
      = new Popup($("ul#dialogs"), ".dialog-close-button", function(nav) {
          // resets dialog to default view state: 
          // makes hidden columns hidden again
          nav.find("div.dialog-column.switch").each(function() {
              var column = $(this);
              column.show();
              if (column.hasClass("hidden")) {
                  column.hide();
              }
          });
      }, ".dialog-close-button-large");

        //make link open menu
        add_share_menu_listener($(".your-technique .button"), $("#floating-nav li.share"));
        add_share_menu_listener($("#postscript-main ul li a.suggest"), $("#floating-nav li.suggest"));
        add_share_menu_listener($("#postscript-main ul li a.status"), $("#floating-nav li.status"));
        add_share_menu_listener($("#postscript-main ul li a.share"), $("#floating-nav li.share"));

        function add_share_menu_listener(link, menu) {
            link.live('click', function(event) {
                event.preventDefault();
                event.stopImmediatePropagation();
                floating_nav.open(menu);
            });
        }


        // make links open dialogs
        add_dialog_link_listeners
      ($(".open-sign-up-dialog"), $("#sign-up-dialog"));
        add_dialog_link_listeners
      ($(".open-sign-in-dialog"), $("#sign-in-dialog"));
        add_dialog_link_listeners
      ($(".open-forgott-password-in-dialog"), $("#sign-in-dialog"));

        add_dialog_link_listeners
      ($(".floating-nav-button-public-view"), $("#sign-in-or-up-dialog"));

        add_live_dialog_link_listeners
      ($(".your-technique-public"), $("#sign-in-or-up-dialog"));

        add_dialog_link_listeners
      ($(".public-share"), $("#sign-in-or-up-dialog"));

        add_dialog_link_listeners
      ($(".public-status"), $("#sign-in-or-up-dialog"));

        add_dialog_link_listeners
      ($(".public-suggest"), $("#sign-in-or-up-dialog"));

        function add_dialog_link_listeners(link, dialog) {
            link.click(function(event) {
                event.preventDefault();
                event.stopImmediatePropagation();
                dialog_manager.open(dialog);
            });
        }
        function add_live_dialog_link_listeners(link, dialog) {
            link.live('click', function(event) {
                event.preventDefault();
                event.stopImmediatePropagation();
                dialog_manager.open(dialog);
            });
        }

        // enable "show comments" links
        $(".expand-link-comments").each(function() {
            $(this).click(function() {
                if ($("#comments-view").css("display") == "none") {
                    $(".close-comments").show();
                    $("#link-container iframe").each(function() {
                        var comments_height = $("#comments-view").show().outerHeight();
                        var new_iframe_height = Math.max($(this).height() - comments_height, 0);
                        $(this).css({ height: new_iframe_height });
                    });
                }
            });
        });

        // enable "close comments" links
        $(".collapse-link-comments").each(function() {
            $(this).click(function() {
                if ($("#comments-view").css("display") == "block") {
                    $(".close-comments").hide();
                    $("#link-container iframe").each(function() {
                        var comments_height = $("#comments-view").outerHeight();
                        $("#comments-view").hide();
                        var new_iframe_height = $(this).height() + comments_height;
                        $(this).css({ height: new_iframe_height });
                    });
                }
            });
        });

        // enable "forgot your password" links: switch to display recovery form
        $("ul#dialogs li.dialog div.dialog-row a.column-switch").each(function() {
            $(this).click(function(event) {
                event.preventDefault();
                event.stopImmediatePropagation();
                var row = $(this).parents("div.dialog-column.switch").parent();
                row.children("div.dialog-column.switch").toggle();
            });
        });

        // enable share-menu links in submission boxes
        $("div.box .show-share-menu").each(function(index, share_link) {
            $(share_link).click(function() {
                var box = $(share_link).parents("div.box");
                box.find("div.share-menu span.close").click(function(index, span) {
                    box.find("div.share-menu").hide();
                });
                box.find("div.share-menu").show();
            });
        });

        // enable share-menu links in submission boxes
        $("div.wide-box .show-share-menu").each(function(index, share_link) {
            $(share_link).click(function() {
                var box = $(share_link).parents("div.wide-box");
                box.find("div.share-menu span.close").click(function(index, span) {
                    box.find("div.share-menu").hide();
                });
                box.find("div.share-menu").show();
            });
        });

        // enable share-menu links in link+video titles
        $("#resource-title-section .open-share-menu").each(function(index, share_link) {
            $(share_link).click(function() {
                var menu = $("#resource-title-section .share-menu");
                menu.find(".close").click(function(index, span) {
                    menu.hide();
                });
                menu.toggle();
            });
        });

        // increase height of iframe for linked content, 
        // to fit the available space depending on browser size
        $("#link-container iframe").each(function() {
            var height = $(document).height();
            var outer_height = $(document.body).outerHeight();
            var available_space = Math.max(height - outer_height, 0);
            var new_iframe_height = $(this).outerHeight() + available_space - 22;
            $(this).animate({ height: new_iframe_height }, 1000);
        });

        // all clicks outside the floating nav closes it
        $("body").click(function(event) {
            if (!$.contains($("#floating-nav")[0], event.target))
                floating_nav.close();
        });

    })();

    // class encapsulates the behaviour of the topic nav,
    // maintains view state, expands, collapses menu and sets
    // appropriate css classes
    function TopicNav(nav, button) {
        nav.hide();

        button.click(function() {
            is_visible() ? hide() : show();
        });

        function is_visible() {
            return button.hasClass("active");
        }

        function hide() {
            nav.slideUp();
            button.removeClass("active");
        }

        function show() {
            nav.slideDown();
            button.addClass("active");
        }
    }

    // class encapsulates popup-dialogs: 
    // the floating nav and various dialog boxes.
    // maintains and initializes view state, opens and closes, sets CSS-classes.
    function Popup(nav, button_selector, initializer, secondary_button_selector) {
        var items = nav.children("li");

        items.each(function(index, element) {
            $(element).find(button_selector).click(function() {
                is_open($(element)) ? close() : open($(element));
            });
            $(element).find(secondary_button_selector).click(function() {
                is_open($(element)) ? close() : open($(element));
            });
        });

        nav.find(".popup-close-button").click(close);
        nav.find(".switch-in").click(switchin);
        nav.find(".switch-up").click(switchup);

        this.switchin = switchin;
        function switchin() {
            items.removeClass("open");
            nav.removeClass("open");
            open($("#sign-up-dialog"));
        }

        this.switchup = switchup;
        function switchup() {
            items.removeClass("open");
            nav.removeClass("open");
            open($("#sign-in-dialog"));
        }

        function is_open(element) {
            return element.hasClass("open");
        }

        this.open = open;
        function open(element) {
            close();
            if (typeof (initializer) == 'function') {
                initializer(nav);
            }
            nav.addClass("open");
            element.addClass("open");
        }

        this.close = close;
        function close() {
            items.removeClass("open");
            nav.removeClass("open");

        }
    }
});
