/**
 * 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.getProperty("sitebrand"),
                contentType:link.getProperty("contenttype"),
                contentId:link.getProperty("contentid"),
                childId:link.getProperty("childid"),
                userId:(sessionuserid != null)?sessionuserid.getText():link.getProperty("userid")
            };

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

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

            /* attach DWR method as a handler to rating link's click event */
            var newLink= link.clone();
            newLink.addEvent("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(new Element("span",{"class":"reportabuselink"}).setHTML("Reported"));
                    }
                    else{
                        alert(remoteResult.statusMessage);
                        a.replaceWith(new Element("span",{"class":"reportabuselink"}).setHTML("Reported"));
                    }
                });
            });

            link.replaceWith(newLink);
        }
    }

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

window.addEvent('domready', function(){
    mdp.reportAbuse = new mdp.app.ReportAbuse();
});

