function ac_addevent(element, eventName, handle) {
    var eventName_ie = 'on' + eventName;
    if(element.addEventListener) {
        element.addEventListener(eventName, handle, false);
    }else if(element.attachEvent) {
        element.attachEvent(eventName_ie, handle);
    }else {
        var old = element[eventName_ie];
        element[eventName_ie] = function() {
            var o=old.apply(this,arguments),n=handle.apply(this,arguments);
            return o==undefined?n:(n==undefined?o:o&&n);
        }
    }
}
var ac_browser = function() {
    this.ua = navigator.userAgent.toLowerCase();
    with(this) {
        this.version = (ua.match( /.+(?:ox|it|ra|ie|on)[\/: ]([\d.]+)/ ) || [])[1];
        this.isOpera = /opera/.test(ua);
        this.isFirefox = /firefox/.test(ua);
        this.isSafari = /safari/.test(ua);
        this.isIE = /msie/.test(ua) && !isOpera && typeof(document.all)!='undefined';
        this.isIE6 = isIE && (/msie 6/).test(ua);
    }
};
var ac_opt = {
    input_id        :   'what',
    query_url       :   '/api/p.php?callback=ac_callback',
    delay           :   200,
    div_id          :   'ac_result_div',
    div_border      :   1,
    li_class        :   '',
    li_hover        :   '',
    show_loading    :   true,
    loading_class   :   '',
    btn_close_html  :   '',
    btn_close_image :   '/images/ac_close.jpg',
    version         :   '0.5'
};
function ac_callback(res) {
    if(ac_opt.show_loading) {
        ac_class(ac_s.o, ac_opt.loading_class, true);
    }
    if(res.status == 0 && res.result.length > 0) {
        var d = res.result;
        var ul = document.createElement('ul');
        ul.style.cssText = 'list-style:none;margin:0px;padding:2px;text-align:left;';
        var l, t;
        for(var i=0;i<d.length;i++) {
            t = d[i][0].replace(/&nbsp;/ig, ' ') + '('+d[i][3]+'ÌõÆÀÂÛ)';
            l = document.createElement('li');
            l.className = ac_opt.li_class;
            l.ac_data = d[i];
            l.ac_num = i;
            l.onmouseover = ac_mouseover;
            l.onmousemove = function() {
                if(ac_s.D) {ac_s.D=0;ac_mouseover.call(this);}
            }
            l.onmousedown = ac_mousedown;
            l.onclick = ac_click;
            l.appendChild(document.createTextNode(t));
            l.title = t;
            ul.appendChild(l);
        }
        var o = ac_create_div();
        ac_s.p = o.appendChild(ul).getElementsByTagName('li');
        ac_s.C = -1;
        if(ac_s.btn_close_html != '' || ac_s.btn_close_image!='') {
            var p = document.createElement('p');
            p.style.cursor = 'default';
            p.style.textAlign = 'right';
            p.style.width = 'auto';
            p.style.margin = '0';
            p.style.padding = '0 2px 2px 0';
            p.style.border = '0 none';
            p.onmousedown = ac_mousedown;

            var c;
            if(ac_opt.btn_close_image) {
                c = new Image();
                c.src = ac_opt.btn_close_image;
            } else {
                c = document.createElement('span');
                c.innerHTML = ac_opt.btn_close_html;
                c.style.fontSize = '10px';
                c.style.color = '#000';
            }
            c.style.cursor = 'pointer';
            c.onclick = function(){ac_close();};
            p.appendChild(c);
            o.appendChild(p);
        }
        ac_show();
    } else {
        ac_close(true);
    }
    ac_s.q = false;
}
function ac_click() {
    ac_s.o.blur();
    window.location.href = '/info/'+this.ac_data[2]+'/'+this.ac_data[4];
}
var ac_s = {A:0,B:null,C:-1,D:0,E:0,R:'',T:0,W:'',o:null,p:null,q:false};
var ac_init = function() {
    var o = document.getElementById(ac_opt.input_id);
    if(!o){return;}
    o.autocomplete = 'off';
    ac_addevent(o, 'blur', function(){ac_close(false);});
    ac_s.B = new ac_browser;
    if(ac_s.B.isOpera) {
        ac_addevent(o, 'keypress', ac_kd);
    } else {
        ac_addevent(o, 'keydown', ac_kd);
        if(ac_s.B.isIE) {
            ac_addevent(o, 'beforedeactivate', function(){
                if(ac_s.E){
                    window.event.cancelBubble=true;
                    window.event.returnValue=false;
                }
                ac_s.E=0;
            });
        }
    }
    ac_addevent(o, (ac_s.B.isOpera && ac_s.B.version >= 10) ? 'input' : 'keyup', ac_ku);
    ac_addevent(window, 'resize', ac_fix);
    ac_s.o = o;
    if(ac_opt.div_id) {
        if(!ac_opt.li_class) {
            ac_opt.li_class = 'ac_li';
            ac_addstyle('#' +ac_opt.div_id + ' .'+ac_opt.li_class, 'height:21px;line-height:21px;padding-left:1%;border:0 none;cursor:pointer;font-size:12px;color:#0227c7;overflow:hidden;white-space:nowrap;');
        }
        if(!ac_opt.li_hover) {
            ac_opt.li_hover = 'ac_li_hover';
            ac_addstyle('#' +ac_opt.div_id +' .'+ac_opt.li_hover, 'background-color:#eeeeee;');
        }
        if(ac_opt.show_loading && !ac_opt.loading_class) {
            ac_opt.loading_class = 'ac_loading';
            ac_addstyle('.' +ac_opt.loading_class, 'border:2px solid #7F9DB9;background:#FFFFFF url(/images/ac_loading.gif) no-repeat scroll right center !important;');
        }
    }
}
ac_addevent(window, 'load', ac_init);
function ac_kd(e) {
    var e = e || window.event;
    var k = ac_gk(e);
    if(k == 27 && ac_isshow()) {
        ac_s.o.value = ac_s.R;
        ac_close();
        e.cancelBubble=true;
        return e.returnValue=false;
    }
    if(k == 38 || k==40) {
        ac_s.A++;
        if(ac_s.A%3==1) {
            ac_roll(k);
        }
        return false;
    }
    k==13 && ac_isshow() && ac_close();
}
function ac_ku(e) {
    var k = ac_gk(e);
    if(ac_s.A==0 && !(k>=37&&k<=40) && k!=27) {
        ac_process(e);
    }
    ac_s.A=0;
}
function ac_process(e) {
    var e = e || window.event;
    var kc = ac_gk(e);
    var keyword = ac_filter(ac_s.o.value);
    if(!keyword || keyword == '') {
        ac_s.W = '';
        ac_set('', true);
        return false;
    }
    if(ac_s.W == keyword) {
        return false;
    }
    var now = (new Date()).getTime();
    if(ac_s.q==true || (ac_s.T > 0 && (now - ac_s.T) < ac_opt.delay)) {
        return false;
    }
    ac_s.T = now;
    ac_s.R = ac_s.W = keyword;
    if(ac_opt.show_loading) {
        ac_class(ac_s.o, ac_opt.loading_class);
    }
    ac_s.q = true;
    var where = '[0,0,31,' + (jQuery.cookie('LIFE_USER_CITY_ID')||2) +']';
    var cls = document.getElementById('search_class').value;
    if(cls > 0) {
        where += ',[0,32,63,'+cls+']';
    }
    keyword = encodeURIComponent(keyword);
    var query = ac_opt.query_url + '&where=[' + where + ']&query=' + keyword;
    var f = function(){ac_q(query, null, 'ac_script');};
    window.setTimeout(f, 10);
}
function ac_class(elem, className, remove) {
    if(remove) {
        var r = new RegExp('\s*'+ className, 'gi');
        elem.className = elem.className.replace(r, '');
    } else {
        elem.className += (elem.className ? " " : "") + className;
    }
}
function ac_roll(k) {
    if(!ac_s.p || ac_s.p.length<1){
        ac_close(true);
        return false;
    }
    if(!ac_isshow()) {
        ac_show();
        return false;
    }
    var len = ac_s.p.length;
    if(ac_s.C == -1) {
        ac_s.R = ac_s.o.value;
    } else {
        ac_class(ac_s.p[ac_s.C], ac_opt.li_hover ,true);
    }
    var t = k==38?ac_s.C-1:ac_s.C+1;
    if(t==len||t==-1) {
        ac_class(ac_s.p[ac_s.C], ac_opt.li_hover ,true);
        ac_s.C = -1;
        ac_set(ac_s.R);
        ac_s.o.focus();
        return false;
    } else if (t>len) {
        t = 0;
    } else if(t<-1) {
        t = len-1;
    }
    ac_s.C = t;
    ac_class(ac_s.p[t], ac_opt.li_hover);
    ac_set(ac_s.p[t].ac_data[0]);

    return false;
}
function ac_mouseover() {
    if(ac_s.C > -1) {
        ac_class(ac_s.p[ac_s.C], ac_opt.li_hover, true);
    }
    ac_class(this, ac_opt.li_hover);
    ac_s.C = this.ac_num;
}
function ac_mousedown(e) {
    ac_s.E = 1;
    if(!ac_s.B.isIE) {
        e.stopPropagation();
    }
    return false;
}
function ac_create_div() {
    if(!document.getElementById(ac_opt.div_id)) {
        var div = document.createElement('div');
        div.id = ac_opt.div_id;
        div.style.position = 'absolute';
        div.style.zIndex = 100001;
        div.style.border = parseInt(ac_opt.div_border) + 'px solid #CDCDCD';
        div.style.padding = '0';
        div.style.backgroundColor = '#FFFFFF';
        div.style.visibility = 'hidden';
        document.body.appendChild(div);
        ac_fix();
        if(ac_s.B.isIE6) {
            var fr = document.createElement('iframe');
            fr.id = ac_opt.div_id + "_frame";
            fr.frameBorder = '0';
            fr.style.display = 'block';
            fr.style.zIndex = 100000;
            fr.style.position = 'absolute';
            fr.style.border = '0 none';
            fr.style.backgroundColor = '#FFFFFF';
            document.body.appendChild(fr);
        }
        return div;
    }
    document.getElementById(ac_opt.div_id).innerHTML = '';
    return document.getElementById(ac_opt.div_id);
}
function ac_q(url, callback, sid) {
    var callback = callback || null;
    var id = sid || '';
    if(document.getElementById(id)) {
        document.getElementById(id).parentNode.removeChild(document.getElementById(id));
    }
    var head = document.getElementsByTagName('head')[0];
    var script = document.createElement('script');
    if(id!='') script.id = id;
    script.type = 'text/javascript';
    script.charset = 'gbk';
    if(typeof(callback) == 'function') {
        script.onload = script.onreadystatechange = function () {
            if (this.readyState == undefined || this.readyState == "loaded" || this.readyState == 'complete') callback();
        }
    }
    script.src = url;
    head.appendChild(script);
}
function ac_gk(e) {
    return e ? (e.keyCode||e.which||e.charCode) : window.event.keyCode;
}
function ac_addstyle(n, v) {
    if(!document.getElementById('ac_style')) {
        var s = document.createElement("style");
        s.id = 'ac_style';
        s.type = 'text/css';
        document.getElementsByTagName("head")[0].appendChild(s);
    } else {
        var s = document.getElementById('ac_style');
    }
    var t = null;
    if(t = s.sheet) {
        var f = function(a,b) {
            var c = a +" { " + b + " }";
            t.insertRule(c,t.cssRules.length);
        };
    }else if (t = s.styleSheet) {
        var f = function(a,b) {
            t.addRule(a,b);
        };
    }else{
        return false;
    }
    f(n,v);
}
function ac_fix() {
    var o = document.getElementById(ac_opt.div_id) || null;
    if(o && ac_s.o) {
        var pos = ac_findpos(ac_s.o);
        o.style.width = pos.oW - (ac_s.B.isIE && ac_getMode()=='BackCompat' ? 0 : 2*parseInt(ac_opt.div_border)) + 'px';
        o.style.left = pos.left + 'px';
        o.style.top = pos.top + pos.oH + 'px';
        if(document.getElementById(ac_opt.div_id + '_frame')) {
            var fr = document.getElementById(ac_opt.div_id + '_frame');
            fr.style.width = pos.oW + 'px';
            fr.style.height = o.offsetHeight + 'px';
            fr.style.left = o.style.left;
            fr.style.top = o.style.top;
        }
    }
}
function ac_set(t, clear) {
    var v = ac_filter(t);
    ac_s.o.value = v;
    clear && ac_close(true);
}
function ac_gc(name) {
    var tmp,reg=new RegExp('(^| )'+name+'=([^;]*)(;|$)','gi');
    return (tmp=reg.exec(document.cookie)) ? tmp[2] : null;
}
function ac_filter(d) {
    var a = d.replace(/^\s+|\s+$/g, '');
    for(var b=0,c="",e="\n\r";b<a.length;b++) {
        if(e.indexOf(a.charAt(b))==-1)c+=a.charAt(b);
        else c+=" ";
    }
    return c;
}
function ac_isshow() {
    return (document.getElementById(ac_opt.div_id) && document.getElementById(ac_opt.div_id).style.visibility != 'hidden');
}
function ac_show() {
    var o = document.getElementById(ac_opt.div_id) || null;
    if(o&&o.innerHTML!='') {
        ac_fix();
        o.style.visibility = 'visible';
        if(ac_s.B.isIE6) {
            document.getElementById(ac_opt.div_id + '_frame').style.visibility = 'visible';
        }
        ac_s.D = 1;
    }
}
function ac_close(clear) {
    var o = document.getElementById(ac_opt.div_id);
    if(o) {
        if(ac_opt.show_loading) {
            ac_class(ac_s.o, ac_opt.loading_class, true);
        }
        if(clear==true) {
            o.innerHTML = '';
        }
        o.style.visibility = 'hidden';
        if(ac_s.B.isIE6) {
            document.getElementById(ac_opt.div_id + '_frame').style.visibility = 'hidden';
        }
    }
}
function ac_findpos(obj) {
    var pos = {top:0, left:0, oW:0, oH:0};
    pos.oW = obj.offsetWidth;
    pos.oH = obj.offsetHeight;
    var i = 0;
    var flag = false;
    while(obj) {
        /*
        if((ac_s.B.isSafari && obj.nodeName == 'TABLE') || (ac_getMode()!='BackCompat' && i>0 && ac_s.B.isIE && (obj.nodeName != 'TD' && obj.nodeName != 'TABLE'))) {
            flag = true;
        }
        if(flag == true) {
            pos.top += ac_getCurrentPixelValue(obj, 'borderTopWidth');
            pos.left += ac_getCurrentPixelValue(obj, 'borderLeftWidth');
            flag = false;
        }
        */
        pos.top += Math.abs(obj.offsetTop);
        pos.left += Math.abs(obj.offsetLeft);
        obj = obj.offsetParent;
        i++;
    }
    if(ac_s.B.isSafari) {
        pos.top += ac_getCurrentPixelValue(document.body, 'borderTopWidth');
        pos.left += ac_getCurrentPixelValue(document.body, 'borderLeftWidth');
    }
    return pos;
}
function ac_getMode() {
    return (document.compatMode || null);
}
function ac_getCurrentPixelValue(obj, prop) {
    return getPixelValue(obj, getCurrentStyle(obj, prop));
}
function getCurrentStyle(obj, prop) {
    if (obj.currentStyle) {
        return obj.currentStyle[prop];
    } else if (document.defaultView && document.defaultView.getComputedStyle) {
        //return document.defaultView.getComputedStyle(obj, null).getPropertyValue(prop.replace(/([A-Z])/g, "-$1").toLowerCase());
        return document.defaultView.getComputedStyle(obj, null)[prop];
    } else if (window.getComputedStyle) {
        return window.getComputedStyle(obj, null)[prop];
    }
    return null;
}
function getPixelValue(element, value) {
    var m = value.match(/^(\d+)(\w+)?$/);
    if(m == null) {
        try{
            var b = {thin:2,medium:4,thick:6};
            return eval('b.'+value);
        }catch(e){
            return 0;
        }
    }
    if(m && (m[1] == 0 || (m[2] && m[2].toLowerCase() == 'px'))) {
        return parseInt(m[1]);
    }
    var style = element.style.left;
    var runtimeStyle = element.runtimeStyle.left;
    element.runtimeStyle.left = element.currentStyle.left;
    element.style.left = value || 0;
    value = element.style.pixelLeft;
    element.style.left = style;
    element.runtimeStyle.left = runtimeStyle;
    return value;
}
