﻿$.fn.dropdown = function(options) {
    var config = {
        choiceText: ":first-child",
        choiceValue: undefined,
        onSelect: undefined,
        slideSpeed: 100,
        fadeInSpeed: 750,
        minimumChoices: 2,
        isLinkList: false,
        dropUp: false
    };

    if (options) { $.extend(config, options); }
    var linkListLink;
    var initialized = false;
    if (config.isLinkList) {
        linkListLink = $("> a", $(this)).clone();
        if ($(this).isVisible())
            $(this).hide();
    }
    this.each(function(index, element) {

        var $element = $(element);
        var $panes = $("> li", element);
        var panes = $panes.get();

        var choices = $("<ul class='choices' />");
        var $choices = $(choices);

        // only create a dropdown if minimumChoices is met
        if (panes.length >= config.minimumChoices) {
            $choices.hide();

            $panes.each(function(index, item) {
                var $item = $(item);
                item.id = item.id || (element.id || "knowit-dropdown") + "-choice" + index;
                if (config.isLinkList === false)
                    var choice =
                $("<li/>", index === 0 ? { "class": "current"} : {})
                    .append($("<a/>", {
                        href: "#" + item.id,
                        text: $(config.choiceText, item).text()
                    }));
                else if (index > -1) {
//                        var choice = "<li>" +
//                    "<a href='" + $(config.choiceText, item).attr('href') + "'" + " target='" + $(config.choiceText, item).attr('target') + "'>"
//                          + $(config.choiceText, item).text() + "<a/>" + "</li>";
                          //Nedan fungerar inte i äldre jquery.
                        var choice = $("<li/>", index === 0 ? { "class": "current"} : {})
                    .append($("<a/>", {
                        href: $(config.choiceText, item).attr('href'),
                        text: $(config.choiceText, item).text(),
                        target: $(config.choiceText, item).attr('target')
                    }));
                }
                choices.append(choice);
            });
            var dropdown = $("<div class='dropdown'/>");
            var $dropdown = $(dropdown);
            if (!config.isLinkList)
            {
                var getSelected = function() { return $(".current:first > a", choices).clone() };
            }
            else
                var getSelected = function() { return linkListLink; }

            $element.before(
              $dropdown
                  .append(getSelected())
                  .append(choices)
            );
            if(config.isLinkList)
             $(">a",dropdown).addClass("dropdown-text");
            if (config.isLinkList)
                $("> li", choices).hover(function() {
                    $("> li", choices).removeClass("hover");
                    $(this).addClass("hover");
                    $("> a", $(this)).focus();
                }, function() {
                    $(this).removeClass("hover");
                });
            var toggleChoices = function(direction) {
                if ($choices.isVisible() && direction !== "down" || direction === "up") {
                    $choices.slideUp(config.slideSpeed);
                    $("> li", choices).removeClass("hover");
                }
                else {
                    $("ul.choices", ".dropdown").slideUp(config.slideSpeed);
                    $choices.slideDown(config.slideSpeed);
                }
                return false;
            }

            var select = function(oninit) {
                var $this = $(this);
                $("> li", choices).removeClass("current");
                $this.parent("li").addClass("current");

                var $clone = $this.clone();
                $("> a", dropdown).replaceWith($clone);

                $choices.slideUp(config.slideSpeed);
                $("> li", choices).removeClass("hover");
                $panes.hide();
                var $valE = $($this.attr("href"))
                        .fadeIn(initialized ? config.fadeInSpeed : 0)
                        .find(config.choiceValue || config.choiceText);

                if (config.onSelect) config.onSelect($valE.val() || $valE.text());

                if (initialized) $("> a", dropdown).focus();

                return false;
            }

            if (config.isLinkList === false)
                $("> li > a", choices).click(select);
            // $(".dropdown > a").live("click", toggleChoices);
            $("> a", dropdown).click(toggleChoices);
            $(document.body).click(function() { $choices.slideUp(config.slideSpeed); });

            $choices.find("> li > a:first").click();

            // keybindings
            $("> a", dropdown).keydown(function(e) {
                if ((config.dropUp && e.keyCode === 38) || (!config.dropUp && e.keyCode === 40)) { // down
                    e.preventDefault()
                    toggleChoices("down");
                    if (config.dropUp)
                        $(this).next().find("> li > a").last().focus();
                    else
                        $(this).next().find("> li > a").first().focus();
                }
                else if ((config.dropUp && ee.keyCode === 40) || e.keyCode === 38) { // up
                    e.preventDefault()
                    toggleChoices("up");
                }
            });

            $("> .choices > li > a", dropdown).keydown(function(e) {
                var next = $(this).parent("li").next().find("a").get(0);
                var prev = $(this).parent("li").prev().find("a").get(0);
                //hbg.global = e;
                if (e.keyCode === 40) { // down
                    e.preventDefault();
                    if (config.dropUp) {
                        if (next) $(next).focus();
                        else {
                            toggleChoices("up");
                            $(this).parent("li").parent("ul").prev("a").focus();
                        }
                    }
                    else
                        if (next) $(next).focus();
                } else if (e.keyCode === 38) { // up
                    e.preventDefault();
                    if (!config.dropUp) {
                        if (prev) {
                            $(prev).focus();
                        } else {
                            toggleChoices("up");
                            $(this).parent("li").parent("ul").prev("a").focus();
                        }
                    }
                    else {
                        if (prev) {
                            $(prev).focus();
                        }
                    }
                } else if (e.keyCode === 13 || e.keyCode === 32) {
                    if (config.isLinkList === false) {
                        e.preventDefault();
                        $(this).click();
                    }
                } else if (e.keyCode === 9) {
                    if (!next && !e.shiftKey) toggleChoices("up");
                }
            })
            .focus(function() {
                $(this).parent("li").siblings().removeClass("hover");
                $(this).parent("li").addClass("hover");
            })
        }
    });

    initialized = true;

    return this;
};
$.fn.isVisible = function() {
    return this.is(':visible');
};
//$.fn.linklist = function(options) {
//    var config = {
//        choiceText: ":first-child",
//        choiceValue: undefined,
//        onSelect: undefined,
//        slideSpeed: 100,
//        fadeInSpeed: 750,
//        minimumChoices: 2
//    };
//    var initialized = false;

