﻿
    (function ($) {

        var methods = {
            init: function (options) {

                var settings = {
                    TotalRatingID: '#fakeSelector',
                    ContainerID: '#fakeID'
                };

                //options
                if (options) {
                    $.extend(settings, options);
                }

                return this.each(function () {

                    //functionality
                    var imageSize = 10;

                    //Initializing widths with given rating marks.
                    var $cont = jQuery(this);
                    //Parsing data from server side from hidden control
                    var markData = JSON.parse($cont.attr('value'));
                    var aboveWidth = ((markData["MaxValue"] - markData["MinValue"]) / markData["Step"]) * imageSize;
                    var belowWidth = ((markData["Rating"] - markData["MinValue"]) / markData["Step"]) * imageSize;
                    
                    //If we page content is empty, then we add above and below divs
                    if (jQuery('#above' + $cont.attr('id')).length == 0) {
                        $cont.parent().parent().after('<td>' + markData["Name"] + '</td><td><div class ="above" style="background-position-y:3px;" id="above' + $cont.attr('id') + '"><div class="below" style="background-position-y:3px;" id="below' + $cont.attr('id') + '"' + '></div></div></td><td class="caption">' + markData["Rating"].toFixed(1) + '</td>');

                        var $below = jQuery('#below' + $cont.attr('id'));
                        $below.css('width', belowWidth + 'px');
                        $below.attr("lang", belowWidth + 'px');

                        var $curAbove = jQuery('#above' + $cont.attr('id'));
                        $curAbove.css('width', aboveWidth + 'px');
                    }
                    //Total rating calculation
                    if (settings.TotalRatingID != "-1") {
                        var $totalRating = jQuery(settings.TotalRatingID);
                        if ($totalRating.length > 0) {
                            var $marks = jQuery('.caption', "#" + settings.ContainerID);
                            var totalSum = 0;
                            $marks.each(
                                function () {
                                    totalSum += Number(jQuery(this).html());
                                }
                            );
                            $totalRating.html((totalSum / $marks.length).toFixed(1));
                        }
                    }
                    if (jQuery('.flag', "#" + settings.ContainerID).attr('ronly') == 'false')
                    {
                        var $above = jQuery('.above', "#" + settings.ContainerID)
                        $above.click(function (e) {
                            var x = e.pageX - jQuery(this).offset().left;
                            var newWidth = Math.round(x / imageSize) * (imageSize);

                            $hf = jQuery("#" + settings.ContainerID).find('#' + jQuery(this).attr('id').replace('above', ''));
                            var markData = JSON.parse($hf.attr('value'));
                            markData['Rating'] = markData['MinValue'] + Math.round(x / imageSize);
                            $hf.attr('value', JSON.stringify(markData));

                            $below = jQuery(this).find('.below');
                            $below.css('width', newWidth);
                            $below.attr('lang', newWidth);

                            jQuery(this).parent().next().html(markData['Rating'].toFixed(1));


                            //Total rating calculation
                            var $totalRating = jQuery(settings.TotalRatingID);

                            if ($totalRating.length > 0) {
                                var $marks = jQuery('.caption', "#" + settings.ContainerID);
                                var totalSum = 0;
                                $marks.each(
                                function () {
                                    totalSum += Number(jQuery(this).html());
                                }
                            );
                                $totalRating.html((totalSum / $marks.length).toFixed(1));
                            }

                        });
                        $above.mousemove(function (e) {
                            var x = e.pageX - jQuery(this).offset().left;
                            var newWidth = Math.round(x / imageSize) * (imageSize);
                            jQuery(this).find('.below').css('width', newWidth);
                        });
                        $above.mouseover(function (e) {
                            var x = e.pageX - jQuery(this).offset().left;
                            var newWidth = Math.round(x / imageSize) * (imageSize);
                            jQuery(this).find('.below').css('width', newWidth);
                        });

                        $above.mouseout(function (e) {
                            $below = jQuery(this).find('.below');
                            $below.css('width', $below.attr("lang"));
                        });
                    }

                });
            },
            method2: function (options) {
                alert('method2');
            }
        };

        $.fn.rating = function (method) {
            if (methods[method]) {
                return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
            } else if (typeof method === 'object' || !method) {
                return methods.init.apply(this, arguments);
            } else {
                $.error('Method ' + method + ' does not exist');
            }

        };

    })(jQuery);
    

