/*
Author: Addam M. Driver
Date: 10/31/2006
Refactored into a module pattern with jQuery for 24comBlogs
*/

var RatingHelper = function(config) {

    //private values
    var _config = config;

    var sMax; // Isthe maximum number of stars
    var holder; // Is the holding pattern for clicked state
    var preSet; // Is the PreSet value onces a selection has been made
    var rated;

    // Rollover for image Stars //
    var rating = function(num) {
        sMax = $("#rateMe").children().length;

        if (!rated) {
            s = num.id.replace("_", ''); // Get the selected star
            a = 0;
            for (i = 1; i <= sMax; i++) {
                if (i <= s) {
                    $("#_" + i).addClass("on");
                    $("#rateStatus").html(num.title);
                    holder = a + 1;
                    a++;
                }
                else {
                    $("#_" + i).removeClass("on");
                }
            }
        }
    };

    // For when you roll out of the the whole thing //
    var off = function(me) {
        if (!rated) {
            if (!preSet) {
                for (i = 1; i <= sMax; i++) {
                    $("#_" + i).removeClass("on");
                    $("#rateStatus").html(me.parentNode.title);
                }
            }
            else {
                rating(preSet);
                $("#rateStatus").html($("#ratingSaved").html());
            }
        }
    };

    // When you actually rate something //
    /*var rateIt = function(me) {
    if (!rated) {
    $("#rateStatus").html($("#ratingSaved").html() + " <br /> " + me.title);
    preSet = me;
    rated = 1;
    rating(me);
    }
    };*/

    var ratePost = function(obj) {

        var rating = obj.id.replace("_", "");
        $.post(_config.ratePostUrl, { postId: _config.postId, blogId: _config.blogId, userId: _config.userId, value: rating }, ratePost_Callback, "json");
    };

    var ratePost_Callback = function(data) {
        var obj = eval(data);
        if (!obj.success) {
            alert(obj.message);
        }
        else {
            var ratingSaved = $("span#ratingSaved");
            ratingSaved.fadeIn("fast");
        }
    };

    var getHistory = function(e) {
        $.post(_config.ratingHistoryUrl, { postId: _config.postId }, getHistory_Callback, "json");

        var box = $("table#box");
        var pos = $("a#in").position();

        box.css('left', pos.left - 150);
        box.css('top', pos.top);
        box.fadeIn("slow");
    };

    var getHistory_Callback = function(data) {
        var detail = $("#ratingdetail");
        detail.setTemplate($("#ratingHistoryTemplate").html(), null, { filter_data: false });
        detail.processTemplate(data);
    };

    return {

        //public
        Setup: function() {
            $.ajaxSetup({ cache: false });
            
            //wireup click events
            $("#rateMe").children().click(function(event) { ratePost(this); });
            $("#rateMe").children().mouseover(function(event) { rating(this); });
            $("#rateMe").children().mouseout(function(event) { off(this); });

            //wireup history events
            $("a#in").click(function(event) { getHistory(); });
            $("tr#close").click(function(event) { $("table#box").hide(); });
        }
    };


};

