﻿//javascript for http://times.hinet.net/
var SERVER_IP = "www.aiweb.tw";
var web_site = "AIWEBAD";   //access web API
var data_site = "ad_acms";  //access data
var browser = navigator.appName;
//一個網頁只觸發一次
var current_page_url = location.href;

//必須先在網頁中產生的欄位
document.write('<script type="text/javascript" src="http://'+SERVER_IP+'/'+web_site+'/js/json2.js"></script>');
document.write('<script type="text/javascript" src="http://'+SERVER_IP+'/'+web_site+'/js/jsr_class.js"></script>');
///顯示廣告的部分切成兩個div
document.write('<iframe id="aiweb_frame_1" name="aiweb_frame_a" style="height:0; width:0; border:0px;"></iframe>');
document.write('<div style="border: 0pt none ; margin: auto; width: 300px;"><table cellspacing="0" cellpadding="0" border="0" width="300" height="100"><tbody><tr><td><div id="aiweb1" align="top" style="width:300px;  border:0;"></div></td></tr><tr><td></td></tbody></table></div>');
document.write('<div id="aiweb_media_info_1" style="display:none;"></div>');


//functions
function post_to_url(path, params, method) {     

    var my_iframe = document.getElementById('aiweb_frame_1');    
    var uni = generateGuid();
   
    if(current_page_url.indexOf("event/auto") != -1)  ///汽車
    {
        value = "auto";     
    }   
    else if(current_page_url.indexOf("magazine.do") != -1) ///雜誌
    {
        value = "magzine";
    }
    else 
    {
        value = get_url_value("option");
        if(value == "") ///首頁
        {
            value = "main";
        }      
    }    
         
    var form = document.createElement("form");
    form.setAttribute("method", method);
    form.setAttribute("action", path+"?uni="+uni+"&channel=19&newstype="+value);
    
    form.setAttribute("target", my_iframe.name);  
    
    //alert(params);
    
    var hiddenField = document.createElement("input");
    hiddenField.setAttribute("type", "hidden");
    hiddenField.setAttribute("name", "data");    
    hiddenField.setAttribute("value", params);

    form.appendChild(hiddenField);    
    document.body.appendChild(form);        
 
    form.submit();         
    
    //wait result in iframe.
    //alert('check');
    if (browser=="Microsoft Internet Explorer")
    {
    //ie中不可動態指定iframe的onload        
        var wait_til_frame_onload = function () {
            var state = null;
            if(document.readyState){
                try{
                    state = my_iframe.document.readyState;
                } catch(e) {state=null;} 
                if(state=="complete" || !state){//loading,interactive,complete
                    get_result(uni);
                    return;
                }
                window.setTimeout(wait_til_frame_onload,10);
            }
        };        
        window.setTimeout(wait_til_frame_onload,100);//call this statement when iframe page start to change
    }
    else
    {
    //其他可以直接指定.
        my_iframe.onload = get_result(uni);        
    }  
     
}

function generateGuid()
{
var result, i, j;
result = '';
for(j=0; j<32; j++)
{
if( j == 8 || j == 12|| j == 16|| j == 20)
result = result + '-';
i = Math.floor(Math.random()*16).toString(16).toUpperCase();
result = result + i;
}
return result
}

function get_result(unique) 
{    
    var req = "http://"+SERVER_IP+"/"+web_site+"/Article_Analysis.aspx?url="+current_page_url+unique;
    //alert(req);
    bObj_0 = new JSONscriptRequest(req);
    // Build the dynamic script tag
    bObj_0.buildScriptTag();
    // Add the script tag to the page
    bObj_0.addScriptTag();
}
 
