//头部搜索框字体颜色
function selectf(id){
document.getElementById("headsearch"+id+"").style.color = "#000000"
document.getElementById("headsearch"+id+"").style.background="#FEFFF1";
}
function selectf1(id){
document.getElementById("headsearch"+id+"").style.color = "#000000"
}
function changestyle(id,idcss){
document.getElementById(""+id+"").className = ""+idcss+""
}
function show(id){ 
	var childobj = document.getElementById(id);
		childobj.style.display = "block";
        document.getElementById('moreproduct').className="moreproduct1";
    }
function hidd(id){ 
	var childobj = document.getElementById(id);
		childobj.style.display = "none";
        document.getElementById('moreproduct').className="moreproduct";
    }
function chanoverbg(id,index){
document.getElementById(""+id+"").className = "cpborder "+index+""
}
function chanoutbg(id,index){
document.getElementById(""+id+"").className = "cpfllist xline "+index+""
}
function chanoutnobg(id,index){
document.getElementById(""+id+"").className = "cpfllist "+index+""
}
//选项标签等
function nTabs(thisObj,Num,Cname,Bname){
if(thisObj.className == ""+Cname+"")return;
var tabObj = thisObj.parentNode.id;
var tabList = document.getElementById(tabObj).getElementsByTagName("dt");
for(i=0; i <tabList.length; i++)
{
  if (i == Num)
  {
   thisObj.className = ""+Cname+""; 
      document.getElementById(tabObj+"_Content"+i).style.display = "block";
  }else{
   tabList[i].className = ""+Bname+""; 
   document.getElementById(tabObj+"_Content"+i).style.display = "none";
  }
} 
}
//选项标签等
function artlist(thisObj,Num){
if(thisObj.className == "lista")return;
var tabObj = thisObj.parentNode.id;
var tabList = document.getElementById(tabObj).getElementsByTagName("a");
for(i=0; i <tabList.length; i++)
{
  if (i == Num)
  {
   thisObj.className = "lista"; 
      document.getElementById(tabObj+"_List"+i).style.display = "block";
  }else{
   tabList[i].className = "listb"; 
   document.getElementById(tabObj+"_List"+i).style.display = "none";
  }
} 
}

//id,标题框[如"ul|li"]，内容框[如"ul|li"]，事件（onmouseover/onclick）,默认显示第几条[-1表示仅在鼠标移出全部隐藏,仅在onmouseover有效]
function tabs(id,hx,box,s,pr)
{	
	var thx=hx.split('|');
	var tbox=box.split('|');
	var hxs =document.getElementById(id).getElementsByTagName(thx[0]);
	var boxs=document.getElementById(id).getElementsByTagName(tbox[0]);
	var ss  =(!s)?"onmouseover":s;
	var prs  =(!pr)?0:pr;
	var taskid = false;
	var nc = -1;

	if(thx.length>1)
	{
		hxs =document.getElementById(id).getElementsByTagName(thx[0])[0].getElementsByTagName(thx[1]);
		if(thx[0]==tbox[0]){boxs=document.getElementById(id).getElementsByTagName(tbox[0])[1].getElementsByTagName(tbox[1]);}
		else{boxs=document.getElementById(id).getElementsByTagName(tbox[0])[0].getElementsByTagName(tbox[1]);}
	}

	for(var i=0;i<hxs.length;i++)
	{
		hxs[i].temp=i;
		hxs[i].old=hxs[i].className;
		hxs[i][ss]=function(){nc = this.temp; taskid = setTimeout(go_to,1000); }
		//if(prs==-1 && ss=="onmouseover"){hxs[i]["onmouseout"]=function(){go_to(-1);}}
		if(prs==-1 && ss=="onmouseover"){hxs[i]["onmouseout"]=function(){nc = -1; taskid = setTimeout(go_to,50);}}
		boxs[i].old=boxs[i].className;
	}

	function go_to(prs)
	{
		prs = (!prs)?nc:prs;
		if(taskid)window.clearInterval(taskid);
		for(var i=0;i<hxs.length;i++)
		{
			hxs[i].className=hxs[i].old;boxs[i].className=boxs[i].old;
		}
		if(prs!=-1){hxs[prs].className+="show";boxs[prs].className+="show";}
		
	}
	go_to(prs);
}

