$(document).ready(function(){
  //$('#search-query').focus();
  //$('#hd').corner();
  //$('#ft').corner();
  //$('#artist-alphabet').corner();
  formatRatings();
});

function print()
{
    link = $('#print-link');
    hasChords = $('#aco');
    if (hasChords.length > 0)
    {
      var append = '/' + acoes + '/' + keyb
    }
    else
    {
      var append = '';
    }
    window.location = 'http://' + document.domain + link.attr('href') + append;
    return false;
}


function vote(whatFor, id, type, callback)
{
    $.getJSON('/vote/' + whatFor + '/' + id + '/' + type, function(data) {
	      if (data.error)
	      {
	          alert(data.error);
	          return;
	      }
	        
	      if (callback)
	      {
	         callback(whatFor, id, type, data);
	      }
    });
}

function updateVoteCounter(whatFor, id, type, data)
{
        switch (whatFor)
        {
          case 1:
          case 2:
              //numVotes = $('#numVotesFor' + whatFor + id);
              //numVotes.html(parseInt(numVotes.html()) + 1);
              //voteAction.html('wrequested');
              rating = $('#rating-total');
              total = parseInt(rating.html());
              
              if (type == 1)
              {
                  total++;
                  $('#rate-yes').addClass('rate-selected');
                  $('#rate-no').addClass('rate-noselected');
              }
              else
              {
                  total--;
                  $('#rate-yes').addClass('rate-noselected');
                  $('#rate-no').addClass('rate-selected');
              }
              rating.html(total);
              formatRatings();
              break;
          case 3:
            voteAction.html(_('jsThankCommentRating'));
            rating = $('#commentRating' + id);
            var ratingValue = parseInt(rating.html());
            if (type == 1)
            {
              ratingValue++;
            }
            else
            {
              ratingValue--;
            }
            if (ratingValue >= 0)
            {
              rating.html('+' + ratingValue);
            }
            else
            {
              rating.html(ratingValue);
            }
            break;
          default:
            break;
        }
}

function formatRatings()
{
    $.each($('.rating'), function () {
       total = parseInt($(this).html());
       if (total > 0)
       {
          $(this).html('+' + total);
          $(this).addClass('rating-positive');
       }
       else if (total < 0)
       {
          $(this).addClass('rating-negative');
       }
       else
       {
          $(this).addClass('rating-null');
       }       
    });
}

function _(msgid, interpolations)
{
    var translated = i18n[msgid];
    if (!translated) {
        translated = msgid;
    };
    if (interpolations) {
        for (var id in interpolations) {
            var value = interpolations[id];
            var reg = new RegExp('\\\$\\\{' + id + '\\\}', 'g');
            console.log(value);
            translated = translated.replace(reg, value);
        };
    };
    return translated;
}
