﻿$(document).ready(function () {
    
    $(".append").click(function () {
        var button = $(this); // Append button
        var category = button.attr('rel');
        var list = $("#cat" + category); // List
        var skip = parseInt(list.find("li").size()); // Skip

        var websiteID = $(".websiteid").text();
        var query = $(".keyword").text();

        $(this).addClass("searchfieldLoading");
        var text = $(this).text();
        $(this).text('');
        $.ajax({
            type: "POST",
            url: "/modules/ecommerce34/ECommerceAjaxService.asmx/AddHitsBySearch",
            data: "{'query':'" + query + "','category':'" + category + "','skip':" + skip + ",'take': 5,'websiteID':" + websiteID + "}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (r) {
                list.append(r.d);

                var addedItems = parseInt(list.find("li").size()) - skip;

                button.removeClass("searchfieldLoading");
                button.text(text);

                if (addedItems < 5) button.slideUp(); // No more items. Hiding append button.
            }
        });
        return false;
    });

    $(".categoryBtn").live('click', function () {
        var category = $(this).attr('data-id');
        var websiteID = $(".websiteid").text();
        
        $(this).removeClass("categoryBtn").addClass("removecategoryBtn");
        $(this).parent().addClass("selected")

        if (window.location.hash == '')
            window.location.hash = category.toLowerCase();
        else
            window.location.hash += "-" + category.toLowerCase();

        updateCategories();
        
        var list = $("#cat" + category);
        var skip = parseInt(list.find("li").size());

        var query = $(".keyword").text();

        $.ajax({
            type: "POST",
            url: "/modules/ecommerce34/ECommerceAjaxService.asmx/AddHitsBySearch",
            data: "{'query':'" + query + "','category':'" + category + "','skip':" + skip + ",'take': 20,'websiteID':" + websiteID + "}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (r) {
                list.append(r.d);
            }
        });

        return false;
    });
    $(".removecategoryBtn").live('click', function () {
        window.location.hash = window.location.hash.replace("-" + $(this).attr('data-id').toLowerCase(), "");
        window.location.hash = window.location.hash.replace($(this).attr('data-id').toLowerCase(), "");

        updateCategories();
        $(this).removeClass("removecategoryBtn").addClass("categoryBtn");
        $(this).parent().removeClass("selected");
        return false;
    });
});

function updateCategories() {
    if (window.location.hash == '') {
        $(".searchCategory").slideDown('fast');
        return;
    }

    $(".searchCategory").each(function () {
        if (window.location.hash.indexOf($(this).attr('id').toLowerCase()) == -1) {
            $(this).slideUp();
        } else {
            $(this).slideDown('fast');
        }

    });
};

