﻿var CategoryRoot = {
    CurrentParentId: 0,
    Categories: null,
    getCategoryPathString: function (Categories, CurrentParentId) {
        if (Categories == null) return 'Home:0';
        for (var i in Categories) {
            if (CurrentParentId == Categories[i].CategoryId) {
                var val = this.getCategoryPathString(Categories, Categories[i].ParentId) + '|' + Categories[i].Name + ':' + Categories[i].CategoryId;
                if (val != 'undefined')
                    return val;
            }
            else if (CurrentParentId == 0) {
                return 'Home:0';
            }
            else {
            }
        }
    },
    getCategoryPath: function (Categories, CurrentParentId) {
        return this.getCategoryPathString(Categories, CurrentParentId).split('|');
    },
    getCategoryNameFromPath: function (categoryName) {
        var arrCategoryNav = categoryName.split(':');
        var NavCategoryName = arrCategoryNav[0];
        var NavCategoryId = arrCategoryNav[1];
        return {
            CategoryName: NavCategoryName,
            CategoryId: NavCategoryId
        };
    },
    SaveCategory: function (CategoryName, Image, CategoryDescription, CategoryHeadKeywordsTag) {
        var CategoryFullName = '', CategoryHeadingTitle = '', CategoryHeadTitleTag = '', CategoryHeadDescriptionTag = '', separator = '|';
        var CategoryArray = this.getCategoryPath(this.Categories, this.CurrentParentId);
        for (var i in CategoryArray) {
            var CatName = CategoryArray[i].split(':')[0];
            if (CatName != 'Home') {
                CategoryFullName = CategoryFullName + CategoryArray[i].split(':')[0] + separator;
            }
        }
        CategoryFullName = CategoryFullName + CategoryName;

        var Category = new MarketYard.Store.Services.Category();
        var retCat = Category.SaveCategory(CategoryFullName, Image, CategoryHeadingTitle, CategoryHeadTitleTag, CategoryHeadKeywordsTag, CategoryHeadDescriptionTag, CategoryDescription, separator, OnCategorySaved, OnWSError, OnWSTimeout);
    },
    DeleteCategory: function (CategoryId) {
        var Category = new MarketYard.Store.Services.Category();
        var retCat = Category.DeleteCategory(CategoryId, OnCategoryDeleted, OnWSError, OnWSTimeout);
    },
    RefreshCategories: function () {
        var Category = new MarketYard.Store.Services.Category();
        var retCat = Category.GetCategories(OnCategoriesLoaded, OnWSError, OnWSTimeout);
    },
    getCurrentCategoryPath: function () {
        return this.getCategoryPath(this.Categories, this.CurrentParentId);
    },
    getCurrentCategory: function () {
        var arrCategoryPath = this.getCurrentCategoryPath();
        return this.getCategoryNameFromPath(arrCategoryPath[arrCategoryPath.length - 1]);
    }
};

function OnCategorySaved(Category) {
    $get('lblMessage').value = Category.Name + ' Saved!';
    CategoryRoot.RefreshCategories();
}

function OnCategoryDeleted() {
    $get('lblMessage').value = 'Category deleted!';
    CategoryRoot.RefreshCategories();
}

function jsDisplayCategories(currentParentId) {
    CategoryRoot.CurrentParentId = currentParentId;
    $get('dvCategories').innerHTML = tmpl('CategoriesTemplate', CategoryRoot);

    $get('dvCategoriesHeader').innerHTML = tmpl('CategoriesHeaderTemplate', CategoryRoot);

    jsDisplayProducts(currentParentId);
}

var ProductsDataRoot = {
    ParentCategoryId: 0
};

var ProductListTableControl = null;

function jsDisplayProducts(currentCategoryId) {
    //  ProductsDataRoot.ParentCategoryId = currentCategoryId;
    // $get('dvProducts').innerHTML = tmpl('ProductsTemplate', ProductsDataRoot);
    //DownloadProductList(null, 0, 30, '');           
    DownloadProductListFirstTime(null, 0, 60, '');
}

function OnCategoriesLoaded(CategoryList) {
    CategoryRoot.Categories = CategoryList;
    jsDisplayCategories(CategoryRoot.CurrentParentId);
}


