/*
 * 需要配合jQuery函式~取得DOM元素.
 */
var host       = 'www.manufacturers.com.tw';
var this_url   = window.location.href;
var this_query = window.location.search;

/*
 * This function retrieves the search query from the URL.
 * 從參數名稱取得關鍵字
 */
function GetParam(name) {
    var match = new RegExp(name + "=([^&]+)","i").exec(this_query);
    return (match) ? decodeURIComponent(match[1]) : null;
}

/*
 * 直接取得關鍵字
 */
function GetValue(value) {
    value = value.replace(/"/,'\"');
    return (value) ? decodeURIComponent(value) : null;
}

/*
 * 從參數名稱指定頁數
 */
function GetPage(page) {
    var match = new RegExp(page + "=([^&]+)","i").exec(this_query);
    return (match) ? decodeURIComponent(match[1]) : 1;
}

/*
 * 定義搜尋連播網頻道
 */
function GetChannel(value) {
    var channel = "";
    
    if (value != "") {
        channel = value
    }else{
        // Online Trade Show Search
        var eo_str1 = host+'/tradeshow/';
        var eo_str2 = host+'/exhibition.php';
        
        // Trade Leads (from : tradeleads.php)
        var tl_str1 = host+'/tradeleads.php';
        
        // Taiwan Shows
        var ts_str1 = host+'/Taiwan-Shows/';
        
        if( this_url.indexOf(ts_str1) != -1) {
            channel = "4058113797";
        }else if( this_url.indexOf(eo_str1) != -1 || this_url.indexOf(eo_str2) != -1) {
            channel = "6231605241";
        }else if( this_url.indexOf(tl_str1) != -1) {
            var match2   = new RegExp("tt=([^&]+)","i").exec(location.search);
            var match_sk = new RegExp("sk=([^&]+)","i").exec(location.search);
            if (match2 && match2[1]) {
                switch (match2[1]) {
                    // Buying Leads Search 
                    case '1' :
                        if(match_sk && match_sk[1]){
                            channel = "5924597528";
                        }else{
                            channel = "2490886019";
                        }
                        break;
                    // Selling Leads Search 
                    case '2' :
                        if(match_sk && match_sk[1]){
                            channel = "6904923939";
                        }else{
                            channel = "7126026067";
                        }
                        break;
                }
            }
            //alert(channel);
        }else{
            var match = new RegExp("st=([^&]+)","i").exec(location.search);
            
            if (match && match[1]) {
                switch (match[1]) {
                    // Company Name Search 
                    case '2' :
                        channel = "7995493936";
                        break;  
                    // Product Search   
                    case '13' :
                        channel = "6804306846";
                        break;    
                    // SPEC Search   
                    case '14' :
                        channel = "0489827589";
                        break; 
                    // Supplier Search (站內)
                    case '1' :
                        channel = "3000030443";
                        break;
                }
            }
            // Supplier Search (版位)
            if(!channel) channel = "5678203520";
        }
    }
    return (channel) ? channel : null;
}


function GetGroupNum(name) {
    var match = new RegExp(name + "([1-9]+)","i").exec(google_afs_ad);
    return (match) ? match[1] : null;
}

/*
 * 指定語系
 */
function GetLang(name) {
    name = name.replace(/\//,""); 
    switch (name){
        // 繁體中文
        case "zh-tw" :
            name = "zh-TW";
            break;
        // 簡體中文
        case "zh-cn" :
            name = "zh-CN";
            break;    
        // 預設值為(英文版)
        default :
            name = "en";
            break;
    }
    //alert(name);
    return (name) ? name : null;
}

/*
* This function is required. It processes the google_ads JavaScript object,
* which contains AFS ads relevant to the user's search query. The name of
* this function <i>must</i> be <b>google_afs_request_done</b>. If this
* function is not named correctly, your page will not display AFS ads.
*/
function google_afs_request_done(google_ads)
{
    var list_wideAds_obj    = $(conf_wideAds);
    var list_narrowAds_obj  = $(conf_narrowAds);
    
    var list_wideAds_obj_size    = list_wideAds_obj.size();
    var list_narrowAds_obj_size  = list_narrowAds_obj.size();
    
    /*
     * Verify that there are actually ads to display.
     */
    var google_num_ads = google_ads.length;
    if (google_num_ads <= 0) return;

    var wideAds = "";       // wide ad unit html text
    var narrowAds = "";     // narrow ad unit html text
    
    var w_j = n_j = 1;
    var w_z = n_z = 0;
    var q = 0;
    
    // copyright
    //var copyright = '<a href="http://services.google.com/feedback/online_hws_feedback"  style="text-decoration:none" target="_blank">Ads by Google</a><br>';
    var copyright = '<div class="by_goole"><a href="http://services.google.com/feedback/online_hws_feedback"  style="text-decoration:none" target="_blank">Ads by Google</a></div>'
    for(i = 0; i < google_num_ads; i++)
    {
        // 寬格式
        if (google_ads[i].type=="text/wide")
        {
            wideAds += '<span>';
            wideAds += '<div class="gaad_h3"><a target="_blank" href="'+google_ads[i].url+'">'+google_ads[i].line1+'</a></div>';
            //wideAds += '<div class="gaad_txt"><a target="_blank" href="'+google_ads[i].url+'">'+google_ads[i].line2 + '&nbsp;' + google_ads[i].line3+'</a></div>';
            wideAds += '<div class="gaad_txt"><a target="_blank" href="'+google_ads[i].url+'">'+google_ads[i].line2 +'</a></div>';
            wideAds += '<div class="gaad_url"><a target="_blank" href="'+google_ads[i].url+'">'+google_ads[i].visible_url+'</a></div>';
            wideAds += '</span>';
            
            if ( w_j == conf_wideAds_group && (w_z+1) != list_wideAds_obj_size ) {
                list_wideAds_obj.eq(w_z).html( copyright + wideAds);
                //document.getElementsByName('gaad_leads')[z].innerHTML = wideAds;
				w_j = 1;
                w_z++;
                wideAds = "";
            }else{
                w_j++;
            }
            q++;
        // 直格式(render a narrow ad)
        }else{
            narrowAds += '<span>';
            narrowAds += '<div class="gaad_h3"><a target="_blank" href="'+google_ads[i].url+'">'+google_ads[i].line1+'</a></div>';
            //narrowAds += '<div class="gaad_txt"><a target="_blank" href="'+google_ads[i].url+'">'+google_ads[i].line2 + '&nbsp;' + google_ads[i].line3+'</a></div>';
            narrowAds += '<div class="gaad_txt"><a target="_blank" href="'+google_ads[i].url+'">'+google_ads[i].line2 +'</a></div>';
            narrowAds += '<div class="gaad_url"><a target="_blank" href="'+google_ads[i].url+'">'+google_ads[i].visible_url+'</a></div>';
            narrowAds += '</span>';
            
            if ( n_j == conf_narrowAds_group && (n_z+1) != list_narrowAds_obj_size) {
                list_narrowAds_obj.eq(n_z).html( copyright + narrowAds);
                n_j = 1;
                n_z++;
                narrowAds = "";
            }else{
                n_j++;
            }
        }
    }
    
    // 剩下資料顯示
    if (wideAds != ""  && list_wideAds_obj.eq(w_z).length>0 ) {
        list_wideAds_obj.eq(w_z).html( copyright + wideAds);
    }
    
    if (narrowAds != "" && list_narrowAds_obj.eq(n_z).length>0 ) {
        list_narrowAds_obj.eq(n_z).html( copyright + narrowAds);
    }
    
    /*
    if (narrowAds != "")
    {
        narrowAds = '<a href="http://services.google.com/feedback/online_hws_feedback"  target="_blank"><strong>Ads by Google</strong></a><br>'+narrowAds ;
    }
    if (wideAds != "")
    {
        wideAds = '<a href="http://services.google.com/feedback/online_hws_feedback"  target="_blank"><strong>Ads by Google</strong></a><br>'+wideAds ;
    }
    */
        
    // Write HTML for wide and narrow ads to the proper <div> elements
    //if(wideAds && document.getElementById("wide_ad_unit")) document.getElementById("wide_ad_unit").innerHTML = wideAds;
    //if(narrowAds && document.getElementById("narrow_ad_unit")) document.getElementById("narrow_ad_unit").innerHTML = narrowAds;
    /*
    if(wideAds && google_wideAds)  google_wideAds.innerHTML= wideAds;	
    if(narrowAds && google_narrowAds ) google_narrowAds.innerHTML = narrowAds;
    */
}

    /**** ---- 共用設定 ---- *****/
    //google_afs_adtest  = "on";
    google_afs_client  = 'pub-0852387203100265';
    google_afs_query   = GetParam('sk');      // 搜尋字詞
    google_afs_ad      = 'n6';                // 廣告格式和數目 
    google_afs_channel = '';                  // 頻道
    //google_afs_adpage  = GetPage('page');   // 可指定應當傳回的廣告網頁(頁碼)
    //google_afs_gl = "";                     // 廣告的地理位置(國家/地區代碼)
    google_afs_hl      = 'en';                // 廣告定位的語系
    google_afs_ie      = 'utf8';              // 用來轉譯查詢字串的字元編碼
    google_afs_oe      = 'utf8';              // 設定 Google 應用來對 JavaScript 結果進行編碼的字元編碼配置

    /**** ---- 指定元素設定 ---- *****/
    //(寬格式)
    var conf_wideAds         = '#wide_ad_unit';     // 定義元素名稱
    var conf_wideAds_group   = GetGroupNum('w');    // 每群組顯示筆數

    //(直格式)
    var conf_narrowAds       = '#narrow_ad_unit';   // 定義元素名稱
    var conf_narrowAds_group = GetGroupNum('n');    // 每群組顯示筆數
    
