﻿//javascript for http://blog.sina.com.tw/

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;
var isScroll= false; 

//必須先在網頁中產生的欄位
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('<div id="aiweb1"></div><div id="aiweb2"></div>');

document.write('<div id="aiweb_media_info_1" style="display:none;"></div>');
document.write('<div id="aiweb_media_info_2" style="display:none;"></div>');
document.write('<iframe id="aiweb_frame_1" name="aiweb_frame_a" style="height:0; width:0; border:0px;"></iframe>');

//functions
function post_to_url(path, params, method) {     

    var my_iframe = document.getElementById('aiweb_frame_1');    
    var uni = generateGuid();
    
    //alert(uni);
        
    var form = document.createElement("form");
    form.setAttribute("method", method);
    form.setAttribute("action", path+"?uni="+uni+"&channel=13");          
    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
                    orz(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 = 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;
    
    myObject = eval(responseText);   
    
    ///第一則廣告
    switch(myObject[0].Media_Type) {
     case 1: //Condition1:  img file.        
        break;        
    case 2: //Condition2:  flash file.
        break;
    case 3: //Condition3:  video        
        var str="";
        break;                
    case 4: //Condition4:  text string
        var str = "";
        if(myObject[0].Media_Address=="") {
            str = "<h4><a href=\"#\" onClick=\"returnInfo_0();\">" + myObject[0].Media_Title + "</a></h4><a href=" + myObject[0].Media_Address + " target=\"_blank\" onClick=\"returnInfo_0();\"><em>" + myObject[0].Media_ShowAddress.replace('http://','') + "</em></a><p>" + myObject[0].Media_Text + "</p>";
        }
        else {
            str = "<h4><a href=" + myObject[0].Media_Address + " target=\"_blank\" onClick=\"returnInfo_0();\">" + myObject[0].Media_Title + "</a></h4><a href=" + myObject[0].Media_Address + " target=\"_blank\" onClick=\"returnInfo_0();\"><em>" + myObject[0].Media_ShowAddress.replace('http://','') + "</em></a> <p>" + myObject[0].Media_Text + "</p>";
        }        
        if(myObject[1].Media_Title=="") {
            str += '<div align="right" style="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:#666666; text-decoration: underline;">分眾精準廣告</span></a></div>';
        }
        break;
    case -1:
        str ="";
    default:  
    }    
    var choose = document.getElementById('aiweb1');
    choose.innerHTML = str; 
    document.getElementById('aiweb_media_info_1').innerHTML = myObject[0].Media_ID;
    
    ///若有第二則廣告
    if(myObject.length==2) {
        switch(myObject[1].Media_Type) {
         case 1: //Condition1:  img file.            
            break;             
        case 2: //Condition2:  flash file.       <a href='+myObject[0].Media_Address+' target="_blank">                       
            break;
        case 3: //Condition3:  video                    
            break;                
        case 4: //Condition4:  text string
            var str_1 = "";
            if(myObject[1].Media_Address=="") {
                str_1 = "<h4><a href=\"#\" onClick=\"returnInfo_1();\">" + myObject[1].Media_Title + "</a></h4><a href=" + myObject[1].Media_Address + " target=\"_blank\" onClick=\"returnInfo_1();\"><em>" + myObject[1].Media_ShowAddress.replace('http://','') + "</em></a><p>" + myObject[1].Media_Text + "</p>";
            }
            else {
                str_1 = "<h4><a href=" + myObject[1].Media_Address + " target=\"_blank\" onClick=\"returnInfo_1();\">" + myObject[1].Media_Title + "</a></h4><a href=" + myObject[1].Media_Address + " target=\"_blank\" onClick=\"returnInfo_1();\"><em>" + myObject[1].Media_ShowAddress.replace('http://','') + "</em></a> <p>" + myObject[1].Media_Text + "</p>";
            }        
            str_1 += '<div align="right" style="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:#666666; text-decoration: underline;">分眾精準廣告</span></a></div>';
            break;
        case -1:
            str_1 ="";            
        default:  
        }
                
        var choose_1 = document.getElementById('aiweb2');        
        choose_1.innerHTML = str_1;         
        document.getElementById('aiweb_media_info_2').innerHTML = myObject[1].Media_ID;
    }
    
    bObj.removeScriptTag();
    
    //check scroll bar
    var vHeight = 0;
    if (document.all) {
      if (document.documentElement) {
        vHeight = document.documentElement.clientHeight;
      } else {
        vHeight = document.body.clientHeight
      }
    } else {
      vHeight = window.innerHeight;
    }

    if (document.body.offsetHeight > vHeight) {
      //insert code for whatever happens
      //when theres a scrollbar
      var obj  = document.getElementById("aiweb1");
      var objY = getTopfunc(obj);
      var window_height=0;
     
      if (navigator.appName=="Netscape") {
          window_height = window.innerHeight;
      }
      if (navigator.appName.indexOf("Microsoft")!=-1) {
        window_height = document.documentElement.clientHeight;
      }
      if(objY < window_height)
      {
        isScroll = true;
        setTimeout("returnCPMInfo_0();", 1000);
    
        if(myObject.length==2) {
            setTimeout("returnCPMInfo_1();", 1000);
        }
      }
      else
      {
      }
    } 
    else
    {
        isScroll = true;
        setTimeout("returnCPMInfo_0();", 1000);
    
        if(myObject.length==2) {
            setTimeout("returnCPMInfo_1();", 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 url = location.href;
    url = escape(url);
    var iframe = document.createElement("iframe");
    //把檔案網址丟給IFRAM
    iframe.src = 'http://'+SERVER_IP+"/"+web_site+"/CPMCount.aspx?id="+id+"&channel=13&url="+url;
    
    //把IFRAME設為不顯示
    iframe.style.display = "none";
    iframe.height="0px";
    iframe.width="0px";
    //把IRAME 附加到BODY
    document.body.appendChild(iframe);       
    return false;      
}

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 url = location.href;
    url = escape(url); 
     var iframe = document.createElement("iframe");
    //把檔案網址丟給IFRAM
    iframe.src = 'http://'+SERVER_IP+"/"+web_site+"/CPCCount.aspx?id="+id+"&channel=13&url="+url;        
    
    //把IFRAME設為不顯示
    iframe.style.display = "none";
    iframe.height="0px";
    iframe.width="0px";
    //把IRAME 附加到BODY
    document.body.appendChild(iframe);
    return false;      
}

function returnCPMInfo_1()
//return CPM information to server via WebAPI(Json format).
{	 
    var id = document.getElementById('aiweb_media_info_2').innerHTML;	    
    if(id=="" || id==-1)
        return;
        var url = location.href;
    url = escape(url);
    var iframe = document.createElement("iframe");
    //把檔案網址丟給IFRAM
    iframe.src = 'http://'+SERVER_IP+"/"+web_site+"/CPMCount.aspx?id="+id+"&channel=13&url="+url;
    
    //把IFRAME設為不顯示
    iframe.style.display = "none";
    iframe.height="0px";
    iframe.width="0px";
    //把IRAME 附加到BODY
    document.body.appendChild(iframe);   
    
    return false;      
}

function returnInfo_1()
//return information to server via WebAPI(Json format).
{	 
    var id = document.getElementById('aiweb_media_info_2').innerHTML;	
    if(id=="" || id==-1)
        return;    
        var url = location.href;
    url = escape(url);
     var iframe = document.createElement("iframe");
    //把檔案網址丟給IFRAM
    iframe.src = 'http://'+SERVER_IP+"/"+web_site+"/CPCCount.aspx?id="+id+"&channel=13&url="+url;
        
    
    //把IFRAME設為不顯示
    iframe.style.display = "none";
    iframe.height="0px";
    iframe.width="0px";
    //把IRAME 附加到BODY
    document.body.appendChild(iframe);
    return false;      
}

function handle_string(inputText) 
{
    inputText = inputText.replace($("div.posted").html(),"");
    inputText = inputText.replace(/\r/g,'');
    inputText = inputText.replace(/\n/g,''); 
    inputText = inputText.replace(/<[^>]+>/ig,''); //去除html tag    
    inputText = inputText.replace(/&/g,'');
    inputText = inputText.replace(/＆/g,'');    
    inputText = inputText.replace(/\(/g,'');
    inputText = inputText.replace(/\)/g,'');

    return inputText;
}

function getTopfunc(targ)
{
   var element = targ;
   var yPos = targ.offsetTop;
   while( targ = targ.offsetParent )
    {
        yPos += targ.offsetTop;
    }
   
   return yPos;
}

function setPosCookies (){
    var scrollX, scrollY;
    var scrollHeight;

    // 儲存Scrollbar的位置 (x, y)
    if (document.all){
        if (!document.documentElement.scrollLeft)
            scrollX = document.body.scrollLeft;
        else
            scrollX = document.documentElement.scrollLeft;
        if (!document.documentElement.scrollTop)
            scrollY = document.body.scrollTop;
        else
            scrollY = document.documentElement.scrollTop;
    }else{
        scrollX = window.pageXOffset;
        scrollY = window.pageYOffset;
    }
    
     var obj = document.getElementById("aiweb1");
     var objY = getTopfunc(obj);

     scrollHeight = document.documentElement.offsetHeight;
     //clientHeight = document.documentElement.clientHeight;
     
      //alert(objY+","+scrollY+","+scrollHeight);
     if(!isScroll && (scrollY *2 > objY ))
     {
        isScroll= true;
        setTimeout("returnCPMInfo_0();", 1000);
        setTimeout("returnCPMInfo_1();", 1000);
     }
    
}


//main function starts here.
$(document).ready(function(){

    //capture title
    CapturedTitle = $("div h1").html();
    CapturedTitle = handle_string(CapturedTitle);
    CapturedTitle = escape(CapturedTitle);
    //alert(  CapturedTitle );

    //capture content
    CapturedContent = $(".pcont").html();
    if(CapturedTitle == null || CapturedContent == null)
        return;
    if(CapturedContent.length > 19000)
       CapturedContent = CapturedContent.substring(0, 19000); 
    CapturedContent = handle_string(CapturedContent);
    CapturedContent = escape(CapturedContent);    
    //alert(  CapturedContent );

    var current_page_info = new Object();
    current_page_info.title = CapturedTitle;
    var url = location.href;
    url = escape(url);
    current_page_info.url = url;
    current_page_info.content = CapturedContent;
 

    var current_page_info_JSONtext = JSON.stringify(current_page_info, function (key,value) {return value;});
    //alert(current_page_info_JSONtext);
    
    //check scroll bar
    var vHeight = 0;
    if (document.all) {
      if (document.documentElement) {
        vHeight = document.documentElement.clientHeight;
      } else {
        vHeight = document.body.clientHeight
      }
    } else {
      vHeight = window.innerHeight;
    }

    if (document.body.offsetHeight > vHeight) {
      //insert code for whatever happens
      //when theres a scrollbar
      var obj  = document.getElementById("aiweb1");
      var objY = getTopfunc(obj);
      var window_height=0;
     
      if (navigator.appName=="Netscape") {
          window_height = window.innerHeight;
      }
      if (navigator.appName.indexOf("Microsoft")!=-1) {
        window_height = document.documentElement.clientHeight;
      }
      
      if(objY < window_height)
      {
        
      }
      else
      {
        window.onscroll = setPosCookies;
      }
    } 
    else
    {

    }

    post_to_url("http://"+SERVER_IP+"/"+web_site+"/Article_Analysis.aspx", current_page_info_JSONtext, "POST");

});