/*
*	Flagger
*	Provides AJAX-based flagging functionality
*	
*	Requires Trapeze jQuery distribution
*	
*	Taylan Pince (tpince at trapeze dot com) - March 20, 2009
*/

$.namespace("trapeze.Flagger");

trapeze.Flagger = $.Class.extend({
    
    url : "",
    data : null,
    link : null,
    box : null,
    
    flag_template : '<a href="javascript:void(0);" title="%(flag_copy)" class="flag">%(flag_copy)</a>',
    message_template : '<div class="message-box"><p>%(confirm_copy)</p></div>',
    link_template : '<a href="javascript:void(0);">%(link_copy)</a>',
    standby_template : '<p class="center">%(standby_copy)</p>',
    done_template : '<p>%(done_copy)</p>',
    
    flag_copy : 'Flag This Item',
    flagged_copy : 'You have flagged this item',
    confirm_copy : 'Would you like to report this item to the moderator for review?',
    report_copy : 'Report',
    cancel_copy : 'Cancel',
    standby_copy : 'Please wait...',
    close_copy : 'Close',
    done_copy : 'Thank you, this item has been flagged for review.',
    
    confirm_flag : function() {
        var pos = this.link.offset();
        
        this.box = $(trapeze.render_template(this.message_template, {
            media_path : trapeze.media_path,
            confirm_copy : this.confirm_copy
        })).appendTo("body").css({
            top : pos.top + "px",
            left : (pos.left + 30) + "px"
        }).fadeIn();
        
        $(trapeze.render_template(this.link_template, {
            link_copy : this.report_copy
        })).appendTo(this.box).click(this.flag_item.bind(this));
        
        $(trapeze.render_template(this.link_template, {
            link_copy : this.cancel_copy
        })).appendTo(this.box).click(this.close_box.bind(this));
    },
    
    close_box : function() {
        this.box.fadeOut("fast", this.remove_box.bind(this));
    },
    
    remove_box : function() {
        this.box.remove();
    },
    
    flag_item : function() {
        this.box.html(trapeze.render_template(this.standby_template, {
            standby_copy : this.standby_copy
        }));
        
        $.ajax({
            url : this.url,
            type : "POST",
            processData : false,
            data : this.data,
            //dataType : "json",
            //contentType : "application/json",
            success : this.flagged_item.bind(this)
        });
    },
    
    flagged_item : function() {
        this.box.html(trapeze.render_template(this.done_template, {
            done_copy : this.done_copy
        }));
        
        $(trapeze.render_template(this.link_template, {
            link_copy : this.close_copy
        })).appendTo(this.box).click(this.close_box.bind(this));
        
        this.link.attr({
            "class" : "flagged",
            "title" : this.flagged_copy
        }).html("You have flagged this item").unbind("click");
        
        setTimeout(this.close_box.bind(this), 4000);
    },
    
    init : function(selector, options) {
        this.selector = selector;
        this.url = $(this.selector).attr("action");
        this.data = $(this.selector).serialize();
        
        if (options) {
            for (opt in options) {
                this[opt] = options[opt];
            }
        }
        
        this.link = $(trapeze.render_template(this.flag_template, {
            flag_copy : this.flag_copy
        })).insertBefore(this.selector).click(this.confirm_flag.bind(this));
        
        $(this.selector).remove();
    }
    
});
