﻿var box;
var boxValue;
var newTd = 0;
var trValue;
$(document).ready(function() {
    box = $('#key');
    box.bind('keyup', function(e) {
        if (box.val().length > 1) {
            var key = window.event ? event.keyCode : e.which;
            if (boxValue != box.val() && key != 40 && key != 38) {

                AjaxData(box.val());
                boxValue = box.val();
            }

            if (key == 40) {
                if (newTd < 6 && newTd >= 0) {
                    newTd++;
                    var tdBox = document.getElementById('ac_odd' + newTd);
                    tdBox.className = "ac_over";

                    if (newTd > 1) {
                        var newj = newTd - 1;
                        var tdBoxj = document.getElementById('ac_odd' + newj);
                        tdBoxj.className = "ac_odd";
                    }
                }
                var classObj = document.getElementById('ac_odd' + newTd);
                 document.getElementById("key").value = classObj.title.replace(/<[^>]*>/gi, "");

            }
            else if (key == 38) {
                if (newTd <= 7 && newTd > 1) {
                    newTd--;
                    var tdBoxj = document.getElementById('ac_odd' + newTd);
                    tdBoxj.className = "ac_over";

                    var newA = newTd + 1;
                    var tdBoxA = document.getElementById('ac_odd' + newA);
                    tdBoxA.className = "ac_odd";


                }
                var classObj = document.getElementById('ac_odd' + newTd);
                document.getElementById("key").value = classObj.title.replace(/<.+?>/gi, "");

            }
            else if (key == 13) {
                gosearchdefault();
            }


        }
        else {

            $("#ac_results").remove();
            boxValue = "";
        }

    })

    // box.bind('blur', function() {
    //$("#ac_results2").remove();
    // });




})


function removesex()
{
    $("#ac_results").remove();
}

function AjaxData() {
    newTd = 0;
//    if (isNumber(box.val())) {
        $.ajax({
            url: '/getsearchdata.ashx?type=1&dt='+new Date()+'&searchstr=' + escape(box.val()),
            type: 'POST',
            async: true,
            timeout: 1000,
            error: function(XmlHttpRequest) {

            },
            success: function(data) {
                //                               alert(data);
                if (data == "无数据") {
                    $("#shDiv").remove();
                }
                else {
                    $("#shDiv").remove();
                    var div = document.createElement("div");
                    div.setAttribute("id", "shDiv")
                    div.style.position = "absolute";
                    div.style.zIndex = 10000;
                    document.body.appendChild(div);
                    div.innerHTML = data;
                    div.style.width = box.width() + 8 + "px";
                    var offsetObj = box.offset();
                    div.style.top = offsetObj.top + box.height()+ 3+ "px";
                    div.style.left = offsetObj.left + "px";


                }
            }
        });

//    } 
//    else {
//        $.ajax({
//            url: '/getsearchdata.ashx?type=2&searchstr=' + escape(box.val()),
//            type: 'POST',
//            async: true,
//            timeout: 1000,
//            error: function(XmlHttpRequest) { },
//            success: function(data) {
//                if (data == "无数据") {
//                    $("#shDiv").remove();
//                }
//                else {
//                    var div = document.createElement("div");
//                    div.setAttribute("id", "shDiv")
//                    div.style.position = "absolute";
//                    div.style.zIndex = 10000;
//                    document.body.appendChild(div);
//                    div.innerHTML = data;
//                    div.style.width = box.width() + 8 + "px";
//                    var offsetObj = box.offset();
//                    div.style.top = offsetObj.top + box.height()+3  + "px";
//                    div.style.left = offsetObj.left + "px";
//                }
//            }
//        });
//    }

}

function jmove(obj) {
    obj.className = 'ac_over';
    newTd = obj.id.substring(6);
}

function jout(obj) {
    obj.className = 'ac_odd';
}

function jclick(obj) {
    var linObj = document.getElementById("ac_link" + newTd);
    linObj.click();
  //  $("#key").val(obj.innerHTML.replace(/<.+?>/gi, ""));
    
  //  gosearchdefault();
}

function isNumber(name)  //数值检测 
{
    if (name.length == 0)
        return false;
    for (i = 0; i < name.length; i++) {
        if (name.charAt(i) < "0" || name.charAt(i) > "9")
            return false;
    }
    return true;
}


function mclick(obj) {
   // $("#key").val(obj.innerHTML.replace(/<.+?>/gi, ""));
    
}


/**
 * @author feiwen
 */
(function($) {
    $.fn.textSlider = function(settings) {
        settings = jQuery.extend({
            speed: "normal",
            line: 2,
            timer: 500
        }, settings);
        return this.each(function() {
            $.fn.textSlider.scllor($(this), settings);
        });
    };
    $.fn.textSlider.scllor = function($this, settings) {
        //alert($this.html());
        var ul = $("ul:eq(0)", $this);
        var timerID;
        var li = ul.children();
        var _btnUp = $(".up:eq(0)", $this)
        var _btnDown = $(".down:eq(0)", $this)
        var liHight = $(li[0]).height();
        var upHeight = 0 - settings.line * liHight; //滚动的高度；
       
        var scrollUp = function() {
            _btnUp.unbind("click", scrollUp);
            ul.animate({ marginTop: upHeight }, settings.speed, function() {
                for (i = 0; i < settings.line; i++) {
                    //$(li[i]).appendTo(ul);
                    ul.find("li:first").appendTo(ul);
                    // alert(ul.html());
                }
                ul.css({ marginTop: 0 });
                _btnUp.bind("click", scrollUp); //Shawphy:绑定向上按钮的点击事件
            });
        };
        var scrollDown = function() {
            _btnDown.unbind("click", scrollDown);
            ul.css({ marginTop: upHeight });
            for (i = 0; i < settings.line; i++) {
                ul.find("li:last").prependTo(ul);
            }
            ul.animate({ marginTop: 0 }, settings.speed, function() {
                _btnDown.bind("click", scrollDown); //Shawphy:绑定向上按钮的点击事件
            });
        };
        timerID = window.setInterval(scrollUp, settings.timer);
        var autoPlay = function() {
            timerID = window.setInterval(scrollUp, settings.timer);
            //alert(settings.timer);
        };
        var autoStop = function() {
            window.clearInterval(timerID);
        };
        //事件绑定
        ul.hover(autoStop, autoPlay).mouseout();
        _btnUp.css("cursor", "pointer").click(scrollUp);
        _btnUp.hover(autoStop, autoPlay);
        _btnDown.css("cursor", "pointer").click(scrollDown);
        _btnDown.hover(autoStop, autoPlay)

    };
})(jQuery);
window.onload = function() {
    $("#scrollDiv").textSlider({ line: 1, speed: 500, timer: 2000 });
    $("#scrollDiv1").textSlider({ line: 1, speed: 500, timer: 2000 });
    if (document.getElementById("scrollDiv2")) {
        $("#scrollDiv2").textSlider({ line: 1, speed: 500, timer: 2000 });
    }
    
}








