/*
Copyright (c) 2009 Paddy Mullen
*/

var counter = 0;

function Animator(termEl, reqUrl){
    this.a = new rxvt_term(document.getElementById('term'),"pre_term");
    this.a.refresh_wait=0;
    this.max_diff = 500;
    this.min_milli_jump=0;
    this.player_speed=1;
    var anim = this;
    this.soundPlayer = $("#jquery_jplayer").jPlayer({
        ready: function () {
            //console.log(ogg_url,"http://static.paddymullen.com/tcast_ogg/7.ogg");
            $(this).setFile(
                mp3_url,
                ogg_url
//                "http://static.paddymullen.com/tcast_ogg/7.ogg" //ogg_url
            );
	    //demoInstanceInfo($(this), $("#jplayer_info"));
            anim.sound_ready=true;
            anim.restOfInit();
            $(this).onPlayBarClick(anim.seek_from_beginning);
	},
	volume: 50,
	oggSupport: true,
        swfPath: jqueryPlayerSwfPath
    })
    .jPlayerId("playBar", "player_progress_play_bar")
    .jPlayerId("play", "player_play")
    .jPlayerId("pause", "player_pause")
    .jPlayerId("stop", "player_stop")
    .jPlayerId("loadBar", "player_progress_load_bar");



    $.ajax({
        url: reqUrl,
        type: 'GET',
        timeout: 100000,
        dataType: 'text',
        success: function(resp){
            anim.tcast_data_ready = true;
            anim.resp= resp;
            anim.restOfInit();
        }
    });
};

Animator.prototype = {
  sound_ready: false,
  tcast_data_ready: false,

  restOfInit : function() {

    if(! (this.sound_ready && this.tcast_data_ready) ){
      return;
    }
    this.setupTiming2();

    var anim = this;

    anim.where = 0;
    anim.mspf = this.output_timing[0];
    anim.timingPointer=0;
    anim.soundPointer=0;
    var output;

    anim.seek_from_beginning = function(seekToMilliseconds){
        console.log("previous pointer", anim.timingPointer);
      anim.soundPlayer.stop();
      anim.a.scr_poweron();
      clearTimeout(anim.hnd);

      anim.where = 0;
      anim.mspf = anim.output_timing[0];
      anim.timingPointer=0;
      anim.soundPointer=0;
      do{
        anim.soundPointer+=anim.mspf;
        anim.mspf=anim.output_timing[anim.timingPointer];
        anim.timingPointer++

      } while (anim.soundPointer < seekToMilliseconds);
      anim.output_line(anim.text_arr.slice(0,anim.timingPointer).join(""));
        console.log("new timingPointer", anim.timingPointer);
      anim.to_func = function() {

        anim.soundPointer+=anim.mspf;
        anim.mspf=anim.output_timing[anim.timingPointer];
        output = anim.text_arr[anim.timingPointer];
        anim.output_line(output);
        //anim.update_watches(anim.soundPointer, anim.timingPointer,output);
        anim.timingPointer++;
        anim.hnd = setTimeout(anim.to_func, anim.mspf * anim.player_speed);
      }; // end to_func
      anim.to_func();
      anim.soundPlayer.playHeadTime(anim.soundPointer);

    };

    anim.to_func = function() {

      anim.soundPointer+=anim.mspf;
      anim.mspf=anim.output_timing[anim.timingPointer];
      output = anim.text_arr[anim.timingPointer];
      anim.output_line(output);
      //anim.update_watches(anim.soundPointer, anim.timingPointer,output);
      anim.timingPointer++;
      anim.hnd = setTimeout(anim.to_func, anim.mspf * anim.player_speed);
    }; // end to_func
  },// end rest_of_init


  update_watches : function(soundPointer, timingPointer, output){
      $("#soundPointer").text(soundPointer);
      $("#timingPointer").text(timingPointer);
      //$('#output_text').text(output);     
  },


    no_animation : false,


    output_line : function (str){
        if(this.is_outputting){
            //console.log("outputting while already outputting");
        }
        this.is_outputting=true;
        //console.log(str);
        this.a.cmd_write(str, str.length);
        this.a.scr_refresh();
        this.is_outputting=false;
    },
    setupTiming2: function(){
        this.output_jumps=[];
        this.output_timing=[];
        var residual_milli_jump=0;
        var residual_jump=timing[0][2];
        var startMilliSeconds = timing[0][0];
        for(var i = 0 ; i < timing.length-1; i++){
          var diff_secs = timing[i][0]- startMilliSeconds;
          this.output_timing.push( diff_secs);
          startMilliSeconds = timing[i][0];
        }
      var cr_regexp = new RegExp( chr(10), "g" );
      var cr_replace = chr(13) + chr(10);
      var text = this.resp.replace(cr_regexp, cr_replace);
      this.text_arr = text.split(chr(65533));
      


    },
    pause : function(){
        clearTimeout(this.hnd);
        this.soundPlayer.pause();
    },
    play : function(){
        this.player_speed=1.0;
        //soundPlay();
        this.soundPlayer.play();
        this.hnd = setTimeout(this.to_func, 0);
    } ,
    fast_forward : function(){
        this.player_speed=.5;
        this.hnd = setTimeout(this.to_func, 0);
    } ,
    play_slow : function(){
        this.player_speed=2;
        this.hnd = setTimeout(this.to_func, 0);
    }
}

$('document').ready(

    function (){
        var a = new Animator("pt",tty_file);
        function animate(){
            doAnimate = true;
            a.startAnimate();
            console.log(" animator setup ");
        }
       
        $('#play_start').click(function() {
                               $('#big_play').addClass("playing");         
                               a.play();});
        $('#big_play').click(function (){        
                $('#play_start').trigger('click');});

        $('#pause').click(function(){ a.pause();});
        $('#fast_forward').click(function(){a.fast_forward();});
        $('#play_slow').click(function(){a.play_slow();});
    });