﻿/// <reference path="jquery.js" />

(function($) {
    var cache = [];

    $.fn.triggerList = function(options) {

        var defaults = {
            event: "click",
            callback: null
        };

        var opts = $.extend(defaults, options);

        return this.each(function() {
            var $this = $(this);
            $this.bind(opts.event, $this.metadata({ type: "attr", name: "data" }), opts.callback);
        });
    };
})(jQuery);

(function($) {
    var cache = [];

    $.fn.subcategoryDrilldown = function(options) {

        var defaults = {
            flyout: "#flyout",
            flyoutContent: "#flyout .content",
            event: "click",
            callback: null
        };

        var opts = $.extend(defaults, options);

        $(this).triggerList(
        {
            callback: function(e) {
                e.preventDefault();
                //console.log("Event on element: %i", e.data.categoryid);

                $.getJSON("webservices/fc.svc/GetChildCategories", { "categoryid": e.data.categoryid }, function(data) {
                    // If there are no subcategories then execute the callback immediately
                    if (data.length == 0) {
                        opts.callback(e.data.categoryName, "");
                    } else {
                        $(opts.flyoutContent).html($.fn.subcategoryDrilldown.renderContent(e.data.categoryName, data, opts.callback));
                        var hovertimer = 0;
                        $(opts.flyout)
                            .fadeIn()     
                            .hover(
                            function() {
                                if (hovertimer != 0) {
                                    clearTimeout(hovertimer);
                                    hovertimer = 0;
                                }
                            },
                            function() {
                                if (hovertimer == 0)
                                    hovertimer = setTimeout(function() { $(opts.flyout).fadeOut(); }, 2500);
                            }
                        );
                    }
                });
            }
        });
    };

    $.fn.subcategoryDrilldown.renderContent = function(rootCat, categories, callback) {
        var list = $("<ul></ul>");

        // Add the "View All" link
        $('<li data="{cat:\'' + rootCat + '\',subcat:\'\'}"><b><a href="SearchResults.aspx?pc=' + rootCat + '">View All</a></b></li>').appendTo(list);

        // Add each of the subcategory links
        for (var i = 0; i < categories.length; i++) {
            var name = categories[i].Name;
            var pname = categories[i].ParentCategoryName;
            $('<li data="{cat:\'' + rootCat + '\',subcat:\'' + name + '\'}"><a href=\"SearchResults.aspx?pc=' + pname + "&c=" + name + "\">" + name + "</a></li>").appendTo(list)
            //console.log("child category %s", data[i].Name);
        }

        list.children().triggerList({
            callback: function(e) {
                e.preventDefault();
                callback(e.data.cat, e.data.subcat);
            }
        });

        return list;
    };
})(jQuery);