

// ------- autoAutomatic-------
var inputAuto = new Class({

    initialize: function(bao, form, input, btn, typeArr) {
        this.typeArr = typeArr;
        this.form = form;
        $(document.body).addEvent('click', this.leave.bindWithEvent(this));
        $('autoAutomatic').addEvent('click', function(e) { e.stopPropagation() } .bindWithEvent());
        $E('#searchBox input').addEvent('click', function(e) { e.stopPropagation() } .bindWithEvent());
        this.input = $(input).addEvent('focus', this.focus.bind(this)).addEvent('keyup', this.handEvent.bindWithEvent(this));
        this.bao = $(bao);
        this.matchCheck = false;
        this.position = -1;
    },

    handEvent: function(e) {
        this.curKey = e.code;
        if ([37, 39, 9, 6, 17, 18, 22].contains(this.curKey))
            return false;
        var value = this.input.value.trim();
        if (value == '') {
            //	this.bao.setStyle('display' , 'none') ;
            this.empty();
            return false;
        }

        if (this.curKey != 38 && this.curKey != 40 && this.curKey != 13) {
            this.cacheWord = value;
            this.matchCheck = false;
            //	this.bao.setStyle('display' , 'none').setHTML('') ;
            this.empty();
            var url = '/search.php?koko=1&k=' + encodeURIComponent(this.cacheWord);
            var callback = this.handEventBack.bind(this);
            var data = "";
            cacheAjax(url, callback);
            // Request.reSend(url,data,callback);
        }
        else
            this.handEventBack();
    },

    handEventBack: function(httpObj) {
        if (httpObj) {
            var ret = httpObj.responseText;
            var obj = Json.evaluate(ret);
            if (obj.length > 0 && this.input.value != '') {
                this.matchCheck = true;
                this.bao.setHTML('');
                for (var i = 0; i < obj.length; i++)
                    this.append(obj[i]);
            }
        }
        if (this.matchCheck) {
            // this.bao.setStyle('display' , 'block') ;
            this.display();
            if (this.curKey == 38 || this.curKey == 40 || this.curKey == 13)
                this.keyScroll();
            else
                this.position = -1;
        }
        else {
            if (this.curKey == 13)
                this.submit('blank');
        }
    },

    changeWord: function(str) {
        this.input.value = str;
    },

    checkPosition: function() {
        var aArr = this.bao.getElementsByTagName("a");
        for (var i = 0; i < aArr.length; i++) {
            if (aArr[i].className == 'autoMouseover') {
                aArr[i].className = 'autoMouseout';
                this.position = i;
                return;
            }
        }
    },

    keyScroll: function() {
        this.checkPosition();
        var aArr = this.bao.getElementsByTagName("a");
        var aLength = aArr.length;
        var kword = this.cacheWord;
        if (this.curKey == 40) {
            if (this.position >= 0)
                aArr[this.position].className = "autoMouseout";
            this.position = (this.position >= aLength - 1) ? -1 : (this.position + 1);
            if (this.position > -1) {
                aArr[this.position].className = "autoMouseover";
                kword = aArr[this.position].innerHTML;
            }
        }

        if (this.curKey == 38) {
            if (this.position >= 0)
                aArr[this.position].className = "autoMouseout";
            this.position = (this.position <= 0) ? ((this.position == 0) ? -1 : aLength - 1) : (this.position - 1);
            if (this.position > -1) {
                aArr[this.position].className = "autoMouseover";
                kword = aArr[this.position].innerHTML;
            }
        }

        if (this.curKey == 13) {
            this.empty();
            // this.bao.setStyle('display' , 'none') ;
            this.display();
            //if(this.position >= 0)
            //	this.input.value = liArr[this.position].innerHTML ;					
            this.submit('blank');

            return true;
        }
        this.changeWord(kword);
    },

    append: function(msg) {
        var fucObj = eval(this.fucName);
        if (!this.bao.getElementsByTagName("ul")[0]) {
            var ul = document.createElement("ul");
            this.bao.appendChild(ul);
        }
        ulEle = this.bao.getElementsByTagName("ul")[0];
        var li = document.createElement("li");
        li.innerHTML = '<a href="#">' + msg + '</a>';
        a = li.getElementsByTagName('a')[0];
        a.onmouseover = this.mouseover.bindWithEvent(this);
        a.onmouseout = this.mouseout.bindWithEvent(this);
        a.onclick = this.dclick.bindWithEvent(this);
        a.className = "autoMouseout";
        ulEle.appendChild(li);
    },

    dclick: function(e) {
        var ele = e.target;
        // this.bao.setStyle('display' , 'none') ;
        this.display();
        this.input.value = ele.innerHTML;
        this.submit();
        e.stop();
    },

    mouseover: function(e) {
        var ele = e.target;
        if (this.position >= 0)
            this.bao.getElementsByTagName("li")[this.position].className = "autoMouseout";
        this.position = -1;

        ele.className = "autoMouseover";
    },

    mouseout: function(e) {
        var ele = e.target;
        ele.className = "autoMouseout";
    },

    getType: function() {

        return this.typeArr.filter(function(e) { return e.checked })[0].value;
    },

    submit: function(style) {
        style = style || 'self'
        if (this.input.value.trim() == '') return false;
        var type = this.getType();
        var url = 'http://6.cn/search.php?t=' + type + '&k=' + encodeURIComponent(this.input.value) + '&ko=1';
		new Image().src = 'http://shrek.6.cn/api/pageViewCount.php?pagename=searchCount' ;
        if (style == 'self')
            window.location.href = url;
    },

    empty: function() {
        this.bao.addClass('displayNone').setHTML('');
    },

    display: function() {
        this.bao.removeClass('displayNone');
    },

    leave: function(e) {
        setTimeout(function() { this.empty(); $('autoAutomatic').addClass('displayNone'); } .bind(this), 200);
    },

    focus: function() {
        $('autoAutomatic').removeClass('displayNone');
    }
})




