
/*

    This is the Thomson Badge Mashlet ported to the latest Mashlet API.

    1) Create the following Service in Wires:
     add a DirectInvoke to the following URL: http://localhost:8080/mashlets/badge/badgeresponse.xml

    2) Save and publish as "SampleBadgeResponse"

    3) Add the following to mashlet.json:
    
    "Sample.Badge": {
                "type": "Sample.Badge",
                "title": "",
                "description": "Sample Inline Mashlet that demonstrates creation of a Badge",
                "properties": {
                },
                "resources": {
                    "js": [
                        { "script": "prototip-min.js", "property":"Prototip" },
                        { "script": "badge.js", "property":"Sample.Badge" }
                    ],
                    "css": ["#{mashletDir}/prototip.css"]

                }
            },

    4) Test using: http://localhost:8080/mashlets/standalone.jsp?mashlet=Sample.Badge
    Note: to do this, you have to restart the Server (as a default mashlet instance gets created on server restart).
    If you do not wish to restart, create a new Mashlet instance from the Type using the Mashlet Maker -> Create from TYpe


 */

Ema.namespace("badge");
badge = Class.create(Ema.Mashlet, {

    render: function() {

        var mashletsUrlContext = this.resolveUrl("#{mashletDir}/"); //getMashletsUrlContext();
        
        this.el.update("<img id='badge_" + this.el.id + "' src='" + mashletsUrlContext + (this.getPreference('size') != "small" ? "rid-idsymbol4" : "badge_sm") + ".gif' />");
        var el = $(this.el);
        var conn = this.getConnection();
        this.dataset = new Presto.Dataset({
            dataProxy: new Presto.ServiceDataProxy(conn)
        });


        this.dataset.load({
            sid: 'TSMashGetProfile',
            oid: 'get',
            params:{
                rid:this.getPreference('rid'),
                mode:'public',
                logmode:'badge',
                url:document.location.toString(),
                max:3
            }

        });
        this.dataset.addListener("load", function(ds) {

            var dsResponse = ds.getData().response;
            var data = dsResponse.ProfileResponse.profile;
            var privacy = dsResponse.ProfileResponse.DataPrivacy;
            var publications = [];
//            if(data.publications && data.publications.publication) {
//                publications = data.publications.publication;
//            }

            if(data.publications) {
                publications = data.publications;
            }

            if (privacy.publicProfile != "false")
            {
                this.tooltipContent = ["<div id='tooltip_" + this.el.id + "' style=''>",
                        "<table width=350px border=0   Cellpadding=0 cellspacing=0 style='font:12px Arial;'>",
                        "<tr>",
                        "<td width=350 height=20 style='background-image:url(" + mashletsUrlContext + "callout_logo.gif);background-repeat:no-repeat;'></td>",
                        "</tr>",
                        "<tr>",
                        "<td style='padding:15px;padding-top:5px;'>",
                        "<div style='font-weight:bold;' id='researcher'>",
                        data.firstName + " " + data.lastName + "<br>RID: " + data.researcherId + (data.institution ? "<br>" + data.institution : ""),

                        "</div>",
                        "</td>",
                        "</tr>"
                        ]
                if (privacy.publicationsVisible != "false")
                {
                    if (publications && publications.length > 0)
                    {
                        //if (publications.constructor == Object)
                            //publications = [publications];                        
                        //var pubs = publications; //[0].publication;
                        this.tooltipContent.push("<tr><td style='padding:5px;padding-left:15px;padding-top:0px;'><I>Selected Publication List:</td></tr>");
                        for (var i = 0; i < publications.length; i++)
                        {

                            this.tooltipContent.push(
                                    "<tr>",
                                    "<td style='padding:5px;padding-left:15px;padding-top:0px;font-weight:bold' id='publications'>",
                                    (i + 1) + ". " + publications[i].itemTitle + "",
                                    "</td>",
                                    "</tr>");
                        }
                    }
                }
                this.tooltipContent.push(
                        "<tr>",
                        "<td align=left style='padding:10px;padding-right:25px;'>",
                        "<a target='reasearcherid' href='http://www.researcherid.com" + "/rid/" + data.researcherId + "' style='text-decoration:none;'>Go to ResearcherID.com to view more information</a>",
                        "</td>",
                        "</tr>",
                        "</table>",
                        "</div>"
                        );
            }
            else
            {
                this.tooltipContent = ["<div id='tooltip_" + this.el.id + "' >",
                        "<table width=300px border=0   Cellpadding=0 cellspacing=0 style='font:12px Arial;'>",
                        "<tr>",
                        "<td width=350 height=20 style='background-image:url(" + mashletsUrlContext + "callout_logo.gif);background-repeat:no-repeat;'></td>",
                        "</tr>",
                        "<tr>",
                        "<td style='padding:15px;padding-top:5px;'>",
                        "<div style='font-weight:bold;' id='researcher'>",
                        "The Researcher has made their information on ResearcherID private.",
                        "</div>",
                        "</td>",
                        "</tr>",
                        "<tr>",
                        "<td align=left style='padding:10px;padding-right:25px;'>",
                        "Please visit <a target='reasearcherid' href='http://www.researcherid.com" + "/rid/" + data.researcherId + "' style='text-decoration:none;'>ResearcherID.com</a> to find other researchers",
                        "</td>",
                        "</tr>",
                        "</table>",
                        "</div>"
                        ]

            }


            var callout = [
                    "<img id='badge_" + this.el.id + "' src='" + mashletsUrlContext + (this.getPreference('size') != "small" ? "rid-idsymbol4" : "badge_sm") + ".gif' />"
                    ].join("");
            this.render = function() {
            };
            this.el.update(callout);
            var vThis = this;
            new Tip('badge_' + this.el.id, this.tooltipContent.join(""), { hideOn: { element: 'tip', event: 'mouseout'}, hideAfter: 1   });
        }, this);

    },

    onRefresh: function() {
        this.render();
    }

});