﻿

if (typeof Rating == "undefined" || !Rating) 
{
    /**
     * The Rating global namespace object.  If Rating is already defined, the
     * existing Rating object will not be overwritten so that defined
     * namespaces are preserved.
     * @class Rating
     * @static
     */
    var Rating = {}; 
}

Rating.RegisterNamespace = function() 
{ 
    var a = arguments;
    var o = null;
    var i;
    var j;
    var d; 

    for (i=0; i<a.length; i=i+1) 
    {
        d = a[i].split(".");
        o = Rating;

        // Rating is implied, so it is ignored if it is included 
        for (j=(d[0] == "Rating") ? 1 : 0; j<d.length; j=j+1) 
        { 
            o[d[j]]=o[d[j]] || {};
            o = o[d[j]];
        }
    }
    return o; 
};

Rating.RegisterNamespace("Rating.Net");

/** 
* Initialize the rating
* @param {String} starBaseClass The star base class
* @param {String} formId        The form id 
* @param {String} cancelText    The cancel text
*/
Rating.Net.Script = function() 
{
    this.starBaseClass = null;  // The star base class 
    this.formId = null;         // The form id
    this.cancelText = null;     // The cancel text

    var me = this;
    
    /** 
    * Initialize the rating 
    * @param {String} starBaseClass The star base class
    * @param {String} formId        The form id
    * @param {String} cancelText    The cancel text  
    */
    this.Init = function(starBaseClass, formId, cancelText) 
    {
        me.starBaseClass = starBaseClass;
        me.formId = formId;
        me.cancelText = cancelText;
        
        //Fix form to enbale AJAX via client script
        $('#' + me.formId).submit
        (
            function(event) {
                eval($(this).attr("onsubmit"));
            }
        );

        //Start the control
        $('.' + me.starBaseClass).rating
        (
            {
                callback: function(value, link) 
                {
                    $('#' + me.formId + " input[type='submit']").click();
                },
                cancel: me.cancelText,
                cancelValue: '0'
            }
        );
        
        //QTip
        var qtipContent = $('#' + me.formId).children("div.rating-qtip");
        if (qtipContent.length > 0) 
        {
            $('#' + me.formId).children("div.divRateThis").qtip(
            {
                content: qtipContent.html(),
                position:
                {
                    //target: 'mouse',
                    adjust:
                    {
                        mouse: true
                    },
                    corner:
                    {
                        target: 'rightTop',
                        tooltip: 'leftTop'
                    }
                },
                style:
                {
                    border:
                    {
                        width: 1,
                        radius: 4
                    },
                    tip: true,
                    width:
                    {
                        min: 0,
                        max: 350
                    }
                }
            });
        }
    };
}

//Reinitializes scripts for every rating module over the page
function RatingInitAllScripts() 
{
    $("div.ratingContainer").each
    (
        function() 
        {
            var form = $(this).children("form");
            var formId = form.attr("id");
            var ratingId = form.children("input.rating-hidden-id").val();
            var radioId = form.children("rating-hidden-radio-id").val();
            new Rating.Net.Script().Init("auto-submit-star-" + ratingId, ratingId, radioId, formId, "0");
        }
    );
}
