$.namespace("trapeze.Flagger");

trapeze.Flagger = $.Class.extend({
    
    conf         : null,
    url_action   : null,
    form_data    : null,
    $link        : null,
    $flag_bubble : null,
    $flag_form   : null,
    
    tpl_flag     : '<div class="flag-wrapper"><a href="javascript:void(0);" title="%(flag_copy)" class="flag">%(flag_copy)</a></div>',
    tpl_message  : '<div class="message-box %(css_classes)"><p>%(confirm_copy)</p></div>',
    tpl_link     : '<a href="javascript:void(0);">%(link_copy)</a>',
    tpl_standby  : '<p class="center">%(standby_copy)</p>',
    tpl_done     : '<p>%(done_copy)</p>',
    
    flaggedItem : function() {
        this.$flag_bubble.html(trapeze.render_template(this.tpl_done, {
            done_copy : this.conf.copy.done
        }));
        $(trapeze.render_template(this.tpl_link, {
            link_copy : this.conf.copy.close
        })).appendTo(this.$flag_bubble).click(this.closeBubble.bind(this));
        
        this.$link.attr({
            'class' : 'flagged',
            'title' : this.conf.copy.flagged
        }).html(this.conf.copy.flagged).unbind('click');
        
        setTimeout(this.closeBubble.bind(this), 4000);
    },

    flagItem : function() {
        this.$flag_bubble.html(trapeze.render_template(this.tpl_standby, {
            standby_copy : this.conf.copy.standby
        }));

        this.form_data  = this.$flag_form.serialize();

        $.ajax({
            url : this.url_action,
            type : "POST",
            processData : false,
            data : this.form_data,
            success : this.flaggedItem.bind(this)
        });

    },

    removeBubble : function() {
        this.$flag_bubble.remove();
    },

    closeBubble : function() {
        this.$flag_bubble.fadeOut("fast", this.removeBubble.bind(this));
    },

    showBubble : function() {
        var pos = (this.$link).offset();
        this.$flag_bubble = $(trapeze.render_template(this.tpl_message, {
            confirm_copy : this.conf.copy.confirm,
            css_classes : this.conf.css_classes
        })).appendTo("body").css({
            top : (pos.top + this.conf.offset_top) + "px",
            left : (pos.left + this.conf.offset_left) + "px"
        });

        $(this.$flag_form).appendTo(this.$flag_bubble);

        $(trapeze.render_template(this.tpl_link, {
            link_copy : this.conf.copy.report
        })).appendTo(this.$flag_bubble).click(this.flagItem.bind(this));
    
        $(trapeze.render_template(this.tpl_link, {
            link_copy : this.conf.copy.cancel
        })).appendTo(this.$flag_bubble).click(this.closeBubble.bind(this));

        $(this.$flag_bubble).fadeIn();
    },

    createFlagLink : function() {
        this.$link = $(trapeze.render_template(this.tpl_flag, {
            flag_copy : this.conf.copy.flag
        })).click(this.showBubble.bind(this)).insertBefore(this.$flag_form);

        $(this.$flag_form).remove();
    },

    init : function(options) {
        var defaults = {
            copy : {
                flag       : 'Flag This Item',
                flagged    : 'You have flagged this item',
                confirm    : 'Would you like to report this item to the moderator for review?',
                report     : 'Report',
                cancel     : 'Cancel',
                standby    : 'Please wait...',
                close      : 'Close',
                done       : 'Thank you, this item has been flagged for review.'
            },
            css_classes : null,
            selector : null,
            offset_top : 0,
            offset_left : 0
        }
        
        this.conf = $.extend({}, defaults, options);
        this.$flag_form = $(this.conf.selector);
        this.url_action = this.$flag_form.attr("action");
        
        this.createFlagLink();
    }
});

