/**
 * 页面输入框的默认文字效果
 * @author wg 2009-6-18
 * 
 */
(function(){
	// add prompt text to text input 
	$.fn.addInputPromptText = function(o){
		o = $.extend({
			text: '请输入值',
			color: 'gray'
		}, o);
		var promptText = $.trim(o.text);
		return this.each(function(){
			var ipt = $(this);
			this.inputPromptText = promptText;// save as the dom element's attribute, can be used for data validation
			this.inputPromptOldColor = this.inputPromptOldColor || ipt.css('color');
			var text = $.trim(ipt.val());
			if( ! text || text==promptText){
				ipt.val(promptText).css('color', o.color);
			}
			ipt.focus(function(){
				var v = $.trim(ipt.val());
				if (v==promptText) {
					ipt.val('')
				}
				ipt.css('color', this.inputPromptOldColor);
			}).blur(function(){
				var v = $.trim(ipt.val());
				if (v=='') {
					ipt.val(promptText).css('color', o.color);
				}
			});
		});
	};
	// remove the prompt text added by $.fn.addInputPromptText
	$.fn.fixInputPromptText = function(){
		return this.each(function(){
			var $this = $(this);
			if( this.inputPromptOldColor )
			{
				$this.css('color', this.inputPromptOldColor);
			}
			if( this.inputPromptText )
			{
				if( this.inputPromptText == $.trim($this.val()) )
					$this.val('');
			}
		});
	};
	//只允许输入正整数，不能包含小数点(需包含string.js) 
	$.fn.onlyPressNum = function() {
		return this.bind("keypress",function(e){
			if(e.ctrlKey == true || e.shiftKey == true)
				return false;
			if((e.which >= 48 && e.which <= 57 && e.ctrlKey == false && e.shiftKey == false) || e.which == 0 || e.which == 8)
			{
				return true;
			}
			else if(e.ctrlKey == true && (e.which == 99 || e.which == 118))
				return false;
			else
			return false;
		})
		.bind("keyup blur",function(){
			var val = $(this).val();
			var cleanVal = string.CtoH(val);
			
			if( cleanVal != val ){
				$(this).val(cleanVal);
			}
			
			cleanVal = $(this).val().replace(/\D/gi,"");
			if (cleanVal != val) {
				$(this).val(cleanVal);
			}
			if(isNaN($(this).val())) return false;
		})
		.bind("contextmenu",function(){return false;})
		.bind("drop",function(){return false;})
		.bind("paste",function(){return false;});
	};
	
	//只允许输入正数，可以包含小数点(需包含string.js)
	$.fn.onlyPressNumeric = function() {
		return this.bind("keypress",function(e){
			if(e.ctrlKey == true || e.shiftKey == true)
				return false;
			if((e.which >= 48 && e.which <= 57 && e.ctrlKey == false && e.shiftKey == false) 
				|| e.which == 0 || e.which == 8 || e.which == 46 )
			{
				return true;
			}
			else if(e.ctrlKey == true && (e.which == 99 || e.which == 118))
				return false;
			else
			return false;
		})
		.bind("keyup blur",function(){
			var val = $(this).val();
			var cleanVal = string.CtoH(val);
			
			if( cleanVal != val ){
				$(this).val(cleanVal);
			}
			
			cleanVal = $(this).val().replace(/[^0-9.]/gi,"");
			cleanVal = cleanVal.replace(/^[.]/,"");
			
			if (cleanVal != val) {
				$(this).val(cleanVal);
			}
			if(isNaN($(this).val())) return false;
		})
		.bind("contextmenu",function(){return false;})
		.bind("drop",function(){return false;})
		.bind("paste",function(){return false;});
	};
	
	// 文本有变动时，触发自定义事件
	$.fn.enableTextUpdateEvent = function(){
		var $this = $(this);
		var trigger = function(){
			$this.trigger('textUpdate');
		};
		return $this.keyup(trigger).focus(trigger);
	};
	
	$.fn.asKeywordSearch = function(o){
		$.extend({
			keywordInputId : null, // required, 关键字输入框的id
			urlPrefix : '', // required, urlManager生成的关键字查询格式化字符串，如 /fang1/d1p1_{0}/
			rule : /\//g,
			log : false,
			categoryId : 0
		}, o);
		var $keywordInput = $('#' + o.keywordInputId);
		var submitSearch = function(){ 
			var next = o.urlPrefix;
			var val = $.trim( $keywordInput.fixInputPromptText().val() );
			if ( val == '' ) {
				next = next.replace(/_\/$/, '/');
  				next = next.replace('//', '/');
			} else {
				//处理关键词查询
				if(o.keywordInputId == "id_keyword"){
					var interim = val.replace(o.rule, '');
					var keyword = encodeURIComponent(interim);
					next = next.replace(/_\/$/, '_'+keyword+'/');
				}
				else
					next = next.replace(/_\/$/, '_'+encodeURIComponent(val)+'/');
			}
			if (o.log) {
				LogServerCount.keywordSearchCount(o.categoryId, keyword);
			}
			window.location = next;
		};
		this.click(function(){
			submitSearch();
			return false;
		});
		$keywordInput.keyup(function(event){
			if (event.keyCode==13) {
				submitSearch();
			}
			return false;
		});
	};
	
})();
