top_search.js 3.71 KB
/**
 * @description top_seatch on diligrp-website
 *
 * @author <a href="mailto:akhuting@hotmail.com">Alex Yang</a>
 * @date 2014年06月24日 17:29
 */
$(function () {
	  $(".sea-tab").hover(
	        function () {
	            $(".sea-tab b").addClass("hover");
	            $(this).children(".sea-tab-ul2").show();
	        },
	        function () {
	            $(".sea-tab b").removeClass("hover");
	            $(this).children(".sea-tab-ul2").hide();

	        }
	    );


	    var timer, sear_trigger, _objInput = $("#sear-input");
	    $(".inputbox").hover(function () {
	        _objInput.click(function () {
	            var _this = $(this);

	            sear_trigger = setTimeout(function () {
	                _this.val('');
	                //$('.g-sear-listbox').addClass("listboxhide");
	                $('.g-sear-listbox').removeClass("listboxhide").children('ul.sear-goods').show();
	                $('.g-sear-listbox').children('ul.sear-history').hide();
	            }, 200);
	            timer = setInterval('indexSearchTs(_objInput)', 200);
	        });

	    }, function () {
	        clearTimeout(sear_trigger);
	        clearInterval(timer);
	        $('.g-sear-listbox').addClass("listboxhide");
	        var sear_data = $("#sear-input").val();
	        if (sear_data != '' && sear_data != tipsname) {
	            $.noop();
	        } else {
	            _objInput.val(tipsname);
	        }
	    });
	
	
    $(".sear-li").bind("click", function () {
        var i = $(this).text();
        var s = $(".sear-d").text();
        var t = $(this).attr("data-searchtype");
        var k = $(".sear-d").attr("data-searchtype");
        $(".sear-d").text(i).attr("data-searchtype", t);
        $(".sear-li").text(s).attr("data-searchtype", k);
        $(".sea-tab-ul2").hide();
    });// 搜索下拉+点击切换文字
    $("#autocomplete").attr("placeholder", search.keywords[(Math.ceil(Math.random() * 2) - 1)]);
    $("#autocomplete").attr("maxLength",50);

    // 搜索框智能提示
    $('#autocomplete').autocomplete({
        serviceUrl: DiliPath.homePath + "tip.html",
        paramName: "key",
        dataType: "jsonp",
        appendTo: ".mo-fn2 span.sp5",
        triggerSelectOnValidInput: false,
        extraParams : function(){
        	var self = this;
        	var searchtype = $(".sear-d").attr("data-searchtype");
            var type = searchtype == 'goods'?'STG' : 'STORE';
            self.params['type'] = type;
        },
        onSelect: function (suggestion) {
            search.topSearch();
        }
    });

    $("#autocomplete").keypress(function (event) {
        if (event.which == 13) {
            search.topSearch();
        }
    });

    $("#search").click(function () {
        search.topSearch();
    });

});

var tipsname = '请输入商品关键字';
function indexSearchTs() {
    var inputData = $("#sear-input").val();
    if (inputData != '' && inputData != tipsname) {
        $('.g-sear-listbox').removeClass("listboxhide");
        $('.g-sear-listbox .sear-goods').hide();
        $('.g-sear-listbox .sear-history').show();
    }
};

var search = {
    // top search function
    topSearch: function () {
        var keyword = $("#autocomplete").val();
        keyword = $.trim(keyword);
        keyword = encodeURIComponent(keyword);
        if (jQuery.trim(keyword) == "") {
            var def = $("#autocomplete").attr("placeholder");
            keyword = def.substring(2, def.indexOf("”"));
        }
        var searchtype = $(".sear-d").attr("data-searchtype");
        window.location.href = DiliPath.homePath + "search.html?" + searchtype
            + "=&key=" + keyword;
    },
    keywords: ["搜“黄瓜”试试,大黄瓜小黄瓜应有尽有", "搜“四川”试试,保证多快好省"]
};