/**
 * 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 = $(parent).find("a.reportabuselink");
        }
        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.length > 0)?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);
				var isExpert = $(a).hasClass("removecomment");
				if (isExpert) {
					SocialMediaService.submitExpertAbuseReport(
							self.args[i].sitebrand,
							self.args[i].contentType, /* needs to be content type of the comment/sharemyphoto/babynamepoll or whatever */
							self.args[i].contentId,   /* needs to be the contentId of the comment/sharemyphoto/etc not the story/slideshow/etc */
							self.args[i].childId,
							self.args[i].userId,
							function(remoteResult){
								if(remoteResult.statusCode == 0){
									$(a).replaceWith($("<span>").addClass("reportabuselink").html("Comment Removed"));
								}
								else{
									alert(remoteResult.statusMessage);
									$(a).replaceWith($("<span>").addClass("reportabuselink").html("Comment Removed"));
								}
							});
				}
				else {
					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>").addClass("reportabuselink").html("Reported"));
						}
						else {
							alert(remoteResult.statusMessage);
							$(a).replaceWith($("<span>").addClass("reportabuselink").html("Reported"));
						}
					});
				}
            });

            link.replaceWith(newLink);
        }
    }

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

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


