﻿var Product = function () {
    var self = {
        init: function () {
            loadAllActiveCategories();
        },
        loadCategory: function (categoryId) {
            loadCategoryData(categoryId);
            loadDistinctProductList(categoryId);
        }
    };
    function loadDistinctProductList(categoryId) {
        var $productGrid = $("#ProductGrid");
        $.ajax({
            type: "POST",
            url: "Services/Product.asmx/GetProductNamesByCategory",
            data: "{categoryId: " + categoryId + "}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            failure: function (msg) {
                //console.log(msg); 
            },
            success: function (msg) {
                $.each(msg.d, function (i, item) {
                    $productGrid.append('<li><div><a class="gridheader" style="font-size: x-small;" href="viewitem.asp?catid=' + item.CategoryId + '&part_num=' + item.PartNumber + '&part_name=' + item.Name + '"><img src="Image.ashx?filename=' + item.ImageFileName + '" height="100" border="0" alt="" /><br />' + item.Name + '</a></div></li>');
                });
            }
        });
    }
    function loadCategoryData(categoryId) {
        var $categoryTitle = $("#categoryTitle");
        var $categoryDesc = $("#categoryDesc");
        $.ajax({
            type: "POST",
            url: "Services/Product.asmx/GetCategory",
            data: "{categoryId: " + categoryId + "}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            failure: function (msg) {
                //console.log(msg); 
            },
            success: function (msg) {
                if (msg.d.RedirectURL === '' || msg.d.RedirectURL === null) {
                    $categoryTitle.html(msg.d.Category + '<hr/>');
                    $categoryDesc.html(msg.d.CategoryContent);
                }
                else {
                    document.location.href = msg.d.RedirectURL;
                }
            }
        });
    }
    function loadAllActiveCategories() {
        var $unorderedList = $("#navigation .leftMenu");
        $.ajax({
            type: "POST",
            url: "Services/Product.asmx/GetCategories",
            data: "{isOnlyActive: true}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            failure: function (msg) {
                // console.log(msg);
            },
            success: function (msg) {
                $.each(msg.d, function (i, item) {
                    if (item.RedirectURL === '') {
                        $unorderedList.append('<li><a href="viewbycategory.asp?catid=' + item.CategoryId + '&desc=' + item.Category + '">' + item.Category + '</a></li>');
                    }
                    else {
                        $unorderedList.append('<li><a href="' + item.RedirectURL + '">' + item.Category + '</a></li>');
                    }
                });
            }
        });
    }

    return self;
} ();
