﻿// RATING
var RATE_TEXT = "Rate this";
var YOUR_RATE = "Your rating";
var RATE_STUB = '#rate-item-';
var RATE_IMG = '#rate-img-';
var RATE_USER_STUB = '#user-rate-item-';
var RATE_LINK = '#rate-a-';
var IMG_ON = 'red';
var IMG_OVER = 'yellow';
var IMG_OFF = 'grey';

function SwithStars(num) {
    $(RATE_STUB + num).toggle();
    $(RATE_USER_STUB + num).toggle();
    return false
}
function RateIt(fId, rId) {
    StarsOut(fId);
    if ($(RATE_USER_STUB + fId).is(':visible')) {
        var inhtml = $(RATE_USER_STUB + fId).html();
        $(RATE_USER_STUB + fId).html("<img src='/images/loading.gif' style='margin-left: 20px'/>");
        doRate(fId, rId, inhtml);
    }
    return false;
}
function doRate(fId, rId, inhtml) {
    $.get("/ajax/rate?fid=" + fId + "&rid=" + rId, {}, function(data, status) { ShowRate(fId, rId, data, inhtml); });
}
function replaceStars(fId, end, src, newSrc) {
    for (var i = 1; i < end; i++) {
        $(RATE_IMG + fId + "-" + i).attr('src', $(RATE_IMG + fId + "-" + i).attr('src').replace(src, newSrc));
    }
}
function StarsOut(fId) {
    replaceStars(fId, 6, IMG_OVER, IMG_OFF);
}
function StarsOver(fId, num) {
    if ($( + fId).html().indexOf(RATE_TEXT) > -1) {
        replaceStars(fId, 6, IMG_OVER, IMG_OFF);
        replaceStars(fId, num + 1, IMG_OFF, IMG_OVER);
    }
}
function ShowRate(fId, rId, wid, inhtml) {
    $(RATE_LINK + fId).html(YOUR_RATE);

    if ($(RATE_USER_STUB + fId).length > 0) $(RATE_USER_STUB + fId).html(inhtml).hide();
    if ($(RATE_STUB + fId).length > 0) $(RATE_STUB + fId).html($(RATE_STUB + fId).html().replace(RATE_TEXT, YOUR_RATE)).show();
    replaceStars(fId, 6, IMG_ON, IMG_OFF);
    replaceStars(fId, rId + 1, IMG_OFF, IMG_ON);
    $('#rate-item-div-' + fId).css('width',wid + 'px');
}
// END RATING