function onWSProductListRetrievedFirstTime(data) {
    var myJSONText = JSON.stringify(data);
    $get('txtErrorBox').value = myJSONText;
    ProductListTableControl.setCurrentPage(0);
    ProductListTableControl.SetTotalRowCount(data.TotalProductCount);
    ProductListTableControl.setDataSource(data.ProductList);
    ProductListTableControl.SetDownloadPageFlags(ProductListTableControl.getCurrentPage());
    ProductListTableControl.RowsLoadingComplete();
    $get('dvProductList').innerHTML = ProductListTableControl.getHTML();
    ProductListTableControl.RegisterEvents();
}

function onWSProductListRetrievedResetAll(data) {
    var myJSONText = JSON.stringify(data);
    $get('txtErrorBox').value = myJSONText;
    ProductListTableControl.SetTotalRowCount(data.TotalProductCount);
    ProductListTableControl.setDataSource(data.ProductList);
    ProductListTableControl.SetDownloadFlag(0, data.TotalProductCount);
    ProductListTableControl.RowsLoadingComplete();
    $get('dvProductList').innerHTML = ProductListTableControl.getHTML();
    ProductListTableControl.RegisterEvents();
}

function onWSProductListRetrieved(data) {
    var myJSONText = JSON.stringify(data);
    $get('txtErrorBox').value = myJSONText;
    ProductListTableControl.appendDataSource(data.ProductList);
    ProductListTableControl.RowsLoadingComplete();
    ProductListTableControl.SetDownloadPageFlags(ProductListTableControl.getCurrentPage());
}

function ProductListTableControl_FirstPage(object) {
    $get("dvProductList").innerHTML = ProductListTableControl.getHTML();
    ProductListTableControl.RegisterEvents();
}

function ProductListTableControl_PreviousPage(object) {
    $get("dvProductList").innerHTML = ProductListTableControl.getHTML();
    ProductListTableControl.RegisterEvents();
}

function ProductListTableControl_NextPage(object) {
    $get("dvProductList").innerHTML = ProductListTableControl.getHTML();
    ProductListTableControl.RegisterEvents();
}

function ProductListTableControl_LastPage(object) {
    $get("dvProductList").innerHTML = ProductListTableControl.getHTML();
    ProductListTableControl.RegisterEvents();
}

function DownloadProductList(obj, rowStart, rowEnd, orderBy) {
    //            $get("rowStart").innerHTML = rowStart;
    //            $get("rowEnd").innerHTML = rowEnd;
    ProductListTableControl.StartLoadingRows();
    $get('dvProductList').innerHTML = ProductListTableControl.getHTML();
    var ProductList3 = new StoreFrontService.ProductList();
    ProductList3.GetProductsByCategory(CategoryRoot.getCurrentCategory().CategoryId, rowStart, rowEnd, '', onWSProductListRetrieved, OnWSError, OnWSTimeout);
}

function DownloadProductListFirstTime(obj, rowStart, rowEnd, orderBy) {
    //            $get("rowStart").innerHTML = rowStart;
    //            $get("rowEnd").innerHTML = rowEnd;
    //alert(CategoryRoot.getCurrentCategory().CategoryName);
    //alert(CategoryRoot.getCurrentCategory().CategoryName);
    var searchTerm = '';
    searchTerm = CategoryRoot.getCurrentCategory().CategoryName;

    var ProductList3 = new StoreFrontService.ProductList();
    ProductList3.GetProductsByCategory(CategoryRoot.getCurrentCategory().CategoryId, rowStart, rowEnd, '', onWSProductListRetrievedFirstTime, OnWSError, OnWSTimeout);
}

function DownloadProductListResetAll(obj, rowStart, rowEnd, orderBy) {
    //            $get("rowStart").innerHTML = rowStart;
    //            $get("rowEnd").innerHTML = rowEnd;
    var ProductList3 = new StoreFrontService.ProductList();
    ProductList3.GetProductsByCategory(CategoryRoot.getCurrentCategory().CategoryId, rowStart, rowEnd, '', onWSProductListRetrievedResetAll, OnWSError, OnWSTimeout);
}
