/*!
 * jQuery FormUtils Plugin
 * version: 0.1
 * @author Rodrigo Saboya
 */
(function($){
	var ns = 'fm_';
	var _checklength = function(elem) {
		var len = elem.val().length;
		var limit = elem.data(ns+'maxlength');
		var label = elem.data(ns+'length_elem');
		if(len > limit) {
			elem.val(elem.val().substring(0,limit));
		}
		if(typeof(label) != 'undefined') {
			$(label).html(limit-elem.val().length);
		}
	};
	$.fn.autoselect = function() {
		return this.each(function(){
			var $this = $(this);
			$this.bind('click',function(){
				var $this = $(this);
				$this.select();
			});
		});
	};
	$.fn.defaultvalue = function(value) {
		return this.each(function(){
			var $this = $(this);
			$this.data(ns+'defaultvalue',(typeof(value))==='undefined'?$this.val():value);
			$this.bind('focus',function(e) {
				var $this = $(this);
				if($this.val() == $this.data(ns+'defaultvalue')) {
					$this.val('');
				}
			});
			$this.bind('blur',function(e){
				var $this = $(this);
				if($this.val().length == 0) {
					$this.val($this.data(ns+'defaultvalue'));
				}
				var maxlength = $this.data(ns+'maxlength');
				if(typeof(maxlength) !== 'undefined') {
					_checklength($this);
				}
			});
		});
	};
	$.fn.limit = function(limit,label) {
		return this.each(function(){
			var $this = $(this);
			$this.data(ns+'maxlength',limit);
			if(typeof(label)!='undefined') {
				$this.data(ns+'length_elem',label);
			}
			_checklength($this);
			$this.bind('focus',function(e) {
				var h = setInterval(function(){_checklength($this);},100);
				$this.data(ns+'maxlength_ihandle',h);
			});
			$this.bind('blur',function(e) {
				clearInterval($this.data(ns+'maxlength_ihandle'));
			});
		});
	};
	$.fn.inputfilter = function(r) {
		return this.each(function() {
			var $this = $(this);
			if(!$this.is('input[type="text"],textarea')) {
				return;
			}
			$this.data(ns+'inputfilter_re',new RegExp(r.toString().match(/^\/([^/]+)/)[1],'g'));
			$this.bind('keyup',function(e) {
				switch(e.keyCode) {
					case $.keyCode.LEFT:
					case $.keyCode.RIGHT:
					case $.keyCode.UP:
					case $.keyCode.DOWN:
					case $.keyCode.HOME:
					case $.keyCode.END:
					case $.keyCode.SHIFT:
					case $.keyCode.TAB:
					case $.keyCode.BACKSPACE:
						return;
				}
				var $this = $(this);
				var temp = $this.val().match($this.data(ns+'inputfilter_re'));
				$this.val(temp===null?'':temp.join(''));
			});
		});
	};
	$.fn.autotab = function(elem) {
		var $elem = $(elem);
		return this.each(function() {
			var $this = $(this);
			$this.bind('keyup',function(e) {
				switch(e.keyCode) {
					case $.keyCode.LEFT:
					case $.keyCode.UP:
					case $.keyCode.RIGHT:
					case $.keyCode.DOWN:
					case $.keyCode.HOME:
					case $.keyCode.END:
					case $.keyCode.SHIFT:
					case $.keyCode.TAB:
					case $.keyCode.BACKSPACE:
						return;
				}
				var s = $this.getSelection();
				if(s.start ==  $this.attr('maxlength')) {
					$elem.focus();
				}
			});
			$elem.bind('keydown',function(e){
				var s = $(elem).getSelection();
				if(s.length != 0) {
					return;
				}
				switch(e.keyCode) {
					case $.keyCode.BACKSPACE:
					case $.keyCode.LEFT:
						if(s.start === 0) {
							$this.focus();
						}
						break;
				}
			});
		});
	};
})(jQuery);  
