﻿// JScript 文件
$(document).ready(function() {
    var tmpInput1 = "Search Products";
    var tmpInput2 = "Search Suppliers";
    $("#inputSearch").val(tmpInput1);
    
    $("#inputSearch").focus(function(){
        if($(this).val()==tmpInput1 || $(this).val()==tmpInput2)
        {
            $(this).val("");
        }
        
    }).blur(function(){
        if($(this).val()==tmpInput1 || $(this).val()==tmpInput2 || $(this).val()=="")
        {
            if($("#shidden").val()=="p")
            {
                $(this).val(tmpInput1);
            }else
            {
                $(this).val(tmpInput2);
            }
        }
    });
    $("#indexpp").click(function(){
        $("#inputSearch").flushCache();
        $('#inputSearch').val(tmpInput1);
        $(this).addClass("c");
        $("#indexff").removeClass("c");
        $('#shidden').val("p");
        $("#inputSearch").focus();
    }); 
    $("#indexff").click(function(){
        $("#inputSearch").flushCache();
        $('#inputSearch').val(tmpInput2);
        $(this).addClass("c");
        $("#indexpp").removeClass("c");
        $('#shidden').val("f");
        $("#inputSearch").focus();
        
    });
    //$("#inputSearch").autocomplete($("#vpath").val()+"/getAjaxComplete.ashx", {   
    $("#inputSearch").autocomplete("getAjaxComplete.ashx", {                                     
        width: 340,
        selectFirst: false,
        cacheLength:1,
        onItemSelect:selectItem,            
        onFindValue:findValue,            
        formatItem:formatItem,
        extraParams: {stype: function() { return $("#shidden").val(); } }
    });
    function findValue(li) {
        if( li == null ) 
        return alert("No match!");    
 		// if coming from an AJAX call, let's use the CityId as the value    
 		if( !!li.extra ) var sValue = li.extra[0];    // otherwise, let's just display the value in the text box    
 		else var sValue = li.selectValue;    
 		//alert("The value you selected was: " + sValue);
    }
    function selectItem(li) {
        findValue(li);
    }
    function formatItem(row) {
        return "<span style=\"float:left\">"+row[0] + "</span><div style=\"color:green\"><span class=\"count\" style=\"float:right\">" + row[1] + " items</span></div>"    
        
    }
//    $("#inputSearch").result(function(event, data, formatted) {
//        if (data)
//        $(this).val(data[1]);
//    });
    $("#btnSearch").click(function(){
        //alert($("#inputSearch").val()+"+参数"+$('#shidden').val())
        var myresult="";
        var searchpp=$("#inputSearch").val()
        if(searchpp!=tmpInput1 && searchpp!=tmpInput2 )
        {
            myresult=searchpp;
        }
        //alert(myresult);
        if($("#shidden").val() == "p")
        {
            //window.location.href = "/ProductList.aspx?result=" + myresult+"&h=1";
            window.location.href = $("#vpath").val()+"/ProductList.aspx?result=" + myresult+"&h=1";
        }
        else
        {
            //window.location.href = "/CompanyList.aspx?result=" + myresult+"&h=1";
            window.location.href = $("#vpath").val()+"/CompanyList.aspx?result=" + myresult+"&h=1";
        }
    });
    $("#clickmore").click(function(){
       if($("#nn11").attr("class")=="s")
      {
        location.href="ProductGroup.aspx?GroupID="+$("#nn11").attr("group")+"&type=new";
      }else
      {
        location.href="ProductGroup.aspx?GroupID="+$("#nn22").attr("group")+"&type=Specials";
      }
    });
});
