/*global window, document, alert, Image, console  */

(function ()
   {
   // private constructor
   function _$(els)
   {
      var i, element;
      this.elements = [];
      for (i = 0; i < els.length; i++)
      {
         element = els[i];

         if (typeof(element) === "object")
         {
            // ok
         }
         else if (element.charAt(0) === '#')
         {
            element = document.getElementById(element.substring(1));
         } 
         else if (typeof element === 'string')
         {
            element = document.getElementsByTagName(element);
         }
         
         this.elements.push(element);
      }
      return this;
   }
   
   _$.prototype = {
      each: function (fn)
      {
         var i, len;
         for (i = 0, len = this.elements.length; i < len; ++i)
         {
            fn.call(this, this.elements[i]);
         }
         return this;
      },
      html: function (str)
      {
         this.each(function (el)
         {
            el.innerHTML = str;
         });
         return this;
      },      
      append: function (ch)
      {
         this.each(function (el)
         {
            console.log(el, ch);
            el.appendChild(ch);
         });
         return this;
      },
      clean: function ()
      {
         this.each(function (el)
         {
            var i;
            if ((el) && (el.hasChildNodes()))
            {
               for (i = el.childNodes.length - 1; i >= 0; i--)
               {
                  el.removeChild(el.childNodes[i]);
               }
            }
            
         });
         return this;
      },
      blur: function ()
      {
         this.each(function (el)
         {
            el.blur();
         });
         return this;
      },
      focus: function ()
      {
         this.each(function (el)
         {
            el.focus();
         });
         return this;
      },
      val: function (val)
      {
         this.each(function (v)
         {
            v.value = val;
         });
         return this;
      },
      setStyle: function (prop, val)
      {
         this.each(function (el)
         {
            el.style[prop] = val;
         });
         return this;
      },
      addClass: function (className)
      {
         this.each(function (el)
         {
            el.className += ' ' + className;
         });
         return this;
      },
      dom: function()
      {
         return this.elements;
      },
      on: function (type, fn)
      {
         var listen = function (el)
         {
            if (window.addEventListener)
            {
               el.addEventListener(type, fn, false);
            }
            else if (window.attachEvent)
            {
               el.attachEvent('on' + type, function ()
               {
                  fn.call(el, window.event);
               });
            }
         };
         this.each(function (el)
         {
            listen(el);
         });
         
         return this;
      },
      css: function (o)
      {
         var that = this;
         this.each(function (el)
         {
            for (var prop in o)
            {
               if (o[prop])
               {
                  that.setStyle(prop, o[prop]);
               }
            }
         });
         return this;
      }
   };
   window.$ = function ()
   {
      return new _$(arguments);
   };
})();

function strfix(str, maxlen)
{
   var strlen, i;
   if (!maxlen)
   {
      maxlen = 6;
   }
   
   strlen = str.length;
   if (maxlen > strlen)
   {
      for (i = strlen; i < maxlen; i++)
      {
         str += '&nbsp;';
      }
   }
   return str;
}
