﻿function RatingDisplay(parent, rNumbers, rTitle, enabledImageUrl, disabledImageUrl, updatesEnabled, eventlineitemid_in, ratingTopicID_in) {
    var parentElement = parent,
                currentControl = this,
                ratingCount = rNumbers,
                oldRating = rNumbers,
                ratingTitle = rTitle,
                controlValueToSet = null,
                enabledImage = '/Content/Images/EnabledStar.png',
                disabledImage = '/Content/Images/DisabledStar.png',
                canSetRatings = false,
                star1 = null,
                star2 = null,
                star3 = null,
                star4 = null,
                star5 = null,
                eventlineitemid = eventlineitemid_in,
                ratingTopicID = ratingTopicID_in;

    if (typeof (enabledImageUrl) !== 'undefined' && enabledImageUrl)
    {
        enabledImage = enabledImageUrl;
    }

    if (typeof (disabledImageUrl) !== 'undefined' && disabledImageUrl)
    {
        disabledImage = disabledImageUrl;
    }

    if (typeof (updatesEnabled) !== 'undefined' && updatesEnabled)
    {
        canSetRatings = (updatesEnabled || false);
    }

    this.Unload = function Seansoft$RatingDisplay$Unload() 
    {
        parentElement.removeChild(childDivs[0].parentNode);
        ratingNumbers = null;
        ratingTitles = null;
        currentControl = null;
    };

    this.SetupElements = function Seansoft$RatingDisplay$SetupElements()
    {
        var parentDiv = document.createElement("DIV"),
            labelSpan = document.createElement("span");

        star1 = document.createElement("img");
        star2 = document.createElement("img");
        star3 = document.createElement("img");
        star4 = document.createElement("img");
        star5 = document.createElement("img");

        parentDiv.className = "imgRatingWrapper";
        labelSpan.className = "ratingTitleLabel";
        star1.className = "imgStarRating";
        star2.className = "imgStarRating";
        star3.className = "imgStarRating";
        star4.className = "imgStarRating";
        star5.className = "imgStarRating";

        labelSpan.innerHTML = ratingTitle;
        parentDiv.appendChild(labelSpan);

        currentControl.SetStarRatingImages();

        parentDiv.appendChild(star1);
        parentDiv.appendChild(star2);
        parentDiv.appendChild(star3);
        parentDiv.appendChild(star4);
        parentDiv.appendChild(star5);

        if (canSetRatings)
        {
            // Make the stars clickable to set the rating.
            $(star1).click(function() { currentControl.UpdateRating(5); });
            $(star2).click(function() { currentControl.UpdateRating(4); });
            $(star3).click(function() { currentControl.UpdateRating(3); });
            $(star4).click(function() { currentControl.UpdateRating(2); });
            $(star5).click(function() { currentControl.UpdateRating(1); });
        }

        // Add us to the page
        parentElement.appendChild(parentDiv);
    };

    this.SetStarRatingImages = function Seansoft$RatingDisplay$SetStarRatingImages()
    {
        star1.src = (ratingCount > 4 ? enabledImage : disabledImage);
        star2.src = (ratingCount > 3 ? enabledImage : disabledImage);
        star3.src = (ratingCount > 2 ? enabledImage : disabledImage);
        star4.src = (ratingCount > 1 ? enabledImage : disabledImage);
        star5.src = (ratingCount > 0 ? enabledImage : disabledImage);
    }

    this.UpdateRating = function Seansoft$RatingDisplay$UpdateRating(newRating)
    {
        // Update internal rating value
        ratingCount = newRating;

        // Call the endpoint and update the rating value
        //$.get('/Rating/MemberRatingOfProvider/' + eventlineitemid + '/' + ratingTopicID + '/' + newRating, function(data) { ; });

        // And setup the stars again.
        currentControl.SetStarRatingImages();
    }

    this.ValidRating = function Seansoft$ValidRating()
    {
        return ratingCount != 0;
    }

    this.ChangedRating = function Seansoft$ChangedRating()
    {
        return ratingCount != oldRating;
    }

    this.GetEventLineItemID = function Seansoft$GetEventLineItemID()
    {
        return eventlineitemid;
    }

    this.GetRatingTopicID = function Seansoft$GetRatingTopicID()
    {
        return ratingTopicID;
    }

    this.GetRatingCount = function Seansoft$GetRatingCount()
    {
        return ratingCount;
    }

    currentControl.SetupElements();
}

