/* Quick Edit & Delete */ 
function addCurrentArticleEditLink() {
  var aID=document.URL.match(/\/archives\/(\d+)\.html/);
  if (!aID)
    return;
  var blogTitle=document.getElementsByTagName('h3')[0];
  if(/ROODOSESSION=\w+;?/.test(document.cookie)&&/u1=NDA5NDQ2LCAsd2xzbG9pLCAsICwgLCA;?/.test(document.cookie)){
    var a=document.createElement('a');
    a.href='http://adm.blog.roodo.com/article/edit&aID='+aID[1];
    a.target='_blank';
    a.appendChild(document.createTextNode('[Edit]'));
    blogTitle.appendChild(a);
    var cIDs=(function(){ 
      var as=document.getElementsByTagName('a'); 
      var cIDs=[]; 
      for(var i=0, n=as.length;i<n;++i){ 
        var p;  
        if((p=/^comment-(\d+)$/.exec(as[i].name))){  
          cIDs.push(p[1]);  
        }  
      } 
      return cIDs;  
    })();  
    if(cIDs){ 
      function getByClass(c,t) { 
        var bb=document,ts=bb.getElementsByTagName(t),ds=[],ids=0; 
        for(var i=0;i<ts.length;++i) 
          if(ts[i].className==c) ds[ids++]=ts[i]; 
        return ds; 
      } 
      var aPosted=getByClass('comments-post', 'div'); 
      var cID, dL, eL; 
      for(var i=aPosted.length-1;i>=0;--i){ 
        cID=cIDs.pop(); 
        dL=document.createElement('a'); 
        dL.href='http://adm.blog.roodo.com/comment/delete&cID='+cID; 
        dL.target='_blank'; 
        dL.appendChild(document.createTextNode('[Del]')); 
        eL=document.createElement('a'); 
        eL.href='http://adm.blog.roodo.com/comment/edit&cID='+cID; 
        eL.target='_blank'; 
        eL.appendChild(document.createTextNode('[Edit]')); 
        aPosted[i].appendChild(eL); 
        aPosted[i].appendChild(dL); 
      } 
      var aTrackback=getByClass('trackback-post', 'div'); 
      for(var i=aTrackback.length-1;i>=0;--i){ 
        cID=cIDs.pop(); 
        dL=document.createElement('a'); 
        dL.href='http://adm.blog.roodo.com/trackback/delete&tID='+cID; 
        dL.target='_blank'; 
        dL.appendChild(document.createTextNode('[Del]')); 
        eL=document.createElement('a'); 
        eL.href='http://adm.blog.roodo.com/trackback/edit&tID='+cID; 
        eL.target='_blank'; 
        eL.appendChild(document.createTextNode('[Edit]')); 
        aTrackback[i].appendChild(eL); 
        aTrackback[i].appendChild(dL); 
      } 
    } 
  }
}

function googleAnalytics()
{
    /*
    if (!((typeof _uacct == 'undefined') || _uacct ==''))
    {
        _uacct="UA-1702639-1"; //換成你的 Google Analytics ID
        _uff=false;            
        urchinTracker(); 
    } */
    var myPageTracker = _gat._getTracker("UA-1702639-1");
    myPageTracker._initData();
    myPageTracker._trackPageview();
    
}

function initCommon()
{
    setTimeout("googleAnalytics();", 250);        
}

function initForArchives()
{
    initCommon();
    
    dp.SyntaxHighlighter.HighlightAll('code');
    
    /* add top adsense */
    var newCss = { overflow:'hidden', display:'' };
    
    
    //blogbody.before($("#divTopAdSenseContentScript").css(newCss));
    //blogbody.prepend($("#divTopAdSenseContentScript").css(newCss));
    var blogdir = $("div.dir:eq(0)", $("#container"));
    blogdir.after($("#divTopAdSenseContentScript").css(newCss));
    
    /* add content adsense */
    var blogbody = $("div.blogbody:eq(1)", $("#container"));
    var posted = $("div.posted:eq(0)", blogbody);
    posted.append( $("#divGoogleTalkContent").removeAttr("style"));
    posted.append( $("#divAdSenseContentScript").removeAttr("style") );    
    
    /* add content adsense for comment */
    var comments = $("div.comments-body", blogbody); 
    for (var i=1; i<comments.length; ++i) {
        if ( (i % 3) == 0) {
            comments.eq(i).before( $("#divAdSenseContentScript").removeAttr("style").clone() );
        }
    }
    /* quick edit */
    addCurrentArticleEditLink();
}

function initForRoot()
{
    initCommon();
    /* add top adsense */
    var newCss = { overflow:'hidden', display:'' };
    /*
    var blogbody = $("div.blogbody:eq(0)", $("#content"));
    if (blogbody.length > 0)
    {
        //blogbody.before($("#divAdSenseContentScript").attr("align", "right").css(newCss));
        blogbody.prepend($("#divTopAdSenseContentScript").css(newCss));
    }
    */
    var blog = $("div.blog:eq(0)", $("#content"));
    blog.prepend($("#divTopAdSenseContentScript").css(newCss));
    
    /* add content adsense for blogbody */
    var blogbody = $("div.blogbody", $("#content")); 
    var ads = $("#divAdSenseContentScript").removeAttr("style");

		//blogbody.eq(2).after( ads.clone() );
/* 
    for (var i=1; i<blogbody.length; ++i) {
        if ( (i % 2) == 0) {
            blogbody.eq(i).after( ads.clone() );
        }
    }
*/
    $("#divLookLocation").append('<a href="http://look.urs.tw/showhistory.php?BlogID=26693" target="_blank"><img src="http://look.urs.tw/images/s4.php?BlogID=26693" width="120" height="100" alt="BlogLook Score and Rank" border="0"></a>');
}

function initForCatglogy()
{
    initCommon();
    /* add top adsense */
    var newCss = { overflow:'hidden', display:'' };
    var blog = $("div.blog:eq(0)", $("#container"));
    blog.prepend($("#divTopAdSenseContentScript").css(newCss));
    
    
    var blogbodies = $("div.blogbody", blog);
    for (var i=1; i<blogbodies.length; ++i) {
        if ( (i % 2) == 0) {
            blogbodies.eq(i).after( $("#divAdSenseContentScript").removeAttr("style").clone() );
        }
    }    
}

function windowAttachEvent(fn, obj)
{
    if (obj == null)
      obj = window;
    if (obj.attachEvent) 
        obj.attachEvent('onload', fn);
    else 
        obj.addEventListener('load', fn, false);
}
/*
 * 檢查網址裡頭有沒有 "post" 字串，
 * 有的話就代表是單篇文章。
 */
if (document.URL.match(/\/archives\/(\d\d\d\d\-)\.html/))
{   /* date */
    windowAttachEvent(initForCatglogy);
} 
else if (document.URL.match(/\/archives\/(\d+)\.html/))
{
    windowAttachEvent(initForArchives);
}
else if (document.URL.indexOf("archives") > 0 )
{ /* catglogy */
    windowAttachEvent(initForCatglogy);
}
else
{
    windowAttachEvent(initForRoot);
}
