/***
 * Generic mtScoring Functions
 * Requires various functions from mt.js: 
 *  - mtGetXmlHttp
 *  - DOM
 *  - mtAttachEvent, if disabling scoring once object is scored as with Dug.
 */

/***
 * mtScore - adds pending class, saves score to database, evaluates response function.
 */
function mtScore(ns, obj_id, score, pending, complete) {
    var el = document.getElementById(ns + obj_id);
    if( !el ) return false;
    // below will stop re-rating
    // if (DOM.hasClassName(el, pending)) return false;
    // if (DOM.hasClassName(el, complete)) return false;
    var xh = mtGetXmlHttp();
    if (!xh) return false;
    DOM.addClassName( el, pending );
    var url = mt.blog.comments.script + '?__mode=' + ns + '_score&static=1&entry_id=' + obj_id + '&score=' + score;
    xh.open('POST', url, true);
    xh.onreadystatechange = function() {
        if ( xh.readyState == 4 ) {
            if ( xh.status && ( xh.status != 200 ) ) {
                // error - ignore
            } else {
                eval(xh.responseText);
            }
        }
    };
    xh.send(null);
}

/***
 * mtScoreResponse - after score saved: removes pending class, adds complete class.
 */
function mtScoreResponse(ns, obj_id, score, pending, complete) {
    var el = document.getElementById(ns + obj_id);
    if (!el)
        return false;
    el.innerHTML = score;
    DOM.removeClassName( el, pending );
    DOM.addClassName( el, complete );
}

/***
 * mtScoreOut - triggered on onmouseout event: calls mtScoreUpdate function.
 */
function mtScoreOut(ns, obj_id, callbackf) {
    var obj = document.getElementById(ns + obj_id);
    if( !obj )
        return false;
    var score = obj.innerHTML;
    callbackf.call(this, obj_id, score, false);
}

/***
 * mtScoreUpdate - triggered on onmouseover event and by mtScoreOut function: updates images.
 */
function mtScoreUpdate(ns, obj_id, score, size, hover, on_img, off_img, zero_img, plugin_name)
{
    var i, image, image_file;
    for( i = 0; i <= size; i++ )
    {
        image = document.getElementById(ns + obj_id + '-' + i);
        if( !image )
            return false;
        if( i > 0 )
            if( i <= score )
                image_file = on_img;
            else
                image_file = off_img;
        else
            image_file = zero_img;
        if( hover )
            if( ( image_file == on_img ) || ( ( image_file == zero_img ) && ( score == 0 ) ) )
                image_file = image_file + '-hover';
        if( image_file != '' )
            image.src = staticWebPath + 'plugins/Score/' + plugin_name +'/' + image_file + '.gif';
    }
    return true;
}