var Transfer = {}; 
Transfer.Base = function(){ }; 
Transfer.Base.prototype = { 
	setOptions: function(options){ 
	if (typeof options != "object") { 
		options = {}; 
	}; 
	this.options = { 
		bCache: options.bCache || false, 
		id: options.id || "scriptTemp", 
		onfailure: options.onfailure || 
		function(){ 
		}, 
		oncomplate: options.oncomplate || 
		function(){ 
		} 
	} 
} 
}; 
	var Class = { 
		create: function(){ 
			return function(){ 
				this.initialize.apply(this, arguments); 
			} 
		} 
}; 
Object.extend = function(destination, source){ 
for (var property in source) { 
destination[property] = source[property]; 
} 
return destination; 
}; 
Transfer.Request = Class.create(); 
Transfer.Request.prototype = Object.extend(new Transfer.Base(), { 
initialize: function(url, options){ 
this.setOptions(options); 
this.request(url); 
}, 
request: function(url){ 
this.url = url; 
this.bCache = this.options.bCache; 
this.id = this.options.id; 
this.oncomplate = this.options.oncomplate; 
this.onfailure = this.options.onfailure; 
this.symbol = "?"; 
if (this.url.indexOf("?") != "-1") 
this.symbol = "&"; 
var head = document.getElementsByTagName("head")[0]; 
var sT = document.getElementById(this.id); 
if (sT && sT.src && sT.src == this.url) { 
this.oncomplate(); 
return; 
} 
if (sT) { 
sT.parentNode.removeChild(sT); 
} 
var s = document.createElement("script"); 
head.appendChild(s); 
s.setAttribute("language", "javascript"); 
s.setAttribute("type", "text/javascript"); 
s.setAttribute("id", this.id); 
s.setAttribute("src", (this.bCache && this.bCache == true) ? this.url + this.symbol + Math.random() : this.url); 
var self = this; 
s.onload = s.onreadystatechange = function(){ 
if (typeof ActiveXObject != "undefined") { 
if (s.readyState && s.readyState == "loaded") 
self.oncomplate(); 
if (s.readyState && s.readyState == "complete") 
return; 
} 
else { 
self.oncomplate(); 
} 
 
} 
s.onerror = function(){ //ie not work 
s.parentNode.removeChild(s); 
self.onfailure(); 
throw new Error("some error occurd,please try again later"); 
} 
} 
});

String.prototype.trim = function() {
	return this.replace(/(^\s*)|(\s*$)/g, "");
}

function get( id ) {
	return document.getElementById(id);
}

function gets(id, tagName) {
	return get(id).getElementsByTagName(tagName);
}

function show( id ) {
	try {get(id).style.display="block";} catch(e) {}	
}

function hide( id ) {
	try {get(id).style.display="none";} catch(e) {}	
}

function sendSuggestRequest( value ) {
	if (value.trim() == "") return ;
	
	var url = 'http://completion.search.yesky.com/queryProduct.do?wordfield=' + value;
	new Transfer.Request(url, {oncomplate: function(){handSuggestReponse(q_result);}, id: 'scriptTest'}); 		
}

function clearCss() {
	var lis = gets("suggestionDiv", "li");
	for (var i = lis.length - 1; i >= 0; i--) {
			lis[i].style.backgroundColor = "";
	}
}

function onMouseOverItem(tag) {
	clearCss();
	tag.style.backgroundColor = "#D5E2FF";
}

function onMouseOutItem(tag) {
	tag.style.backgroundColor = "";
}

function onclickItem(tag) {
	setInputValue(function(){
		get("headsearch1").value = tag.childNodes[0].nodeValue;
	});
}

function resolvingResult( result ) {
	if (typeof(result) == 'undefined' || result == null || result.length == 0)
		return "";
	var content = ["<ul>"];
	for (var i = 0; i < result.length; i++) {
		content.push("<li onMouseOver='onMouseOverItem(this);' onMouseOut='onMouseOutItem(this);'  onclick='onclickItem(this);'  >", result[i], "</li>");
	}
	content.push("</ul>");

	return content.join("");
}

