if(!Array.prototype.push) {
  Array.prototype.push = function array_push() {
    for(var i=0;i<arguments.length;i++)
      this[this.length]=arguments[i];
    return this.length;
  }
}

if(!Array.prototype.pop) {
  Array.prototype.pop = function array_pop() {
    lastElement = this[this.length-1];
    this.length = Math.max(this.length-1,0);
    return lastElement;
  }
}


  var _oly_live = new Object();

  _oly_live.dataUrl = "../live/data/olympic.js";
  _oly_live.loadTime = 15000;

  _oly_live.pgType = 1;

  _oly_live.timeStamp = 0;
  _oly_live.matchList = null;
  _oly_live.loadCount = 0;
  _oly_live.refCount = 100;


  _oly_live.splitDomain = "$";
  _oly_live.splitRecord = "~";
  _oly_live.splitColumn = "^";


  _oly_live.getRndStr = function(len) {
    var str = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";				
    var ret = "", n;
    for (var i=0; i<len; i++) {
      n = Math.round(Math.random()*(str.length-1));
      ret += str.substring(n, n+1);
    }
    return ret;
  }


  _oly_live.olyStatus = new Array();
  _oly_live.olyStatus[0] = "未开始";
  _oly_live.olyStatus[1] = "结束";
  _oly_live.olyStatus[2] = "比赛中";

  _oly_live.fbStatus = new Array();
  _oly_live.fbStatus[0] = "";
  _oly_live.fbStatus[1] = "未开始";
  _oly_live.fbStatus[2] = "待定";
  _oly_live.fbStatus[3] = "上半场";
  _oly_live.fbStatus[4] = "下半场";
  _oly_live.fbStatus[5] = "中场休息";
  _oly_live.fbStatus[6] = "结束";
  _oly_live.fbStatus[7] = "加时赛";
  _oly_live.fbStatus[8] = "加时上";
  _oly_live.fbStatus[9] = "加时下";
  _oly_live.fbStatus[10] = "加时结束";
  _oly_live.fbStatus[11] = "点球";
  _oly_live.fbStatus[12] = "暂停";
  _oly_live.fbStatus[13] = "腰斩";
  _oly_live.fbStatus[14] = "取消";
  _oly_live.fbStatus[15] = "改期";
  _oly_live.fbStatus[16] = "延时";
  _oly_live.fbStatus[17] = "90分钟结束";

  _oly_live.bksStatus = new Array();
  _oly_live.bksStatus[1] = "未开始";
  _oly_live.bksStatus[2] = "待定";
  _oly_live.bksStatus[3] = "上半时";
  _oly_live.bksStatus[4] = "下半时";
  _oly_live.bksStatus[5] = "中场休息";
  _oly_live.bksStatus[6] = "结束";
  _oly_live.bksStatus[7] = "加时";
  _oly_live.bksStatus[8] = "加时上";
  _oly_live.bksStatus[9] = "加时下";
  _oly_live.bksStatus[12] = "暂停";
  _oly_live.bksStatus[13] = "腰斩";
  _oly_live.bksStatus[14] = "取消";
  _oly_live.bksStatus[15] = "延期";
  _oly_live.bksStatus[16] = "延时";
  _oly_live.bksStatus[17] = "第一节";
  _oly_live.bksStatus[18] = "第二节";
  _oly_live.bksStatus[19] = "第三节";
  _oly_live.bksStatus[20] = "第四节";
  _oly_live.bksStatus[21] = "加时1";
  _oly_live.bksStatus[22] = "加时2";
  _oly_live.bksStatus[23] = "加时3";
  _oly_live.bksStatus[24] = "加时4";
  _oly_live.bksStatus[26] = "第一节完";
  _oly_live.bksStatus[27] = "第二节完";
  _oly_live.bksStatus[28] = "第三节完";
  _oly_live.bksStatus[29] = "第四节完";
  _oly_live.bksStatus[30] = "加时1完";
  _oly_live.bksStatus[31] = "加时2完";
  _oly_live.bksStatus[32] = "加时3完";
  _oly_live.bksStatus[33] = "加时4完";

  _oly_live.getStatus = function(spT, stId) {
    var ss;
    if(spT=="1")
      ss = _oly_live.fbStatus[stId];
    else if(spT=="2")
      ss = _oly_live.bksStatus[stId];
    else if(spT=="9")
      ss = _oly_live.olyStatus[stId];
    return ss?ss:"";
  }



  //比赛变化信息
  _oly_live.MatchInfo = function(infoStr) {
    var infoArr = infoStr.split(_oly_live.splitColumn);
    this.olyId = infoArr[0];
    this.spType = infoArr[1];
    this.statId = infoArr[2];
    this.spId = infoArr[3];
    this.t1Score = infoArr[4];
    this.t2Score = infoArr[5];

    this.t1ScoreHalf = infoArr[6];
    this.t2ScoreHalf = infoArr[7];
    this.t1S1 = infoArr[8];
    this.t1S2 = infoArr[9];
    this.t1S3 = infoArr[10];
    this.t1S4 = infoArr[11];
    this.t2S1 = infoArr[12];
    this.t2S2 = infoArr[13];
    this.t2S3 = infoArr[14];
    this.t2S4 = infoArr[15];

    if(this.t1Score!="" && this.t2Score!="")
      this.score = this.t1Score + ":" + this.t2Score;
    else if(_oly_live.pgType==2)
      this.score = "VS";
    else
      this.score = "?:?";

  }


  //通用列表类
  _oly_live.List = function() {
    this.items = new Array();
    this.keys = new Object();

    this.add = function(key, value) {
      if(typeof(key) != "undefined") {
        var vv = typeof(value)=="undefined"?null:value;
        var idx = this.keys[key];
        if(idx==null) {
          idx = this.items.length;
          this.keys[key] = idx;
        }
        this.items[idx] = vv;
      }
    }

    this.get = function(key) {
      var idx = this.keys[key];
      if(idx!=null)
        return this.items[idx];
      return null;
    }

    this.clear = function() {
      for(var k in this.keys) {
        delete this.keys[k];
      }
      delete this.keys;
      this.keys = null;
      this.keys = new Object();

      for(var i=0; i<this.items.length; i++) {
        delete this.items(i);
      }
      delete this.items;
      this.items = null;
      this.items = new Array();
    }
  }



  //ajax调用
  _oly_live.callAjax = function(url, fncb, param, fnfl) {
    var url1 = url + (url.indexOf("?")>=0?"&":"?") + _oly_live.getRndStr(10);
    $.ajax({
      type: "GET",
      url: url1,
      dataType: "text/html; charset=utf-8",
      success: function(data, stat) {
        fncb(data, param);
      },
      error: function(xml, stat, e) {
        if(fnfl) fnfl(stat, param);
      }
    });
  }


  _oly_live.loadDataFaildHandler = function(stat, param) {
    if(param[2]) param[2](stat, param[1]);
  }



  _oly_live.loadLiveData = function(url, fncb, param, fnfl) {
    var pm = Array(fncb, param, fnfl);
    _oly_live.callAjax(url, _oly_live.loadLiveDataHandler, pm, _oly_live.loadDataFaildHandler);
  }

  _oly_live.loadLiveDataHandler = function(data, param) {

    if(!data) return _oly_live.loadDataFaildHandler("nodata", param);

    var _dt = new Object();

    //分隔大数据域
    var domains = data.split(_oly_live.splitDomain);

    //处理公共数据域
    var publicDomain = domains[0].split(_oly_live.splitColumn);
    _dt.timeStamp = parseInt(publicDomain[0], 10);

    _dt.matchList = new Array();
    var match;

    //赛事数据
    var matchDomain = domains[1].split(_oly_live.splitRecord);
    for(var i=0; i<matchDomain.length; i++) {
      if(matchDomain[i].length>5) {
        match = new _oly_live.MatchInfo(matchDomain[i]);
        _dt.matchList.push(match);
      }
    }

    //快讯
    var newsDomain = domains[2].split(_oly_live.splitColumn);
    _dt.newsC = newsDomain[0];


    param[0](_dt, param[1]);
  }


  _oly_live.setContent = function(eid, vv) {
    var ee = document.getElementById(eid);
    if(ee) ee.innerHTML = vv;
  }


  _oly_live.matchList = new _oly_live.List();

  _oly_live.loadData = function() {
    if(_oly_live.loadCount>_oly_live.refCount && _oly_live.pgType!=2) {
      window.location.reload();
      return;
    }
    _oly_live.loadCount++;
    window.setTimeout(_oly_live.loadData, _oly_live.loadTime);
    _oly_live.loadLiveData(_oly_live.dataUrl, _oly_live.dataHandler, null, _oly_live.loadDataError);
  }

  _oly_live.dataHandler = function(data) {
    var i, match, match2;

    if(_oly_live.timeStamp==data.timeStamp) return;

    for(i=0; i<data.matchList.length; i++) {
      match = data.matchList[i];
      match2 = _oly_live.matchList.get(match.spType+"_"+match.spId);
      if(match2==null || match.statId!=match2.statId || match.score!=match2.score) {
        if(_oly_live.pgType==2) {
          if(match.spType=="2") {  //篮球
            _oly_live.setContent("host_"+match.spId, match.t1Score);
            _oly_live.setContent("visit_"+match.spId, match.t2Score);
            _oly_live.setContent("host1_"+match.spId, match.t1S1);
            _oly_live.setContent("host2_"+match.spId, match.t1S2);
            _oly_live.setContent("host3_"+match.spId, match.t1S3);
            _oly_live.setContent("host4_"+match.spId, match.t1S4);
            _oly_live.setContent("visit1_"+match.spId, match.t2S1);
            _oly_live.setContent("visit2_"+match.spId, match.t2S2);
            _oly_live.setContent("visit3_"+match.spId, match.t2S3);
            _oly_live.setContent("visit4_"+match.spId, match.t2S4);
          } else {
            _oly_live.setContent("status_"+match.olyId, _oly_live.getStatus(match.spType, match.statId));
            _oly_live.setContent("goal_"+match.spId, match.score);
          }
        } else {
          _oly_live.setContent("status_"+match.olyId, _oly_live.getStatus(match.spType, match.statId));
          _oly_live.setContent("goal_"+match.spId, match.score);
        }
      }
      _oly_live.matchList.add(match.spType+"_"+match.spId);
    }

    if(_oly_live.pgType==1) _oly_live.setNews(data.newsC);

    _oly_live.timeStamp = data.timeStamp;
    delete data;
  }

  _oly_live.loadDataError = function(stat) {
    
  }


  _oly_live.setNews = function(news) {
    var n1 = document.getElementById('mess1');
    var n2 = document.getElementById('mess2');
    if(n1) {
      n1.innerHTML = news;
      h2 = n1.scrollHeight;
      if(n2) n2.innerHTML = n1.innerHTML;
    }
  }

