/* Copyright 2009 WeatherBill Inc. */
wb = window.wb || {};

(function($){
   /**
    * Return a range of "count" numbers, each of which is "step"
    * greater than the previous. "n" is not included in the range.
    */
   function open_range(n, count, step, prev){
     prev = prev || [];
     if (count == 0) return prev;
     else {
       var next = n + step;
       prev.push(next);
       return open_range(next, count - 1, step, prev);
     }
   }

   $.extend(wb, {
     log10: function(x){ return Math.log(x) * Math.LOG10E; },
     round: function(x, decimals){
       var p = Math.pow(10, decimals);
       return Math.round(x * p) / p;
     },
     priceToFloat: function(price){
       return parseFloat(price.toString().replace("$", "").replace(/,/gi, ""));
     },
     numToFloat: function(num){
       return parseFloat(num.replace(/,/gi, ""));
     },
     openRange: open_range,

     addCommas: function(nStr) {
       nStr += '';
       var x = nStr.split('.');
       var x1 = x[0];
       var x2 = x.length > 1 ? '.' + x[1] : '';
       var rgx = /(\d+)(\d{3})/;
       while (rgx.test(x1)) {
         x1 = x1.replace(rgx, '$1' + ',' + '$2');
       }
       return x1 + x2;
     },

     // format currency
     f$: function(n){
       return "$" + wb.addCommas(n);
     },

     DATE_FORMAT: "yy-mm-dd",
     parseDate: function(dateString){
       return $.datepicker.parseDate(wb.DATE_FORMAT, dateString);
     },

     MIN_TEMP: 3,

     pure: {
       zebra: function(arg){
		     return (arg.pos % 2 == 0) ? 'even' : 'odd';
       },
       zebraAndLead: function(arg){
		     return ((arg.pos % 2 == 0) ? 'even' : 'odd') +
           ' ' + ((arg.pos == 0) ? 'lead' : '');
       }
     }
   });
})(jQuery);
