/**
* @author chuangui.xiecg
* @version 2009-06-10
*/
/*import External file*/
var WidgetGet=function(){
var nidx = 0;
var _node = function(nodeType,attributes,win){
var w = win || window, d=w.document, n=d.createElement(nodeType);
for (var i in attributes) {
if (attributes[i]) {
n.setAttribute(i, attributes[i]);
}
}
return n;
};
var _scriptNode = function(url,win,charset){
var c = charset || "gbk";
return _node("script",{
"id":"alibaba_get_"+(nidx++),
"type":"text/javascript",
"charset": c,
"src":url
},win);
};
var _linkNode = function(url, win, charset) {
var c = charset || "gbk";
return _node("link", {
"id":      "alibaba_get_" + (nidx++),
"type":    "text/css",
"charset": c,
"rel":     "stylesheet",
"href":    url
}, win);
};
var _track=function(type,url,fn,scope){
var w=scope||window, d=w.document, h=d.getElementsByTagName("head")[0], n;
n = type=="script"?_scriptNode(url,w,"gbk"):_linkNode(url,w,"gbk");
h.appendChild(n);
if(document.all&&!document.opera){
n.onreadystatechange = function(){
var rs = this.readyState;
if("loaded" === rs || "complete" === rs){
fn();
}
}
}else{
n.onload = function(){
fn();
}
}
};
return {
script:function(url,fn,scope){
_track("script",url,fn,scope);
},
css:function(url,fn,scope){
_track("css",url,fn,scope);
}
}
}();
/**
* @fileoverview 热门词推荐
* @desc 可以做到随机调用关键词,人为扩展数据接口,人为扩展回调函数,页面类型分析
* @author chuangui.xiecg<chuangui.xiecg@alibaba-inc.com>
* @version 1.0
*/
/**
* <p>Usage: HotKeywordsSuggest('my-popwindow',{});</p>
* @class HotKeywordsSuggest
* @constructor
* @param {String} container 要替换值的标签ID
* @param {Object} config 配置参数对象
*			{String}   config.url 修改原来的数据接口
*			{Boolean}  config.isAutoMatch 是否采用分析url的方式来定义页面类型
*			{String}   config.type 人工指定页面类型,优先级高于config.isAutoMatch,即已经设置config.type,在设置config.isAutoMatch为true,那么最后的类型还是以config.type为准
*          {Function} config.callback 回调函数,即会覆盖原来默认的执行函数,它的参数类型是数组
*          {Int}	   config.idx 人为指定调用数组中的第几个数据,从1开始，以此类推,默认采用随机的方式
*
*/
var HotKeywordsSuggest=function(container,config){
var t = this;
if(!(t instanceof HotKeywordsSuggest)){
return new HotKeywordsSuggest(container,config);
}else{
t._init.apply(this,arguments);
return t;
}
}
HotKeywordsSuggest.config={
url:'http://view.china.alibaba.com/cms/shichang/interface/hot_keywords_suggest.html?iframe_delete=true',	/*数据接口地址*/
regExp:{														/*投放的页面范围*/
newsRegs:[/^http:\/\/info.china.alibaba.com/i],
clubRegs:[/^http:\/\/club.china.alibaba.com/i],
blogRegs:[/^http:\/\/blog.china.alibaba.com/i],
quanRegs:[/^http:\/\/q.china.alibaba.com/i]
},
range:['news','club','blog','quan','price','video','help','pics','fship','service','renmaisearch']							/*返回数据的类型*/
}
HotKeywordsSuggest.prototype={
_init:function(container,config){
var that = this;
if(!container||!that.$(container)){return;}
var _url = config.url || HotKeywordsSuggest.config.url;
if(_url.indexOf("?")!=-1){
_url+="&t="+that.getTime();
}else{
_url+="?t="+that.getTime();
}
that.type="";
if(config.type&&HotKeywordsSuggest.config.range.join("|").indexOf(config.type)!=-1){
that.type = config.type;
}else if(config.isAutoMatch){
that.type = that.getMatchKeywords()||'news';
}
window.HotKeywordsSuggest.type = that.type;
WidgetGet.script(_url,function(){
that.callback(container,config);
},window);
},
$:function(id){
return document.getElementById(id);
},
getTime:function(){
return +new Date();
},
getMatchKeywords:function(){
var url = window.location.href;
try{
for(var i in HotKeywordsSuggest.config.regExp){
for(var j=0;j<HotKeywordsSuggest.config.regExp[i].length;j++){
if(HotKeywordsSuggest.config.regExp[i][j].test(url)){
return i.substring(0,(i.length-4)).toLowerCase();
}
}
}
}catch(e){}
return '';
},
/**
* 回调函数,依据给定的初始参数来显示规定的数据,同时还可以绑定下拉框的事件
*/
callback:function(container,config){
if(!_suggestHotKeywords_){return;};
var data = _suggestHotKeywords_.result[HotKeywordsSuggest.type];
if(typeof config.callback=='function'){
config.callback(data,container,config);
return;
}
var idx = parseInt(Math.random()*(data.length));
if(config.idx&&data.length>parseInt(config.idx)){
idx = parseInt(config.idx)>=1?(parseInt(config.idx)-1):0;
}
if(document.getElementById(container).tagName.toLowerCase()=='input'){
document.getElementById(container).value = data[idx];
document.getElementById(container).setAttribute("dv",data[idx]);
var oldInputEvent = document.getElementById(container).onfocus;
document.getElementById(container).onfocus = function(){
try{
oldInputEvent.call(document.getElementById(container));}
catch(e){}
if(this.getAttribute("dv")==this.value){
this.value="";
//this.select();/*选择所有文字,保留*/
}
}
}else{
return;
}
if(config.selectID&&document.getElementById(config.selectID)){
var oldChgEvent = document.getElementById(config.selectID).onchange;
document.getElementById(config.selectID).onchange=function(){
oldChgEvent && oldChgEvent.call(document.getElementById(config.selectID));
var isFound = false;
for(var i=0;i<HotKeywordsSuggest.config.range.length;i++){
if(this.value.indexOf(HotKeywordsSuggest.config.range[i])!=-1){
try{
var v = _suggestHotKeywords_.result[(HotKeywordsSuggest.config.range[i])][idx]?_suggestHotKeywords_.result[(HotKeywordsSuggest.config.range[i])][idx]:_suggestHotKeywords_.result[(HotKeywordsSuggest.config.range[i])][0];
document.getElementById(container).value = v;
document.getElementById(container).setAttribute("dv",v);
isFound = true;
}catch(e){
}
}
}
if(!isFound){
document.getElementById(container).value = "请输入关键字";
}
}
}
}
}
