//Function to add listeners to My Crestron Product Catalog Links
$(document).ready(
    function addMyCrestronProductCatalogListeners() {
        //category click listener
        $(".myCrestronCatalogCategoryList > li > a").click(
            function () {
                var catId = $(this).attr("rel");                                                //Get the category id from the anchor
                var relatedDiv = "divMyCrestronProductCatalogCategorySubcategories" + catId;    //Get the div that holds the related subcategories
                var linkText = $(this).text();                                                  //Get the link text
                if ($("#" + relatedDiv).html() == "") {                         //They are attempting to expand the div
                    $(this).find("span").html("-");                                     //Change the plus to a minus
                    $.get("/data/widgets/my_crestron/my_crestron_product_catalog/subcategories/" + catId + ".asp", function (data) {
                        $("#" + relatedDiv).html(data);                                 //Load the subcategories into the div
                        loadMyCrestronCatalogSubcategoryClicks();                       //Attach the Subcategory Click Listeners
                        updateMyCrestronExpandedProductCategoriesCookie(catId, true);   //Update the Cookie
                    });
                }
                else {                                                          //They are attempting to minimize the div
                    $(this).find("span").html("+");                                     //Change the min symbol to a max
                    $("#" + relatedDiv).html("");                                       //Clear the related subcategories
                    updateMyCrestronExpandedProductCategoriesCookie(catId, false);      //Update the Cookie
                }
                return false;
            }
        );
        //subcategory click listener
        $(".myCrestronCatalogSubcategoryList > li > a").click(
            function () {
                var subCatId = $(this).attr("rel");                                             //Get the subcategory id from the anchor
                var relatedDiv = "divMyCrestronProductCatalogSubcategoryProducts" + subCatId;   //Get the div that holds the related products
                var linkText = $(this).text();                                                  //Get the link text
                if ($("#" + relatedDiv).html() == "") {                         //They are attempting to expand the div
                    $(this).find("span").html("-");                                     //Change the plus to a minus
                    $.get("/data/widgets/my_crestron/my_crestron_product_catalog/products/" + subCatId + ".asp", function (data) {
                        $("#" + relatedDiv).html(data);                                 //Load the products into the div
                    });
                    updateMyCrestronExpandedProductSubCategoriesCookie(subCatId, true); //Update the Cookie
                }
                else {                                                          //They are attempting to minimize the div
                    $(this).find("span").html("+");                                         //Change the minimize to a maximize
                    $("#" + relatedDiv).html("");                                           //Empty the Products Div
                    updateMyCrestronExpandedProductSubCategoriesCookie(subCatId, false);    //Update the Cookie
                }
                return false;
            }
        );
    }
);


//Separate Function to Handle MyCrestron Catalog Category Clicks Post AJAX Load
function loadMyCrestronCatalogCategoryClicks() {
    //category click listener
    $(".myCrestronCatalogCategoryList > li > a").click(
        function () {
            var catId = $(this).attr("rel");                                                //Get the category id from the anchor
            var relatedDiv = "divMyCrestronProductCatalogCategorySubcategories" + catId;    //Get the div that holds the related subcategories
            var linkText = $(this).text();                                                  //Get the link text
            if ($("#" + relatedDiv).html() == "") {                         //They are attempting to expand the div
                $(this).find("span").html("-");                       //Change the plus to a minus
                $.get("/data/widgets/my_crestron/my_crestron_product_catalog/subcategories/" + catId + ".asp", function (data) {
                    $("#" + relatedDiv).html(data);                                 //Load the subcategories into the div
                    loadMyCrestronCatalogSubcategoryClicks();                       //Attach the Subcategory Click Listeners
                    updateMyCrestronExpandedProductCategoriesCookie(catId, true);   //Update the Cookie
                });
            }
            else {                                                          //They are attempting to minimize the div
                $(this).find("span").html("+");                                     //Change the min symbol to a max
                $("#" + relatedDiv).html("");                                       //Clear the related subcategories
                updateMyCrestronExpandedProductCategoriesCookie(catId, false);      //Update the Cookie
            }
            return false;
        }
    );
}

