﻿jQuery.fn.initButton = function() {

   var ie6 = $.browser.msie && jQuery.browser.version < 7;
   $(this).show();

   return $(this).each(function() {
      if ($(this).hasClass('button')) {
         var width = $(this)[0].offsetWidth;
         if (width <= 33) $(this).addClass('button35');
         else if (width <= 48) $(this).addClass('button50');
         else if (width <= 63) $(this).addClass('button65');
         else if (width <= 78) $(this).addClass('button80');
         else if (width <= 93) $(this).addClass('button95');
         else if (width <= 108) $(this).addClass('button110');
         else if (width <= 123) $(this).addClass('button125');
         else if (width <= 138) $(this).addClass('button140');
         else {
            $(this).removeClass('button');
         }
      }

      var pos = $(this).backgroundPosition();

      $(this).mouseenter(function() { $(this).css('background-position', pos[0] + (pos[1] - 32) + 'px'); $(this).css('cursor', 'pointer'); });
      $(this).mousedown(function() { $(this).css({ 'background-position': pos[0] + (pos[1] - 64) + 'px', 'color': '#d1d3d4' }); });
      $(this).mouseleave(function() { $(this).css({ 'background-position': pos[0] + pos[1] + 'px', 'color': '#ED1C24' }); });
   });
};

jQuery.fn.backgroundPosition = function() {
   var p = $(this).css('background-position');

   if (typeof (p) === 'undefined')
      p = $(this).css('background-position-x') + ' ' + $(this).css('background-position-y');

   p = p.split(' ');
   p[0] += ' ';
   p[1] = p[1].replace('px', '');

   return p;
};