jQuery.fn.extend({
	setThis: function() {
		this.each(function(){
			if (!this.$this) {
				this.$this = $(this);
			}
		});
		return this;
	},
	tabs: function(selector, activeClass){
		var $divs = $(selector), $tabs = this;
		this.setThis();
		this.each(function(index){
			this.tabs = {
					tabIndex: index,
					$tabDiv: $divs.eq(index)
			};
			if (!this.$this.hasClass(activeClass)) {
				this.tabs.$tabDiv.hide();
			}
		});
		this.click(function(){
			if (!this.$this.hasClass(activeClass)) {
				$tabs.removeClass(activeClass);
				this.$this.addClass(activeClass);
				$divs.hide();
				this.tabs.$tabDiv.show();
			}
		});
		return this;
	},
	inputTab: function(selector) {
		var $inp = $(selector);
		this.each(function(index){
			this.$inp = $inp;
			this.inpVal = index;
		});
		this.click(function(){
			this.$inp.attr('value', this.inpVal);
		});
	},
	checker: {
		hasValue: function(value){
			return (value != '');
		},
		email: function(value){
			return (value == '') || /^[0-9a-zA-Z_.-]+@([0-9a-zA-Z_.-]+\.)+[a-zA-Z]{2,4}$/.test(value);
		},
		equals: function(value, otherInput){
			return (value == otherInput.value);
		}
	},
	check: function(checkFunc, errSelector){
		var $err = $(errSelector), args = [], arglen = arguments.length, cf, i;
		if (typeof checkFunc == 'string') {
			checkFunc = this.checker[checkFunc] || null;
		} else if (typeof checkFunc != 'function') {
			checkFunc = null;
		}
		if (!checkFunc) {
			return;
		}
		if (arglen > 2) {
			for (i = 2; i < arglen; i++) {
				args.push(arguments[i]);
			}
		}
		this.setThis();
		this.change(function(){
			checkFunc.apply(this, [this.value].concat(args)) ? $err.hide() : $err.show();
		});
		return this;
	},
	noErrSubmit: function(){
		this.setThis();
		this.submit(function(){
			this.$this.find('input, textarea').change();
			if (this.$this.find('p.error:visible').size()) {
				return false;
			}
		});
	},
	buttonize: function(hoverClass, pressedClass){
		this.setThis();
		this.filter('.'+pressedClass).each(function(){
			this.pressed = true;
		});
		this.mouseenter(function(){
			if ((hoverClass != pressedClass) || !this.pressed) this.$this.addClass(hoverClass);
		}).mouseleave(function(){
			if ((hoverClass != pressedClass) || !this.pressed) this.$this.removeClass(hoverClass);
		}).click(function(){
			this.pressed = this.pressed ? false : true;
			if (hoverClass != pressedClass){
				this.pressed ? this.$this.addClass(pressedClass) : this.$this.removeClass(pressedClass);
			}
		});
		return this;
	},
	transfer: function(fromSelector, toSelector){
		var $selectFrom = $(fromSelector), $selectTo = $(toSelector);
		$selectFrom.find("option").setThis();
		$selectTo.find("option").setThis();
		this.setThis;
		if ($selectFrom.size() && $selectTo.size()) {
			this.click(function(){
				$selectFrom.find("option[selected]").removeAttr("selected").each(function() {
					var transElem = this, $optionsTo = $selectTo.find("option");
					if ($optionsTo.size()) {
						if ($(transElem).attr("value") == '---') {
							$selectTo.prepend($selectFrom.find("option[value=xxx]"));
							$selectTo.prepend(transElem);
						} else {
							$optionsTo.each(function(i) {
								if (this.$this.attr("value") && (this.$this.attr("value") != '---') && (this.text > transElem.text)) {
									this.$this.before(transElem);
									return false;
								}
								if (i == ($optionsTo.size() -1)) {
									this.$this.after(transElem);
									return false;
								}
							});
						}
					} else {
						$selectTo.append(transElem);
						if ($(transElem).attr("value") == '---') {
							$selectTo.append($selectFrom.find("option[value=xxx]"));
						}
					}
				});
			});
		}
		return this;
	},
	selectAll: function(name) {
		this.click(function(){
			if ($(this).attr('checked')) {
				$('input[name='+name+']').attr('checked', '1');
			} else {
				$('input[name='+name+']').removeAttr('checked', '1');
			}
		});
	}
	
});