//Separate Function to Handle MyCrestron Catalog Subcategory Clicks Post AJAX Load
function loadMyCrestronCatalogSubcategoryClicks() {
    //subcategory click listener
    $(".myCrestronCatalogSubcategoryList > li > a").click(
        function () 
        {
            var subCatId = $(this).attr("rel");                                             //Get the subcategory id from the anchor
            var relatedDiv = "divMyCrestronProductCatalogSubcategoryProducts" + subCatId;   //Get the div that holds the related products
            var linkText = $(this).text();                                                  //Get the link text
            if ($("#" + relatedDiv).html() == "") {                         //They are attempting to expand the div
                $(this).find("span").html("-");                                     //Change the plus to a minus
                $.get("/data/widgets/my_crestron/my_crestron_product_catalog/products/" + subCatId + ".asp", function (data) {
                    $("#" + relatedDiv).html(data);                                 //Load the products into the div
                });
                updateMyCrestronExpandedProductSubCategoriesCookie(subCatId, true); //Update the Cookie
            }
            else {                                                          //They are attempting to minimize the div
                $(this).find("span").html("+");                                         //Change the minimize to a maximize
                $("#" + relatedDiv).html("");                                           //Empty the products div
                updateMyCrestronExpandedProductSubCategoriesCookie(subCatId, false);    //Update the Cookie
            }
            return false;
        }
    );
}

//Function to update the users expanded category cookie
function updateMyCrestronExpandedProductCategoriesCookie(categoryId, expandIt) {
    if ($.cookie("MyCrestronExpandedProductCategories") != null) {
        var expandedCategoriesString = $.cookie("MyCrestronExpandedProductCategories").replace(/\+/g, "");
        var expandedCategoriesArray = expandedCategoriesString.split(",");
        var expandedCategoryIndex;
        if (expandIt == true) {
            expandedCategoryIndex = expandedCategoriesArray.find("cat-" + categoryId);
            if (expandedCategoryIndex.toString() == "false") {
                expandedCategoriesArray.push("cat-" + categoryId);                              //Add the category to the array, since it does not exist
            }
        }
        else {
            expandedCategoryIndex = expandedCategoriesArray.find("cat-" + categoryId);
            if (expandedCategoryIndex.toString() == "false") {
                expandedCategoryIndex = expandedCategoriesArray.find("cat-" + categoryId);
            }
            if (expandedCategoryIndex != false) {
                expandedCategoriesArray.splice(expandedCategoryIndex, 1);                   //Remove the category from the array
            }
        }
        expandedCategoriesString = expandedCategoriesArray.toString();
        $.cookie("MyCrestronExpandedProductCategories", expandedCategoriesString, { expires: 365, path: "/" });
        $.ajax({ type: "POST", url: "/my_crestron_account/update_website_preferences.asp", data: "action=update_my_crestron_catalog_expanded_product_categories&preference_value=" + expandedCategoriesString });
    }
}

//Function to update the users expanded subcategory cookie
function updateMyCrestronExpandedProductSubCategoriesCookie(subCategoryId, expandIt) {
    if ($.cookie("MyCrestronExpandedProductSubCategories") != null) {
        var expandedSubCategoriesString = $.cookie("MyCrestronExpandedProductSubCategories").replace(/\+/g, "");
        var expandedSubCategoriesArray = expandedSubCategoriesString.split(",");
        var expandedSubCategoryIndex;
        if (expandIt == true) {
            expandedSubCategoryIndex = expandedSubCategoriesArray.find("cat-" + subCategoryId);
            if (expandedSubCategoryIndex.toString() == "false") {
                expandedSubCategoriesArray.push("cat-" + subCategoryId);                              //Add the category to the array, since it doesn't exist
            }
        }
        else {
            expandedSubCategoryIndex = expandedSubCategoriesArray.find("cat-" + subCategoryId);
            if (expandedSubCategoryIndex.toString() == "false") {
                expandedSubCategoryIndex = expandedSubCategoriesArray.find("cat-" + subCategoryId);
            }
            if (expandedSubCategoryIndex != false) {
                expandedSubCategoriesArray.splice(expandedSubCategoryIndex, 1);                   //Remove the category from the array
            }
        }
        expandedSubCategoriesString = expandedSubCategoriesArray.toString();
        $.cookie("MyCrestronExpandedProductSubCategories", expandedSubCategoriesString, { expires: 365, path: "/" });
        $.ajax({ type: "POST", url: "/my_crestron_account/update_website_preferences.asp", data: "action=update_my_crestron_catalog_expanded_product_subcategories&preference_value=" + expandedSubCategoriesString });
    }
}


