function UGAdminCategories(ugCommon, g_text, g_settingsObj) {

    var g_catClickReady = false;

    this.setHtmlListCats = setHtmlListCats;
    this.getCatsHeight = getCatsHeight;
    this.getArrCats = getArrCats;
    this.getNumCats = getNumCats;
    this.selectCategory = selectCategory;
    this.checkSelectFirstCategory = checkSelectFirstCategory;
    this.editCategoryByID = editCategoryByID;
    this.removeCategoryByID = removeCategoryByID;
    this.addCategory = addCategory;
    this.isCatSelected = isCatSelected;

    /**
     * get height of the categories list
     */
     function getCatsHeight () {
        var catsWrapper = jQuery("#cats_section .cat_list_wrapper");
        var height = catsWrapper.height();

        return (height);
    }


    /**
     * get arr categories
     */
    function getArrCats() {
        var arrCats = jQuery("#list_cats li").get();
        return (arrCats);
    }

    /**
     * get num categories
     */
    function getNumCats() {
        var numCats = jQuery("#list_cats li").length;
        return (numCats);
    }

    /**
     *
     * get category by id
     */
    function getCatByID(catID) {
        var objCat = jQuery("#category_" + catID);
        return (objCat);
    }


    /**
     * check if some category selected
     *
     */
    function isCatSelected(catID) {
        if (catID == ugCommon.g_selectedCatID)
            return (true);

        return (false);
    }


    /**
     * remove category from html
     */
    function removeCategoryFromHtml(catID) {

        jQuery("#category_" + catID).remove();

        if (catID == ugCommon.g_selectedCatID)
            ugCommon.g_selectedCatID = -1;

        disableCatButtons();
    }


    /**
     *
     * open the edit category dialog by category id
     */
    function editCategoryByID(catID) {
        var cat = getCatByID(catID);

        if (cat.length == 0) {
            trace("category with id: " + catID + " don't exists");
            return (false);
        }

        var dialogEdit = jQuery("#dialog_edit_category");

        dialogEdit.data("catid", catID);

        //update catid field
        jQuery("#span_catdialog_id").html(catID);

        var title = cat.data("title");

        jQuery("#input_cat_title").val(title).focus();

        var buttonOpts = {};

        buttonOpts[g_text.cancel] = function () {
            jQuery("#dialog_edit_category").dialog("close");
        };

        buttonOpts[g_text.update] = function () {
            updateCategoryTitle();
        };


        jQuery("#dialog_edit_category").dialog({
            buttons: buttonOpts,
            minWidth: 500,
            modal: true,
            open: function () {
                jQuery("#input_cat_title").select();
            }
        });
    }


    /**
     * set html cats list
     */
    function setHtmlListCats(htmlCats) {
        jQuery("#list_cats").html(htmlCats);
        refreshSortableCategories();
    }

    /**
     * select some category by id
     */
    function selectCategory(catID) {

        var cat = jQuery("#category_" + catID);
        if (cat.length == 0) {
            return (false);
            //g_ugAdmin.showErrorMessage("category with id: "+catID+" not found");
            //return (false);
        }

        if(g_settingsObj.multicategories.enabled){
            cat.parentsUntil($("#list_cats"), "li").each(function(i, el){
                jQuery(el).removeClass("mjs-nestedSortable-collapsed").addClass("mjs-nestedSortable-expanded");
            });
        }

        cat.removeClass("item-hover");

        if (cat.hasClass("selected-item"))
            return (false);

        ugCommon.g_selectedCatID = catID;

        jQuery("#list_cats li").removeClass("selected-item");
        cat.addClass("selected-item");
        enableCatButtons();

        ugCommon.ugItems.getSelectedCatItems();

        ugCommon.ugItems.unselectAllItems("selectCategory");
    }

    /**
     * set first category selected
     */
    function selectFirstCategory() {
        var arrCats = getArrCats();
        if (arrCats.length == 0)
            return (false);

        var firstCat = arrCats[0];
        var catID = jQuery(firstCat).data("id");
        selectCategory(catID);
    }

    /**
     * check if number of cats = 1, if do, select it
     */
    function checkSelectFirstCategory() {
        var arrCats = getArrCats();
        if (arrCats.length == 1)
            selectFirstCategory();
    }


    /**
     * add category
     */
    function addCategory() {
        var $selectedItem = jQuery("#list_cats li.selected-item");
        var data = {};
        if(g_settingsObj.multicategories.enabled){
            data = {
                parent_id: $selectedItem.parent().parent().data("id")
            }
        }
        ugCommon.ugItems.ajaxRequestGallery("add_category", data, g_text.adding_category, function (response) {

            var html = response.htmlCat;

            $selectedItem.parent().append(html);

            //update html cats select
            var htmlSelectCats = response.htmlSelectCats;
            jQuery("#select_item_category").html(htmlSelectCats);

            ugCommon.updateGlobalHeight();
            refreshSortableCategories();
        });
    }


    /**
     * remove some category by id
     */
    function removeCategoryByID(catID) {

        if (confirm(g_text.do_you_sure_remove) == false)
            return (false);

        var data = {};
        data.catID = catID;

        //get if selected category will be removed
        var isSelectedRemoved = (catID == ugCommon.g_selectedCatID);

        ugCommon.ugItems.ajaxRequestGallery("remove_category", data, g_text.removing_category, function (response) {
            removeCategoryFromHtml(catID);

            //update html cats select
            var htmlSelectCats = response.htmlSelectCats;
            jQuery("#select_item_category").html(htmlSelectCats);

            //clear the items panel
            if (isSelectedRemoved == true) {
                ugCommon.ugItems.clearItemsPanel();
                checkSelectFirstCategory();
            }

            ugCommon.updateGlobalHeight();
            refreshSortableCategories();

        });

    }


    /**
     * function invoke from the dialog update button
     */
    function updateCategoryTitle() {
        var dialogEdit = jQuery("#dialog_edit_category");

        var catID = dialogEdit.data("catid");

        var cat = getCatByID(catID);

        var numItems = cat.data("numitems");

        var newTitle = jQuery("#input_cat_title").val();
        var data = {
            catID: ugCommon.g_selectedCatID,
            title: newTitle
        };

        dialogEdit.dialog("close");

        var newTitleShow = newTitle;
        if (numItems && numItems != undefined && numItems > 0)
            newTitleShow += " (" + numItems + ")";

        cat.find("> div > .cat_title").html(newTitleShow);

        cat.data("title", newTitle);

        ugCommon.ugItems.ajaxRequestGallery("update_category", data, g_text.updating_category);
    }

    /**
     * update categories order
     */
    function updateCatOrder(){

        //get sortIDs
        var arrSortCats = jQuery( "#list_cats" ).sortable("toArray");
        var arrSortIDs = [];
        for(var i=0;i < arrSortCats.length; i++){
            var catHtmlID = arrSortCats[i];
            var catID = catHtmlID.replace("category_","");
            arrSortIDs.push(catID);
        }

        var data = {cat_order:arrSortIDs};
        ugCommon.ugItems.ajaxRequestGallery("update_cat_order",data,g_text.updating_categories_order);
    }

    /**
     * init the categories actions
     */
    this.init = function() {

        getCatsHeight();

        //add category
        jQuery("#button_add_category").click(addCategory);

        //remove category:
        jQuery("#button_remove_category").click(function () {
            if (!g_ugAdmin.isButtonEnabled(this))
                return (false);

            if (ugCommon.g_selectedCatID == -1)
                return (false);

            removeCategoryByID(ugCommon.g_selectedCatID);
        });


        //edit category
        jQuery("#button_edit_category").click(function () {
            if (!g_ugAdmin.isButtonEnabled(this))
                return (false);

            if (ugCommon.g_selectedCatID == -1)
                return (false);

            editCategoryByID(ugCommon.g_selectedCatID);
        });

        //list categories actions
        jQuery("#list_cats").delegate("li", "mouseover", function () {
            jQuery(this).addClass("item-hover");

        });

        jQuery("#list_cats").delegate("li", "mouseout", function () {
            jQuery(this).removeClass("item-hover");
        });

        jQuery("#list_cats").delegate("li", "click", function (event) {
            event.stopPropagation();
            if (g_ugAdmin.isRightButtonPressed(event))
                return (true);

            if (g_catClickReady == false)
                return (false);

            if (jQuery(this).hasClass("selected-item"))
                return (false);

            var catID = jQuery(this).data("id");
            selectCategory(catID);
        });


        jQuery("#list_cats").delegate("li", "mousedown", function (event) {

            if (g_ugAdmin.isRightButtonPressed(event))
                return (true);

            g_catClickReady = true;
        });

        // set update title onenter function
        jQuery("#input_cat_title").keyup(function (event) {
            if (event.keyCode == 13)
                updateCategoryTitle();
        });

        initDraggableCategories();
    };



    function initDraggableCategories() {
        var $list_cats = jQuery('#list_cats');
        if(g_settingsObj.multicategories.enabled){

            $list_cats.off('click', 'li .disclose');

            $list_cats.on('click', 'li .disclose', function (e) {
                e.stopPropagation();
                var $li = $(this).closest('li');
                var itemsStates = $list_cats.data("items-states") || {};
                $li.toggleClass('mjs-nestedSortable-collapsed').toggleClass('mjs-nestedSortable-expanded');
                itemsStates[$li.data("id")] = $li.hasClass('mjs-nestedSortable-collapsed');
                $list_cats.data("items-states", itemsStates);
            });

            $list_cats.nestedSortable({
                forcePlaceholderSize: true,
                helper:	'clone',
                listType: "ul",
                items: 'li',
                opacity: .6,
                placeholder: 'placeholder',
                revert: 250,
                tabSize: 25,
                tolerance: 'pointer',
                toleranceElement: '> div',
                maxLevels: g_settingsObj.multicategories.maxDepth,
                cursor: "move",
                isTree: true,
                expandOnHover: 700,
                startCollapsed: true,
                doNotClear: false,
                scroll: false,
                start: function(){
                    g_catClickReady = false;
                },
                stop: function() {
                    var catList = jQuery('#list_cats').nestedSortable('toArray', {startDepthCount: 1});
                    catList = catList.filter(function(cat){
                        return parseInt(cat.item_id) > 0;
                    }).map(function(cat){
                        return {
                            id: cat.item_id,
                            parent_id: cat.parent_id || 0
                        }
                    });
                    var data = {
                        cat_list: catList
                    };
                    ugCommon.ugItems.ajaxRequestGallery("update_nested_cat_order", data, g_text.updating_category);
                    g_catClickReady = true;
                }
            });
        } else {
            //update sortable categories
            $list_cats.sortable({
                axis: 'y',
                start: function (event, ui) {
                    g_catClickReady = false;
                },
                update: function () {
                    updateCatOrder();
                    //save sorting order
                }
            });
        }
    }


    function refreshSortableCategories(){
        if(g_settingsObj.multicategories.enabled) {
            jQuery("#list_cats").nestedSortable("refresh");
            selectCategory(ugCommon.g_selectedCatID);
        }
    }

    function ___________BUTTONS________________(){}


    /**
     * enable category buttons
     */
    function enableCatButtons(){

        //cat butons:
        g_ugAdmin.enableButton("#button_remove_category, #button_edit_category");

        //items buttons:
        g_ugAdmin.enableButton("#button_add_images, #button_add_video");
    }

    /**
     * enable category buttons
     */
    function disableCatButtons(){

        g_ugAdmin.disableButton("#button_remove_category, #button_edit_category");

        //items buttons:
        g_ugAdmin.disableButton("#button_add_images, #button_add_video");
    }
}