﻿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 scroll_check_flag = true;

//必須先在網頁中產生的欄位
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>');
//document.write('<div><div id="aiweb"></div></div>');
document.write('<div id="media_info" style="display:none;"></div>');
document.write('<iframe id="myframe" name="frame_1" style="height:0; width:0; border:0px;"></iframe>');

//functions
function post_to_url(path, params, method) {     
    var my_iframe = document.getElementById('myframe');    
    var uni = generateGuid();
    

    var form = document.createElement("form");
    var hiddenField = document.createElement("input");
    
    form.setAttribute("id", "aiwebform1");
    form.setAttribute("method", method);
    form.setAttribute("action", path+"?uni=" + uni + "&channel=2");          
    form.setAttribute("target", my_iframe.name);  
    if (browser=="Microsoft Internet Explorer")
    {
        form.style.margin = '0'; 
        form.style.padding = '0';
    }
    else
    {
        form.setAttribute("style", "margin:0;padding:0"); 
    }
    
    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.
    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
                    orz(uni)
                    return;
                }
                setTimeout(wait_til_frame_onload,10);
            }
        };        
        setTimeout(wait_til_frame_onload,100);//call this statement when iframe page start to change
    }
    else
    {
    //其他可以直接指定.
        my_iframe.onload = orz(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 orz(unique) 
{    
    var url = location.href;
    url = escape(url);
    var req = "http://"+SERVER_IP+"/"+web_site+"/Article_Analysis.aspx?url="+url+unique;
    //alert(req);
    bObj = new JSONscriptRequest(req);
    // Build the dynamic script tag
    bObj.buildScriptTag();
    // Add the script tag to the page
    bObj.addScriptTag();
}

//根據Server回傳的資訊, 顯示指定的廣告內容
function changeFunction(responseText)
{
    var URL = window.location.href;
    if(responseText=="")
        return;
    //alert(responseText);
    myObject = eval(responseText)    
     
    switch(myObject[0].Media_Type) {
    case 1: //Condition1:  img file.
        var str = '<a href='+myObject[0].Media_Address+' target="_blank"><img style="width:255px; height:85px; border:0px;" src="http://'+SERVER_IP+'/'+data_site+'/'+myObject[0].Media_Name+'" onClick="returnInfo('+myObject[0].Match_C+');"></img></a>';                        
        //str += '<div align="right" style="width:255px; 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>';
        str += '<div align="right" style="width:255px; height=20px; border:0px;" ><a href="http://www.aiweb.tw" target="_blank"><strong><span style=" color: #2c8fda; font-weight: bold;">淘評</span></strong><span style="font-size:11px; color:#000000; text-decoration: underline;">分眾精準廣告</span></a></div>';
        break;
         
    case 2: //Condition2:  flash file.       <a href='+myObject[0].Media_Address+' target="_blank">         
          //var str = '<a href='+myObject[0].Media_Address+' target="_blank" /><embed  style="width:255px; height:85px;" src="http://'+SERVER_IP+'/'+data_site+'/'+myObject[0].Media_Name+'?pageURL='+URL+'&channel=2" allowScriptAccess="always" ></embed>';
          var str = '<object><param name="FlashVars" value="targetWebPath=http://'+SERVER_IP+'/'+web_site+'/CPCFlashCount.aspx?pageurl='+URL+'&id='+myObject[0].Media_ID+'&channel=2&flashPath=http://'+SERVER_IP+'/'+data_site+'/'+myObject[0].Media_Name+'" />';
          str +='<embed src="http://'+SERVER_IP+'/'+web_site+'/AIWebLoader.swf" width="255px" height="85px" FlashVars="targetWebPath=http://'+SERVER_IP+'/'+web_site+'/CPCFlashCount.aspx?pageurl='+URL+'&id='+myObject[0].Media_ID+'&&channel=2&flashPath=http://'+SERVER_IP+'/'+data_site+'/'+myObject[0].Media_Name+'" quality="high"/></object>';
          str += '<div align="right" style="width:255px; height=20px; border:0px;" ><a href="http://www.aiweb.tw" target="_blank"><strong><span style=" color: #2c8fda; font-weight: bold;">淘評</span></strong><span style="font-size:11px; color:#000000; text-decoration: underline;">分眾精準廣告</span></a></div>';
 
        break;
    case 3: //Condition3:  video        
        var str = '<embed type="video/x-ms-wmv" style="width:255px; height:85px;" src="http://'+SERVER_IP+'/'+data_site+'/'+myObject[0].Media_Name+'" autostart="true" loop="true" showtracker="false" showpositioncontrols="false"></embed>';
        str += '<div align="right" style="width:255px; height=20px; border:0px;" ><a href="http://www.aiweb.tw" target="_blank"><strong><span style=" color: #2c8fda; font-weight: bold;">淘評</span></strong><span style="font-size:11px; color:#000000; text-decoration: underline;">分眾精準廣告</span></a></div>';
        break;
        
    case 4: //Condition4:  text string
        if(myObject[0].Media_Address=="") {
            var str = "<div style=\"width:255px; height=85px; vertical-align:middle; background-color:#E7EFD8;\"><a href=\"#\" onClick=\"returnInfo("+myObject[0].Match_C+");\"><font color=\"blue\"><font size=\"+1\"><B>" + myObject[0].Media_Title + "</B></font></font></a><br><font color=\"black\">" + myObject[0].Media_Text + "</font><br><font size=\"-2\" color=\"green\">" + myObject[0].Media_ShowAddress + "</font></div>";
        }
        else {
            var str = "<div style=\"width:255px; height=85px; vertical-align:middle; background-color:#E7EFD8;\"><a href=" + myObject[0].Media_Address + " target=\"_blank\" onClick=\"returnInfo("+myObject[0].Match_C+");\"><font color=\"blue\"><font size=\"+1\"><B>" + myObject[0].Media_Title + "</B></font></font></a><br><font color=\"black\">" + myObject[0].Media_Text + "</font><br><font size=\"-2\" color=\"green\">" + myObject[0].Media_ShowAddress + "</font></div>";
        }        
        str += '<div align="right" style="width:255px; height=20px; border:0px;" ><a href="http://www.aiweb.tw" target="_blank"><strong><span style=" color: #2c8fda; font-weight: bold;">淘評</span></strong><span style="font-size:11px; color:#000000; text-decoration: underline;">分眾精準廣告</span></a></div>';
        break;
    case -1:
        str ="";        
        $("div#articleExt_relate_content").css("width","500px");
    default:  

    }
    
    if(URL.indexOf('http://blog.xuite.net/hllin750521') != -1 ) {        
        var choose = document.getElementById('aiweb');  
        //var choose = document.getElementById('articleExt_relate_ads');  
    }
    else {
        var choose = document.getElementById('articleExt_relate_ads'); 
    }
    
    choose.innerHTML = str; 
    document.getElementById('media_info').innerHTML = myObject[0].Media_ID;
    bObj.removeScriptTag();   
    
    setTimeout("returnCPMInfo();", 1000);
}

function returnCPMInfo()
//return CPM information to server via WebAPI(Json format).
{	 
    var id = document.getElementById('media_info').innerHTML;	    
    if(id=="" || id==-1)
        return;    
    var url = location.href;
    url = escape(url);
    var script = document.createElement('script');  
    script.type = 'text/javascript';  
    script.src = 'http://'+SERVER_IP+"/"+web_site+"/CPMCount.aspx?id="+id+"&channel=2&url="+url;
    //script.src = "http://aiweb1.dyndns.org/"+web_site+"/CPMCount.aspx?id="+id+"&channel=11";
    document.getElementsByTagName('head')[0].appendChild(script);     
    return false;      
}

function returnInfo(mcid)
//return information to server via WebAPI(Json format).
{
    var id = document.getElementById('media_info').innerHTML;
    if(id=="" || id==-1)
        return;
    var url = location.href;
    url = escape(url);

    var script = document.createElement('script');  
    script.type = 'text/javascript';  
    script.src = 'http://'+SERVER_IP+"/"+web_site+"/CPCCount.aspx?id="+id+"&channel=2&url="+url+"&mcid="+mcid;
    //script.src = "http://aiweb1.dyndns.org/"+web_site+"/CPMCount.aspx?id="+id+"&channel=11";
    document.getElementsByTagName('head')[0].appendChild(script);     
    return false;
}

function handle_string(inputText) 
{
    inputText = inputText.replace($("div.posted").html()," ");
    inputText = inputText.replace(/<[^>]+>/ig,' '); //去除html tag        
    inputText = inputText.replace(/\r/g,' ');
    inputText = inputText.replace(/\n/g,' '); 
    inputText = inputText.replace(/\t/g,' ');     
    inputText = inputText.replace(/&/g,' ');
    inputText = inputText.replace(/＆/g,' ');    
    inputText = inputText.replace(/\(/g,' ');
    inputText = inputText.replace(/\)/g,' ');
    inputText = inputText.replace(/◇/g,' ');
    inputText = inputText.replace(/,/g,' ');
    inputText = inputText.replace(/{/g,' ');
    inputText = inputText.replace(/;/g,' ');
    inputText = inputText.replace(/'/g," ");
    inputText = inputText.replace(/}/g,' ');
    inputText = inputText.replace(/>/g,' ');
    inputText = inputText.replace(/"/g,' ');
    inputText = inputText.replace(/\\/g,' ');    
    
    return inputText;
}

//main function starts here.
//$(document).ready(function(){
//$(window).load(function(){
if( (typeof xuiteBlogPlugin_relate_article=="undefined") || (xuiteBlogPlugin_relate_article == null) || (xuiteBlogPlugin_relate_article == 'Y')) { 
window.onload = function() {
//抓文章標題
xuiteCapturedTitle = $(".titlename").html();
xuiteCapturedTitle = handle_string(xuiteCapturedTitle);
xuiteCapturedTitle = escape(xuiteCapturedTitle);
//alert(  xuiteCapturedTitle );

//抓文章內容
xuiteCapturedContent = $("#content_all").html();

if(xuiteCapturedTitle == null || xuiteCapturedContent == null)
    return;
if(xuiteCapturedContent.length > 19000)
    xuiteCapturedContent = xuiteCapturedContent.substring(0, 19000); 
xuiteCapturedContent = handle_string(xuiteCapturedContent);
xuiteCapturedContent = escape(xuiteCapturedContent);
//alert(  xuiteCapturedContent );

var current_page_info = new Object();
current_page_info.title = xuiteCapturedTitle;
var url = location.href;
url = escape(url);
current_page_info.url = url;
current_page_info.content = xuiteCapturedContent;

var current_page_info_JSONtext = JSON.stringify(current_page_info, function (key,value) {return value;});
//alert(current_page_info_JSONtext);
//alert(browser);

if (browser=="Microsoft Internet Explorer") {
//ie中會發生 若dom讀取完成前就執行裡面的script網頁會當的問題, 這邊要處理    
    var wait_til_document_ready = function () {
        //alert('ready?');
        if(document.readyState != "complete") {
            setTimeout("wait_til_document_ready();", 100);
        }
        else {
        //alert('ready');
            post_to_url("http://"+SERVER_IP+"/"+web_site+"/Article_Analysis.aspx", current_page_info_JSONtext, "POST");     
        }
    };
    wait_til_document_ready();
}
else {
//其他瀏覽器沒有這個問題.
    post_to_url("http://"+SERVER_IP+"/"+web_site+"/Article_Analysis.aspx", current_page_info_JSONtext, "POST");
}
};
}