//    this.each(function(index, element) {

//        var $element = $(element);
//        var $panes = $("> li", element);
//        var panes = $panes.get();

//        var choices = $("<ul class='choices' />");
//        var $choices = $(choices);

//        // keybindings
//        $(".dropdown > a").live("keydown", function(e) {
//            if (e.keyCode === 40) { // down
//                e.preventDefault()
//                toggleChoices("down");
//                $(this).next().find("> li > a").first().focus();
//            }
//            else if (e.keyCode === 38) { // up
//                e.preventDefault()
//                toggleChoices("up");
//            }
//        });

//        $(".dropdown > .choices > li > a")
//            .live("keydown", function(e) {
//                var next = $(this).parent("li").next().find("a").get(0);
//                var prev = $(this).parent("li").prev().find("a").get(0);
//                hbg.global = e;
//                if (e.keyCode === 40) { // down
//                    e.preventDefault();
//                    if (next) $(next).focus();
//                } else if (e.keyCode === 38) { // up
//                    e.preventDefault();
//                    if (prev) {
//                        $(prev).focus();
//                    } else {
//                        toggleChoices("up");
//                        $(this).parent("li").parent("ul").prev("a").focus();
//                    }
//                } else if (e.keyCode === 13 || e.keyCode === 32) {
//                    e.preventDefault();
//                    $(this).click();
//                } else if (e.keyCode === 9) {
//                    if (!next && !e.shiftKey) toggleChoices("up");
//                }
//            })
//            .focus(function() {
//                $(this).parent("li").siblings().removeClass("hover");
//                $(this).parent("li").addClass("hover");
//            })
//    });
//    initialized = true;

//    return this;
//};
