var videoRating = 0;
var videoCRating = 0;
var isRated = true;

integrio.js.include("integrio.majax");

function initRating(clickFunction) {
    var img1 = new Image();
    var img2 = new Image();
    var img3 = new Image();
    img1.src = "/templates/fun/images/star_fill.png";
    img2.src = "/templates/fun/images/star_fill05.png";
    img3.src = "/templates/fun/images/star_empty.png";
    var table = document.getElementById("rateTable");
    var tr = table.rows.item(0);
    var tds = tr.cells;
    for (var i = 0; i < tds.length; i++) {
        var td = tds.item(i);
        if (td.className == "star") {
            td.setAttribute("number", i);
            td.style.cursor = "pointer";
            td.onmouseover = function() {
                if (!isRated) {
                    overRating(parseInt(this.getAttribute("number")), 1);
                }
            }
            td.onmouseout = function() {
                if (!isRated) {
                    overRating(videoRating, videoCRating);
                }
            }
            td.onclick = clickFunction!=null ? clickFunction : video_rate_click;
            td.getElementsByTagName("IMG")[0].alt = i;
        }
    }
    videoCRating = parseInt(table.getAttribute("v_c"));
    videoRating = parseInt(table.getAttribute("v_r"));
}

function overRating(v_r, v_c) {
    var table = document.getElementById("rateTable");
    var tds = table.rows.item(0).cells;
    var r = v_r / v_c;
    var i = 1;
    while (r >= 0.7) {
        var td = tds.item(i);
        td.getElementsByTagName("IMG")[0].src = "/templates/fun/images/star_fill.png";
        r--;
        i++;
    }
    if (r > 0.3 && r < 0.7) {
        var td = tds.item(i);
        td.getElementsByTagName("IMG")[0].src = "/templates/fun/images/star_fill05.png";
        i++;
    }
    while (i <= 5) {
        var td = tds.item(i);
        td.getElementsByTagName("IMG")[0].src = "/templates/fun/images/star_empty.png";
        i++;
    }
}

function video_rate_error() {

}

function video_rate_click() {
    if (!isRated) {
        integrio.MAJAX.processAjax("mobus_fun_video_vote", "/modules/fun/ajax/action.jsp", "video_vote", {vid:videoId,r:parseInt(this.getAttribute("number"))}, null);
        isRated = true;
        videoRating += parseInt(this.getAttribute("number"));
        videoCRating++;
        overRating(videoRating, videoCRating);
    }
}

function photo_rate_click() {
    if (!isRated) {
        integrio.MAJAX.processAjax("mobus_fun_photo_vote", "/modules/fun/ajax/action.jsp", "photo_vote", {vid:getPhoto(currentPhoto).id,r:parseInt(this.getAttribute("number"))}, null);
        isRated = true;
        videoRating += parseInt(this.getAttribute("number"));
        videoCRating++;
        var p = getPhoto(currentPhoto);
        p.rating+= parseInt(this.getAttribute("number"));
        p.isRated = true;
        p.countVoting++;
        overRating(p.rating, p.countVoting);
        redrawSmallStars(p);
        if (photoSort!="pos"){
            sortByRating();
        }
    }
}
