﻿

jQuery(document).ready(function() {

    if (navigator.cookieEnabled) {

        var starHtml = "<h2><img src='/CMSWebsite/images/icons/staricon.png' alt='' />Feedback</h2><p>Did you find what you were looking for?</p>";

        for (i = 0; i < 2; i++) {
            //abdul hadi-13/01/12: next line commeted out as no longer want 0.5-10(5 stars) rating with half stars. Now 0/1 for No/Yes  
            //var value = (i + 1) * 0.5;
            var value = i;
            var title;
            (value == 1) ? title = "Yes" : title = "No";
            //abdul hadi-13/01/12: next line commeted out as no need for each radio button to act as half a star anymore. new version below
            //starHtml += "<input name='star' class='star{half:true}' type='radio' value='" + value + "' />";
            starHtml += "<input name='star' class='star' type='radio' value='" + value + "' title='" + title + "' />";
        }

        
        starHtml += "<br/><div class='clearfix'><div class='ratings-average' style='display: none'><span></span>of people found this useful</div></div><br/>";

        starHtml += "<div class='comments' style='display:none'><p>Thank you for your feedback. Please provide any additional comments below.</p>\
                        <label for='commentTextBox'>Comments (optional):</label>\
                        <textarea id='commentTextBox'></textarea>\
                        <button>Submit comment</button>\
                        <br/><br/></div>";

        starHtml += "<div class='thank-you' style='display:none'><p>Thank you for your comments. These will be used to improve the quality of our pages.</p><br/><br/></div>";

        jQuery(".ratings").each(function() {
            var thisJQuery = jQuery(this);
            var pageUrl = thisJQuery.find("input").val();
            thisJQuery.html(starHtml);

            //abdul hadi-24/12/2010: next if statement introduced to not display ratings at all when socitm survey fires and interferes with object.
            if (thisJQuery.find("input").rating) {

                thisJQuery.find("input").rating({
                    callback: function() {
                        thisJQuery.find("input").rating("readOnly", true);
                        jQuery.getJSON("/CMSWebsite/SetRating.aspx?rating=" + thisJQuery.find("input:checked").val() + "\&url=" + escape(pageUrl) + "\&callback=?", function(result) {
                            if (result.avgRating) {
                                thisJQuery.find("div.ratings-average").show().children("span").text(result.avgRating);
                            }
                            thisJQuery.find(".comments").show();


                            //abdul hadi-13/01/12: this extraHtml for email textbox and the participate checkbox is inserted after the comments box
                            var extraHtml = "<label for='emailTextBox'>Email (optional):</label>\
                                <input type='text' id='emailTextBox'/>\
                                <input type='checkbox' id='surveyCheckBox' value='yes'/>\
                                <span class='survey-label'>Tick this box if you would like to take part in improving our website.</span><br />";
                            thisJQuery.find("textarea#commentTextBox").after(extraHtml);
                                
                        });
                    }
                });
                thisJQuery.find("input").rating("readOnly", true);
                jQuery.getJSON("/CMSWebsite/GetRating.aspx?url=" + escape(pageUrl) + "\&callback=?", function(result) {
                    if (result.myRating) {
                        thisJQuery.find("input").rating("readOnly", false);
                        thisJQuery.find("input").rating("select", result.myRating, false);
                        thisJQuery.find("input").rating("readOnly", true);
                    }
                    else {
                        thisJQuery.find("input").rating("readOnly", false);
                    }
                    if (result.avgRating) {
                        thisJQuery.find("div.ratings-average").show().children("span").text(result.avgRating);
                    }
                });
                thisJQuery.find("button").click(function() {
                    thisJQuery.find(".comments").hide();
                    //abdul hadi-13/01/12: values of emailTextBox and surveyCheckBox also passed to the WebService.cs in CCCDIT.Web.UI.Ratings together with the comments field
                    jQuery.getJSON("/CMSWebsite/SetComment.aspx?url=" + escape(pageUrl) + "\&comment=" + escape(thisJQuery.find('textarea').val()) + "\&email=" + escape(thisJQuery.find('input#emailTextBox').val()) + "\&survey=" + escape(thisJQuery.find('input#surveyCheckBox:checked').val()) + "\&callback=?", function() {
                        thisJQuery.find(".thank-you").show();
                    });
                    return false;
                });

            } else {
                thisJQuery.html("");
            }


        });

        /*jQuery(".ratings").html(starHtml);
        jQuery(".ratings input").rating({
        callback: function() {
        jQuery(".ratings input").rating('readOnly', true);
        }
        });*/
    }

});
