// Namespace
var iOffer = iOffer ? iOffer : {}

iOffer.Helpdesk = iOffer.Helpdesk ? iOffer.Helpdesk : {}

iOffer.Helpdesk.EntryExpander = Class.create({

    initialize : function(id) {
        this.id      = id;
        this.link    = $('link_entry_' + id);
        this.desc    = $('desc_entry_' + id);
        this.spinner = $('spinner_entry_' + id);
        this.icon    = $('icon_entry_' + id);
        this.loaded  = false;
        this.sending = false;
        this.register_events();
    },

    register_events : function() {
        this.link.observe('click', this.link_on_click.bind(this));
    },

    link_on_click : function() {
        if (this.loaded) {
            this.toggle_display();
        } else if (!this.sending) {
            this.send_request();
        }
    },

    send_request : function() {
        this.spinner.show();
        this.sending = true;
        new Ajax.Request('/entries/show', {
            method : 'get',
            parameters : {
                id : this.id,
                use_cache: false
            },
            onSuccess  : this.onSuccess.bind(this),
            onComplete : this.onComplete.bind(this),
            onFailure  : this.onFailure.bind(this)
        });
    },

    onSuccess : function(request) {
        var entry = request.responseText.evalJSON();
        this.desc.update( entry.posts.first().body_html );
        this.loaded = true;
    },

    onComplete : function(request) {
        this.sending = false;
        this.spinner.hide();
        this.toggle_display();
        this.desc.insert( this.create_close_link() );
    },

    onFailure : function(request) {
        this.desc.update( this.create_error_message() );
    },

    create_close_link : function() {
        var link = Element('span', {
            className : 'faux-link float-right'
        }).update('Close');
        
        link.observe('click', this.toggle_display.bind(this));

        var link_container = new Element('div', {
            className : 'clearfix'
        }).update(link);
        
        return link_container;
    },

    create_error_message : function() {
        var try_again = Element('span', {
            className : 'faux-link'
        }).update('Try Again');
        try_again.observe('click', function() {
            this.toggle_display();
            this.send_request();
        }.bind(this));

        var div = new Element('div', {
            className : 'error'
        })
        div.update('There was an error retrieving details for this item.&nbsp;');
        div.insert(try_again);
       
        return div
    },

    toggle_display : function() {
        Effect.toggle(this.desc, 'blind', {
            duration: 0.15,
            queue: {
                position: 'end',
                scope: 'helpdesk_' + this.id,
                limit: 2
            },
            afterFinish : this.toggle_icon.bind(this)
        });
    },

    toggle_icon : function() {
        if ( iOffer.Dom.is_hidden(this.desc) ) {
            this.icon.removeClassName('expanded');
            this.icon.addClassName('collapsed');
        } else {
            this.icon.removeClassName('collapsed');
            this.icon.addClassName('expanded');
        }
    }
});