/*
 *
 * Star rating functions
 * GCE-2.6.9
 *
 */

var submitStarRating = function( articleId, userId, rating, element ) {
    try {
        var meta = {
            userId:userId,
            articleId:articleId,
            sectionId:null,
            publicationId:null
        };
        QualificationPluginAjax.submitStarRating( meta, rating,
        {
            callback : function(rating) {
                submitStarRatingCallBack( rating, articleId,element );
            },
            exceptionHandler : submitStarRatingException
        }
        );
    } catch( ex ) {
        // JavaScript exception
        alert('An error has occurred: '+ex.message);
    }
    return false;
}

var submitStarRatingException = function( errorString, exception ) {
    if (errorString == 'user cannot rate his/her own article')
    {
        alert("You may not recommend your own article.")
    }
    else
    {
        alert("You have already recommended this article.");
    }

//alert(errorString + "::" + exception);
}

var submitStarRatingCallBack = function( rating, articleId,element ) {
    if ( rating != null ) {
        // $('#rate-'+ rating.articleId).html( "Rated: " + rating.totalAverage);
        //element.setAttribute("class", "recommended");
        //element.innerHTML = "Recommended";
        document.getElementById('recommendLinkVertical').innerHTML = 'Recommended';
        document.getElementById('recommendLinkVerticalIcon').className = 'recommended';
        document.getElementById('recommendLinkHorizontalIcon').className = 'recommended';
    }
}

/*
 *
 * Favorite functions
 * GCE-2.6.9
 *
 */

var submitFavorite = function( articleId, userId, element ) {
    try {
        var meta = {
            userId:userId,
            articleId:articleId,
            sectionId:null,
            publicationId:null
        };
        QualificationPluginAjax.submitFavorite( meta ,
        {
            callback : function( rating ) {
                submitFavoriteCallBack( rating, element );
            },
            exceptionHandler : submitFavoriteException
        }
        );
    } catch( ex ) {
        // JavaScript exception
        alert('An error has occurred: '+ex.message);
    }
    return false;
}

var submitFavoriteCallBack = function( success, element ) {
    if ( success != null ) {
        if( success == true ) {
    //element.setAttribute("class", "bookmarked");
    }
    }
}

var submitFavoriteException = function( errorString, exception ) {
    alert("You have already bookmarked this article.");
}

/*
 *
 * Flagging functions
 * GCE-2.6.9
 *
 */

var submitFlagging = function( articleId, userId, element ) {
    try {
        var meta = {
            userId:userId,
            articleId:articleId,
            sectionId:null,
            publicationId:null
        };
        QualificationPluginAjax.submitFlagging( meta ,
        {
            callback : function() {
                submitFlaggingCallBack(element );
            },
            exceptionHandler : submitFlaggingException
        }
        );
    } catch( ex ) {
        // JavaScript exception
        alert('An error has occurred: '+ex.message);
    }
    return false;
}

var submitFlaggingCallBack = function(element) {
    element.innerHTML = "Reported";
    element.title = 'You have already reported this comment as abusive';
    element.parentNode.className = 'reportDisabled';
}

var submitFlaggingException = function( errorString, exception ) {
    //alert( errorString + "\n" + exception.javaClassName );
    alert("You have already reported this content as abusive.");
}

