﻿/// <reference path="~/Content/Scripts/jquery-1.3.2-vsdoc.js" />
/// <reference path="~/Content/Scripts/tools-0.1.0.js" />
/*
* 
*/

(function ($) {

    $.extend({

        PageRating: new function () {

            this.defaults = {
                Debug: false,
                RatingService: "/DesktopModules/InvenManager.Comments/PageRating.asmx",
                AppPath: "",
                PageKey: "",
                PageName: ""
			};

            this.construct = function (options) {

                var config = $.extend($.PageRating.defaults, options);

                return this.each(function () {
                    var $this = $(this);
                    var rat = $("#rat", $this);
					
					// hide select list
					$("select", $this).hide();
					
                    // first request
                    Tools.Ajax({
                        url: config.AppPath + config.RatingService + "/Read",
                        data: { 
                            pageKey: config.PageKey
                        },
                        success: function (pageInfo) {
							
							// init page rating
                            rat.stars({
                                inputType: "select",
								disabled: pageInfo.AlreadyVoted,
								oneVoteOnly: true,
                                cancelShow: false,
                                // captionEl: $("#rate-messages", $this),
                                callback: function (ui, type, value) {
                                    // Disable Stars while AJAX connection is active
                                    ui.disable();

                                    // Display message to the user at the begining of request
                                    $("#messages", $this).text("Saving...").stop().css("opacity", 1).fadeIn(30);
                                    Tools.Ajax({
                                        url: config.AppPath + config.RatingService + "/Create",
                                        data: {
                                            pageKey: config.PageKey,
                                            rate: value,
                                            pageName: config.PageName
                                        },
                                        success: function (pageInfo) {
                                            UpdatePageRate($this, pageInfo, config);
                                        }
                                    }); // ajax
                                    UpdatePageRate($this, pageInfo, config);
                                } // callback
                            }); // rat.stars
                            UpdatePageRate($this, pageInfo, config);
                        } // success func
                    }); // ajax

                }); // each

            }; // constructor

            function UpdatePageRate($this, pageInfo, config) {
				var rat = $("#rat", $this);

                // Select stars from "Average rating" control to match the returned average rating value
                rat.stars("select", Math.round(pageInfo.VotesAvg));				
				$("#rate-messages", $this).text(" " + pageInfo.VotesCount + " rating(s)");
            }

        },


        CommentsRating: new function () {

            this.defaults = {
                Debug: false,
                RatingService: "/DesktopModules/InvenManager.Comments/CommentRating.asmx",
                rateUpSelector: "a.rate-up",
                rateDownSelector: "a.rate-down",
                IdRegExp: new RegExp("#(\\d+)$"),
				UserIsLoggedOn: false
            };

            this.construct = function (options, dataItem) {

                var config = $.extend($.CommentsRating.defaults, options);

                return this.each(function () {
                    // store common expression for speed
                    var $this = $(this);
					
					var score = $(".comment-score", $this);
					UpdateScore( $this, config, score.text(), dataItem.UserCanRateThis );
 
                }); // each
            }
			
			function BindLink( $this, link, request, config){
				link.bind("click", function (e) {
							
					Tools.Ajax({
						url: config.AppPath + config.RatingService + "/Create",
						data: request,
						success: function (data) {
							UpdateScore( $this, config, data, false);
						},
						OnError: function(message){
							alert(message);
						}
					});
					
					return false;
					
				});
			}
			
			function UpdateScore($this, config, rating, showLinks){
				var el = $(".comment-score", $this);
				if (rating < 0) {
					el.addClass("rateNegative");
				} else if(rating > 0){
					el.addClass("ratePositive");
				};
				
				if (rating >= 0){
					el.text("+" + rating);
				} else {
					el.text(rating);
				}
				
				var rateUp = $(config.rateUpSelector, $this);
				var rateDown = $(config.rateDownSelector, $this);			
				var id = Tools.FindFirst(rateUp.attr("href"), config.IdRegExp);
				
				if(showLinks){
					BindLink( $this, rateUp, { commentId: id, up: true }, config);
					BindLink( $this, rateDown, { commentId: id, up: false }, config);
				} else {
					rateUp.hide();
					rateDown.hide();
				};
			}
		
        }
    });

    $.fn.extend({
        PageRating: $.PageRating.construct,
        CommentsRating: $.CommentsRating.construct
    });

})(jQuery);
