﻿//Traverlin  新扩展方法[基于Jquery实现头部搜索
jQuery(function(){        

    jQuery("#txttopkeyword").autocomplete("/DataManager/StoneContactDataHandler.ashx?action=GetSuggestKeywordAutoComplete", {      
        minchar: 1,
        width:275,               
        selectFirst:false, 
        formatItem:function(row,i,max){
            var item="<table id='auto"+i+"' style='width:100%;'border='0' cellpadding='0' cellspacing='0'><tr><td align='left'>"+row+"</td></tr></table>"; 
            return item; 
        }, 
        formatResult:function(row,i,max){ 
            return row;
        } 
    });    
    var lastinput='';   
    if(jQuery('#txttopkeyword').length>0){       
        jQuery('#txttopkeyword').blur();        
        lastinput=jQuery('#txttopkeyword').val();
    }    
   
    jQuery('#txttopkeyword').focus(function() {
        var keyword=jQuery("#txttopkeyword").val();
        if(keyword.indexOf('Search ')!=-1){
            jQuery("#txttopkeyword").val('');
        }
    }).blur(function(){
        var keyword=jQuery("#txttopkeyword").val();
        if(keyword==''){
            jQuery("#txttopkeyword").val(lastinput);
        }        
    });  
    jQuery('#txttopkeyword').bind('keydown', 'return', function() {        
            searchPage();            
            return false;
    });
    jQuery('#btntopSearch').click(function(){
        searchPage();
    });   
});

function searchPage(){
    if(jQuery("#txttopkeyword").length>0){
        var keyword=jQuery("#txttopkeyword").val();
        keyword=jQuery.trim(keyword);
        var reg=/\s+/gi;
        if(keyword==''||keyword.indexOf('Search ')!=-1){
            alert("Please Input Keyword!");
            jQuery("#txttopkeyword").focus();
        }
        else {
            var countryid=jQuery("#topcountry").val();
            var countryname=jQuery("#topcountry").find("option:selected").text(); 
            var url='/'; 
            if(_syscurrentpage){
                switch(_syscurrentpage)
                {
                
                    case"Search Product":
                        if(countryid==''){
                            keyword=encodeURIComponent(keyword);
                            url='/Main.aspx?pid=ProductSearch&topKeyword='+keyword;
                        }
                        else{
                            keyword=keyword.replace(reg,"+");
                            url='/Main.aspx?pid=ProductSearch&topKeyword='+keyword+'&countryID='+countryid;
                        }
                        break;
                        
                    case"Search Quarry":
                        if(countryid==''){
                            keyword=encodeURIComponent(keyword);
                            url='/Main.aspx?pid=QuarryList&topKeyword='+keyword;
                        }
                        else{
                            keyword=encodeURIComponent(keyword);
                            url='/Main.aspx?pid=QuarrySearch&country='+countryid+'&keywords='+keyword;
                        }
                        break;
                        
                    case "Search Buying Lead":
                        if(countryid==''){
                            keyword=encodeURIComponent(keyword);
                            url='/Main.aspx?pid=OfferSearch&topKeyword='+keyword;
                        }
                        else{
                            keyword=keyword.replace(reg,"-");
                            url='/buy/'+countryname+'/'+keyword+'.htm';
                        }
                        break;
                     
                    case "Search Company":
                        if(countryid==''){
                            keyword=encodeURIComponent(keyword);
                            url='/Main.aspx?pid=CompanySearch&topKeyword='+keyword;
                        }
                        else{
                            keyword=keyword.replace(reg,"-");
                            url='/company/'+countryname+'/'+keyword+'.htm';
                        }
                        break;
                        
                    case "Search Price List":
                        if(countryid==''){
                            keyword=encodeURIComponent(keyword);
                            url='/Main.aspx?pid=PriceListSearch&topKeyword='+keyword;
                        }
                        else{
                            keyword=keyword.replace(reg,"-");
                            url='/price/'+countryname+'/'+keyword+'.htm';
                        }
                        break;
                        
                   case "Search Natural Stones":
                        if(countryid==''){
                            keyword=encodeURIComponent(keyword);
                            url='/Main.aspx?pid=InfoCenterMaterial&Keyword='+keyword;
                        }
                        else{
                            keyword=encodeURIComponent(keyword);
                            url='/Main.aspx?pid=InfoCenterMaterial&keyword='+keyword+'&countryID='+countryid;
                        }
                        break;   
                }
            }            
            location.href=url;
             
        }
    }
}


