﻿(function($) {
    $.fn.SubscriptionQuery = function(count, url, params, options) {
        // Set the options.
        var opts = $.extend({}, $.fn.SubscriptionQuery.defaults, options);

        // Go through the matched elements and return the jQuery object.
        return this.each(function() {
            // Extend our options object with metadata from the DOM
            var o = $.metadata ? $.extend({}, opts, $(this).metadata()) : opts;

            var subscriptionDiv = $(this);
            $(".public-collection-result-desc a").text("Searching...");

            if (!url) return;

            if (count > 0) {
                $.getJSON(url, params, function(viewModel) {
                    if (viewModel.ServiceMessages.length == 0) {
                        $.each(viewModel.PublicCollections, function(index, publicCollection) {
                            var html = '';
                            $.each(publicCollection.Results, function(index, result) {
                                html += '<li><a href="' + result.Url + '" title="' + result.Title + '" target="_blank">' + result.Title + '</a></li>';
                            });
                            var publicCollectionElem = $("#public-collection-" + publicCollection.Name.replace(/ /g, "-"));
                            $(".results", publicCollectionElem).html(html);
                            $(".public-collection-result-count", publicCollectionElem).text(publicCollection.Results.length);
                            $(".public-collection-result-desc a", publicCollectionElem).text("Items");
                            if (publicCollection.Results.length > 0) {
                                $(".input-button", publicCollectionElem).css("display", "block");

                                $("input", publicCollectionElem).click(function() {
                                    if ($(".results", publicCollectionElem).hasClass("expanded")) {
                                        $(".results", publicCollectionElem).slideUp(600, null).removeClass("expanded");
                                        $("input", publicCollectionElem).val("\u25BC");
                                    } else {
                                        $(".results", publicCollectionElem).slideDown(600, null).addClass("expanded");
                                        $("input", publicCollectionElem).val("\u25B2");
                                    }
                                });
                            }
                        });
                    } else {
                        var message = "";
                        $.each(viewModel.ServiceMessages, function(index, serviceMessage) {
                            message += serviceMessage.Message + " ";
                        });
                    }
                });
            }
            else {
                //messageDiv.html('<p id="no-subscription">Collectish will search the world\'s collections when you add some items!</p>').fadeIn(400, null);
            }                        
        });
    };
    // Public defaults.
    $.fn.SubscriptionQuery.defaults = {
};
})(jQuery);