function handSuggestReponse( result ) {
	var content = resolvingResult(result);
	if (content == "") {
		get("suggestionDiv").innerHTML = "";
		hide("suggestionDiv");
		return false;
	}
	get("suggestionDiv").innerHTML = content;
	show("suggestionDiv");
	result = [];
}

function isHideOrEmpty( id ) {
	return get(id).style.display == "none" || 
		gets("suggestionDiv", "li").length == 0;
}

function isNoneSelect() {
	var lis = gets("suggestionDiv", "li");
	for (var i = lis.length - 1; i >= 0; i--) {
		if (lis[i].style.backgroundColor != ""){
			return false;
		}	
	}
	return true;
}

function setSuggestValue(tag, isAddClass){
	get("headsearch1").value = tag.childNodes[0].nodeValue;
	if (isAddClass) {
		tag.style.backgroundColor = "#D5E2FF";
	}
}

function upSelectItem() {
	var lis = gets("suggestionDiv", "li");
	if (isNoneSelect()) {
		setSuggestValue(lis[lis.length - 1], true);
	} else {
		for (var i = lis.length - 1; i >= 0; i--) {
			if ((lis[i].style.backgroundColor != null) && (lis[i].style.backgroundColor != "")) {
				if (i == 0) {
					get("headsearch1").value = get("suggestionDiv").getAttribute("oldValue");
				}else {
					setSuggestValue(lis[i - 1], true);
				}
				lis[i].style.backgroundColor = "";
				break;
			}
		}
	}
}

function downSelectItem() {
	var lis = gets("suggestionDiv", "li");
	if (isNoneSelect()) {
		setSuggestValue(lis[0], true);
	} else {
		for (var i = (lis.length - 1); i >= 0; i--) {
			if ((lis[i].style.backgroundColor != null) && (lis[i].style.backgroundColor != "")) {
				if (i == (lis.length - 1)) {
					get("headsearch1").value = get("suggestionDiv").getAttribute("oldValue");
				}else {
					setSuggestValue(lis[i + 1], true);
				}
				lis[i].style.backgroundColor = "";
				break;
			}
		}	
	}		
}

function reShowSuggestion( call ){
	if (isHideOrEmpty("suggestionDiv")) {
		show("suggestionDiv");
	} else {
		setInputValue(call);
	}
}

var upKeyCode = 38;
var downKeyCode = 40;
var escKeyCode = 27;
function onkeyupFun( e ) {
	var event = window.event || e;
	if (upKeyCode == event.keyCode) {
		reShowSuggestion(upSelectItem);
	} else if (downKeyCode == event.keyCode) {
		reShowSuggestion(downSelectItem);
	} else if (escKeyCode == event.keyCode) {
		isTrigger = false;
		if (!isHideOrEmpty("suggestionDiv")) {
			setTimeout(function() {
				isTrigger = true;
				setInputValue(function() {
					get("headsearch1").value =  (typeof (get("suggestionDiv").getAttribute("oldValue")) == 'undefined') ? "" : get("suggestionDiv").getAttribute("oldValue");
				});
			}, 10);
			clearCss();
		}else {
			setTimeout(function(){isTrigger = true;}, 10);
			get("suggestionDiv").innerHTML = "";
		}
		hide("suggestionDiv");
	}
}

//now the method is not support function that has parameters
function setInputValue( call ){
	isTrigger = false;
	call();
	isTrigger = true;
}

var isTrigger = true;
var isAutoSend = false;
function valueChange() {
	if (isAutoSend) {
		isAutoSend = false;
		return false;
	}
	if (!isTrigger) return false;
	  
	get("suggestionDiv").setAttribute("oldValue", get("headsearch1").value);
	sendSuggestRequest(get("headsearch1").value);
	return false;
}
function onInputBlur() {
	setTimeout(function(){hide("suggestionDiv")}, 100);
}

window.onload = function() {
	get("headsearch1").setAttribute("autocomplete", "off");
	get("headsearch1").onkeydown = onkeyupFun;
	get("headsearch1").onblur = onInputBlur;
	if ("\v" == "v") {
		isAutoSend = true;
		get("headsearch1").onpropertychange = valueChange;
	} else {
		get("headsearch1").addEventListener("input", valueChange, false);
	}
	return false;
}
