﻿var sCategoryID = 0;
var sSubCategoryID = 0;
var sTypeID = 0;
var sSupplierID = 0;

function doAjaxRequest(url, callback) {
    $.ajax({
        url: url,
        type: "POST",
        dataType: "json",
        data: {
            CategoryID: sCategoryID,
            SubCategoryID: sSubCategoryID,
            TypeID: sTypeID,
            SupplierID: sSupplierID
        },
        success: function (data) {           
            eval(callback + "($.parseJSON(data));");
        },
        error: function (one, two, three) {
            alert(three);
        }
    });
}

function addLinkElements(ar, linkid, contid, callback, defaultMsg) {

    if (ar.length > 0) {
        $('div#' + contid).html('');
        var el_list = $('<ul/>');
        var el_item = $('<li />').append($('<a />', {
            href: '#',
            id: linkid + '0',
            text: 'Show All',
            click: function () {
                displaySelected($(this).parent(), contid);               
                eval(callback + "($(this).attr('id'))");
            } 
        }));
        el_item.appendTo(el_list);

        for (var i = 0; i < ar.length; i++) {
            el_item = $('<li />').append($('<a />', {
                href: '#', id:
                linkid + ar[i].ID,
                text: ar[i].Name,
                click: function () {
                    displaySelected($(this).parent(), contid);
                    eval(callback + "($(this).attr('id'))");
                } 
            }));
            el_item.appendTo(el_list);
        }
        el_list.appendTo('#' + contid);
    }
    else {
        $('#' + contid).append(defaultMsg);
    }

}

function populateSubCategory(data) {

    if (data.ProductSubCategories && sSubCategoryID == 0) {
        addLinkElements(data.ProductSubCategories, 'pscl-', 'menu-subcategories', 'retrieveSubCategory', 'No Sub Categories');
    }
}

function populateProductType(data) {

    if (data.ProductTypes && sTypeID == 0) {
        addLinkElements(data.ProductTypes, 'ptl-', 'menu-producttypes', 'retrieveTypes', 'No Types');
    }
}

function populateSupplier(data) {

    if (data.Suppliers && sSupplierID == 0) {
        addLinkElements(data.Suppliers, 'spl-', 'menu-supplier', 'retrieveSuppliers', 'No Suppliers');
    }
}

function populateProduct(data) {
    
    var html = "";
    if (data.Products.length > 0) {
        for (var i = 0; i < data.Products.length; i++) {            
            html += "<div class='product-item'>" +
                        "<a href='/" + data.Products[i].CategoryName.replace(/ /g, "-") + "/" + data.Products[i].SubCategoryName.replace(/ /g, "-") + "/" + data.Products[i].Name.replace(/ /g, "-") + "'><img src='/Content/images/products/thb/" + data.Products[i].Image + "' width='100' border='0' /></a> <br />" +
                        "<a href='/" + data.Products[i].CategoryName.replace(/ /g, "-") + "/" + data.Products[i].SubCategoryName.replace(/ /g, "-") + "/" + data.Products[i].Name.replace(/ /g, "-") + "'>" + data.Products[i].Name + "</a>" +
                    "</div>";
        }
    }
    else {
        html = "No Products Found";
    }

    $('div#expander-body').html(html);

    populateSubCategory(data);
    populateProductType(data);
    populateSupplier(data);
}

function retrieveCategory(id) {

    sCategoryID = id.substring(4);
    sSubCategoryID = 0;
    sTypeID = 0;
    sSupplierID = 0;

    doAjaxRequest("/Home/DataRequest", "populateProduct");

}

function retrieveSubCategory(id) {

    sSubCategoryID = id.substring(5);

    if (sSubCategoryID == '0') {
        sTypeID = 0;
        sSupplierID = 0;
    }

    doAjaxRequest("/Home/DataRequest", "populateProduct");
}

function retrieveTypes(id) {
    
    sTypeID = id.substring(4);

    if (sTypeID == '0') {
        sSupplierID = 0;
        sSubCategoryID = 0;
    }

    doAjaxRequest("/Home/DataRequest", "populateProduct");
}

function retrieveSuppliers(id) {

    sSupplierID = id.substring(4);

    if (sSupplierID == '0') {
        sTypeID = 0;
        sSubCategoryID = 0;
    }

    doAjaxRequest("/Home/DataRequest", "populateProduct");
}

function displaySelected(element, containerid) {

    $('div#' + containerid).find('li').removeClass('linkselected');
    element.addClass('linkselected');
}

function displaySelectedSuppler(element, containerid) {

    $('div#' + containerid).find('li').removeClass('linkselected');
    element.addClass('linkselected');
}

$(document).ready(function () {

    $('div#breadcrumbs a[id^=pcl-]').click(function () {

        $('div#breadcrumbs a[id^=pcl-]').css('color', '#fff');
        $(this).css('color', 'Orange');

        retrieveCategory($(this).attr('id'));
        return false;
    });

    $('div#menu-subcategories a[id^=pscl-]').click(function () {

        displaySelected($(this).parent(), 'menu-subcategories');
        retrieveSubCategory($(this).attr('id'));
        return false;
    });

    $('div#menu-producttypes a[id^=ptl-]').click(function () {

        displaySelected($(this).parent(), 'menu-producttypes');
        retrieveTypes($(this).attr('id'));
        return false;
    });

    $('div#menu-supplier a[id^=spl-]').click(function () {

        displaySelected($(this).parent(), 'menu-supplier');
        retrieveSuppliers($(this).attr('id'));
        return false;
    });

    $('div.supplieritem a[id^=spi-]').click(function () {

        $('div#menu-supplier a#' + $(this).attr('id').replace('i', 'l')).parent().addClass('linkselected');        
        retrieveSuppliers($(this).attr('id'));
        return false;
    });

});
