(function($){
	$.fn.toggleText = function(s){
		$(this).each(function(){
			var i = $(this), val = i.val();
			if (val == '') {
				val = s;
				i.val(s);
			}
			if (val == s) i.addClass('empty');
			i.focus(function(){
				if (i.val() == s) {
					i.val('').removeClass('empty');
				}
			}).blur(function(){
				if (i.val() == '') {
					i.val(s).addClass('empty');
				} else if (i.val() == s) {
					i.addClass('empty');
				}
			});
		});
		return $(this);
	}
	
	
										
										
})(jQuery);

(function($){

	function hide(s) {
		$(s).val('').change().hide();
	}

	$.fn.jsonCombo = function(opt){

		var box = $(this);
		var o = box;
		if (!o.is('select')) o = o.find('select');
		
		opt = $.extend({
			emptyText: '',
			disableText: ''
		}, opt);

		function ch() {
			var pid = $(this).val();
			if (!pid || pid == 0) {
				//hide(o);
				//return;
			}
			$.getJSON(opt.url, {pid: pid}, function(result){
				if (opt.success) opt.success();
				o.html('');
				if (!result || 0 == result.length) {
					if (opt.hide) box.hide();
					o.append('<option value="0">' + opt.disableText + '</option>');
				} else {
					if (opt.hide) box.show();
					o.append('<option value="0">' + opt.emptyText + '</option>');
					for (var i in result) {
						o.append('<option value="' + result[i].id + '"' + (opt.val == result[i].id ? ' selected' : '') + '>' + result[i].name + '</option>');
					}
				}
				o.change();
			});
		}

		$(opt.parent).change(ch).change();

	};
})(jQuery);
