﻿var zQuery=function(ele,tagName,className){ //核心对象
	if ( window == this ) return new zQuery(ele,tagName,className);
	if(!arr){var arr=new Array;}
	if(ele){
		if(ele.constructor!=zQuery){
			var elem=typeof(ele)=="object"?ele:document.getElementById(ele);
			if(!tagName){
				arr.push(elem);
			}else{
				var tags=elem.all&&!window.opera?tagName=="*"?elem.all:elem.all.tags(tagName):elem.getElementsByTagName(tagName);
				if(!className){
					for(var i=0, l=tags.length; i<l; i++){
						arr.push(tags[i]);
					}
				}else{
					var reClassName=RegExp("(^|\\s+)"+className+"($|\\s+)");
					for(var i=0, l=tags.length; i<l; i++){
						if(reClassName.test(tags[i].className)){
							arr.push(tags[i]);
						}
					}
				}
			}
		}else{
			for(var i=0, l=ele.length; i<l; i++){
				arr=arr.concat(Array.prototype.slice.call(zQuery(ele[i],tagName,className)));
			}
		}
	}
	return this.setArray(arr); //借鉴jQuery方法，这里返回zQuery对象原型（一个DOM集合对象），不再是返回单纯的DOM数组对象
}
zQuery.prototype.setArray = function( arr ) {
	this.length = 0;
	Array.prototype.push.apply( this, arr ); //这里的概念非常重要，zQuery方法对象执行了Array原型的push()方法
	return this;
}
zQuery.fn=zQuery.prototype;
var $Select=zQuery;

//取select的绝对位置、高、宽
function Offset(obj){
	var t = obj.offsetTop;
	var l = obj.offsetLeft;
	var w = obj.offsetWidth;
	var h = obj.offsetHeight-2;
	//var h=obj.style.height;
	while(obj=obj.offsetParent)
	{
		t+=obj.offsetTop;
		l+=obj.offsetLeft;
	}
	return {
		top : t,
		left : l,
		width : w,
		height : h
	}
}
//模拟select
function instSelect(obj){
	var offSet=Offset(obj);
	obj.style.display="none";
	var sDiv=document.createElement("div");
	sDiv.id="div"+obj.name;
	sDiv.className="divSlt";
	sDiv.style.width=offSet.width+"px";
	//sDiv.style.height=offSet.height+"px";
	sDiv.style.left=offSet.left+"px";
	sDiv.style.top=offSet.top+"px";
	document.body.appendChild(sDiv);
	var sSpan=document.createElement("span");
	var spanId=obj.options[obj.selectedIndex].value;
	var spanText=obj.options[obj.selectedIndex].text;
	sSpan.id=spanId;
	sSpan.style.lineHeight=offSet.height+"px";
	sTxt=document.createTextNode(spanText);
	sSpan.appendChild(sTxt);
	sDiv.appendChild(sSpan);
	sSpan.onclick=function(){
		if($Select("div"+obj.name,"ul").length==0){
			var sUl=document.createElement("ul");
			sDiv.appendChild(sUl);
			var optLen=obj.options.length;
			var tmp=document.createDocumentFragment();
			for(var j=0;j<optLen;j++){
				var sltVal=obj.options[j].value;
				var sltTxt=obj.options[j].text;
				var sLi=document.createElement("li");
				sLi.id=sltVal;
				sLi.appendChild(document.createTextNode(sltTxt));
				sLi.onmouseover=function(){
					this.style.background="#000000";
					this.style.color="#ffffff";
				}
				sLi.onmouseout=function(){
					this.style.background="#700707";
					this.style.color="d5aaaa";
				}
				sLi.onclick=function(){
					sSpan.innerHTML=this.innerHTML;
					obj.value=this.id;
					sUl.style.display="none";
				}
				tmp.appendChild(sLi);
			}
			sUl.appendChild(tmp);
//			if(optLen>3){
//				sUl.style.overflowY="scroll";
//				sUl.style.height="100px";
//			}
		}
		else{
			if($Select("div"+obj.name,"ul")[0].style.display=="none") $Select("div"+obj.name,"ul")[0].style.display="block";
			else $Select("div"+obj.name,"ul")[0].style.display="none";
		}
	}
}
//取得slect的个数，并且对每个select执行函数
function initSelect(){
	var slt=$Select(document,"select");
	var sltLen=slt.length;
	for(var i=0;i<sltLen;i++){
	    if(slt[i].id == "ctl00_CategoryBlock1_CategoryList" || slt[i].id =="CategoryBlock1_CategoryList") instSelect(slt[i]);
	}
}

window.onload=initSelect;
document.onclick=function(){
	var evt=getEvent();
	var element=evt.srcElement || evt.target;
	var s=$Select(document,"select");
	if((element.parentNode.parentNode==null||element.parentNode.parentNode.className!="divSlt")&&element.nodeName!="SPAN"){
		for (var i=0; i<s.length; i++) {
		    if(s[i].id == "ctl00_CategoryBlock1_CategoryList" || s[i].id =="CategoryBlock1_CategoryList")
		    {
		        if(!$Select("div" + s[i].name,"ul")[0]) continue;
			    $Select("div" + s[i].name,"ul")[0].style.display="none";
		    }
			
		}
	}
}
function getEvent(){
     //同时兼容ie和ff的写法
     if(document.all)    return window.event;
     func=getEvent.caller;
     while(func!=null){
         var arg0=func.arguments[0];
         if(arg0){
             if((arg0.constructor==Event || arg0.constructor ==MouseEvent)
                || (typeof(arg0)=="object" && arg0.preventDefault && arg0.stopPropagation)){    
                return arg0;
              }
         }
          func=func.caller;
        }
        return null;
}

