// AutoRLink - auto rewrite autolinks, based on Chris Ridings'
// AutoBlink (http://www.searchguild.com/autoblink2.js)
// Fill in the following if you wish to do this. 
// e.g.  var affiliate="news"
// Leave as is to remove links in the normal way
var affiliate="news";
var illegalExternHost=new RegExp(/(sohu\.com)|(focus\.cn)|(focus\.com)|(sogou\.com)|(sina\.com)|(iask\.com)|(163\.com)|(netease\.com)|(yodao\.com)|(\.qq\.com)|(soso\.com)/g);

var linkcount;
function checklinks() {
  if (!(linkcount==document.links.length)) {
    // Something changed the links!
    // Iterate for an link of illegal extern host
    for (i=0; i < document.links.length; i++) {
      if (document.links[i].href.match(illegalExternHost)) {
        // If we find an link match illegalExternHost extern host
        if (affiliate=="") { 
          // No affiliate id defined so remove the links
          var tr = document.links[i].parentTextEdit.createTextRange();
          tr.moveToElementText(document.links[i]);
          tr.execCommand("Unlink",false);
          tr.execCommand("Unselect",false);
          i--;
        } else {
          // An affiliate id defined so rewrite the links
          var q="";
          /*
          if (document.links[i].href.indexOf("query")>0) {
            q=document.links[i].href.substring(document.links[i].href.indexOf("query")+6);
          } else {
            q=document.links[i].innerText;
            if(q == "") {
              q="房地产";
            }
          }
          */
          
          // reg innerText for reset
          preInnerText=document.links[i].innerText;
          if (document.links[i].href.indexOf("?")>0) {
            if (document.links[i].href.indexOf("query=")>0) {
              q=document.links[i].href.substring(document.links[i].href.indexOf("query=")+7) + "&client=sogou";
            } else if(document.links[i].href.indexOf("k=")>0) {
              q=document.links[i].href.substring(document.links[i].href.indexOf("k=")+3) + "&client=iask";
            } else if(document.links[i].href.indexOf("w=")>0) {
              q=document.links[i].href.substring(document.links[i].href.indexOf("w=")+3) + "&client=soso";
            } else if(document.links[i].href.indexOf("q=")>0) {
              q=document.links[i].href.substring(document.links[i].href.indexOf("q=")+3) + "&client=yodao";
            } else {
              q=document.links[i].innerText;
              if(q == "") {
                q="房地产";
              }
            }
            document.links[i].href="http://search.soufun.com/search?dir=" + affiliate + "&q=" + q + "";
          } else {
            document.links[i].href="http://search.soufun.com/rd/rd2.jsp?q=" + document.links[i].href + "";
          }
          // reset innerText to work correct in ie 
          document.links[i].innerText=preInnerText;
        }
      }
    }
  }
  linkcount = document.links.length;
  setTimeout("checklinks()",1000);
}

if (document.getElementById && document.createElement) {
  linkcount=document.links.length;
  setTimeout("checklinks()",1000);
}

