/**
 * Recommend
 *
 * @author Boz
 * @classDescription MDP Report Abuse handler.
 **/

mdp.app.ReportAbuse = function(parent){
    /* ---[ CLASS VARIABLES ]--- */
    var self = this;
    var parent = parent;
    this.args = [];

    /* ---[ CONSTRUCTOR ]--- */
    function init(){

        /* initialization code */
        setupEventListeners();

    }

    /* ---[ PUBLIC METHODS ]--- */
    this.reAttachEvents = function(parent){
        setupEventListeners();
    };

    /* ---[ PRIVATE METHODS ]--- */

    /* ---[ EVENT LISTENERS ]--- */
    function setupEventListeners(){

        /* get all report abuse links */
        var reportabuse;
        if(parent != null){
            reportabuse = $("a.reportabuselink",parent);
        }
        else{
            reportabuse = $("a.reportabuselink");
        }

        /* iterate through elements */
        for(var i=0; i<reportabuse.length; i++){

            /* store parameters */
            var link = reportabuse[i];
            var sessionuserid = $("#sessionuserid");
            self.args[i] = {
                sitebrand:link.attr("sitebrand"),
                contentType:link.attr("contenttype"),
                contentId:link.attr("contentid"),
                childId:link.attr("childid"),
                userId:(sessionuserid != null)?sessionuserid.text():link.attr("userid")
            };

            /* assign id's to report abuse links */
            link.attr("id",i);

            /* strip non-standard element attributes */
            link.removeAttr("sitebrand");
            link.removeAttr("contenttype");
            link.removeAttr("contentid");
            link.removeAttr("childid");
            link.removeAttr("userid");

            /* attach DWR method as a handler to rating link's click event */
            var newLink= link.clone();
            newLink.click(function(){
                var a = this;
                var i = parseInt(a.id,10);
                SocialMediaService.submitAbuseReport(self.args[i].sitebrand, self.args[i].contentType, self.args[i].contentId, self.args[i].childId, self.args[i].userId, function(remoteResult){
                    if(remoteResult.statusCode == 0){
                        a.replaceWith($("<span>").attr({"class":"reportabuselink"}).html("Reported"));
                    }
                    else{
                        alert(remoteResult.statusMessage);
                        a.replaceWith($("<span>").attr({"class":"reportabuselink"}).html("Reported"));
                    }
                });
            });

            link.replaceWith(newLink);
        }
    }

    /* ---[ RUN ]--- */
    init();
};

$(document).ready( function(){
    mdp.reportAbuse = new mdp.app.ReportAbuse();
});


