var PopupList = function(selectors, classes) {
    var that = this;
    
    that._selectors = selectors;
    that._list    = $(that._selectors.list);
    that._classes = classes;
    
    that._list.click(
        function(e) {
            e.stopPropagation();
        }
    )

    $(that._selectors.elements)
        .click(
            function (e) {
                var element = $(this);
                var action = element.hasClass(that._classes.opened);
                $(that._selectors.elements).removeClass(that._classes.opened);
                $(that._selectors.elements).addClass(that._classes.closed);
                element.removeClass(that._classes.closed);
                element.removeClass(that._classes.opened);
                element.addClass(
                    action
                    ? that._classes.closed
                    : that._classes.opened
                );
                
                $(that._selectors.sub).addClass(that._classes.hidden);
                if(action) {
                    element.parent()
                        .find(that._selectors.sub)
                        .addClass(that._classes.hidden);
                } else {
                    element.parent()
                        .find(that._selectors.sub)
                        .removeClass(that._classes.hidden);
                }
                
                // For IE
                $('.' + classes.cornerBL).attr('class', that._classes.cornerBL);
                $('.' + classes.cornerBR).css('bottom', 0);
                
                e.stopPropagation();
            }
        );
        
    that.bind = function(e) {
        var position = $(this).offset();
        that._list
            .css('top',  position.top)
            .css('left', position.left - 10);
            
        that._list.removeClass(that._classes.hidden);
        
        e.stopPropagation();
    }
    
    that.unBind = function() {
        that._list.addClass(that._classes.hidden);
    }
    
    $(that._selectors.link)
        .click(that.bind)
        .attr('href', 'javascript:void(null)');
        
    $(document)
        .click(function() {
            setTimeout(function() {
                that.unBind()
            },
            1000);
        })
        .keyup(
            function(e) {
                if(e.which == 27) {
                    that.unBind();
                }
            }
        )

}
