/**
 * jquery.textnode.filter.js
 *
 * merge jquery.textnode.filter.*
 *
 */
(function($) {

   //replacement
   $.fn.replacement = function (regex,replace) {
     this.each( function() {
          str = $(this).text().replace(regex,replace);
          if (str != $(this).text()) {
            $(this).text(str);
          }
        });
     return this;
   };

   //trim
   $.fn.trim = function() {
     this.replacement(/^\s*(.+)$/,"$1").replacement(/(.+[^\s])\s*$/,"$1");
     return this;
   };

   //comma
   $.fn.comma = function() {
     this.each( function() {
          if ($(this).text().match(/^\d+$/)){
                    $(this).text(
                      $(this).text().replace(/((?:^[-+])?\d{1,3})(?=(?:\d{3})+(?!\d))/g, "$1,")
                      // See:
                    );
          }
        });
     return this;
   };

   //uncomma
   $.fn.uncomma = function() {
     this.each( function() {
          if ($(this).text().match(/^[\d,]+$/)){
                    $(this).text(
                      $(this).text().replace(/,/g, "")
                    );
          }
        });
     return this;
   };

   /**
    * dateformat
    * @param string 'current date format' example:%Y-%m-%d
    * @param string 'new date format'
    */
   $.fn.dateformat = function(c,n) {
     cObj = c.match(/%[a-zA-Z]/g);
     nObj = n.match(/%[a-zA-Z]/g);
     cFormat = c.replace(/%[a-zA-Z]/g,'');
     week = ['日','月','火','水','木','金','土'];
     this.each( function(){
          elmFormat = $(this).text().replace(/[0-9]+/g,'');
          datenum = $(this).text().match(/[0-9]+/g);
          d = new Date();
          if(cFormat == elmFormat && datenum != null){

            if(cObj.length == datenum.length){

              for(i = 0; i < cObj.length; i++){
            switch(cObj[i]){

            case '%Y':
              d.setYear(datenum[i]);
              break;

              //month 0-11
            case '%m':
              d.setMonth(datenum[i]-1);
              break;

            case '%d':
              d.setDate(datenum[i]);
              break;

            default:
              break;
            }
              }
              var temp = n;
              for(i = 0; i < nObj.length; i++){

            switch(nObj[i]){

            case '%Y':
              temp = temp.replace(/%[a-zA-Z]/,d.getYear());
              break;

            case '%n':
              month = d.getMonth() + 1;
              temp = temp.replace(/%[a-zA-Z]/,month);
              break;

            case '%m':
              month = d.getMonth() + 1;
              if(month.toString().length == 1){
                month = "0" + month.toString();
              }
              temp = temp.replace(/%[a-zA-Z]/,month);
              break;

            case '%j':
              date = d.getDate();
              temp = temp.replace(/%[a-zA-Z]/,date);
              break;

            case '%d':
              date = d.getDate();
              if(date.toString().length == 1){
                date = "0" + date.toString();
              }
              temp = temp.replace(/%[a-zA-Z]/,date);
              break;

            case '%w':
              temp = temp.replace(/%[a-zA-Z]/,d.getDay());
              break;

            case '%W':
              temp = temp.replace(/%[a-zA-Z]/,week[d.getDay()]);
              break;

            default:

              break;
            }
              }

              $(this).text(temp);

            }
          }
        });
     return this;
   };

 })(jQuery); // function($)
