var _ts_js_fn = new Object();
if ( ts_host=='undefined' ) var ts_host = 'http://ts.ruten.com.tw';
else if ( ts_host.length<1 ) ts_host = 'http://ts.ruten.com.tw';

_ts_js_fn = {
  'init': function() {
    this._ts_id = this.getCookie('_ts_id');
  },

  'run': function() {
    if(this._ts_id == '') {
      this.id_getter();
      this._ts_checker = setInterval('_ts_js_fn.check_id()',3000);
    } else {
      this.ts_log();
    }
  },

  'id_getter': function() {
    document.write(unescape("%3Cscript src='"+ts_host+"/ts_sender.php' type='text/javascript' %3E%3C/script%3E"));
  },

  'check_id': function() {
    this._ts_id = this.getCookie('_ts_id');
    if( this._ts_id != '' ) {
      this.ts_log();
      clearTimeout(this._ts_checker);
    }
  },
  
  'ts_log': function() {
    var user_set;
    var log_str = '';
    var nowtime = new Date().getTime();
    var newElement;
    _tsElement = document.createElement('div');
    log_str += nowtime + '&';
    log_str += '_ts_id=' + this._ts_id + '&';
    if( this.new_id_log != '') {
      log_str += this.new_id_log;
      this.new_id_log = '';
    }
    if ( document.referrer.length>0 ) log_str += 'ref=' + escape(document.referrer) + '&';
    if ( this.userValue.length>0 ) log_str += 'user_set=' + escape(this.userValue) + '&';
    _tsElement.style.visibility = "hidden";
    _tsElement.innerHTML = '<img src=' + ts_host + '/log.gif?' + log_str + '>';
  },

  'setCookie': function(c_name,value,expiredays) {
    var exdate = new Date();
    exdate.setDate(exdate.getDate()+expiredays);
    document.cookie=c_name+ "=" +escape(value)+((expiredays==null) ? "" : ";expires="+exdate.toGMTString())+";path=/;domain=ruten.com.tw";
  },

  'getCookie': function(c_name) {
    if(document.cookie.length>0) {
      c_start=document.cookie.indexOf(c_name + "=");
      if (c_start!=-1) {
        c_start=c_start + c_name.length+1;
        c_end=document.cookie.indexOf(";",c_start);
        if (c_end==-1) c_end=document.cookie.length;
        return unescape(document.cookie.substring(c_start,c_end));
      }
    }
    return "";
  },

  'userSet': function(d) {
    if(d.length > 500) d = d.substr(0,500);
    this.userValue += d+'&';
  },

  'userValue'  :'',
  '_ts_id'     :'',
  '_ts_checker':'',
  'new_id_log' :''
};

