

// Common vars needed
var ratingSubmitQueue = new Array();
var ratingSubmitQueueLength = 0;
var ajaxObj;
var d=document;
var curRatingIds = new Array(); // will be populated as we build pages w/ widgets
var curRatings = new Array(); // will be populated as we build pages w/ widgets
var origRatings = new Array();
var movieDbIds = new Array(); // also must be populated by pages
var photoDbIds = new Array(); // also must be populated by pages
var videoDbIds = new Array(); // also must be populated by pages
var quizDbIds = new Array(); // also must be populated by pages
var rateSrc = new Array(); // also must be populated by pages
var curAttrRatings = new Array();
var origAttrRatings = new Array();

/** Handle rating actions **/
function rateScoreToImage(scoreID, type) {
    useNew = false;
    /*if (type == 'qck' || type == 'wiz' || type == 'mct') {
        useNew = true;
    }
    */
    if (scoreID == 0) {
        return "/public/static/img/0stars_r." + (useNew ? "new." : "") + "gif";
    }
    if (scoreID == 2) {
        return "/public/static/img/1stars_r." + (useNew ? "new." : "") + "gif";
    }
    if (scoreID == 4) {
        return "/public/static/img/2stars_r." + (useNew ? "new." : "") + "gif";
    }
    if (scoreID == 6) {
        return "/public/static/img/3stars_r." + (useNew ? "new." : "") + "gif";
    }
    if (scoreID == 8) {
        return "/public/static/img/4stars_r." + (useNew ? "new." : "") + "gif";
    }
    if (scoreID == 10) {
        return "/public/static/img/5stars_r." + (useNew ? "new." : "") + "gif";
    }
    if (scoreID == 1) {
        return "/public/static/img/halfstars_r." + (useNew ? "new." : "") + "gif";
    }
    if (scoreID == 3) {
        return "/public/static/img/1_halfstars_r." + (useNew ? "new." : "") + "gif";
    }
    if (scoreID == 5) {
        return "/public/static/img/2_halfstars_r." + (useNew ? "new." : "") + "gif";
    }
    if (scoreID == 7) {
        return "/public/static/img/3_halfstars_r." + (useNew ? "new." : "") + "gif";
    }
    if (scoreID == 9) {
        return "/public/static/img/4_halfstars_r." + (useNew ? "new." : "") + "gif";
    }
    return "/public/static/img/0stars_r." + (useNew ? "new." : "") + "gif";
}

function flashSaveButton(){
    if (getEl("widgetSaveBtn")) {
        document.images["widgetSaveBtn"].src="/public/static/img/bt_save_ani.gif";
    }
}

/** Handle Ajax stuff **/
function commonWgt_ajaxSubmit(mvID, type) {
    var params = commonWgt_getRateSubmitParams(mvID, type);
    //alert("params= " +params);
    if (type == 'vi') {
        YAHOO.util.Connect.asyncRequest('POST', '/rating/video', ratingCallback, params);
    } else if (type == 'ph') {
        YAHOO.util.Connect.asyncRequest('POST', '/rating/photo', ratingCallback, params);
    } else if (type == 'mo') {
        YAHOO.util.Connect.asyncRequest('POST', '/rating/movie', ratingCallback, params);
    } else if (type == 'qu') {
        YAHOO.util.Connect.asyncRequest('POST', '/rating/quiz', ratingCallback, params);
    }
}

var ratingSuccess = function(o){
    //alert("rating success");
};

var ratingFailure = function(o){
   alert("Could not save your rating - please try again");
};

var ratingCallback =
{
    success:ratingSuccess,
    failure:ratingFailure,
    argument:['foo','bar']
};



/**
* flat.widget.js
*/
// vars

var fltWgt_openMovie = "";



function fltWgt_rateStarHover(mvID, starNum, type){

    if (getEl("fltWgt_rateStarsBtn"+mvID)) {
        document.images["fltWgt_rateStarsBtn"+mvID].src=rateScoreToImage(starNum, type);
    }

}

function fltWgt_rateStarsReset(mvID, type){

    rating=curRatings[mvID];

    if (getEl("fltWgt_rateStarsBtn"+mvID)) {
        document.images["fltWgt_rateStarsBtn"+mvID].src=rateScoreToImage(rating, type);
    }
}

