﻿function RatingAgent(settings) {
    var _IdReference = 0;
    var mode = "ProductCategory";
    if (settings.Mode) {
        mode = settings.Mode;
    }

    var currentValue = 3;
    if (settings.CurrentValue) {
        currentValue = settings.CurrentValue;
    }

    function SubmitRating(value) {
        $.ajax({ url: '/services/rating.ashx', dataType: 'json', data: {
            r: value, id: _IdReference, m: mode
        }, success: function(data) {
            var jdata = eval(data);
            if (jdata.IsSuccessfull == "True") {
                if (settings.VoteContainer) {
                    $(settings.VoteContainer).html(jdata.Message);
                }
            }
        }
        });
    }

    function ArrangeList(list, value, delegate) {
        for (var x = 0; x < list.length; x++) {
            var v = delegate(list[x]);
            $(list[x]).removeClass();
            if (Math.round(v) <= Math.round(value)) {
                $(list[x]).addClass(settings.SelectedCss);
            } else {
                $(list[x]).addClass(settings.DefaultCss);
            }
        }
    }

    this.Initialize = function(IdReference) {
        _IdReference = IdReference;
        var list = $("#" + settings.Container).find("li a");
        $(list).each(function(i) {
            var value = $(this).attr("value");
            $(this).addClass(settings.DefaultCss);
            $(this).click(function() {
                currentValue = value;
                SubmitRating(value);
                ArrangeList(list, value, function(item) {
                    return $(item).attr("value");
                });
                return false;
            });
            $(this).hover(function() {
                for (var x = 0; x < list.length; x++) {
                    var v = $(list[x]).attr("value");
                    if (v <= value) {
                        $(list[x]).addClass(settings.HoverCss);
                    }
                }
            });
            $(this).mouseout(function() {
                ArrangeList(list, currentValue, function(item) {
                    return $(item).attr("value");
                });
            });
        });
        ArrangeList(list, currentValue, function(item) {
            return $(item).attr("value");
        });
    }

    this.GetValues = function(idReference, Reference, AssignCallback) {
        var reference = "ProductCategory";
        if (Reference) {
            reference = Reference;
        }
        var list = $("#" + settings.Container).find("li a");
        if (list.length > 0) {
            ArrangeList(list, currentValue, function(item) {
                return $(item).attr("value");
            });
            $(list).each(function(i) {
                $(this).click(function() {
                    return false;
                });
            });
        }
        $.ajax({ url: '/services/rating.ashx', dataType: 'json', data: {
            id: idReference, m: "GetRating", ref: reference
        }, success: function(data) {
            var jdata = eval(data);
            if (jdata.IsSuccessfull == "True") {
                currentValue = jdata.Rating;
                if (list.length > 0) {
                    ArrangeList(list, currentValue, function(item) {
                        return $(item).attr("value");
                    });
                }

                if (AssignCallback) {
                    AssignCallback(currentValue);
                }
                if (settings.VoteContainer) {
                    $(settings.VoteContainer).html(jdata.Message);
                }
            }
        }
        });
    }
}

RatingAgent.SubmitFromRadio = function(setting) {
    var mode = "ProductCategory";
    if (setting.Mode) {
        mode = setting.Mode;
    }
    var selectedRating = $(setting.RadioContainer).find("input:checked").first().val();
    $.ajax({
        url: '/services/rating.ashx',
        dataType: 'json',
        data: { r: selectedRating, id: setting.IdReference, m: mode },
        success: function(data) {
            var jdata = eval(data);
            if (jdata.IsSuccessfull == "True") {
                if (setting.SuccessCallback) {
                    setting.SuccessCallback();
                } else if (setting.MessageContainer) {
                    $(setting.MessageContainer).html(jdata.Message);
                }
            } else if (setting.ErrorCallback) {
                setting.ErrorCallback();
            }
        }
    });
    return false;
}

RatingAgent.ShowStats = function(settings) {
    var reference = "ProductReview";
    if (settings.Reference) {
        reference = settings.Reference;
    }
    var _css = "";
    if (settings.Css) {
        _css = settings.Css;
    }
    $(settings.Container).html('');
    $.ajax({ url: '/services/rating.ashx',
        data: { id: settings.IdReference, m: "ShowStats", ref: reference, css: _css },
        success: function(data) {
            $(settings.Container).html(data);
        }
    });
}

