$(document).ready(function() {
	InitFindWidget();	
});

var bHideFindWidget = {};
var activeFindWidgetItem = {};

function InitFindWidget() {
    var selector = "ul.selector";
    var selectors = [];
    $(selector).each(function(idx) {
        var id = "selector-" + idx;
        $(this).attr("id", id);
        selectors[idx] = "#" + id;
    });
    for (var x=0;x<selectors.length;x++) {
        var id = selectors[x];
        $(id + " li a").click(function() {
            theList = $(this).parents("ul");
            if (!theList.hasClass("show")) {
                $(this).parent("li").removeClass("active").addClass("selected");
                theList.addClass("show").children("li:first").addClass("active");
                return false;
            } else {
                activeFindWidgetItem[theList.attr("id")] = $(this).parent("li");
                theList.children("li").removeClass("active selected");
                $(this).parent("li").addClass("active");
                theList.removeClass("show");
            }
            if ($(id + "+" + "a.round-button").length > 0) return false;
        }).hover(
            function() {
                $(this).parents("li").addClass("hover");
            },
            function() {
                $(this).parents("li").removeClass("hover");
            }
        );
        $(id).mouseout(function() {
            var id = $(this).attr("id");
            if ($(this).hasClass("show"))
                bHideFindWidget[id] = setTimeout(
                    function() {
                        $("#" + id).removeClass("show");
                        if (activeFindWidgetItem[id]) {
                            $("#" + id + " li").removeClass("active selected");
                            activeFindWidgetItem[id].addClass("active");
                        }
                    }, 500);
        }).mouseover(function() {
            var id = $(this).attr("id");
            if (bHideFindWidget[id]) clearTimeout(bHideFindWidget[id]);
        });
        $(id + "+" + "a.round-button").hover(
            function() {
                $(this).addClass("hover");
                $(this).css("cursor", "pointer");
            },
            function() {
                $(this).removeClass("hover");
                $(this).css("cursor", "default");
            }
        ).click(function() {
            var id = $(this).prev("ul.selector").attr("id");
            var href = $("#" + id + " li.active a").attr("href");
            if (!href) return false;
            href = href.trim();
            if (href != "#") location.href = href;
            return false;
        });
    }
}