function fltWgt_rateSet(mvID, rating, type, callback){

    if (curRatings[mvID]==rating && curRatings[mvID] != 0) {
        fltWgt_rateSet(mvID, 0, type);
        if (callback != null) {
            var code = callback + "('" + mvID + "', '0')";
            eval(code);
        }
        return;
    }
    else {
        curRatings[mvID]=rating;

        prepend = "";

        if (rating == 0) {

            if (getEl("fltWgt_rateStarsBtn"+mvID)) {
                document.images["fltWgt_rateStarsBtn"+mvID].src="/public/static/img/0stars_r" + ((type == 'wiz' || type == 'mct') ? ".new" : "") + ".gif";
            }
        }

        curRatings[mvID]=parseInt(rating);

        if (callback != null) {
            var code = callback + "('" + mvID + "', '" + rating + "')";
            eval(code);
        }
    }

    if (document.getElementById('overallRating')) {
        getEl('overallRating').value = parseInt(rating);
    } else {
        fltWgt_rateSave(mvID, type, true);
    }

}


function rateAttrScoreToImage(scoreID, type) {
    useNew = false;
    /*if (type == 'qck' || type == 'wiz' || type == 'mct') {
        useNew = true;
    }
    */
    if ((scoreID >= 0)&&(scoreID <= 5)) {
        return "/public/static/img/bars_" + scoreID + ".gif";
    }
    return "/public/static/img/bars_0.gif";
}


function attrWgt_rateStarsReset(attrRatingIndex, type){

    rating=curAttrRatings[attrRatingIndex];

    if (getEl("attrWgt_rateStarsBtn"+attrRatingIndex)) {
        document.images["attrWgt_rateStarsBtn"+attrRatingIndex].src=rateAttrScoreToImage(rating, type);
    }
}


function attrWgt_rateStarHover(attrRatingIndex, starNum, type){

    if (getEl("attrWgt_rateStarsBtn"+attrRatingIndex)) {
        document.images["attrWgt_rateStarsBtn"+attrRatingIndex].src=rateAttrScoreToImage(starNum, type);
    }
}


function attrWgt_rateSet(attrIndex, rating, type, callback){

    if (curAttrRatings[attrIndex]==rating && curRatings[attrIndex] != 0) {
        attrWgt_rateSet(attrIndex, 0, type);
        return;
    }
    else {
        curAttrRatings[attrIndex]=rating;
        getEl('attrRating' + attrIndex).value = rating;

        prepend = "";

        if (rating == 0) {
            if (getEl("attrWgt_rateStarsBtn"+mvID)) {
                document.images["attrWgt_rateStarsBtn"+mvID].src="/public/static/img/bars_1.gif";
            }
        }

        curAttrRatings[attrIndex]=parseInt(rating);
    }
}


function fltWgt_rateSave(mvID, type, showMessage) {
    commonWgt_rateSave(mvID, type, showMessage);
}

function fltWgt_rateClear(mvID, type){
    //fltWgt_commentSet(mvID,""); // zero out comment
    fltWgt_rateSet(mvID,0,type); // zero out rating
    // set calls save
    //	fltWgt_rateSave(mvID, false); // save it
}


function commonWgt_rateSave(mvID, type, showMessage){

    if (current_user != '') {
        commonWgt_ajaxSubmit(mvID, type);
    } else {
        if (showMessage) {
            msg = "Your ratings will not be saved until you register or sign in!";
            //floatie.show(msg);
            alert("Your ratings will not be saved until you register or sign in!");
        }
    }

    if (getEl("widgetSaveBtn")) {
        document.images["widgetSaveBtn"].src = "/public/static/img/bt_saved.gif";
    }
}

function commonWgt_getRateSubmitParams(mvID, type) {
    // seem to need this to get server responses on duplicate submission

    if ((type=='vi') && videoDbIds && videoDbIds[mvID]) {
        return("videoid=" + videoDbIds[mvID] + "&rating=" + curRatings[mvID]);
    } else if ((type=='ph') && photoDbIds && photoDbIds[mvID]) {
        return("photoid=" + photoDbIds[mvID] + "&rating=" + curRatings[mvID]);
    } else if ((type=='mo') && movieDbIds && movieDbIds[mvID]) {
        return("movieid=" + movieDbIds[mvID] + "&rating=" + curRatings[mvID]);
    } else if ((type=='qu') && quizDbIds && quizDbIds[mvID]) {
        return("quizid=" + quizDbIds[mvID] + "&rating=" + curRatings[mvID]);
    }
}