var childCreate=false;
//取标签的绝对位置
function Offset(e){
    var t = e.offsetTop;
    var l = e.offsetLeft;
    var w = e.offsetWidth;
    var h = e.offsetHeight-2;
     while(e=e.offsetParent){
        t+=e.offsetTop;
        l+=e.offsetLeft;
     }
     return {
        top : t,
        left : l,
        width : w,
        height : h
     }
}

function loadSelect(obj){
    //第一步：取得Select所在的位置
    var offset=Offset(obj);
   
    //第二步：虚拟一个div出来代替select
    var iDiv = document.createElement("div");
    iDiv.id="selectof" + obj.name;
    //iDiv.style.position = "absolute";     
    //iDiv.style.top=offset.top + "px";
   // iDiv.style.left=offset.left + "px";   
    //document.body.appendChild(iDiv);
    obj.parentNode.appendChild(iDiv);
    //第三步：将真的select隐藏
    obj.style.display="none";
    //第四步：将select中默认的选项显示出来
    var tValue=obj.options[obj.selectedIndex].innerHTML;
    iDiv.innerHTML=tValue;  
    //第五步：模拟鼠标点击
    iDiv.onmouseover=function(){//鼠标移到   
    
    }
    iDiv.onmouseout=function(){//鼠标移走  

    }
  
    iDiv.onclick=function(){//鼠标点击
        if (document.getElementById("selectchild" + obj.name)){
            //判断是否创建过div
            if (childCreate){
                //判断当前的下拉是不是打开状态，如果是打开的就关闭掉。是关闭的就打开。
                document.getElementById("selectchild" + obj.name).style.display="none";
                childCreate=false;
            }else{
                document.getElementById("selectchild" + obj.name).style.display="";
                childCreate=true;
            }
        }else{
            //初始一个div放在上一个div下边，当options的替身。
            var cDiv = document.createElement("div");
            cDiv.id="selectchild" + obj.name;
            //cDiv.style.position = "absolute";
            //cDiv.style.top=(offset.top+offset.height-2) + "px";
           // cDiv.style.left=offset.left + "px";               
            var uUl = document.createElement("ul");
           
            uUl.id="uUlchild" + obj.name;
            uUl.style.listStyle="none";
            uUl.style.margin="0";
            uUl.style.padding="0";
            uUl.style.fontSize="12px";
            cDiv.appendChild(uUl);            
            //document.body.appendChild(cDiv);  
            obj.parentNode.appendChild(cDiv);
            childCreate=true;      
            for (var i=0;i<obj.options.length;i++){
                //将原始的select标签中的options添加到li中
                var lLi=document.createElement("li");
                lLi.id=obj.options[i].value;
                lLi.style.textIndent="4px";
                lLi.style.height="20px";
                lLi.style.lineHeight="20px";
                lLi.innerHTML=obj.options[i].innerHTML;
                uUl.appendChild(lLi);
            }
            var liObj=document.getElementById("uUlchild" + obj.name).getElementsByTagName("li");
            for (var j=0;j<obj.options.length;j++){
                //为li标签添加鼠标事件
                liObj[j].onmouseover=function(){
                this.style.background="gray";
                this.style.color="white";
            }
            liObj[j].onmouseout=function(){
            this.style.background="white";
            this.style.color="black";
        }
        liObj[j].onclick=function(){
            //做两件事情，一是将用户选择的保存到原始select标签中，要不做的再好看表单递交后也获取不到select的值了。
            obj.options.length=0;
            obj.options[0]=new Option(this.innerHTML,this.id);
            //同时我们把下拉的关闭掉。
            document.getElementById("selectchild" + obj.name).style.display="none";
            childCreate=false;
            iDiv.innerHTML=this.innerHTML;            
        }
    }
   }
 }
}


//===================================================================================

function goSearchPage()
{
    var objKeyWord = document.getElementById('top_Search_KeyWord');
    var objSearchType = document.getElementById('top_SearchType');
    var objCatalogid = document.getElementById(catalogid).value;
    if  (objKeyWord.value == '' || checkIsDefaultKeyword())
    {
        alert('Please Input Keyword!');
        objKeyWord.focus();
        return;
    }
        if  (objKeyWord.value.length < 2)
    {
        alert('Sorry,your keyword is too short!');
        objKeyWord.focus();
        return;
    }
    var redirUrl = objSearchType.options[objSearchType.selectedIndex].value;
    //如果是StonePicture Search，但未选中 "Marble, Granite  Stone" 则变为 Product Search
    // && objCatalogid != "171"
    if (redirUrl == _urlStonePictureSearch)
    {
        // redirUrl = _urlProductSearch;
        objCatalogid == "171";
    }
    var strUrl = redirUrl;
    if (objKeyWord.value != "") strUrl += '&topKeyword=' + encodeURIComponent(objKeyWord.value);
    if (objCatalogid != "") strUrl +='&topCategoryID='+objCatalogid;
    window.location.href = strUrl;
}

