<% /*
     $Revision$
     $Id$

     File:          rater.inc
     Description:   generic interface function for rating
     documentation:  wbotelhos.com/raty

*/ %>

<%
FCLTHeader.Requires({ plugins: ["jQuery"],
                      js: ["../localscripts/raty/jquery.raty.min.js"],
                      extrahead: _initRater });


function _initRater()
{
  var ratingParams = rating.getRatingParams();
%>
  <script>
    function getScore(rating)
    {
      var score = 0;
      if (<%=S("fac_like_bad")%> > 0 && rating >= <%=S("fac_like_bad")%>) score++;
      if (<%=S("fac_like_poor")%> > 0 && rating >= <%=S("fac_like_poor")%>) score++;
      if (<%=S("fac_like_normal")%> > 0 && rating >= <%=S("fac_like_normal")%>) score++;
      if (<%=S("fac_like_good")%> > 0 && rating >= <%=S("fac_like_good")%>) score++;
      if (<%=S("fac_like_excellent")%> > 0 && rating >= <%=S("fac_like_excellent")%>) score++;

      return score;
    }

    $(document).ready(function ()
    {
      $(".rater").raty({score: function() { return getScore((parseInt($(this).attr('data-score')))); },
                        readOnly: function() { return $(this).attr('readonly'); },
                        showHalf : true,
                        half : true,
                        path: '../localscripts/raty/img/',
                        width: 120,
                        number: <%=ratingParams.nrOfSstars%>,
                        hints: <%=JSON.stringify(ratingParams.hints)%>
                       });
    });
   </script>
<%
}

function rater(rating, params)
{
    params = params||{};
    if (!rating && params.shownone)
        return "";

    likesymbol = "<span class='rater' data-score='" + rating + "'" + (params.readonly? " readonly='1'" : "") + "></span>";
    return likesymbol;
}

var rating = {
      // Gives the (1 out of 5) image that belongs to a given rating
      // If no rating is given (null), then depening on params.shownone
      // either null or the none-picture is given
      getRatingParams: function ()
      {
        var nrOfSstars = 0;
        var hints = [];
        if (S("fac_like_bad") > 0)
        {
          nrOfSstars++;
          hints.push(L('lcl_faclike_statisf1'));
        }
        if (S("fac_like_poor") > 0)
        {
          nrOfSstars++;
          hints.push(L('lcl_faclike_statisf2'));
        }
        if (S("fac_like_normal") > 0)
        {
          nrOfSstars++;
          hints.push(L('lcl_faclike_statisf3'));
        }
        if (S("fac_like_good") > 0)
        {
          nrOfSstars++;
          hints.push(L('lcl_faclike_statisf4'));
        }
        if (S("fac_like_excellent") > 0)
        {
          nrOfSstars++;
          hints.push(L('lcl_faclike_statisf5'));
        }

        return {nrOfSstars: nrOfSstars, hints: hints};
      },

      satisfactionsymbol: function (rating, params)
      {
        var imgstr;
        params = params||{};
        if (!rating && params.shownone)
            imgstr = "../Pictures/flike_none.png";
        else if (rating <= S('fac_like_bad'))
            imgstr = "../Pictures/flike_bad.png";
        else if (rating <= S('fac_like_poor'))
            imgstr = "../Pictures/flike_poor.png";
        else if (rating <= S('fac_like_normal'))
            imgstr = "../Pictures/flike_normal.png";
        else if (rating <= S('fac_like_good'))
            imgstr = "../Pictures/flike_good.png";
        else if (rating <= S('fac_like_excellent'))
            imgstr = "../Pictures/flike_excellent.png";
        return imgstr;
      },

      stars: function (rating, params)
      {
        function getStar(starOn)
        {
          var star = (starOn?"fa-star":"fa-star-o");
          return "<i class='fa fa-fw " + star +"'></i>"
        }
        var imgstr = "";
        params = params||{};
        if (!rating && params.shownone)
        {
          imgstr = "<i class='fa fa-fw fa-question-circle'></i>";
        }
        else
        {
          if (S("fac_like_bad") > 0)
            imgstr += getStar( rating >= S("fac_like_bad"));
          if (S("fac_like_poor") > 0)
            imgstr += getStar( rating >= S("fac_like_poor"));
          if (S("fac_like_normal") > 0)
            imgstr += getStar( rating >= S("fac_like_normal"));
          if (S("fac_like_good") > 0)
            imgstr += getStar( rating >= S("fac_like_good"));
          if (S("fac_like_excellent") > 0)
            imgstr += getStar( rating >= S("fac_like_excellent"));
        }

        if (imgstr != "")
          var id = (params.rating.id ? " id=\'" + params.rating.id + "\'" + "' name=\'" + params.rating.id + "\'" : "");
          var note = (params.rating.note? " title=\'" + safe.htmlattr(params.rating.note) + "\'" : "");
          imgstr = "<span" + id + note + ">"
                 + imgstr
                 + "</span>";

        return imgstr;
      }
  };

%>