j$ = jQuery;
window.searchUrlJson = {
	product : 'http://product.ch.gongchang.com/keyword.php?param=',
	buy : 'http://buy.ch.gongchang.com/keyword.php?param=',
	company : 'http://company.ch.gongchang.com/keyword.php?param=',
	factory :  'http://company.ch.gongchang.com/keyword.php?param=',
	local : 'http://product.ch.gongchang.com/keyword.php?param=',
	productShowd : 'http://product.ch.gongchang.com/', /** indexS.js 中的showd()方法中需要用到 **/
	buyShowd : 'http://buy.ch.gongchang.com/'		/** indexS.js 中的showd()方法中需要用到 **/
}
function searchIndex(btnid) {
	var kwh = {};
	if(btnid) {
		kwh = j$("#"+btnid).focus();
	} else {
		kwh = j$("#kwhead").focus();
	}
	var value = j$.trim(kwh.val());
	if(value != "") {
		var searchType = j$("#searchForm :submit").attr("id");
		var url = searchUrlJson[searchType];
		url += encodeURI(value);
		if(searchType == "local") {
			url += "&location="+encodeURI(j$("#kwlocal").val());
		} else if(searchType == "factory") {
			url += "&busmode=factory";
		}
		window.location = url;
	}
	return false;
}
function changeSearchForm(elem) {
	var searchType = j$(elem).parent().attr("id");
	var sf = j$("#searchForm");
	window.keywordhead = null;
	var kwh = j$("#kwhead");
	if(kwh.length == 1  && kwh.attr("class").indexOf("lightgray") < 0) {
		window.keywordhead = j$("#kwhead").val();
	}
	sf.html(searchFormJson[searchType]);
	window.setTimeout(function() {
		if(window.keywordhead) {
			sf.find("#kwhead").focus().val(window.keywordhead)
		}
	}, 2, sf)
}
function clearKeyWordText() {
	var _this = j$(this);
	var _class = _this.attr("class");
	var _i = _class.indexOf("lightgray");
	if(_i >= 0) {
		_this.val("");
		_this.removeClass("lightgray");
	}
}

function hide_hs_tip() {
	j$("#hs-tip").fadeOut(200)
}

j$(function() {
	window.userAddress = j$.trim(j$("#gc-local-ip").text()).split(";");
	window.userAddress = j$.trim(userAddress[userAddress.length-1]);
	window.userAddressU= encodeURI(userAddress);
	window.searchFormJson = {
		"hs_product" : '<span class="s-input"><input type="text" id="kwhead" /></span><span class="s-btn"><input type="submit" id="product" value="搜 索" /></span>',
		"hs_buy" : '<span class="s-input"><input type="text" id="kwhead" /></span><span class="s-btn"><input type="submit" id="buy" value="搜 索" /></span>',
		"hs_factory" : '<span class="s-input"><input type="text" id="kwhead" /></span><span class="s-btn"><input type="submit" id="factory"  value="搜 索" /></span>',
		"hs_company" : '<span class="s-input"><input type="text" id="kwhead" /></span><span class="s-btn"><input type="submit" id="company" value="搜 索" /></span>',
		"hs_local" : '<span class="s-input-local1"><input type="text" id="kwhead" /></span><span class="s-input-local2"><input type="text" id="kwlocal" value="'+userAddress+'" /></span><span class="s-btn"><input type="submit" id="local" value="搜 索" /></span>'
	};
	window.searchFormTipJson = {
		"hs_product" : '<p>输入关键词,所有的产品都可以搜索</p>',
		"hs_buy" : '<p>输入关键词,搜索采购信息</p>',
		"hs_factory" : '<p>所有的工厂都可以搜索</p>',
		"hs_company" : '<p>所有的公司都可以搜索</p>',
		"hs_local" : '<p>输入关键词,搜索本地产品</p>'
	}
	j$("#searchTypes span a").bind("click" , function(e) {
		window.clearTimeout(window.hstipTimer);

		changeSearchForm(this);
		
		j$("#searchTypes span").removeClass("select");
		var par = j$(this).parent().addClass("select");
		
		var tip = window.searchFormTipJson[par.attr("id")];
		j$("#hs-tip").fadeOut(200, function() {
			j$(this).empty().append(tip).fadeIn(200, function() {
				window.hstipTimer = window.setTimeout(function() {
					j$("#hs-tip").fadeOut(200);
				}, 5000);
			});
		});
		j$("#kwhead").focus().click(function() {
			j$("#hs-tip").fadeOut(200);
		});
		e.stopPropagation();
		j$("#kwhead").focus()
		return false;
	});
	if(window.location.href.search("#")<0) {
		j$("#kwhead").focus();
	}
	
})