// When We Click the TopKeywoed Input Object Will Do This Function
function top_Search_KeyWord_OnClick()
{
    var objKeyWord = $G('top_Search_KeyWord');
    if (checkIsDefaultKeyword())
    {
        objKeyWord.value = '';
//        objKeyWord.className = "topKeyword_force";
    }
}
// When topKeyword Input is Blur Will Do this Function
function top_Search_KeyWord_OnBlur()
{
    var objKeyWord = $G('top_Search_KeyWord');
    if (objKeyWord.value.trim() == '')
    {
        objKeyWord.value = defaultSearchTipInfo;
        objKeyWord.className = "topKeyword_blur";
    }
}

// 
function top_Search_KeyWord_OnKeyDown(currentEvent)
{
    var myEvent = (window.event) ? window.event : currentEvent;
    if (myEvent.keyCode == 13)
    {
        $G("btnTopSearch").click();
    }
}

// 当用户改变搜索的类型时，执行该函数
function changeHotKeywordDisplay()
{

    var objSearchType = document.getElementById('top_SearchType');
    var strUrl = objSearchType.options[objSearchType.selectedIndex].value;
   
    var objtopKeyword = $G("top_Search_KeyWord");
 
    switch (strUrl)
    {
        case _urlProductSearch :
            setAllHotKeywordNodisplay()
            $G("hotSearch_product").style.display="block";
            defaultSearchTipInfo=_productSearchTipInfo;
            reSetKeywordValue(_productSearchTipInfo);
            break;
        case _urlOfferSearch :
            setAllHotKeywordNodisplay()
            $G("hotSearch_offer").style.display="block";
            defaultSearchTipInfo=_offerSearchTipInfo;
            reSetKeywordValue(_offerSearchTipInfo);
            break;
        case _urlCompanySearch :
            setAllHotKeywordNodisplay()
            $G("hotSearch_company").style.display="block";
            defaultSearchTipInfo=_companySearchTipInfo;
            reSetKeywordValue(_companySearchTipInfo);
            break;
        case _urlPriceListSearch :
            setAllHotKeywordNodisplay()
            $G("hotSearch_priceList").style.display="block";
            defaultSearchTipInfo=_PriceListSearchTipInfo;
            reSetKeywordValue(_PriceListSearchTipInfo);
            break;
        case _urlStonePictureSearch :
            setAllHotKeywordNodisplay()
            $G("hotSearch_stonePicture").style.display="block";
            defaultSearchTipInfo=_StonePictureSearchTipInfo;
            reSetKeywordValue(_StonePictureSearchTipInfo);
            break;
         case _urlQuarrySearch :
            setAllHotKeywordNodisplay()
            $G("hotSearch_quarry").style.display="block";
            defaultSearchTipInfo=_QuarrySearchTipInfo;
            reSetKeywordValue(_QuarrySearchTipInfo);
            break;
        default :
            setAllHotKeywordNodisplay()
            $G("hotSearch_product").style.display="block";
            defaultSearchTipInfo=_productSearchTipInfo;
            reSetKeywordValue(_productSearchTipInfo);
            break;
    }
    

}
// 重设关键字的值
function reSetKeywordValue(newValue)
{
    if (checkIsDefaultKeyword())
    {
        $G(topKeyword_id).value = newValue;
    }
}
// 设置所有的热门关键字为不显示的状态
function setAllHotKeywordNodisplay()
{
    $G("hotSearch_product").style.display="none";
    $G("hotSearch_offer").style.display="none";
    $G("hotSearch_company").style.display="none";
    $G("hotSearch_priceList").style.display="none";
    $G("hotSearch_stonePicture").style.display="none";
    $G("hotSearch_quarry").style.display="none";
}
// 检查是否是默认的值
function checkIsDefaultKeyword()
{
    var objKeyWord = $G('top_Search_KeyWord');
    var keyword = $G('top_Search_KeyWord').value;
    if ( keyword==_productSearchTipInfo || keyword==_offerSearchTipInfo || keyword==_companySearchTipInfo
        || keyword==_PriceListSearchTipInfo || keyword==_StonePictureSearchTipInfo || keyword==_QuarrySearchTipInfo)
    {
        return true;
    }
    else
    {
        return false;
    }
}
