﻿String.prototype.trim = function(){
    return this.replace(/(^\s*)|(\s*$)/g,"");
}

function inputText(text,maxLength){

    var result = text.toString();
    result = result.trim();
    if (result.length<1) 
        return result;
    if (result.Length > maxLength)
        result = result.Substring(0, maxLength);
    result = result.replace(/[\s]{2,}/g, " ");	//two or more spaces      
    result = result.replace(/(<[b|B][r|R]*>)+|(<[p|P](.|\n)*?>)/g, "\n");	//<br>
    result = result.replace(/(\s*&[n|N][b|B][s|S][p|P];\s*)+/g, " ");	//&nbsp;
    result = result.replace(/<(.|\n)*?>/g,"");	//any other tags
    result = result.replace("'", "''");
    return result;
}

jQuery.fn.extend({
    "createDropDownList":function (items){
                var ddl={"select":function(){}};
				if(document.getElementById("divSearchC")){$("#divSearchC").remove(); return false}
				var $this = $(this);
				var	top=$(this).offset().top+1;
				var height=$(this).height();
				var width = $this.width();
				var left=$(this).offset().left;			
				var vli="<div id='divSearchC' style=\"background-color:#FFFFFF;border:1px solid #C3BEBA;position:absolute; left:"+left+"px;top:"+(top+height)+"px;z-index:3000;width:"+width+"px;\"><ul>";
				//vli+="<li style=\"line-height:18px;padding:0px 5px 0px 5px;cursor:pointer\" value='0'>所有行业</li>";
				$(items).each(function(){
					var t=$(this)[1].toString();
					var v=$(this)[0].toString();
					vli+="<li style=\"line-height:18px;padding:0px 5px 0px 5px;cursor:pointer\" value='"+v+"'>"+t+"</li>"
				});
	
				vli+="</ul></div>";
				$("body").append(vli);
                
				$("#divSearchC").find("li").bind("click",function(){
					$this.val($(this).text());
					$this.attr("selectedValue",$(this).attr("value"))//用自定义属性selectedValue来表示选择的值
					//$("#hideCategory").val($(this).attr("value"));//用隐藏域来表示选择的值
					$("#divSearchC").remove();
					$.cookie('categoryid',$(this).attr("value"),{path:'/'});
					$.cookie('categoryname',$(this).text(),{path:'/'});		
                    ddl.select({"value":this.getAttribute("value"),"text":$(this).text()});		
					
				}).hover(function(){$(this).css("background-color","#cccccc");},function(){$(this).css("background-color","#fff");});
				//.bind("mouseover",function(){$(this).css("background-color","#cccccc");},)
				//.bind("mouseout",function(){$(this).css("background-color","#fff");});
		        return {"onSelect":function(callBack){
		                            ddl.select = callBack;
		                           // return data;
		                        
		                          }
		               };
			}
			
});


$.getJSON("/JSONProductCategory.ashx",function(categoryArray){

        //var categoryArray=[[0,"所有行业"],[1,"卡酷产品家族"],[2,"卡酷新品上架"]]

        /*初始值*/
        $("#categorySelect").val("所有产品");
        $("#categorySelect").attr("selectedValue","0");
        $("#categorySelect").click(function(){
            $(this).createDropDownList(categoryArray)
        });

})

