/********************************************
Basis of dwsFramework , Writen by CNLei@dws
*********************************************/
Array.prototype.remove = function(dx) {
    if(isNaN(dx) || dx>this.length){return false;}
    this.splice(dx,1);
}
String.prototype.getNumber = function() {return this.replace(/[^\d]/g,""); }
String.prototype.replaceAll = stringReplaceAll;


function stringReplaceAll(AFindText,ARepText){
 var raRegExp = new RegExp(AFindText.replace(/([\(\)\[\]\{\}\^\$\+\-\*\?\.\"\'\|\/\\])/g,"\\$1"),"ig");
 return this.replace(raRegExp,ARepText);
}

var dwsFw = {};

var dwsBrowser = {};
dwsBrowser.agt = navigator.userAgent.toLowerCase();
dwsBrowser.isW3C = document.getElementById ? true:false;
dwsBrowser.isIE = ((dwsBrowser.agt.indexOf("msie") != -1) && (dwsBrowser.agt.indexOf("opera") == -1) && (dwsBrowser.agt.indexOf("omniweb") == -1));
dwsBrowser.isNS6 = dwsBrowser.isW3C && (navigator.appName=="Netscape") ;
dwsBrowser.isOpera = dwsBrowser.agt.indexOf("opera") != -1;
dwsBrowser.isGecko = dwsBrowser.agt.indexOf("gecko") != -1;
dwsBrowser.ieTrueBody  =function (){
	return (document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body;
}

var dwsElement={
  addClassName: function(element, className) {
    if (!(element = $(element))) return;
	dwsElement.delClassName(element, className);
	element.className=element.className+" "+className;
  },
  delClassName: function(element, className) {
    if (!(element = $(element))) return;
	var arrClssOld=element.className.replaceAll("  "," ");
	arrClssOld=arrClssOld.split(" ");
	for (var i=arrClssOld.length-1;i>=0;i--){
	 if (className==arrClssOld[i]){
	 arrClssOld.remove(i);
	 }
	}
	element.className=arrClssOld.join(" ");
  },
	isChild		: function(cNode,pNode){
		while(cNode!=null){
			cNode=cNode.parentNode;
			if(cNode==pNode) return true; 
		}
		return false;
	},
	getChild	: function(oChild,oParent){
		oParent.appendChild(oChild);
	},
	rePos	: function(obj,nLt,nTop){
		var o=$(obj);
		o.style.left	= nLt + "px";
		o.style.top		= nTop + "px";
	},
	getXYWH	: function(o){
		var nLt=0;
		var nTp=0;
		var offsetParent = o;
		while (offsetParent!=null && offsetParent!=document.body) {
			nLt+=offsetParent.offsetLeft;
			nTp+=offsetParent.offsetTop;
			if(dwsBrowser.isIE){
				parseInt(offsetParent.currentStyle.borderLeftWidth)>0?nLt+=parseInt(offsetParent.currentStyle.borderLeftWidth):"";
				parseInt(offsetParent.currentStyle.borderTopWidth)>0?nTp+=parseInt(offsetParent.currentStyle.borderTopWidth):"";
			}
			offsetParent=offsetParent.offsetParent;
		}
		return {"X":nLt, "Y":nTp, "W":o.offsetWidth, "H":o.offsetHeight}
	}
}

var dwsPosition={
	rePos	: function(obj,nLt,nTop){
		var o=$(obj);
		o.style.left	= nLt + "px";
		o.style.top		= nTop + "px";
	}
}

function $(element) {
	return document.getElementById(element)?document.getElementById(element):element;
}

document.getElementsByClassName = function(className,oBox) {
	 this.d= oBox || document;
	 var children = this.d.getElementsByTagName('*') || document.all;
	 var elements = new Array();
	 for (var i = 0; i < children.length; i++) {
		 var child = children[i];
		 var classNames = child.className.split(' ');
		 for (var j = 0; j < classNames.length; j++) {
			 if (classNames[j] == className) {
				 elements.push(child);
				 break;
			 }
		 }
	 }
	 return elements;
}

$Cls = document.getElementsByClassName ;

var dwsTAB = {
	idSuffix		: "_Tabs",
	tabTagName		: "li",
	activeClassName	: "active",
	waitInterval	: null,
	delayTime		: 30,
	tempTAB			: null
}

dwsTAB.init = function(){
	for(var i=0;i<arguments.length;i++) {
			var oTabBox=document.getElementById(arguments[i]+dwsTAB.idSuffix);
			var arrTabs=oTabBox.getElementsByTagName(dwsTAB.tabTagName);	
			var oCurrTAB=null;
			for(var j=0;j<arrTabs.length;j++) {
				oCurrTAB=arrTabs[j];
				if(oCurrTAB.parentNode!=oTabBox) continue;
				var oTabLink=oCurrTAB.getElementsByTagName("a")[0];
				var sDataStaut=oTabLink.getAttribute("rel");
				oCurrTAB.setActive=function(bactive){
					var oDataId = this.getElementsByTagName("a")[0].getAttribute("urn");
					if(bactive){
						this.status="active";
						dwsElement.addClassName(this,dwsTAB.activeClassName);
						dwsElement.delClassName($(oDataId),"hidden");
					}else{
						this.status="normal";
						dwsElement.delClassName(this,dwsTAB.activeClassName);
						dwsElement.addClassName($(oDataId),"hidden");
					}
				}
				oCurrTAB.getTabData=function(){
					this.setActive(true);
					this.parentNode.activetab.setActive(false);
					this.parentNode.activetab=this;					
					var oTabLink=this.getElementsByTagName("a")[0];
				}
				oCurrTAB.onmouseover=function(aEvent){
					var myEvent = window.event ? window.event : aEvent;
					var fm=myEvent.fromElement;
					if(dwsElement.isChild(fm,this) || fm==this) return;
					if(this.status=="active") return;
					dwsTAB.tempTAB=this;
					clearTimeout(dwsTAB.waitInterval);
					dwsTAB.waitInterval=window.setTimeout("dwsTAB.tempTAB.getTabData();",dwsTAB.delayTime);
				}
				oCurrTAB.onmouseout=function(aEvent){
					var myEvent = window.event ? window.event : aEvent;
					var em=myEvent.toElement;
					if(dwsElement.isChild(em,this) || em==this) return;
					if(this.status=="active") return;
					clearTimeout(dwsTAB.waitInterval);
				}
				if(sDataStaut.indexOf("#default")!=-1){
					oCurrTAB.setActive(true);
					oTabBox.activetab=oCurrTAB;
				} else {
					oCurrTAB.setActive(false);
				}

			}
		if(oTabBox.activetab==null) oTabBox.activetab=arrTabs[0];
	}
}

var dwsSelMenu = {
	currMenu	: null,
	waitInterval	: null,
	delayTime		: 30,
	newMenu			: null,
	show	: function(o){
		dwsSelMenu.newMenu = o;
		o.onmouseout = function(){
			dwsSelMenu.newMenu = null;
		}
		setTimeout("dwsSelMenu._doShow()",100);
	},
	_doShow	: function() {
		if (dwsSelMenu.newMenu) {
			var sSelId = dwsSelMenu.newMenu.getElementsByTagName("a")[0].getAttribute("urn");
			dwsSelMenu.setCurrMenu(dwsSelMenu.newMenu,$(sSelId));
			dwsSelMenu.newMenu = null;
		}
	},
	close	: function(){
		var gBox = $("gPosBox");
		gBox.style.visibility = "hidden";
	},
	setCurrMenu	: function(o,oNewMenu){
		var nPos = dwsElement.getXYWH(o);
		var gBox = $("gPosBox");
		if (dwsSelMenu.currMenu) {
			var oBox = $("gHiddenBox");
			dwsElement.getChild(dwsSelMenu.currMenu,oBox);
		}
		gBox.style.visibility = "visible";
		dwsPosition.rePos(gBox,nPos.X,nPos.Y+5);
		dwsElement.getChild(oNewMenu,gBox);
		dwsSelMenu.currMenu= oNewMenu;
		oNewMenu.getElementsByTagName("a")[0].innerHTML=o.getElementsByTagName("a")[0].innerHTML;
		gBox.onmouseover = function(){
			clearTimeout(dwsSelMenu.waitInterval);
		}
		gBox.onmouseout = function(){
			clearTimeout(dwsSelMenu.waitInterval);
			dwsSelMenu.waitInterval=window.setTimeout("dwsSelMenu.close();",dwsSelMenu.delayTime);
		}
	}
}

dwsSelMenu.chose = function (o,t){
	var oLink = o;
/*
	if($("type")){
	}else{
		var itype = document.createElement("input");
			itype.id=itype.name="type";
		
		$("selMenuB").parentNode.appendChild(itype);
		alert($("type").value);
	}
		if(oSubMenu.getAttribute("rel")==0)
			oInput2.parentNode.removeChild(oInput2);
		else
		alert(oInput2.value);
*/
	if (t=="1") {
		var oChoseSubMenu1 = o.parentNode.parentNode.getElementsByTagName("a")[0];
		var oSubMenu = $(o.getAttribute("urn")).getElementsByTagName("a")[0];
		var oSelMenuA = $("selMenuA").getElementsByTagName("a")[0];
		var oSelMenuB = $("selMenuB").getElementsByTagName("a")[0];
		var oInput = $("tid");
		var oInput2 = $("type");
		oChoseSubMenu1.innerHTML = oSelMenuA.innerHTML = o.innerHTML;
		oInput.value = o.getAttribute("rel");
		oSelMenuB.setAttribute("urn",o.getAttribute("urn"));
		oSelMenuB.innerHTML = oSubMenu.innerHTML;
		oInput2.value = oSubMenu.getAttribute("rel");
	} else {
		var oSelMenuB = $("selMenuB").getElementsByTagName("a")[0];
		var oInput2 = $("type");
		var oChoseSubMenu2 = o.parentNode.parentNode.getElementsByTagName("a")[0];
		oInput2.value = o.getAttribute("rel");
		oSelMenuB.innerHTML = oChoseSubMenu2.innerHTML=o.innerHTML;
		oChoseSubMenu2.setAttribute("urn",o.parentNode.parentNode.id);
		oChoseSubMenu2.setAttribute("rel",o.getAttribute("rel"));
	}
	//$("soKeyInp").value=$("bigCateInp").value+":"+$("type").value; //Debug: 观察修改后参数值
	dwsSelMenu.close();
}

var dwsSearch = {
	hoverBtn	: function(o,s){
		dwsElement.addClassName(o,s);
		o.onmouseout = function(){
			dwsElement.delClassName(this,s);
		}
	},
	doInput	: function(o){
		if (o.value==o.defaultValue) {
			o.value="";
		}
		o.onblur = function(){
			o.value==""?o.value=o.defaultValue:"";
		}
	}
}

dwsFw.setHome = function(o,s){
	if (dwsBrowser.isIE) {
		o.style.behavior='url(#default#homepage)';
		o.setHomePage(s);
		return(false);
	}
}
dwsFw.addFavUrl = function(title,url) {
	if (window.sidebar) { 
		window.sidebar.addPanel(title, url,""); 
	} else if( dwsBrowser.isIE ) {
		window.external.AddFavorite( url, title);
	} else if(dwsBrowser.isOpera && window.print ) {
		  var elem = document.createElement('a');
		  elem.setAttribute('href',url);
		  elem.setAttribute('title',title);
		  elem.setAttribute('rel','sidebar');
		  elem.click();
	}
}

var oldBoxNo=0;
function nextFace(sParentId,sSubBoxTag,n){
var arrFaceBox=$(sParentId).getElementsByTagName(sSubBoxTag);
var nNewBoxNo=oldBoxNo+n;
nNewBoxNo<0?nNewBoxNo=(arrFaceBox.length-1):"";
nNewBoxNo>=arrFaceBox.length?nNewBoxNo=0:"";
for (var i=0;i<arrFaceBox.length;i++ ) {
 arrFaceBox[i].style.display=i==nNewBoxNo?"":"none";/**/
}
oldBoxNo=nNewBoxNo;
}

var dwsPopMsg = {};
dwsPopMsg.show = function(o,sId,nOffX,nOffY){
this.offX = nOffX || 0;
this.offY = nOffY || 0;
var oBox = $(sId);
var nPos = dwsElement.getXYWH(o);
var nLt,nTop;
nLt = nPos.X + (nPos.W/2) + this.offX;
nTop = nPos.Y + this.offY;
dwsElement.rePos(oBox,nLt,nTop);
dwsElement.delClassName(oBox,"hidden");
//alert(nPos.X);
}
dwsPopMsg.close = function(sId){
var oBox = $(sId);
dwsElement.addClassName(oBox,"hidden");
}

function goto(){
	if(document.getElementById('tid').value>10){
		document.all.formm.method='post';
		document.all.formm.action='http://so.kuaiche.com/search?t='+document.getElementById('tid').value.substr(1,1);
	}else{
		document.all.formm.method='get'; 
		document.all.formm.action='http://se.kuaiche.com/search'
	}
}
