    var $j = jQuery.noConflict();
        
    // Global, set this to "." to use classes    
    var thumbSelectorPrefix = "#";    
    function TUpdateThumbs(mmmid, userid, ip, thumbsUp,idpostfix, infoid) {

        var hasCookies = TCookiesEnabled();
        
        if(THasVoted(mmmid) == true || hasCookies == false)
        {
            var newtext = "Du liker denne låta. Bra!"
            if(hasCookies == false)
            {
                newtext = "'Cookies' må skrus på for at du skal kunne gjøre dette.";
            }
            
            TNoVoteAndFadeOut(newtext, mmmid, infoid);            
            return;
        }
         var userContext = new Array();
         userContext[0] = mmmid;
         userContext[1] = idpostfix;
         userContext[2] = infoid;
         
         Nrk.Untouched.WebService.TrackReview.UpdateThumbsUpDown(mmmid, thumbsUp, userid, ip, TOnThumbsSucceeded, null, userContext);
    }

    function TUpdateArtistThumb(subjectid, userid, ip, idpostfix, infoid) {
        var hasCookies = TCookiesEnabled();

        if (userid < 1 && (THasVotedOnArtist(subjectid) == true || hasCookies == false)) {
            var newtext = "Du liker denne artisten. Bra!"            
            if (hasCookies == false) {
                newtext = "'Cookies' må skrus på for at du skal kunne stemme.";
            }

            TNoVoteAndFadeOut(newtext, subjectid, infoid);
            return;
        }
        var userContext = new Array();
        userContext[0] = subjectid;
        userContext[1] = idpostfix;
        userContext[2] = infoid;
        userContext[3] = userid;

        Nrk.Untouched.WebService.SubjectService.UpdateThumbsUp(subjectid, userid, ip, TOnArtistThumbsSucceeded, null, userContext);
    }

    function TOnArtistThumbsSucceeded(result, userContext) {
        if (result == null) {
            return;
        }
        var subjectid = userContext[0];
        var idpostfix = userContext[1];
        var infoid = userContext[2];
        var userid = userContext[3];

        var up = result[0];
        var text = 'Du liker denne artisten.'
        if(userid > 0)
        {
            text = 'Du er en fan!';
            if(up > 1)
            {
                text = text + ' Du er også fan av ';
                if(up > 2)
                {
                    text = text + (up-1) + ' andre artister.';
                }
                else
                {
                    text = text + 'en annen artist.';
                }                
            }            
        }else {
            if(up > 1)
            {
                text = text + ' Det gjør også ';
                if(up > 2)
                {
                    text = text + (up-1) + ' andre';
                }
                else
                {
                    text = text + 'en annen person.';
                }
            }
        
        }

        TVoteRegisteredAndFadeOut(text, subjectid, infoid);

        TStoreArtistVote(subjectid); // mmmid

        var down = '';
        TSetThumb(subjectid, idpostfix, up, down);
    }
    
    function TOnThumbsSucceeded(result, userContext) {   
       if(result == null){
        return;
       }
       var mmmid = userContext[0];
       var idpostfix = userContext[1];          
       var infoid = userContext[2];          
                 
       TVoteRegisteredAndFadeOut('Du liker denne låta. Bra!', mmmid, infoid);
       
       TStoreVote(mmmid); // mmmid
       
       var up = result[0];
       var down = result[1];
            
       TSetThumb(mmmid, idpostfix, up, down); 
    }

   function TNoVoteAndFadeOut(newtext, mmmid, infoid) {
        var sel = thumbSelectorPrefix+infoid;
       jQuery(sel).each(function() {jQuery(this).text(newtext).css("color", "red").show().fadeOut(3000);});         
   }

   function TVoteRegisteredAndFadeOut(newtext, mmmid, infoid)
   {
       jQuery(thumbSelectorPrefix+infoid).each(function() {jQuery(this).text(newtext).css("color", "green").show().fadeOut(3000);});        
   }
   
   function TSetThumb(mmmid, idpostfix, up, down)
   {        
       var idup = thumbSelectorPrefix + "totalthumbsup"+idpostfix;
       var iddown = thumbSelectorPrefix + "totalthumbsdown"+idpostfix;
       jQuery(idup).each( function() {jQuery(this).html(up);});
       jQuery(iddown).each( function() { jQuery(this).html(down);});
       
    }
             
    function TStoreVote(mmmid)
    {    
        var votestring = TGetCookie("ThumbsUpDownCookie");
        if(votestring.length > 0)
            votestring = votestring + "&";
        
        votestring = votestring + mmmid;
        
        TSetCookie("ThumbsUpDownCookie", votestring, 1);
    
    }
    function TStoreArtistVote(subjectid) {
        var votestring = TGetCookie("ArtistThumbsUpCookie");
        if (votestring.length > 0)
            votestring = votestring + "&";

        votestring = votestring + subjectid;

        TSetCookie("ArtistThumbsUpCookie", votestring, 1);

    }

    // hasCookies determines if there are support for cookies
    function TCookiesEnabled()
    {
        var cookieName = "TestTrackCookie";
        TSetCookie(cookieName, "xyz", 1);
        if(TGetCookie(cookieName)=="xyz")
        {
            TSetCookie(cookieName, "xyz", -2);
            return true;
        }
        else return false;
    }

    // hasVoted() checks if a user has voted for a track. 
    function THasVoted(mmmid)
    {
        var votestring = TGetCookie("ThumbsUpDownCookie");
        
        if(votestring == null || votestring == "")
            return false;
            
        var mmmids = votestring.split("&");    
        
        for (var i=0; i<mmmids.length; i++) 
        {
            if(mmmid == mmmids[i])
                return true;
        }
        return false;
    }

    // hasVoted() checks if a user has voted for an artist. 
    function THasVotedOnArtist(subjectid) {
        var votestring = TGetCookie("ArtistThumbsUpCookie");

        if (votestring == null || votestring == "")
            return false;

        var subjectids = votestring.split("&");

        for (var i = 0; i < subjectids.length; i++) {
            if (subjectid == subjectids[i])
                return true;
        }
        return false;
    }

    function TGetCookie(c_name)
    {
        if (document.cookie.length>0)
        {
            c_start=document.cookie.indexOf(c_name + "=");
            if (c_start!=-1)
            { 
                c_start=c_start + c_name.length+1; 
                c_end=document.cookie.indexOf(";",c_start);
                if (c_end==-1) c_end=document.cookie.length;
                return unescape(document.cookie.substring(c_start,c_end));
            } 
        }
        return "";
    }

    function TSetCookie(c_name,value,expiredays)
    {
        var exdate=new Date();
        exdate.setDate(exdate.getDate()+expiredays);
        document.cookie=c_name+ "=" +escape(value)+((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
    }


