var FadeCheck = {
    classes: {
        dropdown : 'check-dropdown',
        submit   : 'check-submit',
        active   : 'activeOne'
    },

    bindedCallbackList: {},

    titleParams: {
        length: 17,
        ignore: [Lang._('Check all'), Lang._('All types'), Lang._('Submit')]
    },

    open: function(div) {
        div.slideDown();
        div.prev('a.' + this.classes.dropdown).css('background-position','right bottom');
        div.addClass(this.classes.active);

        // Check for callbacks
        FadeCheck.call(div,'open');
    },

    close: function(div) {
        div.slideUp();
        div.removeClass(this.classes.active);
        div.prev('a.' + this.classes.dropdown).css('background-position','right center');

        // Check for callbacks
        FadeCheck.call(div,'close');
    },

    init: function() {
        $('a.' + this.classes.dropdown).unbind()
            .bind('click', {
                parent: this
            }, this.onDropDownClick);
            
        $('a.' + this.classes.submit).unbind()
            .bind('click', {
                parent: this
            }, this.onSubmit);

        // When change focus to some input element
        // we have to close all checkbox div
        $('input[type!=checkbox], select').bind('focus', {}, function(){
            $('a.' + FadeCheck.classes.submit).click();
        });

        // Add title on the page startup
        $('a.' + this.classes.dropdown).parent('div').each(function(){
            FadeCheck.title($(this));
        });
    },

    // Build anchor title by the list of selected checkboxes
    title: function(div) {
        title  = '';
        coma   = '';
        prefix = '';

        div.find('input[type=checkbox]:checked').each(function(){
            label = $(this).parent('li').find('label').text();
            // Don`t use labels from Ignore list
            if (_.include(FadeCheck.titleParams.ignore, label)) {
                return ;
            }

            if (title.length < FadeCheck.titleParams.length) {
                if ('' == coma && '' != title) {
                    coma = ', ';
                }

                title += coma + label;
            } else {
                prefix = '...';
            }
        });

        if ('' == title) {
            title = Lang._('Select');
        } else {
            cat   = title.length;
            title = title.substr(0,FadeCheck.titleParams.length);
            if ('' !== prefix || cat != title.length) {
                title += ' ...';
            }
        }

        div.prev('a.' + FadeCheck.classes.dropdown).text(title);
        div.find('a.' + FadeCheck.classes.dropdown).text(title);
    },

    // Bind additional action to some element (div) on few actions
    bind: function(id, action, callback) {
        if (!this.bindedCallbackList[action]
                || !this.bindedCallbackList[action][id]
                || !this.bindedCallbackList[action][id].length) {
            this.bindedCallbackList[action] = {};
            this.bindedCallbackList[action][id] = [ callback ];
        } else {
            this.bindedCallbackList[action][id].push(callback);
        }
    },

    // Execute all callbacks
    call: function(div,action) {
        // Check Div ID
        id = div.attr('id').replace('#','');
        if (!id) {
            return false;
        }

        if (FadeCheck.bindedCallbackList[action] && FadeCheck.bindedCallbackList[action][id]) {
            FadeCheck.bindedCallbackList[action][id][0](div);
        }
    },

    onDropDownClick: function() {
        div = $(this).next('div');

        // Check for callbacks
        FadeCheck.call(div,'dropDownClick');

        // Close div with dropdown
        if (div.hasClass(FadeCheck.classes.active)) {
            FadeCheck.close(div);
            return false;
        }

        // Close other same divs
        $('a.' + FadeCheck.classes.dropdown).next('div').each(function(){
            closeObject = $(this);

            if(closeObject.hasClass(FadeCheck.classes.active)) {
                FadeCheck.close(closeObject);
            }
        });
        
        // Open this one
        FadeCheck.open(div);

        return false;
    },

    onSubmit: function() {
        div = $(this).parent('li').parent('ul').parent('div');
        // Build title for selectbox
        FadeCheck.title(div);

        // Close all div
        FadeCheck.close(div);

        // Check for callbacks
        FadeCheck.call(div,'submit');

        return false;
    }
}

