new Namespace("eu.mckinley.products.selection");
eu.mckinley.products.selection = Class.create({

    ajaxurl : "/?controller=product&action=ajax",
    mainsections : new Array(),
    sections : new Array(),
    actives : new Array(),
    functions : new Array(),
    users : new Array(),

    initialize : function (elems) {

        this.submitButton();

        for (var i = 0; i < elems.length; i++) {

            var mysection = new eu.mckinley.products.selection.section(elems[i]);

            this.sections[i] = mysection;

            switch (mysection.getType()) {

                case "section":
                    this.mainsections[mysection.value] = mysection.status();
                    break;
                case "main":
                    this.actives[mysection.value] = mysection.status();
                    break;
                case "function":
                    this.functions[mysection.value] = mysection.status();
                    break;
                case "user":
                    this.users[mysection.value] = mysection.status();
                    break;
                default:
                    break;
            }
        }

    },
    /**
     * Toggle Main Category
     */
    toogleMainCategory : function (category, subids) {

        var maincategory = new eu.mckinley.products.selection.section($('section_' + category));
        var i = 0;
        if (maincategory.status() == true) {
            maincategory.disable();
            this.mainsections[category] = false;
            if (subids.length > 0) {
                for (i; i < subids.length; i ++) {
                    this.disableCategory(subids[i]);
                }
            }
        }
        else {
            maincategory.enable();
            this.mainsections[category] = true;
            if (subids.length > 0) {
                for (i; i < subids.length; i ++) {
                    this.enableCategory(subids[i]);
                }
            }
        }
        this.submitAjax();
    },
    /**
     * Toggle Single Category
     */
    toogleCategory : function (id, maincategory, allcategory) {
        if (this.statusCategory(id)) {
            this.disableCategory(id);
            new eu.mckinley.products.selection.section($('section_' + maincategory)).disable();
        }
        else {
            this.enableCategory(id);
            var enablemain = true;
            for (var i = 0; i < allcategory.length; i ++) {
                if (!this.statusCategory(allcategory[i])) {
                    enablemain = false;
                }
            }
            if (enablemain) {
                this.mainsections[maincategory] = true;
                new eu.mckinley.products.selection.section($('section_' + maincategory)).enable();
            }
        }
        this.submitAjax();
    },
    /**
     * Toggle Function
     */
    toogleFunction : function (name) {
        if (this.statusFunction(name)) {
            this.disableFunction(name);
        }
        else {
            this.enableFunction(name);
        }
        this.submitAjax();
    },
    /**
     * Toggle User
     */
    toogleUser : function (name) {
        if (this.statusUser(name)) {
            this.disableUser(name);
        }
        else {
            this.enableUser(name);
        }
        this.submitAjax();
    },
    /**
     * Private Functions Category
     */
    enableCategory : function (id) {
        this.actives[id] = true;
        new eu.mckinley.products.selection.section($('section_main_' + id)).enable();
    },
    disableCategory : function (id) {
        this.actives[id] = false;
        new eu.mckinley.products.selection.section($('section_main_' + id)).disable();
    },
    statusCategory : function (id) {
        return this.actives[id];
    },
    /**
     * Private Functions SportFunctions
     */
    enableFunction : function (name) {
        this.functions[name] = true;
        new eu.mckinley.products.selection.section($('section_function_' + name)).enable();
    },
    disableFunction : function (name) {
        this.functions[name] = false;
        new eu.mckinley.products.selection.section($('section_function_' + name)).disable();
    },
    statusFunction : function (name) {
        return this.functions[name];
    },
    /**
     * Private Functions User
     */
    enableUser : function (name) {
        this.users[name] = true;
        new eu.mckinley.products.selection.section($('section_user_' + name)).enable();
    },
    disableUser : function (name) {
        this.users[name] = false;
        new eu.mckinley.products.selection.section($('section_user_' + name)).disable();
    },
    statusUser : function (name) {
        return this.users[name];
    },
    /**
     * Submit Functions
     */
    setSubmitText : function (text) {
        $('productsubmit').value = text;
    },
    showSubmit : function () {
        $('productsubmit').show();
    },
    hideSubmit : function () {
        $('productsubmit').hide();
    },
    submitButton : function () {
        // Show Submit Button on zero Result Count
        return;
        if ($('searchproductcounter').value > 0) {
            this.showSubmit();
        }
        else {
            this.hideSubmit();
        }
    },
    submitAjax : function () {

        new Ajax.Request(this.ajaxurl, {
            method: 'get',
            parameters: $('menuproductselection').serialize(),
            onComplete: function (resp) {
                var response = resp.responseText;

                var data = eval('(' + response + ')');

                this.setSubmitText(data.text);

                $('searchproductcounter').value = data.counter;

                this.submitButton();


            }.bind(this),
            onFailure: function(resp) {
            },
            onException: function(resp, exception) {
            }
        })
    },
    submitForm : function () {
        if ($('searchproductcounter').value > 0) {
            return true;
        }
        else {
            return false;
        }
    },
    userMouseout : function (element) {
        element.removeClassName("click")
    }
});

eu.mckinley.products.selection.section = Class.create({

    value : false,

    initialize : function (el) {
        this.el = el;
        var input = el.childElements();
        this.input = input [0];
        this.value = this.input.value;

        if (this.input.checked) {
            this.enable();
        }
    },
    getType : function () {
        var temp = this.el.id.split('_');
        switch (temp.length) {
            case 2:
                return (temp[0]);

            case 3:
                return (temp[1]);

            default:
                break;
        }
    },
    enable : function () {
        this.el.childElements()[0].checked = true;
        this.el.addClassName("active");
        return true;
    },
    disable : function () {
        this.el.childElements()[0].checked = false;
        this.el.removeClassName("active");
        this.el.addClassName("click");
        return false;
    },
    status : function () {
        return this.el.childElements()[0].checked;
    },
    toogle : function () {
        if (this.status) {
            this.disable();
        }
        else {
            this.enable();
        }
    }
});

