var AdminArea = {
    init: function(){
        window.addEvent("domready", this.initialize.bind(this));
    },
    initialize: function(){
        this.handleFormErrors();
        this.setEvents();
    },
    setEvents: function(){
        $$("input[maxlength]").addEvent("keyup", this.remainingChars);
    },
    remainingChars: function(ev) {
        var input = ev.target, chars, charsLeft;
        chars = input.getParent("div.casilla")
        if (!$defined(chars)) return;
        chars = chars.getElement("span.contador")
        if (!$defined(chars)) return;
        charsLeft = input.get("maxlength") - input.value.length;
        chars.set("text", "{n} máx.".substitute({n: charsLeft}));
    },
    addImageInput: function() {
        if ($$("#images li").length < 15){
            var input = new Element('input', {'type':'file', 'class':'file', 'name':'image'}), li = new Element('li');
            li.grab(input);
            $("images").grab(li);
        }
    },
    handleFormErrors: function(){
        var errors = $$(".form-errors").setStyle("display","none");
        if (errors.length > 0)
            jQuery.fancybox(errors.pop().innerHTML);
    },
    richEditor: function(element){
        return new punymce.Editor({
            id : element,
            toolbar : 'bold'
            //plugins: 'ForceNL'
        });
    },
    ajaxSearch: function(element, url){
        var element = $(element);
        return new Meio.Autocomplete(element, url, {
            delay: 200,
            minChars: 0,
            cacheLength: 20,
            cacheType: 'shared',
            selectOnTab: true,
            maxVisibleItems: 100,
            filter: {
                type: 'contains',
                path: 'name'
            }
        });
    }
}