﻿//http: //www.akchauhan.com/create-simple-left-tree-menu-using-jquery/
jQuery.fn.exists = function(){return this.length>0;}

function initTreeMarchi() {
    $("#Marchi ul").each(function() { $(this).css("display", "none"); });
    $("#Marchi .category").click(function() {
        var childid = "#" + $(this).attr("childid");
        if ($(childid).css("display") == "none") { $(childid).css("display", "block"); }
        else { $(childid).css("display", "none"); }
        if ($(this).hasClass("cat_close")) { $(this).removeClass("cat_close").addClass("cat_open"); }
        else { $(this).removeClass("cat_open").addClass("cat_close"); }
    });
    
    if ($("#Marchi").exists())Presel();

}

function preselTreeMarchi(idMarchio) {
    $("#Marchi a").each(function() {
        if ($(this).attr("valueid") == idMarchio) {
            $(this).addClass("selected");

            var childid = "#" + $(this).parents('ul').attr('id');
                $(childid).css("display", "block");

            var sChildId = $(this).parents('ul').attr('id');

            if ($(this).parents('ul').attr('id') != $("#Marchi").attr('id')) {
                $("#Marchi a").each(function() {
                    if ($(this).attr("childid") == sChildId)// && $(this).hasClass("cat_close")) 
                        $(this).removeClass("cat_close").addClass("cat_open");
                }
            );
            }
        };
    }
    );
}

function preselTreeCategorie(idCategoria) {
    $("#Categorie a").each(function() {
        if ($(this).attr("valueid") == idCategoria) $(this).addClass("selected");
    });
}

function initTreeCategoriePerMarchio() {
    $("#CategoriePerMarchio ul").each(function() { $(this).css("display", "none"); });
    $("#CategoriePerMarchio .category").click(function() {
        var childid = "#" + $(this).attr("childid");
        if ($(childid).css("display") == "none") { $(childid).css("display", "block"); }
        else { $(childid).css("display", "none"); }
        if ($(this).hasClass("cat_close")) { $(this).removeClass("cat_close").addClass("cat_open"); }
        else { $(this).removeClass("cat_open").addClass("cat_close"); }
    });

    if ($("#CategoriePerMarchio").exists())Presel();

}    

