$(document).ready(function() {
  UFOAnimate.init();
  
  $("#unknown_cont div").mouseenter(function() {
    var ind = this.id.substr(4);
    UFOAnimate.currInd = ind - 2;
    Animate.clean();
    UFOAnimate.start();      
  }).click(function() {
     location.href = UFOAnimate.stmt[UFOAnimate.currInd][0];
  });  
});

var UFOAnimate = {
  stmt : [
    ["http://www.asinidea.com/oldVersion", "你们公司网站怎么打不开了，哦，原来的网站是这个地址啊。"],
    ["http://www.zhonglianit.com", "有关项训的介绍怎么找不到了呢？"],
    ["http://hmu035091.chinaw3.com/", "新的版本还在测试中呢，你可以一览为快~"]
  ],  
  
  currInd : -1,
  
  arrowPix : [30, 102, 167],
  
  init : function() {
    Talk.bind("right_cont_animate");
    UFOAnimate.start();
  },
  
  stop : function() {
    Talk.stop();
  },
  
  start : function() {      
    this.currInd = (this.currInd + 1) % 3;
    Talk.clear();
    Talk.add(this.stmt[this.currInd][1]);
    
    UFOAnimate.move(function() {      
      Talk.say(function() {
        UFOAnimate.start();      
      });
    });
  },
  
  move : function(foo) {
    $("#unknown_cont div").attr("class", "");
    $("#un_r" + (UFOAnimate.currInd + 1)).hide().attr("class", "r" + (UFOAnimate.currInd + 1)).fadeIn(300);
    $("#right_cont_arrow").stop(true).animate({
      left : UFOAnimate.arrowPix[UFOAnimate.currInd]
    }, 300, function() {
      foo();
    });
  }
};

var Animate = {
    
  queque : new Array(),
  
  timer : null,
  
  fin : null,
  
  joinQueque : function(foo, delay) {
    this.queque.push([foo, delay]);
  },
  
  excuteQueque : function(foo) {
    if (foo) {
      this.fin = foo;
    }
    
    if (this.queque.length == 0) {
      if (this.fin)
        this.fin();
      return;
    }
    var animate = this.queque.shift();        
    if ($.isFunction(animate[0])) {
      animate[0]();
    } else {
      eval(animate[0]);
    }
    this.timer = window.setTimeout(function() {
      Animate.excuteQueque();
    }, animate[1]);
  },
  
  stop : function() {
    window.clearTimeout(this.timer);
  },
  
  clean : function() {
    this.stop();
    this.queque = new Array();
    this.fin = null;
    this.timer = null;
  }
};

  
var Talk = {
  
  target : null,
  queque : new Array(),
  
  config : {
    OP : 1,  // 开始时停顿几次
    ED : 2,
    OP_I : 300, // 开始和技术时停顿的间隔
    C_I : 80  //动画过程中的停顿间隔
  },
  
  clear : function() {
    this.queque = new Array();  
  },
  
  bind : function(id) {
    this.target = $("#" + id);
  },    
  
  add : function(str) {
    this.queque.push(str);
  },
  
  say : function(foo) {
    this.sayWithNewLine(foo);
//    this.sayOneByOne();
  },
  
  sayOneByOne : function() {
    if (this.queque.length == 0) {
      return;
    }
    var str = this.queque.shift();  
    this.opening();
    this.opeStmt(str);    
    this.ending(str);
    Animate.excuteQueque(function() {
      Talk.sayOneByOne();
    });
  },
  
  sayWithNewLine : function(foo) {
    this.opening();
    for (var i = 0; i < this.queque.length; i++) {
      var str = this.queque[i];
      this.opeStmt(str);    
      this.ending();
      if (i != this.queque.length - 1) {
        Animate.joinQueque(function() {
          Talk.apendHtml("<br>_");
        }, Talk.config.C_I);
      }
    }
    
    Animate.excuteQueque(foo);
  },
  
  apendHtml : function(c) {
    var preHtml = Talk.target.html();
    Talk.target.html(preHtml + c);
  },
  
  replaceLast : function(c) {
    var preHtml = Talk.target.html();
    preHtml = preHtml.substr(0, preHtml.length - 1);
    Talk.target.html(preHtml + c);
  },
  
  opeStmt : function(str) {   
    for (var i = 0; i < str.length; i++) {
      Animate.joinQueque("Talk.replaceLast('" + str.substr(i, 1) + "')", Talk.config.C_I);
      if (i == str.length - 1) break;
      Animate.joinQueque(function() {
        Talk.apendHtml("_");
      }, Talk.config.C_I);
    }
  },
  
  ending : function(str) {
    for (var i = 0; i < Talk.config.ED; i++) {
      Animate.joinQueque(function() {
        Talk.apendHtml("_");
      }, Talk.config.OP_I);
      
      Animate.joinQueque(function() {
        Talk.replaceLast("");
      }, Talk.config.OP_I);
    }
  },
  
  opening : function() {
    for (var i = 0; i < Talk.config.OP; i++) {
      Animate.joinQueque(function() {
        Talk.target.html("_");
      }, Talk.config.OP_I);
      
      Animate.joinQueque(function() {
        Talk.target.html("");
      }, Talk.config.OP_I);
    }
  },
  
  stop : function() {
    Animate.stop();
  }
};