//根據Server回傳的資訊, 顯示指定的廣告內容
function changeFunction(responseText)
{ 
    var URL = window.location.href;
    if(responseText=="")
        return;
    
    myObject_0 = eval(responseText);    
    ///第一則廣告
    switch(myObject_0[0].Media_Type) {
     case 1: //Condition1:  img file.
        var str = '<a href='+myObject_0[0].Media_Address+' target="_blank"><img style="width:300px; height:100px; border:0px;" src="http://'+SERVER_IP+'/'+data_site+'/'+myObject_0[0].Media_Name+'" onClick="returnInfo_0();"></img></a>';                        
        //str += '<div align="right" style="width:300px; background-color:White; height=20px; border:0px;" ><a href="http://www.aiweb.tw" target="_blank"><img style="width:119px; height=20px; border:0px;" src="http://'+SERVER_IP+'/'+web_site+'/logo.jpg" /></a></div>';
        break;
        
    case 2: //Condition2:  flash file.       <a href='+myObject[0].Media_Address+' target="_blank">         
          //var str = '<embed  style="width:300px; height:100px;" src="http://'+SERVER_IP+'/'+data_site+'/'+myObject_0[0].Media_Name+'?pageURL='+URL+'&channel=19" allowScriptAccess="always" ></embed>';        
          var str = '<object><param name="FlashVars" value="targetWebPath=http://'+SERVER_IP+'/'+web_site+'/CPCFlashCount.aspx?pageurl='+URL+'&id='+myObject_0[0].Media_ID+'&channel=19&flashPath=http://'+SERVER_IP+'/'+data_site+'/'+myObject_0[0].Media_Name+'" />';
          str +='<embed src="http://'+SERVER_IP+'/'+web_site+'/AIWebLoader.swf" width="300px" height="100px" FlashVars="targetWebPath=http://'+SERVER_IP+'/'+web_site+'/CPCFlashCount.aspx?pageurl='+URL+'&id='+myObject_0[0].Media_ID+'&&channel=19&flashPath=http://'+SERVER_IP+'/'+data_site+'/'+myObject_0[0].Media_Name+'" quality="high"/></object>';
          //str += '<div align="right" style="width:300px; background-color:White; border:0px;" ><a href="http://www.aiweb.tw" target="_blank"><img style="width:119px; height=20px; border:0px;" src="http://'+SERVER_IP+'/'+web_site+'/logo.jpg" /></a></div>'; 
        break;
    case 3: //Condition3:  video        
        var str="";
        break;                
    case 4: //Condition4:  text string
        if(myObject_0[0].Media_Address=="") {
            var str = "<div style=\"padding:5px 5px 5px 5px; background-color:White; width:290px; \"><div style=\"text-align:left; width:280px; vertical-align:middle; font-size: 11.5pt; border: 1pt solid #AAAAAA; background-color:White; padding:5px 5px 5px 5px;\"><a style=\"text-decoration:none; color:#006699;\" href=\"#\" onClick=\"returnInfo_0();\"><span style=\"font-size: 10pt; color:#215DA9;\"><B>" + myObject_0[0].Media_Title + "</B></span></a><br><<span style=\"font-size: 10pt; color:#888888;\">" + myObject_0[0].Media_Text + "</span><br><span style=\"font-size: 10pt; color:#555555;\">" + myObject_0[0].Media_ShowAddress + "</span></div>";
        }
        else {
            var str = "<div style=\"padding:5px 5px 5px 5px; background-color:White; width:290px; \"><div style=\"text-align:left; width:280px; vertical-align:middle; font-size: 11.5pt; border: 1pt solid #AAAAAA; background-color:White; padding:5px 5px 5px 5px;\"><a style=\"text-decoration:none; color:#006699;\" href=" + myObject_0[0].Media_Address + " target=\"_blank\" onClick=\"returnInfo_0();\"><B>" + myObject_0[0].Media_Title + "</B></a><br><span style=\"font-size: 10pt; color:#888888;\">" + myObject_0[0].Media_Text + "</span><br><span style=\"font-size: 10pt; color:#555555;\">" + myObject_0[0].Media_ShowAddress.replace('http://','') + "</span></div>";
        }
        //str += '<div align="right" style="height=20px; border:0px;" ><a href="http://www.aiweb.tw" target="_blank"><img style="width:119px; height=20px; border:0px;" src="http://'+SERVER_IP+'/'+web_site+'/logo.jpg" /></a></div></div>';
        break;
    case -1:
        str ="";
    default:  
    }    
    var choose = document.getElementById('aiweb1');
    //var str_2 = '<div style="text-align:right; width:300px; height=20px; border:0px; background-color:White;" ><a href="http://www.aiweb.tw" target="_blank"><img style="width:119px; height=20px; border:0px;" src="http://'+SERVER_IP+'/'+web_site+'/logo.jpg" /></a></div>';
    choose.innerHTML = str; 
    document.getElementById('aiweb_media_info_1').innerHTML = myObject_0[0].Media_ID;
    
//    var choose_2 = document.getElementById('aiweb3');
//    choose_2.innerHTML = str_2;     
    
    bObj_0.removeScriptTag();              
    setTimeout("returnCPMInfo_0();", 1000);        
}

function returnCPMInfo_0()
//return CPM information to server via WebAPI(Json format).
{	 
//    var id = document.getElementById('aiweb_media_info_1').innerHTML;	    
//    if(id=="" || id==-1)
//        return;
//    var script = document.createElement('script');  
//    script.type = 'text/javascript';  
//    script.src = 'http://'+SERVER_IP+"/"+web_site+"/CPMCount.aspx?id="+id+"&channel=19";
//    
//    document.getElementsByTagName('head')[0].appendChild(script);     
}

function returnInfo_0()
//return information to server via WebAPI(Json format).
{	 
//    var id = document.getElementById('aiweb_media_info_1').innerHTML;	
//    if(id=="" || id==-1)
//        return;    
//    var script = document.createElement('script');  
//    script.type = 'text/javascript';  
//    script.src = 'http://'+SERVER_IP+"/"+web_site+"/CPCCount.aspx?id="+id+"&channel=19";
//    
//    document.getElementsByTagName('head')[0].appendChild(script);       
}
 
function get_url_value (req_key) {
    url = location.search.substr(1);
    if(url[0]=='&')
        url = url.substr(1);
    if (url.length > 0) {
        ar = url.split(/[&=]/);
        for (i = 0; i < ar.length; i += 2) {
            if(ar[i]==req_key)
                return ar[i+1];            
        }
    }
    return "";
}    

//window.setTimeout(show_aiweb_ad,1000);
window.onload = function() {
    show_aiweb_ad();
}

//main function starts here.
function show_aiweb_ad() {
    if(current_page_url.indexOf("?") != -1) 
    {
        current_page_url = current_page_url.substring(0, current_page_url.indexOf("?"));
    }


    var current_page_info = new Object();
    current_page_info.title = '';
    current_page_info.content = '';
    current_page_info.url = current_page_url;

    var current_page_info_JSONtext = JSON.stringify(current_page_info, function (key,value) {return value;});
    //alert(current_page_info_JSONtext);

    post_to_url("http://"+SERVER_IP+"/"+web_site+"/Article_Analysis.aspx", current_page_info_JSONtext, "POST");
}
